Debian Codegraph @

src/trio/_tests/tools/test_mypy_annotate.py

raw
1 from __future__ import annotations
2
3 import io
4 import sys
5 from typing import TYPE_CHECKING
6
7 import pytest
8
9 from trio._tools.mypy_annotate import Result, export, main, process_line
10
11 if TYPE_CHECKING:
12 from pathlib import Path
13
14
15 @pytest.mark.parametrize(
16 ("src", "expected"),
17 [
18 ("", None),
19 ("a regular line\n", None),
20 (
21 "package\\filename.py:42:8: note: Some info\n",
22 Result(
23 kind="notice",
24 filename="package\\filename.py",
25 start_line=42,
26 start_col=8,
27 end_line=None,
28 end_col=None,
29 message=" Some info",
30 ),
31 ),
32 (
33 "package/filename.py:42:1:46:3: error: Type error here [code]\n",
34 Result(
35 kind="error",
36 filename="package/filename.py",
37 start_line=42,
38 start_col=1,
39 end_line=46,
40 end_col=3,
41 message=" Type error here [code]",
42 ),
43 ),
44 (
45 "package/module.py:87: warn: Bad code\n",
46 Result(
47 kind="warning",
48 filename="package/module.py",
49 start_line=87,
50 message=" Bad code",
51 ),
52 ),
53 ],
54 ids=["blank", "normal", "note-wcol", "error-wend", "warn-lineonly"],
55 )
56 def test_processing(src: str, expected: Result | None) -> None:
57 result = process_line(src)
58 assert result == expected
59
60
61 def test_export(capsys: pytest.CaptureFixture[str]) -> None:
62 results = {
63 Result(
64 kind="notice",
65 filename="package\\filename.py",
66 start_line=42,
67 start_col=8,
68 end_line=None,
69 end_col=None,
70 message=" Some info",
71 ): ["Windows", "Mac"],
72 Result(
73 kind="error",
74 filename="package/filename.py",
75 start_line=42,
76 start_col=1,
77 end_line=46,
78 end_col=3,
79 message=" Type error here [code]",
80 ): ["Linux", "Mac"],
81 Result(
82 kind="warning",
83 filename="package/module.py",
84 start_line=87,
85 message=" Bad code",
86 ): ["Linux"],
87 }
88 export(results)
89 std = capsys.readouterr()
90 assert std.err == ""
91 assert std.out == (
92 "::notice file=package\\filename.py,line=42,col=8,"
93 "title=Mypy-Windows+Mac::package\\filename.py:(42:8): Some info"
94 "\n"
95 "::error file=package/filename.py,line=42,col=1,endLine=46,endColumn=3,"
96 "title=Mypy-Linux+Mac::package/filename.py:(42:1 - 46:3): Type error here [code]"
97 "\n"
98 "::warning file=package/module.py,line=87,"
99 "title=Mypy-Linux::package/module.py:87: Bad code\n"
100 )
101
102
103 def test_endtoend(
104 tmp_path: Path,
105 monkeypatch: pytest.MonkeyPatch,
106 capsys: pytest.CaptureFixture[str],
107 ) -> None:
108 import trio._tools.mypy_annotate as mypy_annotate
109
110 inp_text = """\
111 Mypy begun
112 trio/core.py:15: error: Bad types here [misc]
113 trio/package/module.py:48:4:56:18: warn: Missing annotations [no-untyped-def]
114 Found 3 errors in 29 files
115 """
116 result_file = tmp_path / "dump.dat"
117 assert not result_file.exists()
118 with monkeypatch.context():
119 monkeypatch.setattr(sys, "stdin", io.StringIO(inp_text))
120
121 mypy_annotate.main(
122 ["--dumpfile", str(result_file), "--platform", "SomePlatform"],
123 )
124
125 std = capsys.readouterr()
126 assert std.err == ""
127 assert std.out == inp_text # Echos the original.
128
129 assert result_file.exists()
130
131 main(["--dumpfile", str(result_file)])
132
133 std = capsys.readouterr()
134 assert std.err == ""
135 assert std.out == (
136 "::error file=trio/core.py,line=15,title=Mypy-SomePlatform::trio/core.py:15: Bad types here [misc]\n"
137 "::warning file=trio/package/module.py,line=48,col=4,endLine=56,endColumn=18,"
138 "title=Mypy-SomePlatform::trio/package/module.py:(48:4 - 56:18): Missing "
139 "annotations [no-untyped-def]\n"
140 )