Skip to main content

tests/test_get_live_text.py

1#!/usr/bin/env python3
3import os
4from pathlib import Path
5import re
6import subprocess
8import pytest
11def test_gets_empty_result_if_no_text(bin_path: Path) -> None:
12 """
13 If you pass an image without any text, you get empty output.
14 """
15 proc = subprocess.Popen(
16 [bin_path, "tests/fixtures/checkerboard.png"],
17 stdout=subprocess.PIPE,
18 stderr=subprocess.PIPE,
19 )
20 stdout, stderr = proc.communicate()
22 assert proc.returncode == 0
23 assert stdout == b"\n"
25 if os.environ.get("GITHUB_ACTIONS") != "true":
26 assert stderr == b""
29def test_gets_text_from_image(bin_path: Path) -> None:
30 """
31 If you pass an image that contains text, it gets printed to stdout.
32 """
33 proc = subprocess.Popen(
34 [bin_path, "tests/fixtures/with_text.png"],
35 stdout=subprocess.PIPE,
36 stderr=subprocess.PIPE,
37 )
38 stdout, stderr = proc.communicate()
40 assert proc.returncode == 0
41 assert stdout == b"This is an image with more than one block of text\n"
43 if os.environ.get("GITHUB_ACTIONS") != "true":
44 assert stderr == b""
47def test_gives_useful_error_if_no_such_file(bin_path: Path) -> None:
48 """
49 If you pass a path that doesn't exist, you get a useful error.
50 """
51 proc = subprocess.Popen(
52 [bin_path, "tests/fixtures/doesnotexist.gif"],
53 stdout=subprocess.PIPE,
54 stderr=subprocess.PIPE,
55 )
56 stdout, stderr = proc.communicate()
58 assert proc.returncode == 1
59 assert stdout == b""
60 assert stderr == b"Cannot find file at path: tests/fixtures/doesnotexist.gif\n"
63def test_gives_useful_error_if_cannot_recognize_image(bin_path: Path) -> None:
64 """
65 If you pass a path that doesn't look like an image, you get a useful error.
66 """
67 proc = subprocess.Popen(
68 [bin_path, "README.md"],
69 stdout=subprocess.PIPE,
70 stderr=subprocess.PIPE,
71 )
72 stdout, stderr = proc.communicate()
74 assert proc.returncode == 1
75 assert stdout == b""
76 assert stderr.startswith(b"Unable to recognise text:")
79@pytest.mark.parametrize(
80 "argv",
81 [
82 pytest.param([], id="no_arguments"),
83 pytest.param(
84 ["example.png", "example.gif", "--debug"], id="too_many_arguments"
85 ),
86 ],
88def test_it_fails_if_you_supply_the_wrong_arguments(
89 bin_path: Path, argv: list[str]
90) -> None:
91 """
92 If you pass the wrong arguments, you get an error explaining how to use it.
93 """
94 proc = subprocess.Popen(
95 [bin_path] + argv,
96 stdout=subprocess.PIPE,
97 stderr=subprocess.PIPE,
98 )
99 stdout, stderr = proc.communicate()
101 assert proc.returncode == 1
102 assert stdout == b""
103 assert stderr.startswith(b"Usage:")
106def test_prints_the_version(bin_path: Path) -> None:
107 """
108 If you run it with the --version flag, it prints the version number.
109 """
110 proc = subprocess.Popen(
111 [bin_path, "--version"],
112 stdout=subprocess.PIPE,
113 stderr=subprocess.PIPE,
114 )
115 stdout, stderr = proc.communicate()
117 assert proc.returncode == 0
118 assert re.match(r"get_live_text [0-9]+\.[0-9]+\.[0-9]+\n$", stdout.decode("utf8"))
119 assert stderr == b""