diff --git a/swh/lister/opam/lister.py b/swh/lister/opam/lister.py --- a/swh/lister/opam/lister.py +++ b/swh/lister/opam/lister.py @@ -51,7 +51,12 @@ scheduler=scheduler, credentials=credentials, url=url, instance=instance, ) self.env = os.environ.copy() + # Opam root folder is initialized in the :meth:`get_pages` method as no + # side-effect should happen in the constructor to ease instantiation self.opamroot = tempfile.mkdtemp(prefix="swh_opam_lister") + + def get_pages(self) -> Iterator[PageType]: + # Initialize the opam root directory with the opam instance data to list. call( [ "opam", @@ -61,13 +66,12 @@ "--no-setup", "--root", self.opamroot, - instance, - url, + self.instance, + self.url, ], env=self.env, ) - - def get_pages(self) -> Iterator[PageType]: + # Actually list opam instance data proc = Popen( [ "opam", diff --git a/swh/lister/opam/tests/test_lister.py b/swh/lister/opam/tests/test_lister.py --- a/swh/lister/opam/tests/test_lister.py +++ b/swh/lister/opam/tests/test_lister.py @@ -6,24 +6,41 @@ import io from unittest.mock import MagicMock -from swh.lister.opam.lister import OpamLister +import pytest +from swh.lister.opam.lister import OpamLister -def test_urls(swh_scheduler, mocker): +module_name = "swh.lister.opam.lister" - instance_url = "https://opam.ocaml.org" - lister = OpamLister(swh_scheduler, url=instance_url, instance="opam") +@pytest.fixture +def mock_opam(mocker): + """Fixture to bypass the actual opam calls within the test context. + """ + # inhibits the real `subprocess.call` which prepares the required internal opam + # state + mock_init = mocker.patch(f"{module_name}.call", return_value=None) + # replaces the real Popen with a fake one (list origins command) mocked_popen = MagicMock() mocked_popen.stdout = io.BytesIO(b"bar\nbaz\nfoo\n") + mock_open = mocker.patch(f"{module_name}.Popen", return_value=mocked_popen) + return mock_init, mock_open + - # replaces the real Popen with a fake one - mocker.patch("swh.lister.opam.lister.Popen", return_value=mocked_popen) +def test_urls(swh_scheduler, mock_opam): + mock_init, mock_popen = mock_opam + + instance_url = "https://opam.ocaml.org" + + lister = OpamLister(swh_scheduler, url=instance_url, instance="opam") # call the lister and get all listed origins urls stats = lister.run() + assert mock_init.called + assert mock_popen.called + assert stats.pages == 3 assert stats.origins == 3 @@ -41,7 +58,6 @@ def test_opam_binary(datadir, swh_scheduler): - instance_url = f"file://{datadir}/fake_opam_repo" lister = OpamLister(swh_scheduler, url=instance_url, instance="fake")