Skip to content

Updated the test for extract_zipped_paths to run correctly if test_utils.py is changed #6889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down