From 03c3541bdcde69368c08aab791c6213ae539ca99 Mon Sep 17 00:00:00 2001 From: Alex Pyrgiotis Date: Mon, 24 Oct 2022 15:00:13 +0300 Subject: [PATCH] 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. --- Makefile | 5 ++++- tests/__init__.py | 2 +- tests/test_cli.py | 24 ++++++++++++------------ tests/test_util.py | 6 +++--- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index 48d2c75..2e5b8ef 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,10 @@ mypy-host: mypy-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 lint: lint-black lint-isort mypy ## check the code with various linters diff --git a/tests/__init__.py b/tests/__init__.py index cad40ab..08a7b2e 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -3,7 +3,7 @@ from pathlib import Path import pytest -sys.dangerzone_dev = True +sys.dangerzone_dev = True # type: ignore[attr-defined] SAMPLE_DIRECTORY = "test_docs" BASIC_SAMPLE = "sample.pdf" diff --git a/tests/test_cli.py b/tests/test_cli.py index 1215a73..ced864c 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -11,7 +11,7 @@ from typing import Sequence import pytest 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 @@ -125,17 +125,17 @@ class TestCli(TestBase): class TestCliBasic(TestCli): - def test_no_args(self): + def test_no_args(self) -> None: """``$ dangerzone-cli``""" result = self.run_cli() result.assert_failure() - def test_help(self): + def test_help(self) -> None: """``$ dangerzone-cli --help``""" result = self.run_cli("--help") 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 (out, err) = capfd.readouterr() plain_lines = [strip_ansi(line) for line in out.splitlines()] @@ -148,38 +148,38 @@ class TestCliBasic(TestCli): 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.assert_failure() @for_each_doc - def test_formats(self, doc): + def test_formats(self, doc: Path) -> None: result = self.run_cli(str(doc)) result.assert_success() - def test_output_filename(self): + def test_output_filename(self) -> None: temp_dir = tempfile.mkdtemp(prefix="dangerzone-") output_filename = str(Path(temp_dir) / "safe.pdf") result = self.run_cli([self.sample_doc, "--output-filename", output_filename]) result.assert_success() - def test_output_filename_spaces(self): + def test_output_filename_spaces(self) -> None: temp_dir = tempfile.mkdtemp(prefix="dangerzone-") output_filename = str(Path(temp_dir) / "safe space.pdf") result = self.run_cli([self.sample_doc, "--output-filename", output_filename]) 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") result = self.run_cli([self.sample_doc, "--output-filename", output_filename]) 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") result = self.run_cli(input_filename) 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.assert_success() @@ -191,7 +191,7 @@ class TestCliConversion(TestCliBasic): "spaces test.pdf", ], ) - def test_filenames(self, filename): + def test_filenames(self, filename: str) -> None: tempdir = tempfile.mkdtemp(prefix="dangerzone-") doc_path = os.path.join(filename) shutil.copyfile(self.sample_doc, doc_path) diff --git a/tests/test_util.py b/tests/test_util.py index 953259c..e0cf2b6 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -9,7 +9,7 @@ import dangerzone.util as util VERSION_FILE_NAME = "version.txt" -def test_get_resource_path(): +def test_get_resource_path() -> None: share_dir = Path("share").resolve() resource_path = Path(util.get_resource_path(VERSION_FILE_NAME)).parent assert share_dir.samefile( @@ -18,6 +18,6 @@ def test_get_resource_path(): @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() - assert isinstance(startupinfo, subprocess.STARTUPINFO) + assert isinstance(startupinfo, subprocess.STARTUPINFO) # type: ignore[attr-defined]