tests: Run Mypy against tests

Run Mypy static checks against our tests. This brings them inline with
the rest of the codebase, and we have an extra level of certainty that
the tests (and unit tests in particular) will not significantly diverge
from the code they are testing.
This commit is contained in:
Alex Pyrgiotis 2022-10-24 15:00:13 +03:00
parent 2279d48807
commit 03c3541bdc
No known key found for this signature in database
GPG key ID: B6C15EBA0357C9AA
4 changed files with 20 additions and 17 deletions

View file

@ -27,7 +27,10 @@ mypy-host:
mypy-container: mypy-container:
mypy $(MYPY_ARGS) container mypy $(MYPY_ARGS) container
mypy: mypy-host mypy-container ## check type hints with mypy mypy-tests:
mypy $(MYPY_ARGS) tests
mypy: mypy-host mypy-container mypy-tests ## check type hints with mypy
.PHONY: lint .PHONY: lint
lint: lint-black lint-isort mypy ## check the code with various linters lint: lint-black lint-isort mypy ## check the code with various linters

View file

@ -3,7 +3,7 @@ from pathlib import Path
import pytest import pytest
sys.dangerzone_dev = True sys.dangerzone_dev = True # type: ignore[attr-defined]
SAMPLE_DIRECTORY = "test_docs" SAMPLE_DIRECTORY = "test_docs"
BASIC_SAMPLE = "sample.pdf" BASIC_SAMPLE = "sample.pdf"

View file

@ -11,7 +11,7 @@ from typing import Sequence
import pytest import pytest
from click.testing import CliRunner, Result from click.testing import CliRunner, Result
from strip_ansi import strip_ansi # type: ignore from strip_ansi import strip_ansi
from dangerzone.cli import cli_main, display_banner from dangerzone.cli import cli_main, display_banner
@ -125,17 +125,17 @@ class TestCli(TestBase):
class TestCliBasic(TestCli): class TestCliBasic(TestCli):
def test_no_args(self): def test_no_args(self) -> None:
"""``$ dangerzone-cli``""" """``$ dangerzone-cli``"""
result = self.run_cli() result = self.run_cli()
result.assert_failure() result.assert_failure()
def test_help(self): def test_help(self) -> None:
"""``$ dangerzone-cli --help``""" """``$ dangerzone-cli --help``"""
result = self.run_cli("--help") result = self.run_cli("--help")
result.assert_success() result.assert_success()
def test_display_banner(self, capfd): def test_display_banner(self, capfd) -> None: # type: ignore[no-untyped-def]
display_banner() # call the test subject display_banner() # call the test subject
(out, err) = capfd.readouterr() (out, err) = capfd.readouterr()
plain_lines = [strip_ansi(line) for line in out.splitlines()] plain_lines = [strip_ansi(line) for line in out.splitlines()]
@ -148,38 +148,38 @@ class TestCliBasic(TestCli):
class TestCliConversion(TestCliBasic): class TestCliConversion(TestCliBasic):
def test_invalid_lang(self): def test_invalid_lang(self) -> None:
result = self.run_cli([self.sample_doc, "--ocr-lang", "piglatin"]) result = self.run_cli([self.sample_doc, "--ocr-lang", "piglatin"])
result.assert_failure() result.assert_failure()
@for_each_doc @for_each_doc
def test_formats(self, doc): def test_formats(self, doc: Path) -> None:
result = self.run_cli(str(doc)) result = self.run_cli(str(doc))
result.assert_success() result.assert_success()
def test_output_filename(self): def test_output_filename(self) -> None:
temp_dir = tempfile.mkdtemp(prefix="dangerzone-") temp_dir = tempfile.mkdtemp(prefix="dangerzone-")
output_filename = str(Path(temp_dir) / "safe.pdf") output_filename = str(Path(temp_dir) / "safe.pdf")
result = self.run_cli([self.sample_doc, "--output-filename", output_filename]) result = self.run_cli([self.sample_doc, "--output-filename", output_filename])
result.assert_success() result.assert_success()
def test_output_filename_spaces(self): def test_output_filename_spaces(self) -> None:
temp_dir = tempfile.mkdtemp(prefix="dangerzone-") temp_dir = tempfile.mkdtemp(prefix="dangerzone-")
output_filename = str(Path(temp_dir) / "safe space.pdf") output_filename = str(Path(temp_dir) / "safe space.pdf")
result = self.run_cli([self.sample_doc, "--output-filename", output_filename]) result = self.run_cli([self.sample_doc, "--output-filename", output_filename])
result.assert_success() result.assert_success()
def test_output_filename_new_dir(self): def test_output_filename_new_dir(self) -> None:
output_filename = str(Path("fake-directory") / "my-output.pdf") output_filename = str(Path("fake-directory") / "my-output.pdf")
result = self.run_cli([self.sample_doc, "--output-filename", output_filename]) result = self.run_cli([self.sample_doc, "--output-filename", output_filename])
result.assert_failure() result.assert_failure()
def test_sample_not_found(self): def test_sample_not_found(self) -> None:
input_filename = str(Path("fake-directory") / "fake-file.pdf") input_filename = str(Path("fake-directory") / "fake-file.pdf")
result = self.run_cli(input_filename) result = self.run_cli(input_filename)
result.assert_failure() result.assert_failure()
def test_lang_eng(self): def test_lang_eng(self) -> None:
result = self.run_cli([self.sample_doc, "--ocr-lang", "eng"]) result = self.run_cli([self.sample_doc, "--ocr-lang", "eng"])
result.assert_success() result.assert_success()
@ -191,7 +191,7 @@ class TestCliConversion(TestCliBasic):
"spaces test.pdf", "spaces test.pdf",
], ],
) )
def test_filenames(self, filename): def test_filenames(self, filename: str) -> None:
tempdir = tempfile.mkdtemp(prefix="dangerzone-") tempdir = tempfile.mkdtemp(prefix="dangerzone-")
doc_path = os.path.join(filename) doc_path = os.path.join(filename)
shutil.copyfile(self.sample_doc, doc_path) shutil.copyfile(self.sample_doc, doc_path)

View file

@ -9,7 +9,7 @@ import dangerzone.util as util
VERSION_FILE_NAME = "version.txt" VERSION_FILE_NAME = "version.txt"
def test_get_resource_path(): def test_get_resource_path() -> None:
share_dir = Path("share").resolve() share_dir = Path("share").resolve()
resource_path = Path(util.get_resource_path(VERSION_FILE_NAME)).parent resource_path = Path(util.get_resource_path(VERSION_FILE_NAME)).parent
assert share_dir.samefile( assert share_dir.samefile(
@ -18,6 +18,6 @@ def test_get_resource_path():
@pytest.mark.skipif(platform.system() != "Windows", reason="Windows-specific") @pytest.mark.skipif(platform.system() != "Windows", reason="Windows-specific")
def test_get_subprocess_startupinfo(): def test_get_subprocess_startupinfo() -> None:
startupinfo = util.get_subprocess_startupinfo() startupinfo = util.get_subprocess_startupinfo()
assert isinstance(startupinfo, subprocess.STARTUPINFO) assert isinstance(startupinfo, subprocess.STARTUPINFO) # type: ignore[attr-defined]