diff --git a/README.md b/README.md index cc9599a..e8d99e1 100644 --- a/README.md +++ b/README.md @@ -32,4 +32,4 @@ Usage License ------- -`pytest-ty` is licensed under the MIT license (LICENSE or https://opensource.org/licenses/MIT). +`pytest-ty` is licensed under the MIT license ([`LICENSE`](./LICENSE) or https://opensource.org/licenses/MIT). diff --git a/pyproject.toml b/pyproject.toml index 9d7affc..c095877 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ build-backend = "uv_build" [project] name = "pytest-ty" description = "A pytest plugin to run the ty type checker" -version = "0.1.3" +version = "0.1.4" readme = "README.md" requires-python = ">=3.10" authors = [{ name = "Raphael Boidol", email = "pytest-ty@boidol.dev" }] @@ -30,6 +30,12 @@ classifiers = [ ] dependencies = ["pytest>=7.0.0", "ty"] +[dependency-groups] +dev = [ + "pytest", + "pytest-cov", +] + [project.urls] Repository = "https://github.com/boidolr/pytest-ty" diff --git a/src/pytest_ty/plugin.py b/src/pytest_ty/plugin.py index af743f0..50ced25 100644 --- a/src/pytest_ty/plugin.py +++ b/src/pytest_ty/plugin.py @@ -83,8 +83,9 @@ def handler(self, path): command = [ self.__ty, "check", - path, "--output-format=full", + "--force-exclude", + path, ] child = Popen(command, stdout=PIPE, stderr=PIPE) # noqa: S603 stdout, _ = child.communicate() diff --git a/tests/test_pytest_ty.py b/tests/test_pytest_ty.py index 1c4cee9..c346c27 100644 --- a/tests/test_pytest_ty.py +++ b/tests/test_pytest_ty.py @@ -1,34 +1,45 @@ -from unittest import mock +import pytest -from pytest_ty.plugin import set_stash +@pytest.fixture +def failing_test(pytester): + pytester.makepyfile( + test_ignored_file=""" + def test_failure() -> None: + value: int = "1" + assert True + """ + ) -def test_no_ty_run_if_disabled(pytester): - """Make sure that `ty` does not run if plugin is disabled.""" +@pytest.fixture +def passing_test(pytester): pytester.makepyfile(""" - def test_sth() -> None: + def test_case() -> None: assert True """) - set_stash_mock = mock.MagicMock(spec=set_stash) - mock.patch("pytest_ty.set_stash", set_stash_mock) - result = pytester.runpytest("-p no:ty", "-v") - set_stash_mock.assert_not_called() +@pytest.mark.usefixtures("passing_test") +def test_ty_skipped_if_disabled(pytester): + """Make sure that `ty` does not run if plugin is disabled.""" + + pytester.makefile(".ini", pytest="[pytest]\naddopts=--ty\n") + + result = pytester.runpytest("-v") + result.stdout.fnmatch_lines(["*::ty*"]) assert result.ret == 0 + pytester.makefile(".ini", pytest="[pytest]\naddopts=-p no:ty\n") -def test_ty_checking_passes(pytester): - """Make sure that `ty` runs on code.""" + result = pytester.runpytest("-v") + result.stdout.no_fnmatch_line("*::ty*") + assert result.ret == 0 - pytester.makepyfile(""" - from pytest import mark - @mark.parametrize("bar", ["test_value"]) - def test_sth(bar: str) -> None: - assert bar == "test_value" - """) +@pytest.mark.usefixtures("passing_test") +def test_ty_checking_passes(pytester): + """Make sure that `ty` runs on code.""" result = pytester.runpytest("--ty", "-v") @@ -36,22 +47,52 @@ def test_sth(bar: str) -> None: assert result.ret == 0 +@pytest.mark.usefixtures("failing_test") def test_ty_checking_fails(pytester): """Make sure that `ty` runs on code and detects issues.""" - pytester.makepyfile(""" - from pytest import mark + result = pytester.runpytest("--ty", "-v") + + result.stdout.fnmatch_lines(["*::ty FAILED*"]) + assert result.ret == 1 + + +@pytest.mark.usefixtures("failing_test") +def test_ty_exclude_ignores_matching_file(pytester): + """Make sure that configured excludes are respected.""" - @mark.parametrize("bar", ["test_value"]) - def test_sth(bar: str) -> str: - assert bar == "test_value" + result = pytester.runpytest("--ty", "-v") + + result.stdout.fnmatch_lines(["*::ty FAILED*"]) + assert result.ret == 1 + + pytester.makepyprojecttoml(""" + [tool.ty.src] + exclude = ["test_ignored_file.py"] """) result = pytester.runpytest("--ty", "-v") + result.stdout.fnmatch_lines(["*::ty PASSED*"]) + assert result.ret == 0 + + +@pytest.mark.usefixtures("failing_test") +def test_ty_config_disables_rule(pytester): + """Make sure that configured excludes are respected.""" + + result = pytester.runpytest("--ty", "-v") + result.stdout.fnmatch_lines(["*::ty FAILED*"]) assert result.ret == 1 + pytester.makefile(".toml", ty='[rules]\ninvalid-assignment="ignore"\n') + + result = pytester.runpytest("--ty", "-v") + + result.stdout.fnmatch_lines(["*::ty PASSED*"]) + assert result.ret == 0 + def test_help_message(pytester): result = pytester.runpytest("--help")