Changeset View
Changeset View
Standalone View
Standalone View
swh/loader/core/tests/test_utils.py
# Copyright (C) 2019 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 | ||||
from datetime import datetime | from datetime import datetime | ||||
import os | import os | ||||
from pathlib import Path | |||||
import signal | import signal | ||||
import tempfile | |||||
from time import sleep | from time import sleep | ||||
from unittest.mock import patch | from unittest.mock import patch | ||||
import pytest | import pytest | ||||
from swh.core.tarball import uncompress | |||||
from swh.loader.core.utils import ( | from swh.loader.core.utils import ( | ||||
CloneFailure, | CloneFailure, | ||||
CloneTimeout, | CloneTimeout, | ||||
clean_dangling_folders, | clean_dangling_folders, | ||||
clone_with_timeout, | clone_with_timeout, | ||||
nix_hashes, | |||||
parse_visit_date, | parse_visit_date, | ||||
) | ) | ||||
from swh.loader.exception import MissingOptionalDependency | |||||
from .conftest import nix_store_missing | |||||
def prepare_arborescence_from(tmpdir, folder_names): | def prepare_arborescence_from(tmpdir, folder_names): | ||||
"""Prepare arborescence tree with folders | """Prepare arborescence tree with folders | ||||
Args: | Args: | ||||
tmpdir (Either[LocalPath, str]): Root temporary directory | tmpdir (Either[LocalPath, str]): Root temporary directory | ||||
folder_names (List[str]): List of folder names | folder_names (List[str]): List of folder names | ||||
▲ Show 20 Lines • Show All 151 Lines • ▼ Show 20 Lines | |||||
def test_utils_parse_visit_date_now(): | def test_utils_parse_visit_date_now(): | ||||
actual_date = parse_visit_date("now") | actual_date = parse_visit_date("now") | ||||
assert isinstance(actual_date, datetime) | assert isinstance(actual_date, datetime) | ||||
def test_utils_parse_visit_date_fails(): | def test_utils_parse_visit_date_fails(): | ||||
with pytest.raises(ValueError, match="invalid"): | with pytest.raises(ValueError, match="invalid"): | ||||
parse_visit_date(10) # not a string nor a date | parse_visit_date(10) # not a string nor a date | ||||
@patch( | |||||
"swh.loader.core.utils.shutil.which", | |||||
return_value=None, | |||||
) | |||||
def test_nix_hashes_missing_nix_store(mock_which): | |||||
with pytest.raises(MissingOptionalDependency, match="nix-store"): | |||||
nix_hashes("some-irrelevant-filepath", ["sha1"]) | |||||
@pytest.mark.skipif(nix_store_missing, reason="requires nix-bin installed (bullseye)") | |||||
def test_nix_hashes_compute(tarball_with_nar_hashes): | |||||
tarball_path, nar_checksums = tarball_with_nar_hashes | |||||
with tempfile.TemporaryDirectory() as tmpdir: | |||||
directory_path = Path(tmpdir) / "src" | |||||
directory_path.mkdir(parents=True, exist_ok=True) | |||||
uncompress(tarball_path, dest=str(directory_path)) | |||||
directory = next(directory_path.iterdir()) | |||||
actual_multihash = nix_hashes(directory, nar_checksums.keys()) | |||||
assert actual_multihash.hexdigest() == nar_checksums |