Skip to content

Commit 81958cc

Browse files
committed
update tests to use tmp_path fixture
1 parent e838522 commit 81958cc

File tree

3 files changed

+22
-25
lines changed

3 files changed

+22
-25
lines changed

procrunner/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ def _path_resolve(obj):
273273
return obj
274274

275275

276-
def _windows_resolve(command):
276+
def _windows_resolve(command, path=None):
277277
"""
278278
Try and find the full path and file extension of the executable to run.
279279
This is so that e.g. calls to 'somescript' will point at 'somescript.cmd'
@@ -288,7 +288,7 @@ def _windows_resolve(command):
288288
if not command or not isinstance(command[0], str):
289289
return command
290290

291-
found_executable = shutil.which(command[0])
291+
found_executable = shutil.which(command[0], path=path)
292292
if found_executable:
293293
logger.debug("Resolved %s as %s", command[0], found_executable)
294294
return (found_executable, *command[1:])
@@ -297,7 +297,7 @@ def _windows_resolve(command):
297297
# Special case. shutil.which may not detect file extensions if a full
298298
# path is given, so try to resolve the executable explicitly
299299
for extension in os.getenv("PATHEXT").split(os.pathsep):
300-
found_executable = shutil.which(command[0] + extension)
300+
found_executable = shutil.which(command[0] + extension, path=path)
301301
if found_executable:
302302
return (found_executable, *command[1:])
303303

tests/test_procrunner_resolution.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,27 +58,25 @@ def test_name_resolution_for_simple_exe():
5858

5959

6060
@pytest.mark.skipif(sys.platform != "win32", reason="windows specific test only")
61-
def test_name_resolution_for_complex_cases(tmpdir):
62-
tmpdir.chdir()
63-
61+
def test_name_resolution_for_complex_cases(tmp_path):
6462
bat = "simple_bat_extension"
6563
cmd = "simple_cmd_extension"
6664
exe = "simple_exe_extension"
6765
dotshort = "more_complex_filename_with_a.dot"
6866
dotlong = "more_complex_filename.withadot"
6967

70-
(tmpdir / bat + ".bat").ensure()
71-
(tmpdir / cmd + ".cmd").ensure()
72-
(tmpdir / exe + ".exe").ensure()
73-
(tmpdir / dotshort + ".bat").ensure()
74-
(tmpdir / dotlong + ".cmd").ensure()
68+
(tmp_path / (bat + ".bat")).touch()
69+
(tmp_path / (cmd + ".cmd")).touch()
70+
(tmp_path / (exe + ".exe")).touch()
71+
(tmp_path / (dotshort + ".bat")).touch()
72+
(tmp_path / (dotlong + ".cmd")).touch()
7573

7674
def is_valid(command):
7775
assert len(command) == 1
78-
assert os.path.exists(command[0])
76+
assert os.path.exists(tmp_path / command[0])
7977

80-
is_valid(procrunner._windows_resolve([bat]))
81-
is_valid(procrunner._windows_resolve([cmd]))
82-
is_valid(procrunner._windows_resolve([exe]))
83-
is_valid(procrunner._windows_resolve([dotshort]))
84-
is_valid(procrunner._windows_resolve([dotlong]))
78+
is_valid(procrunner._windows_resolve([bat], path=os.fspath(tmp_path)))
79+
is_valid(procrunner._windows_resolve([cmd], path=os.fspath(tmp_path)))
80+
is_valid(procrunner._windows_resolve([exe], path=os.fspath(tmp_path)))
81+
is_valid(procrunner._windows_resolve([dotshort], path=os.fspath(tmp_path)))
82+
is_valid(procrunner._windows_resolve([dotlong], path=os.fspath(tmp_path)))

tests/test_procrunner_system.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,10 @@ def test_decode_invalid_utf8_input(capsys):
4040
assert err == ""
4141

4242

43-
def test_running_wget(tmpdir):
44-
tmpdir.chdir()
43+
def test_running_wget(tmp_path):
4544
command = ["wget", "https://www.google.com", "-O", "-"]
4645
try:
47-
result = procrunner.run(command)
46+
result = procrunner.run(command, working_directory=tmp_path)
4847
except OSError as e:
4948
if e.errno == 2:
5049
pytest.skip("wget not available")
@@ -54,15 +53,15 @@ def test_running_wget(tmpdir):
5453
assert b"google" in result.stdout
5554

5655

57-
def test_path_object_resolution(tmpdir):
56+
def test_path_object_resolution(tmp_path):
5857
sentinel_value = b"sentinel"
59-
tmpdir.join("tempfile").write(sentinel_value)
60-
tmpdir.join("reader.py").write("print(open('tempfile').read())")
58+
tmp_path.joinpath("tempfile").write_bytes(sentinel_value)
59+
tmp_path.joinpath("reader.py").write_text("print(open('tempfile').read())")
6160
assert "LEAK_DETECTOR" not in os.environ
6261
result = procrunner.run(
63-
[sys.executable, tmpdir.join("reader.py")],
62+
[sys.executable, tmp_path / "reader.py"],
6463
environment_override={"PYTHONHASHSEED": "random", "LEAK_DETECTOR": "1"},
65-
working_directory=tmpdir,
64+
working_directory=tmp_path,
6665
)
6766
assert result.returncode == 0
6867
assert not result.stderr

0 commit comments

Comments
 (0)