Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

obtain module suggestions from installed modules #1624

Merged
merged 22 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
75 changes: 65 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,25 @@ 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

# Check repository type
try:
pipeline_dir, repo_type = nf_core.modules.module_utils.get_repo_type(".", use_prompt=False)
log.debug(f"Found {repo_type} repo: {pipeline_dir}")
except UserWarning as e:
log.debug(f"Only showing remote info: {e}")
pipeline_dir = None

super().__init__(pipeline_dir, remote_url, branch, no_pull)
self.get_pipeline_modules(True) # To change!

def run(self):
"""Run test steps"""
if not self.no_prompts:
Expand All @@ -71,25 +85,66 @@ def run(self):
def _check_inputs(self):
"""Do more complex checks about supplied flags."""

# Retrieving installed modules
if self.repo_type == "modules":
local = False
installed_modules = self.module_names["modules"]
else:
local = questionary.confirm("Is the module local?", style=nf_core.utils.nfcore_question_style).unsafe_ask()
if local:
installed_modules = self.module_names.get("local/modules")
else:
installed_modules = self.module_names.get("nf-core/modules")
mirpedrol marked this conversation as resolved.
Show resolved Hide resolved

# 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(
"No installed modules were found from '{self.modules_repo.remote_url}'.\n"
"Are you running the tests inside the nf-core/modules main directory?\n"
"Otherwise, make sure that the directory structure is modules/TOOL/SUBTOOL/ and tests/modules/TOOLS/SUBTOOL/"
mirpedrol marked this conversation as resolved.
Show resolved Hide resolved
)
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(local)

def _validate_folder_structure(self, local=False):
"""Validate that the modules follow the correct folder structure to run the tests:
- modules/TOOL/SUBTOOL/
- tests/modules/TOOL/SUBTOOL/

local (bool): Testing local modules (True) or nf-core modules (False)
"""
if local:
basedir = "modules/local"
else:
basedir = "modules/nf-core"

if self.repo_type == "modules":
module_path = Path("modules") / self.module_name
mirpedrol marked this conversation as resolved.
Show resolved Hide resolved
test_path = Path("tests/modules") / self.module_name
else:
module_path = Path(f"{basedir}/modules") / self.module_name
mirpedrol marked this conversation as resolved.
Show resolved Hide resolved
test_path = Path(f"{basedir}/tests/modules") / self.module_name

if not 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?"
mirpedrol marked this conversation as resolved.
Show resolved Hide resolved
)
if not test_path.is_dir():
mirpedrol marked this conversation as resolved.
Show resolved Hide resolved
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?"
mirpedrol marked this conversation as resolved.
Show resolved Hide resolved
"Do you have tests for the specified module?"
)

def _set_profile(self):
Expand Down
9 changes: 7 additions & 2 deletions nf_core/modules/modules_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self, dir, remote_url=None, branch=None, no_pull=False, base_path=N
except LookupError as e:
raise UserWarning(e)

def get_pipeline_modules(self):
def get_pipeline_modules(self, retrieve_local=False):
mirpedrol marked this conversation as resolved.
Show resolved Hide resolved
"""
Get the modules installed in the current directory.

Expand All @@ -44,14 +44,19 @@ def get_pipeline_modules(self):
is a clone of nf-core/modules the filed is set to
`{"modules": modules_in_dir}`

retrieve_local (bool): True if retrieving local modules

"""

self.module_names = {}

module_base_path = f"{self.dir}/modules/"

if self.repo_type == "pipeline":
repo_owners = (owner for owner in os.listdir(module_base_path) if owner != "local")
if retrieve_local:
repo_owners = os.listdir(module_base_path)
else:
repo_owners = (owner for owner in os.listdir(module_base_path) if owner != "local")
repo_names = (
f"{repo_owner}/{name}"
for repo_owner in repo_owners
Expand Down
17 changes: 17 additions & 0 deletions tests/modules/module_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,20 @@ 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_proper_path(self):
"""Test the check_inputs() function - raise UserWarning because installed modules were not found"""
cwd = os.getcwd()
with pytest.raises(SystemError):
assert nf_core.modules.ModulesTest(".", False, "")


def test_modules_test_all_modules_listed(self):
"""Test the check_inputs() function - raise UserWarning because installed modules were not found"""
cwd = os.getcwd()
meta_builder = nf_core.modules.ModulesTest(".", False, "")
with pytest.raises(UserWarning) as excinfo:
meta_builder._check_inputs()
os.chdir(cwd)
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 @@ -110,6 +110,7 @@ def test_modulesrepo_class(self):
from .modules.module_test import (
test_modules_test_check_inputs,
test_modules_test_no_name_no_prompts,
test_modules_test_no_proper_path,
)
from .modules.remove import (
test_modules_remove_trimgalore,
Expand Down