diff --git a/procrunner/__init__.py b/procrunner/__init__.py index 231fecd..fba7bb9 100644 --- a/procrunner/__init__.py +++ b/procrunner/__init__.py @@ -273,7 +273,7 @@ return obj -def _windows_resolve(command): +def _windows_resolve(command, path=None): """ Try and find the full path and file extension of the executable to run. This is so that e.g. calls to 'somescript' will point at 'somescript.cmd' @@ -288,7 +288,7 @@ if not command or not isinstance(command[0], str): return command - found_executable = shutil.which(command[0]) + found_executable = shutil.which(command[0], path=path) if found_executable: logger.debug("Resolved %s as %s", command[0], found_executable) return (found_executable, *command[1:]) @@ -297,7 +297,7 @@ # Special case. shutil.which may not detect file extensions if a full # path is given, so try to resolve the executable explicitly for extension in os.getenv("PATHEXT").split(os.pathsep): - found_executable = shutil.which(command[0] + extension) + found_executable = shutil.which(command[0] + extension, path=path) if found_executable: return (found_executable, *command[1:]) diff --git a/tests/test_procrunner_resolution.py b/tests/test_procrunner_resolution.py index 2237c7c..e099be4 100644 --- a/tests/test_procrunner_resolution.py +++ b/tests/test_procrunner_resolution.py @@ -58,27 +58,25 @@ @pytest.mark.skipif(sys.platform != "win32", reason="windows specific test only") -def test_name_resolution_for_complex_cases(tmpdir): - tmpdir.chdir() - +def test_name_resolution_for_complex_cases(tmp_path): bat = "simple_bat_extension" cmd = "simple_cmd_extension" exe = "simple_exe_extension" dotshort = "more_complex_filename_with_a.dot" dotlong = "more_complex_filename.withadot" - (tmpdir / bat + ".bat").ensure() - (tmpdir / cmd + ".cmd").ensure() - (tmpdir / exe + ".exe").ensure() - (tmpdir / dotshort + ".bat").ensure() - (tmpdir / dotlong + ".cmd").ensure() + (tmp_path / (bat + ".bat")).touch() + (tmp_path / (cmd + ".cmd")).touch() + (tmp_path / (exe + ".exe")).touch() + (tmp_path / (dotshort + ".bat")).touch() + (tmp_path / (dotlong + ".cmd")).touch() def is_valid(command): assert len(command) == 1 - assert os.path.exists(command[0]) + assert os.path.exists(tmp_path / command[0]) - is_valid(procrunner._windows_resolve([bat])) - is_valid(procrunner._windows_resolve([cmd])) - is_valid(procrunner._windows_resolve([exe])) - is_valid(procrunner._windows_resolve([dotshort])) - is_valid(procrunner._windows_resolve([dotlong])) + is_valid(procrunner._windows_resolve([bat], path=os.fspath(tmp_path))) + is_valid(procrunner._windows_resolve([cmd], path=os.fspath(tmp_path))) + is_valid(procrunner._windows_resolve([exe], path=os.fspath(tmp_path))) + is_valid(procrunner._windows_resolve([dotshort], path=os.fspath(tmp_path))) + is_valid(procrunner._windows_resolve([dotlong], path=os.fspath(tmp_path))) diff --git a/tests/test_procrunner_system.py b/tests/test_procrunner_system.py index cbd126d..a78db0c 100644 --- a/tests/test_procrunner_system.py +++ b/tests/test_procrunner_system.py @@ -40,11 +40,10 @@ assert err == "" -def test_running_wget(tmpdir): - tmpdir.chdir() +def test_running_wget(tmp_path): command = ["wget", "https://www.google.com", "-O", "-"] try: - result = procrunner.run(command) + result = procrunner.run(command, working_directory=tmp_path) except OSError as e: if e.errno == 2: pytest.skip("wget not available") @@ -54,15 +53,15 @@ assert b"google" in result.stdout -def test_path_object_resolution(tmpdir): +def test_path_object_resolution(tmp_path): sentinel_value = b"sentinel" - tmpdir.join("tempfile").write(sentinel_value) - tmpdir.join("reader.py").write("print(open('tempfile').read())") + tmp_path.joinpath("tempfile").write_bytes(sentinel_value) + tmp_path.joinpath("reader.py").write_text("print(open('tempfile').read())") assert "LEAK_DETECTOR" not in os.environ result = procrunner.run( - [sys.executable, tmpdir.join("reader.py")], + [sys.executable, tmp_path / "reader.py"], environment_override={"PYTHONHASHSEED": "random", "LEAK_DETECTOR": "1"}, - working_directory=tmpdir, + working_directory=tmp_path, ) assert result.returncode == 0 assert not result.stderr