Skip to content

Commit 34eeaf0

Browse files
Refactor 'check_configuration_file_reader' so we can assert other files
Necessary prior to #4720
1 parent 96a7d1d commit 34eeaf0

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

tests/config/test_config.py

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22
import os
33
import unittest.mock
44
from pathlib import Path
5-
from typing import Union
5+
from typing import Optional, Set, Union
66

77
import pylint.lint
88
from pylint.lint.run import Run
99

10-
11-
def check_configuration_file_reader(config_file: Union[str, Path]) -> Run:
12-
"""Initialize pylint with the given configuration file and check that
13-
what we initialized the linter with what was expected.
14-
"""
10+
def get_runner_from_config_file(
11+
config_file: Union[str, Path], expected_exit_code: int = 0
12+
) -> Run:
13+
"""Initialize pylint with the given configuration file and return the Run"""
1514
args = ["--rcfile", str(config_file), __file__]
1615
# If we used `pytest.raises(SystemExit)`, the `runner` variable
1716
# would not be accessible outside the `with` block.
@@ -22,16 +21,24 @@ def check_configuration_file_reader(config_file: Union[str, Path]) -> Run:
2221
# in `_msg_states`, used by `is_message_enabled`.
2322
with unittest.mock.patch("pylint.lint.pylinter.check_parallel"):
2423
runner = pylint.lint.Run(args)
24+
mocked_exit.assert_called_once_with(expected_exit_code)
25+
return runner
26+
2527

26-
# "logging-not-lazy" and "logging-format-interpolation"
27-
expected_disabled = {"W1201", "W1202"}
28+
def check_configuration_file_reader(
29+
runner: Run,
30+
expected_disabled: Optional[Set[str]] = None,
31+
expected_jobs: int = 10,
32+
expected_reports_truthey: bool = True,
33+
) -> None:
34+
"""Check that what we initialized the linter with what was expected."""
35+
if expected_disabled is None:
36+
# "logging-not-lazy" and "logging-format-interpolation"
37+
expected_disabled = {"W1201", "W1202"}
2838
for msgid in expected_disabled:
2939
assert not runner.linter.is_message_enabled(msgid)
30-
assert runner.linter.config.jobs == 10
31-
assert runner.linter.config.reports
32-
33-
mocked_exit.assert_called_once_with(0)
34-
return runner
40+
assert runner.linter.config.jobs == expected_jobs
41+
assert bool(runner.linter.config.reports) == expected_reports_truthey
3542

3643

3744
def test_can_read_ini(tmp_path: Path) -> None:
@@ -45,7 +52,8 @@ def test_can_read_ini(tmp_path: Path) -> None:
4552
reports = yes
4653
"""
4754
)
48-
check_configuration_file_reader(config_file)
55+
run = get_runner_from_config_file(config_file)
56+
check_configuration_file_reader(run)
4957

5058

5159
def test_can_read_setup_cfg(tmp_path: Path) -> None:
@@ -60,7 +68,8 @@ def test_can_read_setup_cfg(tmp_path: Path) -> None:
6068
reports = yes
6169
"""
6270
)
63-
check_configuration_file_reader(config_file)
71+
run = get_runner_from_config_file(config_file)
72+
check_configuration_file_reader(run)
6473

6574

6675
def test_can_read_toml(tmp_path: Path) -> None:
@@ -75,7 +84,8 @@ def test_can_read_toml(tmp_path: Path) -> None:
7584
reports = "yes"
7685
"""
7786
)
78-
check_configuration_file_reader(config_file)
87+
run = get_runner_from_config_file(config_file)
88+
check_configuration_file_reader(run)
7989

8090

8191
def test_can_read_toml_rich_types(tmp_path: Path) -> None:
@@ -94,7 +104,8 @@ def test_can_read_toml_rich_types(tmp_path: Path) -> None:
94104
reports = true
95105
"""
96106
)
97-
check_configuration_file_reader(config_file)
107+
run = get_runner_from_config_file(config_file)
108+
check_configuration_file_reader(run)
98109

99110

100111
def test_can_read_toml_env_variable(tmp_path: Path) -> None:
@@ -110,4 +121,5 @@ def test_can_read_toml_env_variable(tmp_path: Path) -> None:
110121
)
111122
env_var = "tmp_path_env"
112123
os.environ[env_var] = str(config_file)
113-
check_configuration_file_reader(f"${env_var}")
124+
run = get_runner_from_config_file(f"${env_var}")
125+
check_configuration_file_reader(run)

0 commit comments

Comments
 (0)