diff --git a/CHANGELOG.md b/CHANGELOG.md index 30e7d12568..3e2501f1d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ - Update how we interface with git remotes. ([#1626](https://github.com/nf-core/tools/issues/1626)) - Add prompt for module name to `nf-core modules info` ([#1644](https://github.com/nf-core/tools/issues/1644)) - Update docs with example of custom git remote ([#1645](https://github.com/nf-core/tools/issues/1645)) +- Command `nf-core modules test` obtains module name suggestions from installed modules ([#1624](https://github.com/nf-core/tools/pull/1624)) - Add `--base-path` flag to `nf-core modules` to specify the base path for the modules in a remote. Also refactored `modules.json` code. ([#1643](https://github.com/nf-core/tools/issues/1643)) ## [v2.4.1 - Cobolt Koala Patch](https://github.com/nf-core/tools/releases/tag/2.4) - [2022-05-16] diff --git a/nf_core/modules/module_test.py b/nf_core/modules/module_test.py index a57235027f..2311416fe1 100644 --- a/nf_core/modules/module_test.py +++ b/nf_core/modules/module_test.py @@ -13,14 +13,14 @@ import questionary import rich +import nf_core.modules.module_utils import nf_core.utils - -from .modules_repo import ModulesRepo +from nf_core.modules.modules_command import ModuleCommand log = logging.getLogger(__name__) -class ModulesTest(object): +class ModulesTest(ModuleCommand): """ Class to run module pytests. @@ -52,11 +52,17 @@ def __init__( module_name=None, no_prompts=False, pytest_args="", + remote_url=None, + branch=None, + no_pull=False, ): self.module_name = module_name self.no_prompts = no_prompts self.pytest_args = pytest_args + super().__init__(".", remote_url, branch, no_pull) + self.get_pipeline_modules() + def run(self): """Run test steps""" if not self.no_prompts: @@ -71,25 +77,57 @@ def run(self): def _check_inputs(self): """Do more complex checks about supplied flags.""" + # Retrieving installed modules + if self.repo_type == "modules": + installed_modules = self.module_names["modules"] + else: + installed_modules = self.module_names.get(self.modules_repo.fullname) + # Get the tool name if not specified if self.module_name is None: if self.no_prompts: raise UserWarning( "Tool name not provided and prompts deactivated. Please provide the tool name as TOOL/SUBTOOL or TOOL." ) - modules_repo = ModulesRepo() - modules_repo.get_modules_file_tree() + if installed_modules is None: + raise UserWarning( + f"No installed modules were found from '{self.modules_repo.remote_url}'.\n" + f"Are you running the tests inside the nf-core/modules main directory?\n" + f"Otherwise, make sure that the directory structure is modules/TOOL/SUBTOOL/ and tests/modules/TOOLS/SUBTOOL/" + ) self.module_name = questionary.autocomplete( "Tool name:", - choices=modules_repo.get_avail_modules(), + choices=installed_modules, style=nf_core.utils.nfcore_question_style, ).unsafe_ask() - module_dir = Path("modules") / self.module_name - # First, sanity check that the module directory exists - if not module_dir.is_dir(): + # Sanity check that the module directory exists + self._validate_folder_structure() + + def _validate_folder_structure(self): + """Validate that the modules follow the correct folder structure to run the tests: + - modules/TOOL/SUBTOOL/ + - tests/modules/TOOL/SUBTOOL/ + + """ + basedir = "modules/nf-core" + + if self.repo_type == "modules": + module_path = Path("modules") / self.module_name + test_path = Path("tests/modules") / self.module_name + else: + module_path = Path(f"{basedir}/modules") / self.module_name + test_path = Path(f"{basedir}/tests/modules") / self.module_name + + if not (self.dir / module_path).is_dir(): + raise UserWarning( + f"Cannot find directory '{module_path}'. Should be TOOL/SUBTOOL or TOOL. Are you running the tests inside the nf-core/modules main directory?" + ) + if not (self.dir / test_path).is_dir(): raise UserWarning( - f"Cannot find directory '{module_dir}'. Should be TOOL/SUBTOOL or TOOL. Are you running the tests inside the nf-core/modules main directory?" + f"Cannot find directory '{test_path}'. Should be TOOL/SUBTOOL or TOOL. " + "Are you running the tests inside the nf-core/modules main directory? " + "Do you have tests for the specified module?" ) def _set_profile(self): diff --git a/nf_core/utils.py b/nf_core/utils.py index db45cb8b2f..e406108302 100644 --- a/nf_core/utils.py +++ b/nf_core/utils.py @@ -52,7 +52,7 @@ NFCORE_CACHE_DIR = os.path.join( os.environ.get("XDG_CACHE_HOME", os.path.join(os.getenv("HOME"), ".cache")), - "nf-core", + "nfcore", ) NFCORE_DIR = os.path.join(os.environ.get("XDG_CONFIG_HOME", os.path.join(os.getenv("HOME"), ".config")), "nfcore") diff --git a/tests/modules/module_test.py b/tests/modules/module_test.py index 40035c2724..c29297392a 100644 --- a/tests/modules/module_test.py +++ b/tests/modules/module_test.py @@ -26,3 +26,17 @@ def test_modules_test_no_name_no_prompts(self): meta_builder._check_inputs() os.chdir(cwd) assert "Tool name not provided and prompts deactivated." in str(excinfo.value) + + +def test_modules_test_no_installed_modules(self): + """Test the check_inputs() function - raise UserWarning because installed modules were not found""" + cwd = os.getcwd() + os.chdir(self.nfcore_modules) + meta_builder = nf_core.modules.ModulesTest(None, False, "") + meta_builder.module_names["modules"] = None + meta_builder.repo_type = "modules" + with pytest.raises(UserWarning) as excinfo: + meta_builder._check_inputs() + os.chdir(cwd) + print(excinfo.value) + assert "No installed modules were found" in str(excinfo.value) diff --git a/tests/test_modules.py b/tests/test_modules.py index 798686d4ed..d96b3b85f6 100644 --- a/tests/test_modules.py +++ b/tests/test_modules.py @@ -109,6 +109,7 @@ def test_modulesrepo_class(self): ) from .modules.module_test import ( test_modules_test_check_inputs, + test_modules_test_no_installed_modules, test_modules_test_no_name_no_prompts, ) from .modules.remove import (