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

Modules: Fix empty json output for nf-core modules list local #2668

Merged
merged 7 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@

### Linting

- Fix linting of a pipeline with patched custom module ([#2669](https://github.com/nf-core/tools/pull/2669))
- linting a pipeline also lints the installed subworkflows ([#2677](https://github.com/nf-core/tools/pull/2677))
- environment.yml name must be lowercase ([#2676](https://github.com/nf-core/tools/pull/2676))

### Modules

- Fix linting of a pipeline with patched custom module ([#2669](https://github.com/nf-core/tools/pull/2669))
- Fix empty json output for `nf-core list local` ([#2668](https://github.com/nf-core/tools/pull/2668))

### Subworkflows

Expand Down
3 changes: 2 additions & 1 deletion nf_core/components/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def pattern_msg(keywords: List[str]) -> str:
# We have a pipeline - list what's installed
else:
# Check that we are in a pipeline directory

print(f"{self.repo_type=}")
try:
if self.repo_type != "pipeline":
raise UserWarning(
Expand Down Expand Up @@ -141,6 +141,7 @@ def pattern_msg(keywords: List[str]) -> str:
date = "[red]Not Available"
message = "[red]Not Available"
table.add_row(component, repo_url, version_sha, message, date)
components.append(component)

if print_json:
return json.dumps(components, sort_keys=True, indent=4)
Expand Down
76 changes: 76 additions & 0 deletions tests/modules/list.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import json
from pathlib import Path

import yaml
from rich.console import Console

import nf_core.modules
Expand Down Expand Up @@ -56,3 +60,75 @@ def test_modules_install_gitlab_and_list_pipeline(self):
console.print(listed_mods)
output = console.export_text()
assert "fastqc" in output


def test_modules_list_local_json(self):
"""Test listing locally installed modules as JSON"""
mods_list = nf_core.modules.ModuleList(self.pipeline_dir, remote=False)
listed_mods = mods_list.list_components(print_json=True)
listed_mods = json.loads(listed_mods)
assert "fastqc" in listed_mods
assert "multiqc" in listed_mods


def test_modules_list_remote_json(self):
"""Test listing available modules as JSON"""
mods_list = nf_core.modules.ModuleList(None, remote=True)
listed_mods = mods_list.list_components(print_json=True)
listed_mods = json.loads(listed_mods)
assert "fastqc" in listed_mods
assert "multiqc" in listed_mods


def test_modules_list_with_one_keyword(self):
"""Test listing available modules with one keyword"""
mods_list = nf_core.modules.ModuleList(None, remote=True)
listed_mods = mods_list.list_components(keywords=["qc"])
console = Console(record=True)
console.print(listed_mods)
output = console.export_text()
assert "multiqc" in output


def test_modules_list_with_keywords(self):
"""Test listing available modules with multiple keywords"""
mods_list = nf_core.modules.ModuleList(None, remote=True)
listed_mods = mods_list.list_components(keywords=["fastq", "qc"])
console = Console(record=True)
console.print(listed_mods)
output = console.export_text()
assert "fastqc" in output


def test_modules_list_with_unused_keyword(self):
"""Test listing available modules with an unused keyword"""
mods_list = nf_core.modules.ModuleList(None, remote=True)
with self.assertLogs(level="INFO") as log:
listed_mods = mods_list.list_components(keywords=["you_will_never_find_me"])
self.assertIn("No available", log.output[0])
# expect empty list
assert listed_mods == ""


def test_modules_list_in_wrong_repo_fail(self):
"""Test listing available modules in a non-pipeline repo"""
# modify repotype in .nf-core.yml
with open(Path(self.pipeline_dir, ".nf-core.yml")) as fh:
nf_core_yml = yaml.safe_load(fh)
nf_core_yml_orig = nf_core_yml.copy()
nf_core_yml["repository_type"] = "modules"
nf_core_yml["org_path"] = "nf-core"

print(nf_core_yml)
with open(Path(self.pipeline_dir, ".nf-core.yml"), "w") as fh:
yaml.safe_dump(nf_core_yml, fh)
# expect error logged
with self.assertLogs(level="ERROR") as log:
mods_list = nf_core.modules.ModuleList(self.pipeline_dir, remote=False)
listed_mods = mods_list.list_components()
self.assertIn("must be run from a pipeline directory", log.output[0])
# expect empty list
assert listed_mods == ""
# restore .nf-core.yml
with open(Path(self.pipeline_dir, ".nf-core.yml"), "w") as fh:
yaml.safe_dump(nf_core_yml_orig, fh)
6 changes: 6 additions & 0 deletions tests/test_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,15 @@ def test_modulesrepo_class(self):
from .modules.list import ( # type: ignore[misc]
test_modules_install_and_list_pipeline,
test_modules_install_gitlab_and_list_pipeline,
test_modules_list_in_wrong_repo_fail,
test_modules_list_local_json,
test_modules_list_pipeline,
test_modules_list_remote,
test_modules_list_remote_gitlab,
test_modules_list_remote_json,
test_modules_list_with_keywords,
test_modules_list_with_one_keyword,
test_modules_list_with_unused_keyword,
)
from .modules.modules_json import ( # type: ignore[misc]
test_get_modules_json,
Expand Down
Loading