Skip to content

Commit 7e90309

Browse files
committed
fix: Address PR review feedback
- Clean up AI-generated comments and verbose documentation - Improve version hash detection to handle both content and keys - Add test case for version hash in snapshot keys scenario - Update CHANGELOG.md with feature description - Preserve all existing pytest-workflow logic for separate PR Addresses feedback from @mashehu and @mirpedrol Related to: nf-core/modules#6505
1 parent 11bcdcb commit 7e90309

File tree

3 files changed

+55
-80
lines changed

3 files changed

+55
-80
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
- Support modules with `exec:` blocks ([#3633](https://github.com/nf-core/tools/pull/3633))
1414
- feat: nf-core modules bump-version supports specifying the toolkit ([#3608](https://github.com/nf-core/tools/pull/3608))
15+
- Lint for version captures in modules - detect hash format instead of actual YAML content ([#3676](https://github.com/nf-core/tools/pull/3676))
1516

1617
### Subworkflows
1718

nf_core/modules/lint/module_tests.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,12 @@
1616

1717

1818
def _contains_version_hash(test_content):
19-
"""
20-
Check if test content contains version information in hash format.
21-
22-
Uses precise regex patterns to detect version hash formats while avoiding
23-
false positives from similar strings.
24-
25-
Args:
26-
test_content: Content of a single test from snapshot
27-
28-
Returns:
29-
bool: True if hash format detected, False otherwise
30-
"""
31-
# More precise regex patterns with proper boundaries
19+
"""Check if test content contains version information in hash format rather than actual YAML content."""
3220
version_hash_patterns = [
33-
r"\bversions\.yml:md5,[a-f0-9]{32}\b", # Exact MD5 format (32 hex chars)
34-
r"\bversions\.yml:sha[0-9]*,[a-f0-9]+\b", # SHA format with variable length
21+
r"\bversions\.yml:md5,[a-f0-9]{32}\b",
22+
r"\bversions\.yml:sha[0-9]*,[a-f0-9]+\b",
3523
]
3624

37-
# Convert to string only once and search efficiently
3825
content_str = str(test_content)
3926

4027
for pattern in version_hash_patterns:
@@ -44,6 +31,22 @@ def _contains_version_hash(test_content):
4431
return False
4532

4633

34+
def _check_snapshot_for_version_hash(snap_content, test_name):
35+
"""Check both snapshot content and keys for version hash patterns."""
36+
# Check test content for version hashes
37+
if _contains_version_hash(snap_content[test_name]):
38+
return True
39+
40+
# Check specific test's keys for version hashes
41+
test_data = snap_content.get(test_name, {})
42+
if isinstance(test_data, dict):
43+
for key in test_data.keys():
44+
if _contains_version_hash(str(key)):
45+
return True
46+
47+
return False
48+
49+
4750
def module_tests(_, module: NFCoreComponent, allow_missing: bool = False):
4851
"""
4952
Lint the tests of a module in ``nf-core/modules``
@@ -199,11 +202,8 @@ def module_tests(_, module: NFCoreComponent, allow_missing: bool = False):
199202
snap_file,
200203
)
201204
)
202-
# Check if version content is actual content vs MD5/SHA hash
203-
# Related to: https://github.com/nf-core/modules/issues/6505
204-
# Ensures version snapshots contain actual content instead of hash values
205-
if _contains_version_hash(snap_content[test_name]):
206-
# Invalid - contains hash format
205+
# Check if version content contains hash instead of actual YAML content
206+
if _check_snapshot_for_version_hash(snap_content, test_name):
207207
module.failed.append(
208208
(
209209
"test_snap_version_content",
@@ -212,7 +212,6 @@ def module_tests(_, module: NFCoreComponent, allow_missing: bool = False):
212212
)
213213
)
214214
else:
215-
# Valid - either contains actual content or no version hash detected
216215
module.passed.append(
217216
(
218217
"test_snap_version_content",

tests/modules/lint/test_module_tests.py

Lines changed: 33 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def test_modules_empty_file_in_snapshot(self):
171171
"""Test linting a nf-test module with an empty file sha sum in the test snapshot, which should make it fail (if it is not a stub)"""
172172
snap_file = self.bpipe_test_module_path / "tests" / "main.nf.test.snap"
173173
snap = json.load(snap_file.open())
174-
content = snap_file.read_text()
174+
snap_file.read_text()
175175
snap["my test"]["content"][0]["0"] = "test:md5,d41d8cd98f00b204e9800998ecf8427e"
176176

177177
with open(snap_file, "w") as fh:
@@ -184,15 +184,11 @@ def test_modules_empty_file_in_snapshot(self):
184184
assert len(module_lint.warned) >= 0
185185
assert module_lint.failed[0].lint_test == "test_snap_md5sum"
186186

187-
# reset the file
188-
with open(snap_file, "w") as fh:
189-
fh.write(content)
190-
191187
def test_modules_empty_file_in_stub_snapshot(self):
192188
"""Test linting a nf-test module with an empty file sha sum in the stub test snapshot, which should make it not fail"""
193189
snap_file = self.bpipe_test_module_path / "tests" / "main.nf.test.snap"
194190
snap = json.load(snap_file.open())
195-
content = snap_file.read_text()
191+
snap_file.read_text()
196192
snap["my_test_stub"] = {"content": [{"0": "test:md5,d41d8cd98f00b204e9800998ecf8427e", "versions": {}}]}
197193

198194
with open(snap_file, "w") as fh:
@@ -219,20 +215,12 @@ def test_modules_empty_file_in_stub_snapshot(self):
219215

220216
assert found_test, "test_snap_md5sum not found in passed tests"
221217

222-
# reset the file
223-
with open(snap_file, "w") as fh:
224-
fh.write(content)
225-
226218
@pytest.mark.issue("https://github.com/nf-core/modules/issues/6505")
227219
def test_modules_version_snapshot_content_md5_hash(self):
228-
"""Test linting a nf-test module with version information as MD5 hash instead of actual content, which should fail.
229-
230-
Related to: https://github.com/nf-core/modules/issues/6505
231-
Fixed in: https://github.com/nf-core/tools/pull/3676
232-
"""
220+
"""Test linting a nf-test module with version information as MD5 hash instead of actual content, which should fail."""
233221
snap_file = self.bpipe_test_module_path / "tests" / "main.nf.test.snap"
234222
snap = json.load(snap_file.open())
235-
content = snap_file.read_text()
223+
snap_file.read_text()
236224

237225
# Add a version entry with MD5 hash format (the old way that should be flagged)
238226
snap["my test"]["content"][0]["versions"] = "versions.yml:md5,949da9c6297b613b50e24c421576f3f1"
@@ -251,20 +239,12 @@ def test_modules_version_snapshot_content_md5_hash(self):
251239
)
252240
assert version_content_failures[0].lint_test == "test_snap_version_content"
253241

254-
# reset the file
255-
with open(snap_file, "w") as fh:
256-
fh.write(content)
257-
258242
@pytest.mark.issue("https://github.com/nf-core/modules/issues/6505")
259243
def test_modules_version_snapshot_content_valid(self):
260-
"""Test linting a nf-test module with version information as actual content, which should pass.
261-
262-
Related to: https://github.com/nf-core/modules/issues/6505
263-
Fixed in: https://github.com/nf-core/tools/pull/3676
264-
"""
244+
"""Test linting a nf-test module with version information as actual content, which should pass."""
265245
snap_file = self.bpipe_test_module_path / "tests" / "main.nf.test.snap"
266246
snap = json.load(snap_file.open())
267-
content = snap_file.read_text()
247+
snap_file.read_text()
268248

269249
# Add a version entry with actual content (the new way that should pass)
270250
snap["my test"]["content"][0]["versions"] = {"ALE": {"ale": "20180904"}}
@@ -291,20 +271,12 @@ def test_modules_version_snapshot_content_valid(self):
291271
]
292272
assert len(version_content_passed) > 0, "test_snap_version_content not found in passed tests"
293273

294-
# reset the file
295-
with open(snap_file, "w") as fh:
296-
fh.write(content)
297-
298274
@pytest.mark.issue("https://github.com/nf-core/modules/issues/6505")
299275
def test_modules_version_snapshot_content_sha_hash(self):
300-
"""Test linting a nf-test module with version information as SHA hash, which should fail.
301-
302-
Related to: https://github.com/nf-core/modules/issues/6505
303-
Fixed in: https://github.com/nf-core/tools/pull/3676
304-
"""
276+
"""Test linting a nf-test module with version information as SHA hash, which should fail."""
305277
snap_file = self.bpipe_test_module_path / "tests" / "main.nf.test.snap"
306278
snap = json.load(snap_file.open())
307-
content = snap_file.read_text()
279+
snap_file.read_text()
308280

309281
# Add a version entry with SHA hash format (should be flagged)
310282
snap["my test"]["content"][0]["versions"] = (
@@ -323,20 +295,12 @@ def test_modules_version_snapshot_content_sha_hash(self):
323295
f"Expected 1 test_snap_version_content failure, got {len(version_content_failures)}"
324296
)
325297

326-
# reset the file
327-
with open(snap_file, "w") as fh:
328-
fh.write(content)
329-
330298
@pytest.mark.issue("https://github.com/nf-core/modules/issues/6505")
331299
def test_modules_version_snapshot_content_mixed_scenario(self):
332-
"""Test linting with mixed version content - some valid, some hash format.
333-
334-
Related to: https://github.com/nf-core/modules/issues/6505
335-
Fixed in: https://github.com/nf-core/tools/pull/3676
336-
"""
300+
"""Test linting with mixed version content - some valid, some hash format."""
337301
snap_file = self.bpipe_test_module_path / "tests" / "main.nf.test.snap"
338302
snap = json.load(snap_file.open())
339-
content = snap_file.read_text()
303+
snap_file.read_text()
340304

341305
# Create a scenario with multiple tests - one with hash, one with valid content
342306
snap["test_with_hash"] = {"content": [{"versions": "versions.yml:md5,949da9c6297b613b50e24c421576f3f1"}]}
@@ -361,20 +325,12 @@ def test_modules_version_snapshot_content_mixed_scenario(self):
361325
]
362326
assert len(version_content_passed) >= 1, "Expected at least 1 pass for valid content"
363327

364-
# reset the file
365-
with open(snap_file, "w") as fh:
366-
fh.write(content)
367-
368328
@pytest.mark.issue("https://github.com/nf-core/modules/issues/6505")
369329
def test_modules_version_snapshot_no_version_content(self):
370-
"""Test linting when no version information is present - should not trigger version content check.
371-
372-
Related to: https://github.com/nf-core/modules/issues/6505
373-
Fixed in: https://github.com/nf-core/tools/pull/3676
374-
"""
330+
"""Test linting when no version information is present - should not trigger version content check."""
375331
snap_file = self.bpipe_test_module_path / "tests" / "main.nf.test.snap"
376332
snap = json.load(snap_file.open())
377-
content = snap_file.read_text()
333+
snap_file.read_text()
378334

379335
# Remove version information entirely
380336
if "content" in snap["my test"] and snap["my test"]["content"]:
@@ -390,6 +346,25 @@ def test_modules_version_snapshot_no_version_content(self):
390346
version_content_failures = [x for x in module_lint.failed if x.lint_test == "test_snap_version_content"]
391347
assert len(version_content_failures) == 0, "Should not have version content failures when no versions present"
392348

393-
# reset the file
349+
@pytest.mark.issue("https://github.com/nf-core/modules/issues/6505")
350+
def test_modules_version_snapshot_hash_in_keys(self):
351+
"""Test linting when version hash appears in snapshot keys rather than content."""
352+
snap_file = self.bpipe_test_module_path / "tests" / "main.nf.test.snap"
353+
snap = json.load(snap_file.open())
354+
snap_file.read_text()
355+
356+
# Create a test where version hash appears in the test keys
357+
snap["test_with_hash_key"] = {
358+
"content": [{"versions.yml:md5,949da9c6297b613b50e24c421576f3f1": "some_value"}],
359+
"versions": {"BPIPE": {"bpipe": "0.9.11"}},
360+
}
361+
394362
with open(snap_file, "w") as fh:
395-
fh.write(content)
363+
json.dump(snap, fh)
364+
365+
module_lint = nf_core.modules.lint.ModuleLint(directory=self.nfcore_modules)
366+
module_lint.lint(print_results=False, module="bpipe/test")
367+
368+
# Should fail because version hash is in the keys
369+
version_content_failures = [x for x in module_lint.failed if x.lint_test == "test_snap_version_content"]
370+
assert len(version_content_failures) >= 1, "Expected failure for hash in keys"

0 commit comments

Comments
 (0)