diff --git a/CHANGELOG.md b/CHANGELOG.md index d94953c51d..c8da17542f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ - The default branch can now be specified when creating a new pipeline repo [#1959](https://github.com/nf-core/tools/pull/1959). - Only warn when checking that the pipeline directory contains a `main.nf` and a `nextflow.config` file if the pipeline is not an nf-core pipeline [#1964](https://github.com/nf-core/tools/pull/1964) - Add file `versions.yml` when generating `test.yml` with `nf-core modules create-test-yml` but don't check for md5sum [#1963](https://github.com/nf-core/tools/pull/1963) +- Mock biocontainers and anaconda api calls in modules and subworkflows tests [#1967](https://github.com/nf-core/tools/pull/1967) ### Modules diff --git a/requirements-dev.txt b/requirements-dev.txt index 011cbcc3c4..8fc8d1c7de 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -6,3 +6,4 @@ pytest-cov pytest-datafiles Sphinx sphinx_rtd_theme +requests_mock diff --git a/tests/modules/create.py b/tests/modules/create.py index db7a66fa5c..61a8777b14 100644 --- a/tests/modules/create.py +++ b/tests/modules/create.py @@ -1,43 +1,55 @@ import os import pytest +import requests_mock import nf_core.modules +from tests.utils import mock_api_calls def test_modules_create_succeed(self): """Succeed at creating the TrimGalore! module""" - module_create = nf_core.modules.ModuleCreate( - self.pipeline_dir, "trimgalore", "@author", "process_single", True, True, conda_name="trim-galore" - ) - module_create.create() + with requests_mock.Mocker() as mock: + mock_api_calls(mock, "trim-galore", "0.6.7") + module_create = nf_core.modules.ModuleCreate( + self.pipeline_dir, "trimgalore", "@author", "process_single", True, True, conda_name="trim-galore" + ) + module_create.create() assert os.path.exists(os.path.join(self.pipeline_dir, "modules", "local", "trimgalore.nf")) def test_modules_create_fail_exists(self): """Fail at creating the same module twice""" - module_create = nf_core.modules.ModuleCreate( - self.pipeline_dir, "trimgalore", "@author", "process_single", False, False, conda_name="trim-galore" - ) - module_create.create() - with pytest.raises(UserWarning) as excinfo: + with requests_mock.Mocker() as mock: + mock_api_calls(mock, "trim-galore", "0.6.7") + module_create = nf_core.modules.ModuleCreate( + self.pipeline_dir, "trimgalore", "@author", "process_single", False, False, conda_name="trim-galore" + ) module_create.create() + with pytest.raises(UserWarning) as excinfo: + module_create.create() assert "Module file exists already" in str(excinfo.value) def test_modules_create_nfcore_modules(self): """Create a module in nf-core/modules clone""" - module_create = nf_core.modules.ModuleCreate(self.nfcore_modules, "fastqc", "@author", "process_low", False, False) - module_create.create() + with requests_mock.Mocker() as mock: + mock_api_calls(mock, "fastqc", "0.11.9") + module_create = nf_core.modules.ModuleCreate( + self.nfcore_modules, "fastqc", "@author", "process_low", False, False + ) + module_create.create() assert os.path.exists(os.path.join(self.nfcore_modules, "modules", "nf-core", "fastqc", "main.nf")) assert os.path.exists(os.path.join(self.nfcore_modules, "tests", "modules", "nf-core", "fastqc", "main.nf")) def test_modules_create_nfcore_modules_subtool(self): """Create a tool/subtool module in a nf-core/modules clone""" - module_create = nf_core.modules.ModuleCreate( - self.nfcore_modules, "star/index", "@author", "process_medium", False, False - ) - module_create.create() + with requests_mock.Mocker() as mock: + mock_api_calls(mock, "star", "2.8.10a") + module_create = nf_core.modules.ModuleCreate( + self.nfcore_modules, "star/index", "@author", "process_medium", False, False + ) + module_create.create() assert os.path.exists(os.path.join(self.nfcore_modules, "modules", "nf-core", "star", "index", "main.nf")) assert os.path.exists(os.path.join(self.nfcore_modules, "tests", "modules", "nf-core", "star", "index", "main.nf")) diff --git a/tests/test_modules.py b/tests/test_modules.py index a4e777a623..157de39979 100644 --- a/tests/test_modules.py +++ b/tests/test_modules.py @@ -7,6 +7,8 @@ import tempfile import unittest +import requests_mock + import nf_core.create import nf_core.modules @@ -17,6 +19,7 @@ GITLAB_URL, OLD_TRIMGALORE_BRANCH, OLD_TRIMGALORE_SHA, + mock_api_calls, ) @@ -32,9 +35,12 @@ def create_modules_repo_dummy(tmp_dir): with open(os.path.join(root_dir, ".nf-core.yml"), "w") as fh: fh.writelines(["repository_type: modules", "\n"]) - # bpipe is a valid package on bioconda that is very unlikely to ever be added to nf-core/modules - module_create = nf_core.modules.ModuleCreate(root_dir, "bpipe/test", "@author", "process_single", False, False) - module_create.create() + # mock biocontainers and anaconda response + with requests_mock.Mocker() as mock: + mock_api_calls(mock, "bpipe", "0.9.11") + # bpipe is a valid package on bioconda that is very unlikely to ever be added to nf-core/modules + module_create = nf_core.modules.ModuleCreate(root_dir, "bpipe/test", "@author", "process_single", False, False) + module_create.create() return root_dir diff --git a/tests/test_subworkflows.py b/tests/test_subworkflows.py index e5d3f7d3f4..de52ed589c 100644 --- a/tests/test_subworkflows.py +++ b/tests/test_subworkflows.py @@ -7,11 +7,13 @@ import tempfile import unittest +import requests_mock + import nf_core.create import nf_core.modules import nf_core.subworkflows -from .utils import GITLAB_URL +from .utils import GITLAB_URL, mock_api_calls def create_modules_repo_dummy(tmp_dir): @@ -29,9 +31,11 @@ def create_modules_repo_dummy(tmp_dir): with open(os.path.join(root_dir, ".nf-core.yml"), "w") as fh: fh.writelines(["repository_type: modules", "\n"]) - # bpipe is a valid package on bioconda that is very unlikely to ever be added to nf-core/modules - module_create = nf_core.modules.ModuleCreate(root_dir, "bpipe/test", "@author", "process_medium", False, False) - module_create.create() + with requests_mock.Mocker() as mock: + mock_api_calls(mock, "bpipe", "0.9.11") + # bpipe is a valid package on bioconda that is very unlikely to ever be added to nf-core/modules + module_create = nf_core.modules.ModuleCreate(root_dir, "bpipe/test", "@author", "process_medium", False, False) + module_create.create() return root_dir diff --git a/tests/utils.py b/tests/utils.py index fb592a6209..483f817f20 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -66,3 +66,22 @@ def set_wd(path: Path): yield finally: os.chdir(start_wd) + + +def mock_api_calls(mock, module, version): + """Mock biocontainers and anaconda api calls for module""" + biocontainers_api_url = f"https://api.biocontainers.pro/ga4gh/trs/v2/tools/{module}/versions/{module}-{version}" + anaconda_api_url = f"https://api.anaconda.org/package/bioconda/{module}" + mock.register_uri("GET", biocontainers_api_url, text="to modify when the api works and I can know what to add") + anaconda_mock = { + "status_code": 200, + "latest_version": version, + "summary": "", + "doc_url": "", + "dev_url": "", + "files": [{"version": version}], + "license": "", + } + biocontainers_mock = {"status_code": 200, "images": [{"image_type": "Docker", "image_name": f"{module}-{version}"}]} + mock.register_uri("GET", anaconda_api_url, json=anaconda_mock) + mock.register_uri("GET", biocontainers_api_url, json=biocontainers_mock)