From b91e8e9b6b7ce92cad3995595232a931135b6a98 Mon Sep 17 00:00:00 2001 From: yuxinNing Date: Mon, 28 Jul 2025 16:01:35 +0200 Subject: [PATCH 1/3] format --- nf_core/pipelines/lint/rocrate_readme_sync.py | 37 ++++++------------- .../lint/test_rocrate_readme_sync.py | 27 +------------- 2 files changed, 13 insertions(+), 51 deletions(-) diff --git a/nf_core/pipelines/lint/rocrate_readme_sync.py b/nf_core/pipelines/lint/rocrate_readme_sync.py index 7cd503382d..9ee0a6981f 100644 --- a/nf_core/pipelines/lint/rocrate_readme_sync.py +++ b/nf_core/pipelines/lint/rocrate_readme_sync.py @@ -8,14 +8,12 @@ def rocrate_readme_sync(self): """ Check if the RO-Crate description in ro-crate-metadata.json matches the README.md content. - If the --fix is set, the RO-Crate description will be updated to match the README.md content. + If not, the RO-Crate description will be automatically updated to match the README.md content during linting. """ passed = [] - failed = [] ignored = [] fixed = [] - could_fix: bool = False # Check if the file exists before trying to load it metadata_file = Path(self.wf_path, "ro-crate-metadata.json") @@ -27,7 +25,7 @@ def rocrate_readme_sync(self): ignored.append("`ro-crate-metadata.json` not found") if not readme_file.exists(): ignored.append("`README.md` not found") - return {"passed": passed, "failed": failed, "ignored": ignored} + return {"passed": passed, "ignored": ignored} try: metadata_content = metadata_file.read_text(encoding="utf-8") @@ -35,7 +33,7 @@ def rocrate_readme_sync(self): except json.JSONDecodeError as e: log.error("Failed to decode JSON from `ro-crate-metadata.json`: %s", e) ignored.append("Invalid JSON in `ro-crate-metadata.json`") - return {"passed": passed, "failed": failed, "ignored": ignored} + return {"passed": passed, "ignored": ignored} readme_content = readme_file.read_text(encoding="utf-8") graph = metadata_dict.get("@graph") if not graph or not isinstance(graph, list) or not graph[0] or not isinstance(graph[0], dict): @@ -43,30 +41,19 @@ def rocrate_readme_sync(self): else: # Check if the 'description' key is present if "description" not in graph[0]: - if "rocrate_readme_sync" in self.fix: - metadata_dict.get("@graph")[0]["description"] = readme_content - fixed.append("Fixed: add the same description from `README.md` to the RO-Crate metadata.") - else: - ignored.append("No description found in `ro-crate-metadata.json`.") - return {"passed": passed, "failed": failed, "ignored": ignored} + ignored.append("No description found in `ro-crate-metadata.json`.") + metadata_dict.get("@graph")[0]["description"] = readme_content + fixed.append("Fixed: add the same description from `README.md` to the RO-Crate metadata.") rc_description_graph = metadata_dict.get("@graph", [{}])[0].get("description") # Compare the two strings and add a linting error if they don't match if readme_content != rc_description_graph: - # If the --fix flag is set, you could overwrite the RO-Crate description with the README content: - if "rocrate_readme_sync" in self.fix: - metadata_dict.get("@graph")[0]["description"] = readme_content - fixed.append("Fixed: add the same description from `README.md` to the RO-Crate metadata.") - with metadata_file.open("w", encoding="utf-8") as f: - json.dump(metadata_dict, f, indent=4) - passed.append("RO-Crate description matches the `README.md`.") - fixed.append("Mismatch fixed: RO-Crate description updated from `README.md`.") - else: - failed.append( - "The RO-Crate descriptions do not match the README.md content. Use `nf-core pipelines lint --fix rocrate_readme_sync` to update." - ) - could_fix = True + metadata_dict.get("@graph")[0]["description"] = readme_content + with metadata_file.open("w", encoding="utf-8") as f: + json.dump(metadata_dict, f, indent=4) + fixed.append("Fixed: sync the description from `README.md` to the RO-Crate metadata.") + passed.append("RO-Crate description matches the `README.md`.") else: passed.append("RO-Crate descriptions are in sync with `README.md`.") - return {"passed": passed, "failed": failed, "ignored": ignored, "fixed": fixed, "could_fix": could_fix} + return {"passed": passed, "ignored": ignored, "fixed": fixed} diff --git a/tests/pipelines/lint/test_rocrate_readme_sync.py b/tests/pipelines/lint/test_rocrate_readme_sync.py index 6b8e7f7d93..cd600481e2 100644 --- a/tests/pipelines/lint/test_rocrate_readme_sync.py +++ b/tests/pipelines/lint/test_rocrate_readme_sync.py @@ -12,25 +12,6 @@ def test_rocrate_readme_sync_pass(self): assert len(results.get("failed", [])) == 0 assert len(results.get("passed", [])) > 0 - def test_rocrate_readme_sync_fail(self): - self.lint_obj._load() - - json_path = Path(self.lint_obj.wf_path, "ro-crate-metadata.json") - with open(json_path) as f: - try: - rocrate = json.load(f) - except json.JSONDecodeError as e: - raise UserWarning(f"Unable to load JSON file '{json_path}' due to error {e}") - rocrate["@graph"][0]["description"] = "This is a test script" - with open(json_path, "w") as f: - json.dump(rocrate, f, indent=4) - results = self.lint_obj.rocrate_readme_sync() - assert len(results.get("failed", [])) == 1 - assert ( - "The RO-Crate descriptions do not match the README.md content. Use `nf-core pipelines lint --fix rocrate_readme_sync` to update." - in results.get("failed", []) - ) - def test_rocrate_readme_sync_fixed(self): self.lint_obj._load() json_path = Path(self.lint_obj.wf_path, "ro-crate-metadata.json") @@ -43,12 +24,6 @@ def test_rocrate_readme_sync_fixed(self): with open(json_path, "w") as f: json.dump(rocrate, f, indent=4) - results = self.lint_obj.rocrate_readme_sync() - assert len(results.get("failed", [])) == 1 - - # Fix the issue - assert "rocrate_readme_sync" in self.lint_obj.lint_tests - self.lint_obj.fix = ["rocrate_readme_sync"] - self.lint_obj._load() results = self.lint_obj.rocrate_readme_sync() assert len(results.get("failed", [])) == 0 + assert len(results.get("fixed", [])) == 1 From fc9d3b94a38cb1cee3d0f89d08e8acf571cc65fd Mon Sep 17 00:00:00 2001 From: yuxinNing Date: Mon, 28 Jul 2025 20:13:02 +0200 Subject: [PATCH 2/3] format --- nf_core/pipelines/lint/rocrate_readme_sync.py | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/nf_core/pipelines/lint/rocrate_readme_sync.py b/nf_core/pipelines/lint/rocrate_readme_sync.py index 9ee0a6981f..45f9cabcb4 100644 --- a/nf_core/pipelines/lint/rocrate_readme_sync.py +++ b/nf_core/pipelines/lint/rocrate_readme_sync.py @@ -13,6 +13,7 @@ def rocrate_readme_sync(self): passed = [] ignored = [] + failed = [] fixed = [] # Check if the file exists before trying to load it @@ -25,7 +26,7 @@ def rocrate_readme_sync(self): ignored.append("`ro-crate-metadata.json` not found") if not readme_file.exists(): ignored.append("`README.md` not found") - return {"passed": passed, "ignored": ignored} + return {"passed": passed, "failed": failed, "ignored": ignored} try: metadata_content = metadata_file.read_text(encoding="utf-8") @@ -33,27 +34,40 @@ def rocrate_readme_sync(self): except json.JSONDecodeError as e: log.error("Failed to decode JSON from `ro-crate-metadata.json`: %s", e) ignored.append("Invalid JSON in `ro-crate-metadata.json`") - return {"passed": passed, "ignored": ignored} + return {"passed": passed, "failed": failed, "ignored": ignored} readme_content = readme_file.read_text(encoding="utf-8") graph = metadata_dict.get("@graph") + + # Ensure the fix rocrate_readme_sync is in the fix list + if "rocrate_readme_sync" not in self.fix: + self.fix += ("rocrate_readme_sync",) + if not graph or not isinstance(graph, list) or not graph[0] or not isinstance(graph[0], dict): ignored.append("Invalid RO-Crate metadata structure.") else: # Check if the 'description' key is present if "description" not in graph[0]: - ignored.append("No description found in `ro-crate-metadata.json`.") - metadata_dict.get("@graph")[0]["description"] = readme_content - fixed.append("Fixed: add the same description from `README.md` to the RO-Crate metadata.") + if "rocrate_readme_sync" in self.fix: + metadata_dict.get("@graph")[0]["description"] = readme_content + fixed.append("Fixed: add the same description from `README.md` to the RO-Crate metadata.") + else: + ignored.append("No description found in `ro-crate-metadata.json`.") + return {"passed": passed, "failed": failed, "ignored": ignored} rc_description_graph = metadata_dict.get("@graph", [{}])[0].get("description") # Compare the two strings and add a linting error if they don't match if readme_content != rc_description_graph: - metadata_dict.get("@graph")[0]["description"] = readme_content - with metadata_file.open("w", encoding="utf-8") as f: - json.dump(metadata_dict, f, indent=4) - fixed.append("Fixed: sync the description from `README.md` to the RO-Crate metadata.") - passed.append("RO-Crate description matches the `README.md`.") + if "rocrate_readme_sync" in self.fix: + metadata_dict.get("@graph")[0]["description"] = readme_content + with metadata_file.open("w", encoding="utf-8") as f: + json.dump(metadata_dict, f, indent=4) + passed.append("RO-Crate description matches the `README.md`.") + fixed.append("Mismatch fixed: RO-Crate description updated from `README.md`.") + else: + failed.append( + "The RO-Crate descriptions do not match the README.md content. Use `nf-core pipelines lint --fix rocrate_readme_sync` to update." + ) else: passed.append("RO-Crate descriptions are in sync with `README.md`.") - return {"passed": passed, "ignored": ignored, "fixed": fixed} + return {"passed": passed, "failed": failed, "ignored": ignored, "fixed": fixed} From 6b713933fefb19a900b1bca92e66a099b312682d Mon Sep 17 00:00:00 2001 From: yuxinNing Date: Tue, 2 Sep 2025 10:56:39 +0200 Subject: [PATCH 3/3] update --- nf_core/pipelines/lint/rocrate_readme_sync.py | 34 ++++++------------- 1 file changed, 10 insertions(+), 24 deletions(-) diff --git a/nf_core/pipelines/lint/rocrate_readme_sync.py b/nf_core/pipelines/lint/rocrate_readme_sync.py index 45f9cabcb4..ffe9679b3f 100644 --- a/nf_core/pipelines/lint/rocrate_readme_sync.py +++ b/nf_core/pipelines/lint/rocrate_readme_sync.py @@ -13,7 +13,6 @@ def rocrate_readme_sync(self): passed = [] ignored = [] - failed = [] fixed = [] # Check if the file exists before trying to load it @@ -26,7 +25,7 @@ def rocrate_readme_sync(self): ignored.append("`ro-crate-metadata.json` not found") if not readme_file.exists(): ignored.append("`README.md` not found") - return {"passed": passed, "failed": failed, "ignored": ignored} + return {"passed": passed, "fixed": fixed, "ignored": ignored} try: metadata_content = metadata_file.read_text(encoding="utf-8") @@ -34,40 +33,27 @@ def rocrate_readme_sync(self): except json.JSONDecodeError as e: log.error("Failed to decode JSON from `ro-crate-metadata.json`: %s", e) ignored.append("Invalid JSON in `ro-crate-metadata.json`") - return {"passed": passed, "failed": failed, "ignored": ignored} + return {"passed": passed, "fixed": fixed, "ignored": ignored} readme_content = readme_file.read_text(encoding="utf-8") graph = metadata_dict.get("@graph") - # Ensure the fix rocrate_readme_sync is in the fix list - if "rocrate_readme_sync" not in self.fix: - self.fix += ("rocrate_readme_sync",) - if not graph or not isinstance(graph, list) or not graph[0] or not isinstance(graph[0], dict): ignored.append("Invalid RO-Crate metadata structure.") else: # Check if the 'description' key is present if "description" not in graph[0]: - if "rocrate_readme_sync" in self.fix: - metadata_dict.get("@graph")[0]["description"] = readme_content - fixed.append("Fixed: add the same description from `README.md` to the RO-Crate metadata.") - else: - ignored.append("No description found in `ro-crate-metadata.json`.") - return {"passed": passed, "failed": failed, "ignored": ignored} + metadata_dict.get("@graph")[0]["description"] = readme_content + fixed.append("Fixed: add the same description from `README.md` to the RO-Crate metadata.") rc_description_graph = metadata_dict.get("@graph", [{}])[0].get("description") # Compare the two strings and add a linting error if they don't match if readme_content != rc_description_graph: - if "rocrate_readme_sync" in self.fix: - metadata_dict.get("@graph")[0]["description"] = readme_content - with metadata_file.open("w", encoding="utf-8") as f: - json.dump(metadata_dict, f, indent=4) - passed.append("RO-Crate description matches the `README.md`.") - fixed.append("Mismatch fixed: RO-Crate description updated from `README.md`.") - else: - failed.append( - "The RO-Crate descriptions do not match the README.md content. Use `nf-core pipelines lint --fix rocrate_readme_sync` to update." - ) + metadata_dict.get("@graph")[0]["description"] = readme_content + with metadata_file.open("w", encoding="utf-8") as f: + json.dump(metadata_dict, f, indent=4) + passed.append("RO-Crate description matches the `README.md`.") + fixed.append("Mismatch fixed: RO-Crate description updated from `README.md`.") else: passed.append("RO-Crate descriptions are in sync with `README.md`.") - return {"passed": passed, "failed": failed, "ignored": ignored, "fixed": fixed} + return {"passed": passed, "fixed": fixed, "ignored": ignored}