2
2
import os
3
3
import unittest .mock
4
4
from pathlib import Path
5
- from typing import Union
5
+ from typing import Optional , Set , Union
6
6
7
7
import pylint .lint
8
8
from pylint .lint .run import Run
9
9
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"""
15
14
args = ["--rcfile" , str (config_file ), __file__ ]
16
15
# If we used `pytest.raises(SystemExit)`, the `runner` variable
17
16
# would not be accessible outside the `with` block.
@@ -22,16 +21,24 @@ def check_configuration_file_reader(config_file: Union[str, Path]) -> Run:
22
21
# in `_msg_states`, used by `is_message_enabled`.
23
22
with unittest .mock .patch ("pylint.lint.pylinter.check_parallel" ):
24
23
runner = pylint .lint .Run (args )
24
+ mocked_exit .assert_called_once_with (expected_exit_code )
25
+ return runner
26
+
25
27
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" }
28
38
for msgid in expected_disabled :
29
39
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
35
42
36
43
37
44
def test_can_read_ini (tmp_path : Path ) -> None :
@@ -45,7 +52,8 @@ def test_can_read_ini(tmp_path: Path) -> None:
45
52
reports = yes
46
53
"""
47
54
)
48
- check_configuration_file_reader (config_file )
55
+ run = get_runner_from_config_file (config_file )
56
+ check_configuration_file_reader (run )
49
57
50
58
51
59
def test_can_read_setup_cfg (tmp_path : Path ) -> None :
@@ -60,7 +68,8 @@ def test_can_read_setup_cfg(tmp_path: Path) -> None:
60
68
reports = yes
61
69
"""
62
70
)
63
- check_configuration_file_reader (config_file )
71
+ run = get_runner_from_config_file (config_file )
72
+ check_configuration_file_reader (run )
64
73
65
74
66
75
def test_can_read_toml (tmp_path : Path ) -> None :
@@ -75,7 +84,8 @@ def test_can_read_toml(tmp_path: Path) -> None:
75
84
reports = "yes"
76
85
"""
77
86
)
78
- check_configuration_file_reader (config_file )
87
+ run = get_runner_from_config_file (config_file )
88
+ check_configuration_file_reader (run )
79
89
80
90
81
91
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:
94
104
reports = true
95
105
"""
96
106
)
97
- check_configuration_file_reader (config_file )
107
+ run = get_runner_from_config_file (config_file )
108
+ check_configuration_file_reader (run )
98
109
99
110
100
111
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:
110
121
)
111
122
env_var = "tmp_path_env"
112
123
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