@@ -32,39 +32,47 @@ def test_fall_back_on_base_config(tmp_path: Path) -> None:
32
32
33
33
34
34
@pytest .fixture
35
- def _create_subconfig_test_fs (tmp_path : Path ) -> tuple [Path , Path , Path , Path ]:
35
+ def _create_subconfig_test_fs (tmp_path : Path ) -> tuple [Path , ... ]:
36
36
level1_dir = tmp_path / "level1_dir"
37
37
level1_init = level1_dir / "__init__.py"
38
38
conf_file1 = level1_dir / "pylintrc"
39
39
test_file1 = level1_dir / "a.py"
40
40
test_file3 = level1_dir / "z.py"
41
+ level1_dir_without_config = tmp_path / "level1_dir_without_config"
42
+ level1_init2 = level1_dir_without_config / "__init__.py"
43
+ test_file4 = level1_dir_without_config / "aa.py"
41
44
subdir = level1_dir / "sub"
42
45
level2_init = subdir / "__init__.py"
43
46
conf_file2 = subdir / "pylintrc"
44
47
test_file2 = subdir / "b.py"
48
+ os .makedirs (level1_dir_without_config )
45
49
os .makedirs (subdir )
46
50
level1_init .touch ()
51
+ level1_init2 .touch ()
47
52
level2_init .touch ()
48
53
test_file_text = "#LEVEL1\n #LEVEL2\n #ALL_LEVELS\n #TODO\n "
49
54
test_file1 .write_text (test_file_text )
50
55
test_file2 .write_text (test_file_text )
51
56
test_file3 .write_text (test_file_text )
57
+ test_file4 .write_text (test_file_text )
52
58
conf1 = "[MISCELLANEOUS]\n notes=LEVEL1,ALL_LEVELS"
53
59
conf2 = "[MISCELLANEOUS]\n notes=LEVEL2,ALL_LEVELS"
54
60
conf_file1 .write_text (conf1 )
55
61
conf_file2 .write_text (conf2 )
56
- return level1_dir , test_file1 , test_file2 , test_file3
62
+ return level1_dir , test_file1 , test_file2 , test_file3 , test_file4
57
63
58
64
59
65
# check that use-parent-configs doesn't break anything
60
66
@pytest .mark .parametrize (
61
67
"local_config_args" ,
62
68
[["--use-local-configs=y" ], ["--use-local-configs=y" , "--use-parent-configs=y" ]],
63
69
)
64
- # check files and configs from top-level package or subpackage
70
+ # check modules and use of configuration files from top-level package or subpackage
65
71
@pytest .mark .parametrize ("test_file_index" , [0 , 1 , 2 ])
66
72
# check cases when cwd contains pylintrc or not
67
- @pytest .mark .parametrize ("start_dir_modificator" , ["." , ".." ])
73
+ @pytest .mark .parametrize (
74
+ "start_dir_modificator" , ["." , ".." , "../level1_dir_without_config" ]
75
+ )
68
76
def test_subconfig_vs_root_config (
69
77
_create_subconfig_test_fs : tuple [Path , ...],
70
78
capsys : CaptureFixture ,
@@ -90,28 +98,70 @@ def test_subconfig_vs_root_config(
90
98
test_file = test_file .parent
91
99
os .chdir (orig_cwd )
92
100
93
- expected_note = f"LEVEL { ( test_file_index % 2 ) + 1 } "
94
- assert (
95
- expected_note in output [ 1 ]
96
- ), f"readable debug output:\n { output [0 ]} \n { output [1 ]} "
97
- assert (
98
- expected_note in output [ 2 ]
99
- ), f"readable debug output: \n { output [ 0 ] } \n { output [ 2 ] } "
101
+ expected_note = "LEVEL1 "
102
+ if test_file_index == 1 :
103
+ expected_note = "LEVEL2"
104
+ assert_message = f"Wrong note after checking FILE. Readable debug output:\n { output [0 ]} \n { output [1 ]} "
105
+ assert expected_note in output [ 1 ], assert_message
106
+ assert_message = f"Wrong note after checking DIRECTORY. Readable debug output: \n { output [ 0 ] } \n { output [ 2 ] } "
107
+ assert expected_note in output [ 2 ], assert_message
100
108
101
109
if test_file_index == 0 :
102
110
# 'pylint level1_dir/' should use config from subpackage when checking level1_dir/sub/b.py
103
- assert (
104
- "LEVEL2" in output [2 ]
105
- ), f"readable debug output:\n { output [0 ]} \n { output [2 ]} "
111
+ assert_message = f"Wrong note after checking DIRECTORY. Readable debug output:\n { output [0 ]} \n { output [2 ]} "
112
+ assert "LEVEL2" in output [2 ], assert_message
106
113
if test_file_index == 1 :
107
114
# 'pylint level1_dir/sub/b.py' and 'pylint level1_dir/sub/' should use
108
115
# level1_dir/sub/pylintrc, not level1_dir/pylintrc
109
- assert (
110
- "LEVEL1" not in output [1 ]
111
- ), f"readable debug output:\n { output [0 ]} \n { output [1 ]} "
112
- assert (
113
- "LEVEL1" not in output [2 ]
114
- ), f"readable debug output:\n { output [0 ]} \n { output [2 ]} "
116
+ assert_message = f"Wrong note after checking FILE. Readable debug output:\n { output [0 ]} \n { output [1 ]} "
117
+ assert "LEVEL1" not in output [1 ], assert_message
118
+ assert_message = f"Wrong note after checking DIRECTORY. Readable debug output:\n { output [0 ]} \n { output [2 ]} "
119
+ assert "LEVEL1" not in output [2 ], assert_message
120
+
121
+
122
+ # check that use-parent-configs doesn't break anything
123
+ @pytest .mark .parametrize (
124
+ "local_config_args" ,
125
+ [["--use-local-configs=y" ], ["--use-local-configs=y" , "--use-parent-configs=y" ]],
126
+ )
127
+ # check cases when test_file without local config belongs to cwd subtree or not
128
+ @pytest .mark .parametrize (
129
+ "start_dir_modificator" , ["." , ".." , "../level1_dir_without_config" ]
130
+ )
131
+ def test_missing_local_config (
132
+ _create_subconfig_test_fs : tuple [Path , ...],
133
+ capsys : CaptureFixture ,
134
+ local_config_args : list [str ],
135
+ start_dir_modificator : str ,
136
+ ) -> None :
137
+ """Test that when checked file or module doesn't have config
138
+ in its own directory, it uses default config or config from cwd.
139
+ """
140
+ level1_dir , * tmp_files = _create_subconfig_test_fs
141
+ # file from level1_dir_without_config
142
+ test_file = tmp_files [3 ]
143
+ start_dir = (level1_dir / start_dir_modificator ).resolve ()
144
+
145
+ orig_cwd = os .getcwd ()
146
+ output = [f"{ start_dir = } , { test_file = } " ]
147
+ os .chdir (start_dir )
148
+ for _ in range (2 ):
149
+ # _Run adds --rcfile, which overrides config from cwd, so we need original Run here
150
+ LintRun ([* local_config_args , str (test_file )], exit = False )
151
+ output .append (capsys .readouterr ().out .replace ("\\ n" , "\n " ))
152
+
153
+ test_file = test_file .parent
154
+ os .chdir (orig_cwd )
155
+
156
+ # from default config
157
+ expected_note = "TODO"
158
+ if start_dir_modificator == "." :
159
+ # from config in level1_dir
160
+ expected_note = "LEVEL1"
161
+ assert_message = f"Wrong note after checking FILE. Readable debug output:\n { output [0 ]} \n { output [1 ]} "
162
+ assert expected_note in output [1 ], assert_message
163
+ assert_message = f"Wrong note after checking DIRECTORY. Readable debug output:\n { output [0 ]} \n { output [2 ]} "
164
+ assert expected_note in output [2 ], assert_message
115
165
116
166
117
167
@pytest .mark .parametrize ("test_file_index" , [0 , 1 ])
@@ -120,7 +170,7 @@ def test_subconfig_vs_cli_arg(
120
170
capsys : CaptureFixture ,
121
171
test_file_index : int ,
122
172
) -> None :
123
- """Test that cli args have priority over subconfigs."""
173
+ """Test that CLI arguments have priority over subconfigs."""
124
174
test_root , * tmp_files = _create_subconfig_test_fs
125
175
test_file = tmp_files [test_file_index ]
126
176
orig_cwd = os .getcwd ()
@@ -129,9 +179,9 @@ def test_subconfig_vs_cli_arg(
129
179
output = capsys .readouterr ().out .replace ("\\ n" , "\n " )
130
180
os .chdir (orig_cwd )
131
181
132
- # check that cli arg overrides default value
182
+ # check that CLI argument overrides default value
133
183
assert "TODO" not in output
134
- # notes=FIXME in cli should override all pylintrc configs
184
+ # notes=FIXME in arguments should override all pylintrc configs
135
185
assert "ALL_LEVELS" not in output
136
186
137
187
@@ -162,3 +212,18 @@ def test_subconfig_in_parent(tmp_path: Path, capsys: CaptureFixture) -> None:
162
212
# check that file is linted with config from ../, which is not a cwd
163
213
assert "TODO" not in output1
164
214
assert "LEVEL1" in output1
215
+
216
+
217
+ def test_register_local_config_accepts_directory (
218
+ _create_subconfig_test_fs : tuple [Path , ...]
219
+ ) -> None :
220
+ """Test that register_local_config can handle directory as argument."""
221
+ level1_dir , * tmp_files = _create_subconfig_test_fs
222
+ # init linter without local configs
223
+ linter = LintRun ([str (tmp_files [0 ])], exit = False ).linter
224
+ assert level1_dir not in linter ._directory_namespaces
225
+
226
+ # call register_local_config with directory as argument
227
+ assert level1_dir .is_dir ()
228
+ linter .register_local_config (str (level1_dir ))
229
+ assert level1_dir in linter ._directory_namespaces
0 commit comments