Skip to content

Commit 87da8ff

Browse files
author
Aleksey Petryankin
committed
Review-changes 1: replace chdir with _test_cwd in tests
1 parent 6f613b0 commit 87da8ff

File tree

1 file changed

+19
-27
lines changed

1 file changed

+19
-27
lines changed

tests/config/test_per_directory_config.py

+19-27
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from pylint.lint import Run as LintRun
1515
from pylint.testutils._run import _Run as Run
16+
from pylint.testutils.utils import _test_cwd
1617

1718

1819
def test_fall_back_on_base_config(tmp_path: Path) -> None:
@@ -86,16 +87,13 @@ def test_subconfig_vs_root_config(
8687
test_file = tmp_files[test_file_index]
8788
start_dir = (level1_dir / start_dir_modificator).resolve()
8889

89-
orig_cwd = os.getcwd()
9090
output = [f"{start_dir = }\n{test_file = }\n"]
91-
os.chdir(start_dir)
92-
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"))
96-
97-
test_file = test_file.parent
98-
os.chdir(orig_cwd)
91+
with _test_cwd(start_dir):
92+
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"))
96+
test_file = test_file.parent
9997

10098
expected_note = "LEVEL1"
10199
if test_file_index == 1:
@@ -140,16 +138,14 @@ def test_missing_local_config(
140138
test_file = tmp_files[3]
141139
start_dir = (level1_dir / start_dir_modificator).resolve()
142140

143-
orig_cwd = os.getcwd()
144141
output = [f"{start_dir = }\n{test_file = }\n"]
145-
os.chdir(start_dir)
146-
for _ in range(2):
147-
# _Run adds --rcfile, which overrides config from cwd, so we need original Run here
148-
LintRun([*local_config_args, str(test_file)], exit=False)
149-
output.append(capsys.readouterr().out.replace("\\n", "\n"))
142+
with _test_cwd(start_dir):
143+
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"))
150147

151-
test_file = test_file.parent
152-
os.chdir(orig_cwd)
148+
test_file = test_file.parent
153149

154150
# from default config
155151
expected_note = "TODO"
@@ -171,11 +167,9 @@ def test_subconfig_vs_cli_arg(
171167
"""Test that CLI arguments have priority over subconfigs."""
172168
test_root, *tmp_files = _create_subconfig_test_fs
173169
test_file = tmp_files[test_file_index]
174-
orig_cwd = os.getcwd()
175-
os.chdir(test_root)
176-
LintRun(["--notes=FIXME", "--use-local-configs=y", str(test_file)], exit=False)
177-
output = capsys.readouterr().out.replace("\\n", "\n")
178-
os.chdir(orig_cwd)
170+
with _test_cwd(test_root):
171+
LintRun(["--notes=FIXME", "--use-local-configs=y", str(test_file)], exit=False)
172+
output = capsys.readouterr().out.replace("\\n", "\n")
179173

180174
# check that CLI argument overrides default value
181175
assert "TODO" not in output
@@ -199,11 +193,9 @@ def _create_parent_subconfig_fs(tmp_path: Path) -> Path:
199193
def test_subconfig_in_parent(tmp_path: Path, capsys: CaptureFixture) -> None:
200194
"""Test that searching local configs in parent directories works."""
201195
test_file = _create_parent_subconfig_fs(tmp_path)
202-
orig_cwd = os.getcwd()
203-
os.chdir(tmp_path)
204-
LintRun(["--use-local-configs=y", str(test_file)], exit=False)
205-
output1 = capsys.readouterr().out.replace("\\n", "\n")
206-
os.chdir(orig_cwd)
196+
with _test_cwd(tmp_path):
197+
LintRun(["--use-local-configs=y", str(test_file)], exit=False)
198+
output1 = capsys.readouterr().out.replace("\\n", "\n")
207199

208200
# check that file is linted with config from ../, which is not a cwd
209201
assert "TODO" not in output1

0 commit comments

Comments
 (0)