13
13
14
14
from pylint .lint import Run as LintRun
15
15
from pylint .testutils ._run import _Run as Run
16
+ from pylint .testutils .utils import _test_cwd
16
17
17
18
18
19
def test_fall_back_on_base_config (tmp_path : Path ) -> None :
@@ -86,16 +87,13 @@ def test_subconfig_vs_root_config(
86
87
test_file = tmp_files [test_file_index ]
87
88
start_dir = (level1_dir / start_dir_modificator ).resolve ()
88
89
89
- orig_cwd = os .getcwd ()
90
90
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
99
97
100
98
expected_note = "LEVEL1"
101
99
if test_file_index == 1 :
@@ -140,16 +138,14 @@ def test_missing_local_config(
140
138
test_file = tmp_files [3 ]
141
139
start_dir = (level1_dir / start_dir_modificator ).resolve ()
142
140
143
- orig_cwd = os .getcwd ()
144
141
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 " ))
150
147
151
- test_file = test_file .parent
152
- os .chdir (orig_cwd )
148
+ test_file = test_file .parent
153
149
154
150
# from default config
155
151
expected_note = "TODO"
@@ -171,11 +167,9 @@ def test_subconfig_vs_cli_arg(
171
167
"""Test that CLI arguments have priority over subconfigs."""
172
168
test_root , * tmp_files = _create_subconfig_test_fs
173
169
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 " )
179
173
180
174
# check that CLI argument overrides default value
181
175
assert "TODO" not in output
@@ -199,11 +193,9 @@ def _create_parent_subconfig_fs(tmp_path: Path) -> Path:
199
193
def test_subconfig_in_parent (tmp_path : Path , capsys : CaptureFixture ) -> None :
200
194
"""Test that searching local configs in parent directories works."""
201
195
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 " )
207
199
208
200
# check that file is linted with config from ../, which is not a cwd
209
201
assert "TODO" not in output1
0 commit comments