Package list python-procrunner / 81958cc
update tests to use tmp_path fixture Markus Gerstel 1 year, 4 months ago
3 changed file(s) with 22 addition(s) and 25 deletion(s). Raw diff Collapse all Expand all
272272 return obj
273273
274274
275 def _windows_resolve(command):
275 def _windows_resolve(command, path=None):
276276 """
277277 Try and find the full path and file extension of the executable to run.
278278 This is so that e.g. calls to 'somescript' will point at 'somescript.cmd'
287287 if not command or not isinstance(command[0], str):
288288 return command
289289
290 found_executable = shutil.which(command[0])
290 found_executable = shutil.which(command[0], path=path)
291291 if found_executable:
292292 logger.debug("Resolved %s as %s", command[0], found_executable)
293293 return (found_executable, *command[1:])
296296 # Special case. shutil.which may not detect file extensions if a full
297297 # path is given, so try to resolve the executable explicitly
298298 for extension in os.getenv("PATHEXT").split(os.pathsep):
299 found_executable = shutil.which(command[0] + extension)
299 found_executable = shutil.which(command[0] + extension, path=path)
300300 if found_executable:
301301 return (found_executable, *command[1:])
302302
5757
5858
5959 @pytest.mark.skipif(sys.platform != "win32", reason="windows specific test only")
60 def test_name_resolution_for_complex_cases(tmpdir):
61 tmpdir.chdir()
62
60 def test_name_resolution_for_complex_cases(tmp_path):
6361 bat = "simple_bat_extension"
6462 cmd = "simple_cmd_extension"
6563 exe = "simple_exe_extension"
6664 dotshort = "more_complex_filename_with_a.dot"
6765 dotlong = "more_complex_filename.withadot"
6866
69 (tmpdir / bat + ".bat").ensure()
70 (tmpdir / cmd + ".cmd").ensure()
71 (tmpdir / exe + ".exe").ensure()
72 (tmpdir / dotshort + ".bat").ensure()
73 (tmpdir / dotlong + ".cmd").ensure()
67 (tmp_path / (bat + ".bat")).touch()
68 (tmp_path / (cmd + ".cmd")).touch()
69 (tmp_path / (exe + ".exe")).touch()
70 (tmp_path / (dotshort + ".bat")).touch()
71 (tmp_path / (dotlong + ".cmd")).touch()
7472
7573 def is_valid(command):
7674 assert len(command) == 1
77 assert os.path.exists(command[0])
75 assert os.path.exists(tmp_path / command[0])
7876
79 is_valid(procrunner._windows_resolve([bat]))
80 is_valid(procrunner._windows_resolve([cmd]))
81 is_valid(procrunner._windows_resolve([exe]))
82 is_valid(procrunner._windows_resolve([dotshort]))
83 is_valid(procrunner._windows_resolve([dotlong]))
77 is_valid(procrunner._windows_resolve([bat], path=os.fspath(tmp_path)))
78 is_valid(procrunner._windows_resolve([cmd], path=os.fspath(tmp_path)))
79 is_valid(procrunner._windows_resolve([exe], path=os.fspath(tmp_path)))
80 is_valid(procrunner._windows_resolve([dotshort], path=os.fspath(tmp_path)))
81 is_valid(procrunner._windows_resolve([dotlong], path=os.fspath(tmp_path)))
3939 assert err == ""
4040
4141
42 def test_running_wget(tmpdir):
43 tmpdir.chdir()
42 def test_running_wget(tmp_path):
4443 command = ["wget", "https://www.google.com", "-O", "-"]
4544 try:
46 result = procrunner.run(command)
45 result = procrunner.run(command, working_directory=tmp_path)
4746 except OSError as e:
4847 if e.errno == 2:
4948 pytest.skip("wget not available")
5352 assert b"google" in result.stdout
5453
5554
56 def test_path_object_resolution(tmpdir):
55 def test_path_object_resolution(tmp_path):
5756 sentinel_value = b"sentinel"
58 tmpdir.join("tempfile").write(sentinel_value)
59 tmpdir.join("reader.py").write("print(open('tempfile').read())")
57 tmp_path.joinpath("tempfile").write_bytes(sentinel_value)
58 tmp_path.joinpath("reader.py").write_text("print(open('tempfile').read())")
6059 assert "LEAK_DETECTOR" not in os.environ
6160 result = procrunner.run(
62 [sys.executable, tmpdir.join("reader.py")],
61 [sys.executable, tmp_path / "reader.py"],
6362 environment_override={"PYTHONHASHSEED": "random", "LEAK_DETECTOR": "1"},
64 working_directory=tmpdir,
63 working_directory=tmp_path,
6564 )
6665 assert result.returncode == 0
6766 assert not result.stderr