|
| 1 | +import pytest |
| 2 | + |
| 3 | +import nf_core.modules.lint |
| 4 | +import nf_core.modules.patch |
| 5 | +from nf_core.modules.lint.main_nf import check_container_link_line, check_process_labels |
| 6 | + |
| 7 | +from ...test_modules import TestModules |
| 8 | +from .test_lint_utils import MockModuleLint |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.parametrize( |
| 12 | + "content,passed,warned,failed", |
| 13 | + [ |
| 14 | + # Valid process label |
| 15 | + ("label 'process_high'\ncpus 12", 1, 0, 0), |
| 16 | + # Non-alphanumeric characters in label |
| 17 | + ("label 'a:label:with:colons'\ncpus 12", 0, 2, 0), |
| 18 | + # Conflicting labels |
| 19 | + ("label 'process_high'\nlabel 'process_low'\ncpus 12", 0, 1, 0), |
| 20 | + # Duplicate labels |
| 21 | + ("label 'process_high'\nlabel 'process_high'\ncpus 12", 0, 2, 0), |
| 22 | + # Valid and non-standard labels |
| 23 | + ("label 'process_high'\nlabel 'process_extra_label'\ncpus 12", 1, 1, 0), |
| 24 | + # Non-standard label only |
| 25 | + ("label 'process_extra_label'\ncpus 12", 0, 2, 0), |
| 26 | + # Non-standard duplicates without quotes |
| 27 | + ("label process_extra_label\nlabel process_extra_label\ncpus 12", 0, 3, 0), |
| 28 | + # No label found |
| 29 | + ("cpus 12", 0, 1, 0), |
| 30 | + ], |
| 31 | +) |
| 32 | +def test_process_labels(content, passed, warned, failed): |
| 33 | + """Test process label validation""" |
| 34 | + mock_lint = MockModuleLint() |
| 35 | + check_process_labels(mock_lint, content.splitlines()) |
| 36 | + |
| 37 | + assert len(mock_lint.passed) == passed |
| 38 | + assert len(mock_lint.warned) == warned |
| 39 | + assert len(mock_lint.failed) == failed |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.parametrize( |
| 43 | + "content,passed,warned,failed", |
| 44 | + [ |
| 45 | + # Single-line container definition should pass |
| 46 | + ('container "quay.io/nf-core/gatk:4.4.0.0" //Biocontainers is missing a package', 2, 0, 0), |
| 47 | + # Multi-line container definition should pass |
| 48 | + ( |
| 49 | + '''container "${ workflow.containerEngine == 'singularity' && !task.ext.singularity_pull_docker_container ? |
| 50 | + 'https://depot.galaxyproject.org/singularity/gatk4:4.4.0.0--py36hdfd78af_0': |
| 51 | + 'biocontainers/gatk4:4.4.0.0--py36hdfd78af_0' }"''', |
| 52 | + 6, |
| 53 | + 0, |
| 54 | + 0, |
| 55 | + ), |
| 56 | + # Space in container URL should fail |
| 57 | + ( |
| 58 | + '''container "${ workflow.containerEngine == 'singularity' && !task.ext.singularity_pull_docker_container ? |
| 59 | + 'https://depot.galaxyproject.org/singularity/gatk4:4.4.0.0--py36hdfd78af_0 ': |
| 60 | + 'biocontainers/gatk4:4.4.0.0--py36hdfd78af_0' }"''', |
| 61 | + 5, |
| 62 | + 0, |
| 63 | + 1, |
| 64 | + ), |
| 65 | + # Incorrect quoting of container string should fail |
| 66 | + ( |
| 67 | + '''container "${ workflow.containerEngine == 'singularity' && !task.ext.singularity_pull_docker_container ? |
| 68 | + 'https://depot.galaxyproject.org/singularity/gatk4:4.4.0.0--py36hdfd78af_0 ': |
| 69 | + "biocontainers/gatk4:4.4.0.0--py36hdfd78af_0" }"''', |
| 70 | + 4, |
| 71 | + 0, |
| 72 | + 1, |
| 73 | + ), |
| 74 | + ], |
| 75 | +) |
| 76 | +def test_container_links(content, passed, warned, failed): |
| 77 | + """Test container link validation""" |
| 78 | + mock_lint = MockModuleLint() |
| 79 | + |
| 80 | + for line in content.splitlines(): |
| 81 | + if line.strip(): |
| 82 | + check_container_link_line(mock_lint, line, registry="quay.io") |
| 83 | + |
| 84 | + assert len(mock_lint.passed) == passed |
| 85 | + assert len(mock_lint.warned) == warned |
| 86 | + assert len(mock_lint.failed) == failed |
| 87 | + |
| 88 | + |
| 89 | +class TestMainNfLinting(TestModules): |
| 90 | + """ |
| 91 | + Test main.nf linting functionality. |
| 92 | +
|
| 93 | + This class tests various aspects of main.nf file linting including: |
| 94 | + - Process label validation and standards compliance |
| 95 | + - Container definition syntax and URL validation |
| 96 | + - Integration testing with alternative registries |
| 97 | + - General module linting workflow |
| 98 | + """ |
| 99 | + |
| 100 | + def setUp(self): |
| 101 | + """Set up test fixtures by installing required modules""" |
| 102 | + super().setUp() |
| 103 | + # Install samtools/sort module for all tests in this class |
| 104 | + if not self.mods_install.install("samtools/sort"): |
| 105 | + self.skipTest("Could not install samtools/sort module") |
| 106 | + |
| 107 | + def test_main_nf_lint_with_alternative_registry(self): |
| 108 | + """Test main.nf linting with alternative container registry""" |
| 109 | + # Test with alternative registry - should warn/fail when containers don't match the registry |
| 110 | + module_lint = nf_core.modules.lint.ModuleLint(directory=self.pipeline_dir, registry="public.ecr.aws") |
| 111 | + module_lint.lint(print_results=False, module="samtools/sort") |
| 112 | + |
| 113 | + # Alternative registry should produce warnings or failures for container mismatches |
| 114 | + # since samtools/sort module likely uses biocontainers/quay.io, not public.ecr.aws |
| 115 | + total_issues = len(module_lint.failed) + len(module_lint.warned) |
| 116 | + assert total_issues > 0, ( |
| 117 | + "Expected warnings/failures when using alternative registry that doesn't match module containers" |
| 118 | + ) |
| 119 | + |
| 120 | + # Test with default registry - should pass cleanly |
| 121 | + module_lint = nf_core.modules.lint.ModuleLint(directory=self.pipeline_dir) |
| 122 | + module_lint.lint(print_results=False, module="samtools/sort") |
| 123 | + assert len(module_lint.failed) == 0, f"Linting failed with {[x.__dict__ for x in module_lint.failed]}" |
| 124 | + assert len(module_lint.passed) > 0 |
0 commit comments