Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

- accept `process_low_memory` as a standard module label ([#4264](https://github.com/nf-core/tools/pull/4264))
- improve linting for `modules.json` to add support for only installing subworkflows from a repository and provide more explicit error messages ([#4287](https://github.com/nf-core/tools/pull/4287))
- Replace `ext.when` linting to disallow use ([#4314](https://github.com/nf-core/tools/pull/4314))

### Modules

Expand Down
3 changes: 0 additions & 3 deletions nf_core/module-template/main.nf
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ process {{ component_name_underscore|upper }} {
{%- endif %}
tuple val("${task.process}"), val('{{ component }}'), eval("{{ component }} --version"), topic: versions, emit: versions_{{ component }}

when:
task.ext.when == null || task.ext.when

script:
def args = task.ext.args ?: ''
{% if has_meta -%}
Expand Down
31 changes: 21 additions & 10 deletions nf_core/modules/lint/main_nf.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,22 +341,33 @@ def check_script_section(self, lines):

def check_when_section(self, lines):
"""
Lint the when: section
Checks whether the line is modified from 'task.ext.when == null || task.ext.when'
Lint the when: section.

The module template no longer allows `task.ext.when` in `when:` blocks.
"""
if len(lines) == 0:
self.failed.append(("main_nf", "when_exist", "when: condition has been removed", self.main_nf))
return
if len(lines) > 1:
self.failed.append(("main_nf", "when_exist", "when: condition has too many lines", self.main_nf))
self.passed.append(("main_nf", "when_exist", "No when: condition found", self.main_nf))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.passed.append(("main_nf", "when_exist", "No when: condition found", self.main_nf))
self.passed.append(("main_nf", "no_when", "No `when:` condition found", self.main_nf))

better to use a new linting rule name if the rule changed to the opposite

return
self.passed.append(("main_nf", "when_exist", "when: condition is present", self.main_nf))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should also be removed then.


# Check the condition hasn't been changed.
if lines[0].strip() != "task.ext.when == null || task.ext.when":
self.failed.append(("main_nf", "when_condition", "when: condition has been altered", self.main_nf))
if any("task.ext.when" in line for line in lines):
self.failed.append(
(
"main_nf",
"when_condition",
"task.ext.when is no longer allowed in when: condition",
self.main_nf,
)
)
return
self.passed.append(("main_nf", "when_condition", "when: condition is unchanged", self.main_nf))
self.passed.append(
(
"main_nf",
"when_condition",
"when: condition does not use task.ext.when",
self.main_nf,
)
)


def check_process_section(self, lines, registry, fix_version, progress_bar):
Expand Down
21 changes: 21 additions & 0 deletions tests/modules/lint/test_main_nf.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
check_container_link_line,
check_process_labels,
check_script_section,
check_when_section,
)

from ...test_modules import TestModules
Expand Down Expand Up @@ -101,6 +102,26 @@ def test_container_links(content, passed, warned, failed):
assert len(mock_lint.failed) == failed


def test_when_section_rejects_task_ext_when():
"""Test that task.ext.when in a when: block fails linting"""
mock_lint = MockModuleLint()

check_when_section(mock_lint, ["task.ext.when == null || task.ext.when"])

assert len(mock_lint.failed) == 1
assert "task.ext.when" in mock_lint.failed[0][2]


def test_when_section_allows_missing_when_block():
"""Test that modules without a when: block pass this lint"""
mock_lint = MockModuleLint()

check_when_section(mock_lint, [])

assert len(mock_lint.failed) == 0
assert any("No when: condition found" in message for _, _, message, _ in mock_lint.passed)


class TestMainNfLinting(TestModules):
"""
Test main.nf linting functionality.
Expand Down
Loading