diff --git a/tests/test_utils.py b/tests/test_utils.py index 5e9b56ea64..278f13592a 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -104,20 +104,23 @@ def test_string(self): ("rb", 0), ), ) - def test_file(self, tmpdir, mode, warnings_num, recwarn): - file_obj = tmpdir.join("test.txt") - file_obj.write("Test") - with file_obj.open(mode) as fd: + def test_file(self, tmp_path, mode, warnings_num, recwarn): + file_path = tmp_path / "test.txt" + with file_path.open("w") as file_obj: + file_obj.write("Test") + + with file_path.open(mode) as fd: assert super_len(fd) == 4 assert len(recwarn) == warnings_num - def test_tarfile_member(self, tmpdir): - file_obj = tmpdir.join("test.txt") - file_obj.write("Test") + def test_tarfile_member(self, tmp_path): + file_path = tmp_path / "test.txt" + with file_path.open("w") as file_obj: + file_obj.write("Test") - tar_obj = str(tmpdir.join("test.tar")) + tar_obj = str(tmp_path / "test.tar") with tarfile.open(tar_obj, "w") as tar: - tar.add(str(file_obj), arcname="test.txt") + tar.add(file_path, arcname="test.txt") with tarfile.open(tar_obj) as tar: member = tar.extractfile("test.txt") @@ -323,19 +326,25 @@ class TestExtractZippedPaths: def test_unzipped_paths_unchanged(self, path): assert path == extract_zipped_paths(path) - def test_zipped_paths_extracted(self, tmpdir): - zipped_py = tmpdir.join("test.zip") - with zipfile.ZipFile(zipped_py.strpath, "w") as f: + def test_zipped_paths_extracted(self, tmp_path): + zipped_py = tmp_path / "test.zip" + with zipfile.ZipFile(str(zipped_py), "w") as f: f.write(__file__) _, name = os.path.splitdrive(__file__) - zipped_path = os.path.join(zipped_py.strpath, name.lstrip(r"\/")) + zipped_path = os.path.join(str(zipped_py), name.lstrip(r"\/")) extracted_path = extract_zipped_paths(zipped_path) assert extracted_path != zipped_path assert os.path.exists(extracted_path) assert filecmp.cmp(extracted_path, __file__) + # If we don't remove the extracted_path at the end of the test case, + # any subsequent changes to the current file will cause this particular + # test case to fail, since the extract_zipped_paths function does not + # rewrite a file to the extracted_path if a file already exists there. + os.remove(extracted_path) + def test_invalid_unc_path(self): path = r"\\localhost\invalid\location" assert extract_zipped_paths(path) == path