Skip to content

Commit 2b6b60c

Browse files
Automatically sync the Rocrate and README content during pipelines lint (#3688)
* format * format * update * update MultiQC version
1 parent 75e534e commit 2b6b60c

File tree

2 files changed

+13
-51
lines changed

2 files changed

+13
-51
lines changed

nf_core/pipelines/lint/rocrate_readme_sync.py

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@
88
def rocrate_readme_sync(self):
99
"""
1010
Check if the RO-Crate description in ro-crate-metadata.json matches the README.md content.
11-
If the --fix is set, the RO-Crate description will be updated to match the README.md content.
11+
If not, the RO-Crate description will be automatically updated to match the README.md content during linting.
1212
"""
1313

1414
passed = []
15-
failed = []
1615
ignored = []
1716
fixed = []
18-
could_fix: bool = False
1917

2018
# Check if the file exists before trying to load it
2119
metadata_file = Path(self.wf_path, "ro-crate-metadata.json")
@@ -27,46 +25,35 @@ def rocrate_readme_sync(self):
2725
ignored.append("`ro-crate-metadata.json` not found")
2826
if not readme_file.exists():
2927
ignored.append("`README.md` not found")
30-
return {"passed": passed, "failed": failed, "ignored": ignored}
28+
return {"passed": passed, "fixed": fixed, "ignored": ignored}
3129

3230
try:
3331
metadata_content = metadata_file.read_text(encoding="utf-8")
3432
metadata_dict = json.loads(metadata_content)
3533
except json.JSONDecodeError as e:
3634
log.error("Failed to decode JSON from `ro-crate-metadata.json`: %s", e)
3735
ignored.append("Invalid JSON in `ro-crate-metadata.json`")
38-
return {"passed": passed, "failed": failed, "ignored": ignored}
36+
return {"passed": passed, "fixed": fixed, "ignored": ignored}
3937
readme_content = readme_file.read_text(encoding="utf-8")
4038
graph = metadata_dict.get("@graph")
39+
4140
if not graph or not isinstance(graph, list) or not graph[0] or not isinstance(graph[0], dict):
4241
ignored.append("Invalid RO-Crate metadata structure.")
4342
else:
4443
# Check if the 'description' key is present
4544
if "description" not in graph[0]:
46-
if "rocrate_readme_sync" in self.fix:
47-
metadata_dict.get("@graph")[0]["description"] = readme_content
48-
fixed.append("Fixed: add the same description from `README.md` to the RO-Crate metadata.")
49-
else:
50-
ignored.append("No description found in `ro-crate-metadata.json`.")
51-
return {"passed": passed, "failed": failed, "ignored": ignored}
45+
metadata_dict.get("@graph")[0]["description"] = readme_content
46+
fixed.append("Fixed: add the same description from `README.md` to the RO-Crate metadata.")
5247

5348
rc_description_graph = metadata_dict.get("@graph", [{}])[0].get("description")
5449

5550
# Compare the two strings and add a linting error if they don't match
5651
if readme_content != rc_description_graph:
57-
# If the --fix flag is set, you could overwrite the RO-Crate description with the README content:
58-
if "rocrate_readme_sync" in self.fix:
59-
metadata_dict.get("@graph")[0]["description"] = readme_content
60-
fixed.append("Fixed: add the same description from `README.md` to the RO-Crate metadata.")
61-
with metadata_file.open("w", encoding="utf-8") as f:
62-
json.dump(metadata_dict, f, indent=4)
63-
passed.append("RO-Crate description matches the `README.md`.")
64-
fixed.append("Mismatch fixed: RO-Crate description updated from `README.md`.")
65-
else:
66-
failed.append(
67-
"The RO-Crate descriptions do not match the README.md content. Use `nf-core pipelines lint --fix rocrate_readme_sync` to update."
68-
)
69-
could_fix = True
52+
metadata_dict.get("@graph")[0]["description"] = readme_content
53+
with metadata_file.open("w", encoding="utf-8") as f:
54+
json.dump(metadata_dict, f, indent=4)
55+
passed.append("RO-Crate description matches the `README.md`.")
56+
fixed.append("Mismatch fixed: RO-Crate description updated from `README.md`.")
7057
else:
7158
passed.append("RO-Crate descriptions are in sync with `README.md`.")
72-
return {"passed": passed, "failed": failed, "ignored": ignored, "fixed": fixed, "could_fix": could_fix}
59+
return {"passed": passed, "fixed": fixed, "ignored": ignored}

tests/pipelines/lint/test_rocrate_readme_sync.py

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,6 @@ def test_rocrate_readme_sync_pass(self):
1212
assert len(results.get("failed", [])) == 0
1313
assert len(results.get("passed", [])) > 0
1414

15-
def test_rocrate_readme_sync_fail(self):
16-
self.lint_obj._load()
17-
18-
json_path = Path(self.lint_obj.wf_path, "ro-crate-metadata.json")
19-
with open(json_path) as f:
20-
try:
21-
rocrate = json.load(f)
22-
except json.JSONDecodeError as e:
23-
raise UserWarning(f"Unable to load JSON file '{json_path}' due to error {e}")
24-
rocrate["@graph"][0]["description"] = "This is a test script"
25-
with open(json_path, "w") as f:
26-
json.dump(rocrate, f, indent=4)
27-
results = self.lint_obj.rocrate_readme_sync()
28-
assert len(results.get("failed", [])) == 1
29-
assert (
30-
"The RO-Crate descriptions do not match the README.md content. Use `nf-core pipelines lint --fix rocrate_readme_sync` to update."
31-
in results.get("failed", [])
32-
)
33-
3415
def test_rocrate_readme_sync_fixed(self):
3516
self.lint_obj._load()
3617
json_path = Path(self.lint_obj.wf_path, "ro-crate-metadata.json")
@@ -43,12 +24,6 @@ def test_rocrate_readme_sync_fixed(self):
4324
with open(json_path, "w") as f:
4425
json.dump(rocrate, f, indent=4)
4526

46-
results = self.lint_obj.rocrate_readme_sync()
47-
assert len(results.get("failed", [])) == 1
48-
49-
# Fix the issue
50-
assert "rocrate_readme_sync" in self.lint_obj.lint_tests
51-
self.lint_obj.fix = ["rocrate_readme_sync"]
52-
self.lint_obj._load()
5327
results = self.lint_obj.rocrate_readme_sync()
5428
assert len(results.get("failed", [])) == 0
29+
assert len(results.get("fixed", [])) == 1

0 commit comments

Comments
 (0)