Skip to content

Commit d7674fb

Browse files
author
Aleksey Petryankin
committed
Review-changes 2: fix --use-local-configs when used with --jobs
- fixed assert on _worker_linter.reporter - added another run of per_directory_config tests with --jobs argument - rephrasing of assertion messages to better indicate intention of checks
1 parent 87da8ff commit d7674fb

File tree

2 files changed

+50
-22
lines changed

2 files changed

+50
-22
lines changed

pylint/lint/pylinter.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,9 @@ def register_local_config(self, file_or_dir: str) -> None:
638638
# existing self.config, so we need to save original self.config to restore it later
639639
original_config_ref = self.config
640640
self.config = copy.deepcopy(self.config)
641-
_config_initialization(self, self._cli_args, config_file=local_conf)
641+
_config_initialization(
642+
self, self._cli_args, reporter=self.reporter, config_file=local_conf
643+
)
642644
self._directory_namespaces[basedir.resolve()] = (self.config, {})
643645
# keep dict keys reverse-sorted so that
644646
# iteration over keys in _get_namespace_for_file gets the most nested path first
@@ -775,6 +777,7 @@ def check_single_file_item(self, file: FileItem) -> None:
775777
776778
initialize() should be called before calling this method
777779
"""
780+
self.set_current_module(file.name, file.filepath)
778781
with self._astroid_module_checker() as check_astroid_module:
779782
self._check_file(self.get_ast, check_astroid_module, file)
780783

tests/config/test_per_directory_config.py

+46-21
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66

77
import os
88
import os.path
9+
from io import StringIO
910
from pathlib import Path
1011

1112
import pytest
1213
from pytest import CaptureFixture
1314

1415
from pylint.lint import Run as LintRun
1516
from pylint.testutils._run import _Run as Run
16-
from pylint.testutils.utils import _test_cwd
17+
from pylint.testutils.utils import _patch_streams, _test_cwd
1718

1819

1920
def test_fall_back_on_base_config(tmp_path: Path) -> None:
@@ -65,7 +66,7 @@ def _create_subconfig_test_fs(tmp_path: Path) -> tuple[Path, ...]:
6566

6667
@pytest.mark.parametrize(
6768
"local_config_args",
68-
[["--use-local-configs=y"]],
69+
[["--use-local-configs=y"], ["--use-local-configs=y", "--jobs=2"]],
6970
)
7071
# check modules and use of configuration files from top-level package or subpackage
7172
@pytest.mark.parametrize("test_file_index", [0, 1, 2])
@@ -75,7 +76,6 @@ def _create_subconfig_test_fs(tmp_path: Path) -> tuple[Path, ...]:
7576
)
7677
def test_subconfig_vs_root_config(
7778
_create_subconfig_test_fs: tuple[Path, ...],
78-
capsys: CaptureFixture,
7979
test_file_index: int,
8080
local_config_args: list[str],
8181
start_dir_modificator: str,
@@ -87,46 +87,63 @@ def test_subconfig_vs_root_config(
8787
test_file = tmp_files[test_file_index]
8888
start_dir = (level1_dir / start_dir_modificator).resolve()
8989

90-
output = [f"{start_dir = }\n{test_file = }\n"]
90+
output = [f"{start_dir = }"]
9191
with _test_cwd(start_dir):
9292
for _ in range(2):
93-
# _Run adds --rcfile, which overrides config from cwd, so we need original Run here
94-
LintRun([*local_config_args, str(test_file)], exit=False)
95-
output.append(capsys.readouterr().out.replace("\\n", "\n"))
93+
out = StringIO()
94+
with _patch_streams(out):
95+
# _Run adds --rcfile, which overrides config from cwd, so we need original Run here
96+
LintRun([*local_config_args, str(test_file)], exit=False)
97+
current_file_output = f"{test_file = }\n" + out.getvalue()
98+
output.append(current_file_output)
9699
test_file = test_file.parent
97100

98101
expected_note = "LEVEL1"
99102
if test_file_index == 1:
100103
expected_note = "LEVEL2"
101-
assert_message = f"Wrong note after checking FILE. Readable debug output:\n{output[0]}\n{output[1]}"
104+
assert_message = (
105+
"local pylintrc was not used for checking FILE. "
106+
f"Readable debug output:\n{output[0]}\n{output[1]}"
107+
)
102108
assert expected_note in output[1], assert_message
103-
assert_message = f"Wrong note after checking DIRECTORY. Readable debug output:\n{output[0]}\n{output[2]}"
109+
assert_message = (
110+
"local pylintrc was not used for checking DIRECTORY. "
111+
f"Readable debug output:\n{output[0]}\n{output[2]}"
112+
)
104113
assert expected_note in output[2], assert_message
105114

106115
if test_file_index == 0:
107116
# 'pylint level1_dir/' should use config from subpackage when checking level1_dir/sub/b.py
108-
assert_message = f"Wrong note after checking DIRECTORY. Readable debug output:\n{output[0]}\n{output[2]}"
117+
assert_message = (
118+
"local pylintrc was not used for checking DIRECTORY. "
119+
f"Readable debug output:\n{output[0]}\n{output[2]}"
120+
)
109121
assert "LEVEL2" in output[2], assert_message
110122
if test_file_index == 1:
111123
# 'pylint level1_dir/sub/b.py' and 'pylint level1_dir/sub/' should use
112124
# level1_dir/sub/pylintrc, not level1_dir/pylintrc
113-
assert_message = f"Wrong note after checking FILE. Readable debug output:\n{output[0]}\n{output[1]}"
125+
assert_message = (
126+
"parent config was used instead of local for checking FILE. "
127+
f"Readable debug output:\n{output[0]}\n{output[1]}"
128+
)
114129
assert "LEVEL1" not in output[1], assert_message
115-
assert_message = f"Wrong note after checking DIRECTORY. Readable debug output:\n{output[0]}\n{output[2]}"
130+
assert_message = (
131+
"parent config was used instead of local for checking DIRECTORY. "
132+
f"Readable debug output:\n{output[0]}\n{output[2]}"
133+
)
116134
assert "LEVEL1" not in output[2], assert_message
117135

118136

119137
@pytest.mark.parametrize(
120138
"local_config_args",
121-
[["--use-local-configs=y"]],
139+
[["--use-local-configs=y"], ["--use-local-configs=y", "--jobs=2"]],
122140
)
123141
# check cases when test_file without local config belongs to cwd subtree or not
124142
@pytest.mark.parametrize(
125143
"start_dir_modificator", [".", "..", "../level1_dir_without_config"]
126144
)
127145
def test_missing_local_config(
128146
_create_subconfig_test_fs: tuple[Path, ...],
129-
capsys: CaptureFixture,
130147
local_config_args: list[str],
131148
start_dir_modificator: str,
132149
) -> None:
@@ -138,23 +155,31 @@ def test_missing_local_config(
138155
test_file = tmp_files[3]
139156
start_dir = (level1_dir / start_dir_modificator).resolve()
140157

141-
output = [f"{start_dir = }\n{test_file = }\n"]
158+
output = [f"{start_dir = }"]
142159
with _test_cwd(start_dir):
143160
for _ in range(2):
144-
# _Run adds --rcfile, which overrides config from cwd, so we need original Run here
145-
LintRun([*local_config_args, str(test_file)], exit=False)
146-
output.append(capsys.readouterr().out.replace("\\n", "\n"))
147-
161+
out = StringIO()
162+
with _patch_streams(out):
163+
# _Run adds --rcfile, which overrides config from cwd, so we need original Run here
164+
LintRun([*local_config_args, str(test_file)], exit=False)
165+
current_file_output = f"{test_file = }\n" + out.getvalue()
166+
output.append(current_file_output)
148167
test_file = test_file.parent
149168

150169
# from default config
151170
expected_note = "TODO"
152171
if start_dir_modificator == ".":
153172
# from config in level1_dir
154173
expected_note = "LEVEL1"
155-
assert_message = f"Wrong note after checking FILE. Readable debug output:\n{output[0]}\n{output[1]}"
174+
assert_message = (
175+
"wrong config was used for checking FILE. "
176+
f"Readable debug output:\n{output[0]}\n{output[1]}"
177+
)
156178
assert expected_note in output[1], assert_message
157-
assert_message = f"Wrong note after checking DIRECTORY. Readable debug output:\n{output[0]}\n{output[2]}"
179+
assert_message = (
180+
"wrong config was used for checking DIRECTORY. "
181+
f"Readable debug output:\n{output[0]}\n{output[2]}"
182+
)
158183
assert expected_note in output[2], assert_message
159184

160185

0 commit comments

Comments
 (0)