diff --git a/CHANGELOG.md b/CHANGELOG.md index 73f0d55586..7ea7986b93 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/nf_core/components/list.py b/nf_core/components/list.py index 47c0eaad62..b0c5af219f 100644 --- a/nf_core/components/list.py +++ b/nf_core/components/list.py @@ -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( @@ -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) diff --git a/tests/modules/list.py b/tests/modules/list.py index d92cd58dd5..3cb00a84d6 100644 --- a/tests/modules/list.py +++ b/tests/modules/list.py @@ -1,3 +1,7 @@ +import json +from pathlib import Path + +import yaml from rich.console import Console import nf_core.modules @@ -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) diff --git a/tests/test_modules.py b/tests/test_modules.py index f7ada2a483..f9c3b6f2a7 100644 --- a/tests/test_modules.py +++ b/tests/test_modules.py @@ -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,