Changeset View
Changeset View
Standalone View
Standalone View
swh/loader/package/pypi/tests/test_pypi.py
# Copyright (C) 2019-2020 The Software Heritage developers | # Copyright (C) 2019-2020 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 copy | |||||
import os | import os | ||||
from os import path | from os import path | ||||
from unittest.mock import patch | from unittest.mock import patch | ||||
import pytest | import pytest | ||||
import yaml | |||||
from swh.core.pytest_plugin import requests_mock_datadir_factory | from swh.core.pytest_plugin import requests_mock_datadir_factory | ||||
from swh.core.tarball import uncompress | from swh.core.tarball import uncompress | ||||
from swh.loader.package import __version__ | from swh.loader.package import __version__ | ||||
from swh.loader.package.pypi.loader import ( | from swh.loader.package.pypi.loader import ( | ||||
PyPILoader, | PyPILoader, | ||||
artifact_to_revision_id, | artifact_to_revision_id, | ||||
author, | author, | ||||
▲ Show 20 Lines • Show All 119 Lines • ▼ Show 20 Lines | def test_author_malformed_3(): | ||||
) | ) | ||||
actual_author == expected_author | actual_author == expected_author | ||||
# configuration error # | # configuration error # | ||||
def test_badly_configured_loader_raise(monkeypatch): | @pytest.fixture | ||||
def swh_config_missing_storage_key(swh_loader_config, monkeypatch, tmp_path): | |||||
"""Explicitly override the swh_loader_config to make it missing the storage key""" | |||||
config = copy.deepcopy(swh_loader_config) | |||||
config.pop("storage") # misconfigure explicitly so the storage key is missing | |||||
# then flush to disk | |||||
conf_path = os.path.join(str(tmp_path), "loader.yml") | |||||
with open(conf_path, "w") as f: | |||||
f.write(yaml.dump(config)) | |||||
monkeypatch.setenv("SWH_CONFIG_FILENAME", conf_path) | |||||
return conf_path | |||||
def test_badly_configured_loader_raise(swh_config_missing_storage_key): | |||||
"""Badly configured loader should raise""" | """Badly configured loader should raise""" | ||||
monkeypatch.delenv("SWH_CONFIG_FILENAME", raising=False) | with pytest.raises( | ||||
with pytest.raises(ValueError) as e: | ValueError, match="Misconfiguration, at least the storage key should be set" | ||||
): | |||||
PyPILoader(url="some-url") | PyPILoader(url="some-url") | ||||
assert "Misconfiguration" in e.value.args[0] | |||||
def test_pypi_api_url(): | def test_pypi_api_url(): | ||||
"""Compute pypi api url from the pypi project url should be ok""" | """Compute pypi api url from the pypi project url should be ok""" | ||||
url = pypi_api_url("https://pypi.org/project/requests") | url = pypi_api_url("https://pypi.org/project/requests") | ||||
assert url == "https://pypi.org/pypi/requests/json" | assert url == "https://pypi.org/pypi/requests/json" | ||||
def test_pypi_api_url_with_slash(): | def test_pypi_api_url_with_slash(): | ||||
▲ Show 20 Lines • Show All 710 Lines • Show Last 20 Lines |