Skip to content

Commit d145f7a

Browse files
Add unit tests for LintModuleOutputUpdate
1 parent cfa4f96 commit d145f7a

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
2+
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
3+
4+
# pylint: disable=redefined-outer-name
5+
from pathlib import Path
6+
from typing import Callable, Tuple
7+
8+
import pytest
9+
10+
from pylint.testutils import FunctionalTestFile
11+
from pylint.testutils.functional import LintModuleOutputUpdate
12+
13+
14+
@pytest.fixture()
15+
def lint_module_fixture(
16+
tmp_path: Path,
17+
) -> Callable[[str], Tuple[Path, Path, LintModuleOutputUpdate]]:
18+
def inner(base: str) -> Tuple[Path, Path, LintModuleOutputUpdate]:
19+
filename = tmp_path / f"{base}.py"
20+
expected_output_file = tmp_path / f"{base}.txt"
21+
lmou = LintModuleOutputUpdate(
22+
test_file=FunctionalTestFile(str(tmp_path), str(filename))
23+
)
24+
return filename, expected_output_file, lmou
25+
26+
return inner
27+
28+
29+
def test_lint_module_output_update_fail_before(
30+
lint_module_fixture: Callable[[str], Tuple[Path, Path, LintModuleOutputUpdate]]
31+
) -> None:
32+
"""There is a fail before the output need to be updated."""
33+
filename, expected_output_file, lmou = lint_module_fixture("foo")
34+
filename.write_text("", encoding="utf8")
35+
assert not expected_output_file.exists()
36+
with pytest.raises(AssertionError, match="1: disallowed-name"):
37+
lmou.runTest()
38+
assert not expected_output_file.exists()
39+
40+
41+
def test_lint_module_output_update_effective(
42+
lint_module_fixture: Callable[[str], Tuple[Path, Path, LintModuleOutputUpdate]]
43+
) -> None:
44+
"""The file is updated following a successful tests with wrong output."""
45+
filename, expected_output_file, lmou = lint_module_fixture("foo")
46+
filename.write_text("# [disallowed-name]\n", encoding="utf8")
47+
lmou.runTest()
48+
assert (expected_output_file).exists()
49+
assert (
50+
expected_output_file.read_text(encoding="utf8")
51+
== 'disallowed-name:1:0:None:None::"Disallowed name ""foo""":UNDEFINED\n'
52+
)
53+
54+
55+
def test_lint_module_output_update_remove_useless_txt(
56+
lint_module_fixture: Callable[[str], Tuple[Path, Path, LintModuleOutputUpdate]]
57+
) -> None:
58+
"""The file is updated following a successful tests with wrong output."""
59+
filename, expected_output_file, lmou = lint_module_fixture("fine_name")
60+
expected_output_file.write_text("", encoding="utf8")
61+
filename.write_text("", encoding="utf8")
62+
lmou.runTest()
63+
assert not (expected_output_file).exists()

0 commit comments

Comments
 (0)