Skip to content

Commit fbb1c60

Browse files
committed
Add type check results parsing unit tests
1 parent e76774c commit fbb1c60

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

tests/test_type_checking.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
from inline_snapshot import snapshot
2+
from mutmut.type_checking import parse_mypy_report, parse_pyrefly_report
3+
4+
from pathlib import Path
5+
from mutmut.type_checking import TypeCheckingError
6+
7+
8+
def test_mypy_parsing():
9+
mypy_output = [
10+
{
11+
"file": "src/type_checking/__init__.py",
12+
"line": 40,
13+
"column": 20,
14+
"message": 'Incompatible types in assignment (expression has type "None", variable has type "str")',
15+
"hint": None,
16+
"code": "assignment",
17+
"severity": "error",
18+
},
19+
{
20+
"file": "src/type_checking/__init__.py",
21+
"line": 73,
22+
"column": 11,
23+
"message": 'Unsupported left operand type for - ("str")',
24+
"hint": None,
25+
"code": "operator",
26+
"severity": "error",
27+
},
28+
{
29+
"file": "src/type_checking/__init__.py",
30+
"line": 114,
31+
"column": 4,
32+
"message": "By default the bodies of untyped functions are not checked, consider using --check-untyped-defs",
33+
"hint": None,
34+
"code": "annotation-unchecked",
35+
"severity": "note",
36+
},
37+
]
38+
39+
result = parse_mypy_report(mypy_output)
40+
_make_pahts_relative(result)
41+
42+
assert result == snapshot(
43+
[
44+
TypeCheckingError(
45+
file_path=Path("src/type_checking/__init__.py"),
46+
line_number=40,
47+
error_description='Incompatible types in assignment (expression has type "None", variable has type "str")',
48+
),
49+
TypeCheckingError(
50+
file_path=Path("src/type_checking/__init__.py"),
51+
line_number=73,
52+
error_description='Unsupported left operand type for - ("str")',
53+
),
54+
]
55+
)
56+
57+
58+
def test_pyrefly_parsing():
59+
pyrefly_output = {
60+
"errors": [
61+
{
62+
"line": 40,
63+
"column": 21,
64+
"stop_line": 40,
65+
"stop_column": 25,
66+
"path": "src/type_checking/__init__.py",
67+
"code": -2,
68+
"name": "bad-assignment",
69+
"description": "`None` is not assignable to `str`",
70+
"concise_description": "`None` is not assignable to `str`",
71+
"severity": "error",
72+
},
73+
{
74+
"line": 73,
75+
"column": 12,
76+
"stop_line": 73,
77+
"stop_column": 25,
78+
"path": "src/type_checking/__init__.py",
79+
"code": -2,
80+
"name": "unsupported-operation",
81+
"description": "`-` is not supported between `str` and `Literal['2']`\n Cannot find `__sub__` or `__rsub__`",
82+
"concise_description": "`-` is not supported between `str` and `Literal['2']`",
83+
"severity": "error",
84+
},
85+
]
86+
}
87+
88+
result = parse_pyrefly_report(pyrefly_output)
89+
_make_pahts_relative(result)
90+
91+
assert result == snapshot(
92+
[
93+
TypeCheckingError(
94+
file_path=Path("src/type_checking/__init__.py"),
95+
line_number=40,
96+
error_description="`None` is not assignable to `str`",
97+
),
98+
TypeCheckingError(
99+
file_path=Path("src/type_checking/__init__.py"),
100+
line_number=73,
101+
error_description="`-` is not supported between `str` and `Literal['2']`",
102+
),
103+
]
104+
)
105+
106+
107+
def _make_pahts_relative(errors: list[TypeCheckingError]):
108+
cwd = Path(".").resolve()
109+
for error in errors:
110+
# make sure that we mapped the relative paths to absolute paths
111+
assert cwd in error.file_path.parents
112+
# then convert it to relative path, so it's easy to use snapshot(...)
113+
error.file_path = error.file_path.relative_to(cwd)

0 commit comments

Comments
 (0)