update tests to use tmp_path fixture
Markus Gerstel
3 years ago
272 | 272 | return obj |
273 | 273 | |
274 | 274 | |
275 | def _windows_resolve(command): | |
275 | def _windows_resolve(command, path=None): | |
276 | 276 | """ |
277 | 277 | Try and find the full path and file extension of the executable to run. |
278 | 278 | This is so that e.g. calls to 'somescript' will point at 'somescript.cmd' |
287 | 287 | if not command or not isinstance(command[0], str): |
288 | 288 | return command |
289 | 289 | |
290 | found_executable = shutil.which(command[0]) | |
290 | found_executable = shutil.which(command[0], path=path) | |
291 | 291 | if found_executable: |
292 | 292 | logger.debug("Resolved %s as %s", command[0], found_executable) |
293 | 293 | return (found_executable, *command[1:]) |
296 | 296 | # Special case. shutil.which may not detect file extensions if a full |
297 | 297 | # path is given, so try to resolve the executable explicitly |
298 | 298 | 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) | |
300 | 300 | if found_executable: |
301 | 301 | return (found_executable, *command[1:]) |
302 | 302 |
57 | 57 | |
58 | 58 | |
59 | 59 | @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): | |
63 | 61 | bat = "simple_bat_extension" |
64 | 62 | cmd = "simple_cmd_extension" |
65 | 63 | exe = "simple_exe_extension" |
66 | 64 | dotshort = "more_complex_filename_with_a.dot" |
67 | 65 | dotlong = "more_complex_filename.withadot" |
68 | 66 | |
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() | |
74 | 72 | |
75 | 73 | def is_valid(command): |
76 | 74 | assert len(command) == 1 |
77 | assert os.path.exists(command[0]) | |
75 | assert os.path.exists(tmp_path / command[0]) | |
78 | 76 | |
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))) |
39 | 39 | assert err == "" |
40 | 40 | |
41 | 41 | |
42 | def test_running_wget(tmpdir): | |
43 | tmpdir.chdir() | |
42 | def test_running_wget(tmp_path): | |
44 | 43 | command = ["wget", "https://www.google.com", "-O", "-"] |
45 | 44 | try: |
46 | result = procrunner.run(command) | |
45 | result = procrunner.run(command, working_directory=tmp_path) | |
47 | 46 | except OSError as e: |
48 | 47 | if e.errno == 2: |
49 | 48 | pytest.skip("wget not available") |
53 | 52 | assert b"google" in result.stdout |
54 | 53 | |
55 | 54 | |
56 | def test_path_object_resolution(tmpdir): | |
55 | def test_path_object_resolution(tmp_path): | |
57 | 56 | 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())") | |
60 | 59 | assert "LEAK_DETECTOR" not in os.environ |
61 | 60 | result = procrunner.run( |
62 | [sys.executable, tmpdir.join("reader.py")], | |
61 | [sys.executable, tmp_path / "reader.py"], | |
63 | 62 | environment_override={"PYTHONHASHSEED": "random", "LEAK_DETECTOR": "1"}, |
64 | working_directory=tmpdir, | |
63 | working_directory=tmp_path, | |
65 | 64 | ) |
66 | 65 | assert result.returncode == 0 |
67 | 66 | assert not result.stderr |