Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
8 changes: 7 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }]
Expand All @@ -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"

Expand Down
3 changes: 2 additions & 1 deletion src/pytest_ty/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
85 changes: 63 additions & 22 deletions tests/test_pytest_ty.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,98 @@
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")

result.stdout.fnmatch_lines(["*::ty PASSED*"])
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")
Expand Down