diff --git a/CHANGELOG.md b/CHANGELOG.md index d46348e2db..74b5f00fd5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/nf_core/module-template/main.nf b/nf_core/module-template/main.nf index ac40bb511f..3a5a7e59af 100644 --- a/nf_core/module-template/main.nf +++ b/nf_core/module-template/main.nf @@ -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 -%} diff --git a/nf_core/modules/lint/main_nf.py b/nf_core/modules/lint/main_nf.py index b589bc3f0c..42157c311b 100644 --- a/nf_core/modules/lint/main_nf.py +++ b/nf_core/modules/lint/main_nf.py @@ -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)) return self.passed.append(("main_nf", "when_exist", "when: condition is present", self.main_nf)) - # 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): diff --git a/tests/modules/lint/test_main_nf.py b/tests/modules/lint/test_main_nf.py index f6c3848f71..a5344e1cce 100644 --- a/tests/modules/lint/test_main_nf.py +++ b/tests/modules/lint/test_main_nf.py @@ -7,6 +7,7 @@ check_container_link_line, check_process_labels, check_script_section, + check_when_section, ) from ...test_modules import TestModules @@ -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.