diff --git a/nf_core/modules/modules_json.py b/nf_core/modules/modules_json.py index de37917ef..ea3527663 100644 --- a/nf_core/modules/modules_json.py +++ b/nf_core/modules/modules_json.py @@ -546,6 +546,16 @@ def check_up_to_date(self): self.load() if not self.has_git_url_and_modules(): raise UserWarning + # check that all "installed_by" entries are lists and not strings + # [these strings come from an older dev version, so this check can probably be removed in a future release] + for _, repo_entry in self.modules_json.get("repos", {}).items(): + for component_type in ["modules", "subworkflows"]: + if component_type in repo_entry: + for install_dir, install_dir_entry in repo_entry[component_type].items(): + for _, component in install_dir_entry.items(): + if "installed_by" in component and isinstance(component["installed_by"], str): + log.debug(f"Updating {component} in modules.json") + component["installed_by"] = [component["installed_by"]] except UserWarning: log.info("The 'modules.json' file is not up to date. Recreating the 'modules.json' file.") self.create() diff --git a/tests/modules/install.py b/tests/modules/install.py index 1c3f1aefc..d01459f14 100644 --- a/tests/modules/install.py +++ b/tests/modules/install.py @@ -71,3 +71,15 @@ def test_modules_install_different_branch_succeed(self): modules_json.get_component_branch(self.component_type, "fastp", GITLAB_URL, GITLAB_REPO) == GITLAB_BRANCH_TEST_BRANCH ) + + +def test_modules_install_tracking(self): + """Test installing a module and finding 'modules' in the installed_by section of modules.json""" + self.mods_install.install("trimgalore") + + # Verify that the installed_by entry was added correctly + modules_json = ModulesJson(self.pipeline_dir) + mod_json = modules_json.get_modules_json() + assert mod_json["repos"]["https://github.com/nf-core/modules.git"]["modules"]["nf-core"]["trimgalore"][ + "installed_by" + ] == ["modules"] diff --git a/tests/subworkflows/install.py b/tests/subworkflows/install.py index 94b2d79ba..6c04c9ad2 100644 --- a/tests/subworkflows/install.py +++ b/tests/subworkflows/install.py @@ -76,3 +76,67 @@ def test_subworkflows_install_different_branch_fail(self): install_obj = SubworkflowInstall(self.pipeline_dir, remote_url=GITLAB_URL, branch=GITLAB_BRANCH_TEST_BRANCH) # The bam_stats_samtools subworkflow does not exists in the branch-test branch assert install_obj.install("bam_stats_samtools") is False + + +def test_subworkflows_install_tracking(self): + """Test installing a subworkflow and finding the correct entries in installed_by section of modules.json""" + self.subworkflow_install.install("bam_sort_stats_samtools") + + # Verify that the installed_by entry was added correctly + modules_json = ModulesJson(self.pipeline_dir) + mod_json = modules_json.get_modules_json() + assert mod_json["repos"]["https://github.com/nf-core/modules.git"]["subworkflows"]["nf-core"][ + "bam_sort_stats_samtools" + ]["installed_by"] == ["subworkflows"] + assert mod_json["repos"]["https://github.com/nf-core/modules.git"]["subworkflows"]["nf-core"]["bam_stats_samtools"][ + "installed_by" + ] == ["bam_sort_stats_samtools"] + assert mod_json["repos"]["https://github.com/nf-core/modules.git"]["modules"]["nf-core"]["samtools/stats"][ + "installed_by" + ] == ["bam_stats_samtools"] + assert mod_json["repos"]["https://github.com/nf-core/modules.git"]["modules"]["nf-core"]["samtools/sort"][ + "installed_by" + ] == ["bam_sort_stats_samtools"] + + # Clean directory + self.subworkflow_remove.remove("bam_sort_stats_samtools") + + +def test_subworkflows_install_tracking_added_already_installed(self): + """Test installing a subworkflow and finding the correct entries in installed_by section of modules.json""" + self.subworkflow_install.install("bam_sort_stats_samtools") + self.subworkflow_install.install("bam_stats_samtools") + + # Verify that the installed_by entry was added correctly + modules_json = ModulesJson(self.pipeline_dir) + mod_json = modules_json.get_modules_json() + assert mod_json["repos"]["https://github.com/nf-core/modules.git"]["subworkflows"]["nf-core"][ + "bam_sort_stats_samtools" + ]["installed_by"] == ["subworkflows"] + assert sorted( + mod_json["repos"]["https://github.com/nf-core/modules.git"]["subworkflows"]["nf-core"]["bam_stats_samtools"][ + "installed_by" + ] + ) == sorted(["bam_sort_stats_samtools", "subworkflows"]) + + # Clean directory + self.subworkflow_remove.remove("bam_sort_stats_samtools") + self.subworkflow_remove.remove("bam_stats_samtools") + + +def test_subworkflows_install_tracking_added_super_subworkflow(self): + """Test installing a subworkflow and finding the correct entries in installed_by section of modules.json""" + self.subworkflow_install.install("bam_stats_samtools") + self.subworkflow_install.install("bam_sort_stats_samtools") + + # Verify that the installed_by entry was added correctly + modules_json = ModulesJson(self.pipeline_dir) + mod_json = modules_json.get_modules_json() + assert mod_json["repos"]["https://github.com/nf-core/modules.git"]["subworkflows"]["nf-core"][ + "bam_sort_stats_samtools" + ]["installed_by"] == ["subworkflows"] + assert sorted( + mod_json["repos"]["https://github.com/nf-core/modules.git"]["subworkflows"]["nf-core"]["bam_stats_samtools"][ + "installed_by" + ] + ) == sorted(["subworkflows", "bam_sort_stats_samtools"]) diff --git a/tests/test_modules.py b/tests/test_modules.py index b34bdb4f0..72f7e3730 100644 --- a/tests/test_modules.py +++ b/tests/test_modules.py @@ -152,6 +152,7 @@ def test_modulesrepo_class(self): test_modules_install_from_gitlab, test_modules_install_nomodule, test_modules_install_nopipeline, + test_modules_install_tracking, test_modules_install_trimgalore, test_modules_install_trimgalore_twice, ) diff --git a/tests/test_subworkflows.py b/tests/test_subworkflows.py index c831582f5..552a2ab17 100644 --- a/tests/test_subworkflows.py +++ b/tests/test_subworkflows.py @@ -107,6 +107,9 @@ def tearDown(self): test_subworkflows_install_emptypipeline, test_subworkflows_install_from_gitlab, test_subworkflows_install_nosubworkflow, + test_subworkflows_install_tracking, + test_subworkflows_install_tracking_added_already_installed, + test_subworkflows_install_tracking_added_super_subworkflow, ) from .subworkflows.list import ( test_subworkflows_install_and_list_subworkflows,