Changeset View
Changeset View
Standalone View
Standalone View
swh/core/tests/test_tarball.py
# Copyright (C) 2019-2021 The Software Heritage developers | # Copyright (C) 2019-2022 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import hashlib | import hashlib | ||||
import io | |||||
import os | import os | ||||
import shutil | import shutil | ||||
import stat | import stat | ||||
import tarfile | |||||
import pytest | import pytest | ||||
from swh.core import tarball | from swh.core import tarball | ||||
@pytest.fixture | @pytest.fixture | ||||
def prepare_shutil_state(): | def prepare_shutil_state(): | ||||
▲ Show 20 Lines • Show All 186 Lines • ▼ Show 20 Lines | for file in tmp_path.iterdir(): | ||||
assert file.stat().st_mode == stat.S_IFDIR + 0o000755 | assert file.stat().st_mode == stat.S_IFDIR + 0o000755 | ||||
else: | else: | ||||
if int(file.name) & 0o100: # original file was executable for its owner | if int(file.name) & 0o100: # original file was executable for its owner | ||||
assert file.stat().st_mode == 0o100755 | assert file.stat().st_mode == 0o100755 | ||||
else: | else: | ||||
assert file.stat().st_mode == 0o100644 | assert file.stat().st_mode == 0o100644 | ||||
def test_unpcompress_zip_imploded(tmp_path, datadir): | def uncompress_archive_test(archive_path, tmp_path): | ||||
"""Unpack a zip archive with compression type 6 (implode), | assert os.path.exists(archive_path) | ||||
not supported by python zipfile module. | |||||
""" | extract_dir = os.path.join(tmp_path, "unpack", os.path.basename(archive_path)) | ||||
filename = "msk316src.zip" | tarball.uncompress(archive_path, extract_dir) | ||||
zippath = os.path.join(datadir, "archives", filename) | assert len(os.listdir(extract_dir)) > 0 | ||||
assert os.path.exists(zippath) | |||||
extract_dir = os.path.join(tmp_path, filename) | def test_uncompress_tar_fallback_when_unpack_archive_failed(tmp_path): | ||||
"""shutil.unpack_archive can fail unpacking a tarball with missing | |||||
directory bit set on folder contained in it while the tar command | |||||
succeeds to perform the same task so check the fallback of using | |||||
tar command works for such edge case. | |||||
""" | |||||
archive_path = os.path.join(tmp_path, "repro.tar.gz") | |||||
tf = tarfile.open(archive_path, "w:gz") | |||||
ti = tarfile.TarInfo("dir") | |||||
ti.mode = 0o777 | |||||
ti.type = tarfile.DIRTYPE | |||||
tf.addfile(ti) | |||||
ti = tarfile.TarInfo("dir/file") | |||||
tf.addfile(ti, io.BytesIO(b"hello world")) | |||||
tf.close() | |||||
uncompress_archive_test(archive_path, tmp_path) | |||||
tarball.uncompress(zippath, extract_dir) | |||||
assert len(os.listdir(extract_dir)) > 0 | def test_uncompress_zip_imploded(tmp_path, datadir): | ||||
"""Unpack a zip archive with compression type 6 (implode), | |||||
not supported by python zipfile module. | |||||
""" | |||||
archive_path = os.path.join(datadir, "archives", "msk316src.zip") | |||||
uncompress_archive_test(archive_path, tmp_path) | |||||
def test_uncompress_upper_archive_extension(tmp_path, datadir): | def test_uncompress_upper_archive_extension(tmp_path, datadir): | ||||
"""Copy test archives in a temporary directory but turn their names | """Copy test archives in a temporary directory but turn their names | ||||
to uppercase, then check they can be successfully extracted. | to uppercase, then check they can be successfully extracted. | ||||
""" | """ | ||||
archives_path = os.path.join(datadir, "archives") | archives_path = os.path.join(datadir, "archives") | ||||
archive_files = [ | archive_files = [ | ||||
Show All 31 Lines |