Changeset View
Changeset View
Standalone View
Standalone View
swh/core/tests/test_tarball.py
Show First 20 Lines • Show All 109 Lines • ▼ Show 20 Lines | def test_compress_uncompress_tar_modes(tmp_path): | ||||
destdir = tmp_path / "destdir" | destdir = tmp_path / "destdir" | ||||
tarball.uncompress(str(tarfile), str(destdir)) | tarball.uncompress(str(tarfile), str(destdir)) | ||||
(executable_path, text_path) = sorted(destdir.iterdir()) | (executable_path, text_path) = sorted(destdir.iterdir()) | ||||
assert text_path.stat().st_mode == 0o100644 | assert text_path.stat().st_mode == 0o100644 | ||||
assert executable_path.stat().st_mode == 0o100755 | assert executable_path.stat().st_mode == 0o100755 | ||||
def test__unpack_tar_failure(tmp_path, datadir): | def test_uncompress_tar_failure(tmp_path, datadir): | ||||
"""Unpack inexistent tarball should fail | """Unpack inexistent tarball should fail | ||||
""" | """ | ||||
tarpath = os.path.join(datadir, "archives", "inexistent-archive.tar.Z") | tarpath = os.path.join(datadir, "archives", "inexistent-archive.tar.Z") | ||||
assert not os.path.exists(tarpath) | assert not os.path.exists(tarpath) | ||||
with pytest.raises( | with pytest.raises(ValueError, match=f"Problem during unpacking {tarpath}"): | ||||
shutil.ReadError, match=f"Unable to uncompress {tarpath} to {tmp_path}" | tarball.uncompress(tarpath, tmp_path) | ||||
): | |||||
tarball._unpack_tar(tarpath, tmp_path) | |||||
def test__unpack_tar_failure2(tmp_path, datadir): | def test_uncompress_tar_failure2(tmp_path, datadir): | ||||
"""Unpack Existent tarball into an inexistent folder should fail | """Unpack Existent tarball into an inexistent folder should fail | ||||
""" | """ | ||||
filename = "groff-1.02.tar.Z" | filename = "groff-1.02.tar.Z" | ||||
tarpath = os.path.join(datadir, "archives", filename) | tarpath = os.path.join(datadir, "archives", filename) | ||||
assert os.path.exists(tarpath) | assert os.path.exists(tarpath) | ||||
extract_dir = os.path.join(tmp_path, "dir", "inexistent") | extract_dir = os.path.join(tmp_path, "dir", "inexistent") | ||||
with pytest.raises( | with pytest.raises(ValueError, match=f"Problem during unpacking {tarpath}"): | ||||
shutil.ReadError, match=f"Unable to uncompress {tarpath} to {tmp_path}" | tarball.uncompress(tarpath, extract_dir) | ||||
): | |||||
tarball._unpack_tar(tarpath, extract_dir) | |||||
def test__unpack_tar_failure3(tmp_path, datadir): | def test_uncompress_tar(tmp_path, datadir): | ||||
"""Unpack unsupported tarball should fail | |||||
""" | |||||
filename = "hello.zip" | |||||
tarpath = os.path.join(datadir, "archives", filename) | |||||
assert os.path.exists(tarpath) | |||||
with pytest.raises( | |||||
shutil.ReadError, match=f"Unable to uncompress {tarpath} to {tmp_path}" | |||||
): | |||||
tarball._unpack_tar(tarpath, tmp_path) | |||||
def test__unpack_tar(tmp_path, datadir): | |||||
"""Unpack supported tarball into an existent folder should be ok | """Unpack supported tarball into an existent folder should be ok | ||||
""" | """ | ||||
filename = "groff-1.02.tar.Z" | filename = "groff-1.02.tar.Z" | ||||
tarpath = os.path.join(datadir, "archives", filename) | tarpath = os.path.join(datadir, "archives", filename) | ||||
assert os.path.exists(tarpath) | assert os.path.exists(tarpath) | ||||
extract_dir = os.path.join(tmp_path, filename) | extract_dir = os.path.join(tmp_path, filename) | ||||
os.makedirs(extract_dir, exist_ok=True) | os.makedirs(extract_dir, exist_ok=True) | ||||
output_directory = tarball._unpack_tar(tarpath, extract_dir) | tarball.uncompress(tarpath, extract_dir) | ||||
assert extract_dir == output_directory | |||||
assert len(os.listdir(extract_dir)) > 0 | assert len(os.listdir(extract_dir)) > 0 | ||||
def test_register_new_archive_formats(prepare_shutil_state): | def test_register_new_archive_formats(prepare_shutil_state): | ||||
"""Registering new archive formats should be fine | """Registering new archive formats should be fine | ||||
""" | """ | ||||
unpack_formats_v1 = [f[0] for f in shutil.get_unpack_formats()] | unpack_formats_v1 = [f[0] for f in shutil.get_unpack_formats()] | ||||
▲ Show 20 Lines • Show All 64 Lines • ▼ Show 20 Lines | def test_unpcompress_zip_imploded(tmp_path, datadir): | ||||
filename = "msk316src.zip" | filename = "msk316src.zip" | ||||
zippath = os.path.join(datadir, "archives", filename) | zippath = os.path.join(datadir, "archives", filename) | ||||
assert os.path.exists(zippath) | assert os.path.exists(zippath) | ||||
extract_dir = os.path.join(tmp_path, filename) | extract_dir = os.path.join(tmp_path, filename) | ||||
os.makedirs(extract_dir, exist_ok=True) | os.makedirs(extract_dir, exist_ok=True) | ||||
output_directory = tarball.uncompress(zippath, extract_dir) | tarball.uncompress(zippath, extract_dir) | ||||
assert extract_dir == output_directory | |||||
assert len(os.listdir(extract_dir)) > 0 | assert len(os.listdir(extract_dir)) > 0 |