diff --git a/swh/loader/package/loader.py b/swh/loader/package/loader.py --- a/swh/loader/package/loader.py +++ b/swh/loader/package/loader.py @@ -27,7 +27,7 @@ import attr import sentry_sdk -from swh.core.config import SWHConfig +from swh.core.config import load_from_envvar from swh.core.tarball import uncompress from swh.loader.package.utils import download from swh.model import from_disk @@ -117,15 +117,17 @@ TPackageInfo = TypeVar("TPackageInfo", bound=BasePackageInfo) +DEFAULT_CONFIG = { + "max_content_size": 100 * 1024 * 1024, + "create_authorities": True, + "create_fetchers": True, +} + + class PackageLoader(Generic[TPackageInfo]): # Origin visit type (str) set by the loader visit_type = "" - DEFAULT_CONFIG = { - "create_authorities": ("bool", True), - "create_fetchers": ("bool", True), - } - def __init__(self, url): """Loader's constructor. This raises exception if the minimal required configuration is missing (cf. fn:`check` method). @@ -135,7 +137,7 @@ """ # This expects to use the environment variable SWH_CONFIG_FILENAME - self.config = SWHConfig.parse_config_file() + self.config = load_from_envvar(DEFAULT_CONFIG) self._check_configuration() self.storage: StorageInterface = get_storage(**self.config["storage"]) self.url = url diff --git a/swh/loader/package/pypi/tests/test_pypi.py b/swh/loader/package/pypi/tests/test_pypi.py --- a/swh/loader/package/pypi/tests/test_pypi.py +++ b/swh/loader/package/pypi/tests/test_pypi.py @@ -3,11 +3,13 @@ # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information +import copy import os from os import path from unittest.mock import patch import pytest +import yaml from swh.core.pytest_plugin import requests_mock_datadir_factory from swh.core.tarball import uncompress @@ -143,13 +145,18 @@ # configuration error # -def test_badly_configured_loader_raise(monkeypatch): +def test_badly_configured_loader_raise(tmp_path, swh_loader_config, monkeypatch): """Badly configured loader should raise""" - monkeypatch.delenv("SWH_CONFIG_FILENAME", raising=False) - with pytest.raises(ValueError) as e: - PyPILoader(url="some-url") + wrong_config = copy.deepcopy(swh_loader_config) + wrong_config.pop("storage") + + conf_path = os.path.join(str(tmp_path), "loader.yml") + with open(conf_path, "w") as f: + f.write(yaml.dump(wrong_config)) + monkeypatch.setenv("SWH_CONFIG_FILENAME", conf_path) - assert "Misconfiguration" in e.value.args[0] + with pytest.raises(ValueError, match="Misconfiguration"): + PyPILoader(url="some-url") def test_pypi_api_url(): diff --git a/swh/loader/package/tests/test_loader.py b/swh/loader/package/tests/test_loader.py --- a/swh/loader/package/tests/test_loader.py +++ b/swh/loader/package/tests/test_loader.py @@ -4,6 +4,7 @@ # See top-level LICENSE file for more information import attr +import pytest from swh.loader.package.loader import BasePackageInfo, PackageLoader @@ -66,3 +67,21 @@ actual_id = p_info.artifact_identity() assert actual_id == [1, 2] + + +def test_no_env_swh_config_filename_raise(monkeypatch): + """No SWH_CONFIG_FILENAME environment variable makes package loader init raise + + """ + + class DummyPackageLoader(PackageLoader): + """A dummy package loader for test purpose""" + + pass + + monkeypatch.delenv("SWH_CONFIG_FILENAME", raising=False) + + with pytest.raises( + AssertionError, match="SWH_CONFIG_FILENAME environment variable is undefined" + ): + DummyPackageLoader(url="some-url")