Skip to content

Commit

Permalink
Merge pull request #1624 from mirpedrol/module-selection
Browse files Browse the repository at this point in the history
obtain module suggestions from installed modules
  • Loading branch information
ewels authored Jul 5, 2022
2 parents ee54b9f + 0b06097 commit 7eaeaf8
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
58 changes: 48 additions & 10 deletions nf_core/modules/module_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion nf_core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
14 changes: 14 additions & 0 deletions tests/modules/module_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions tests/test_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit 7eaeaf8

Please sign in to comment.