Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lint check for System.exit calls #2211

Merged
merged 5 commits into from
Mar 28, 2023
Merged
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
5 changes: 5 additions & 0 deletions docs/api/_src/pipeline_lint_tests/system_exit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# system_exit

```{eval-rst}
.. automethod:: nf_core.lint.PipelineLint.system_exit
```
2 changes: 2 additions & 0 deletions nf_core/lint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ class PipelineLint(nf_core.utils.Pipeline):
from .schema_description import schema_description
from .schema_lint import schema_lint
from .schema_params import schema_params
from .system_exit import system_exit
from .template_strings import template_strings
from .version_consistency import version_consistency

Expand Down Expand Up @@ -223,6 +224,7 @@ def _get_all_lint_tests(release_mode):
"template_strings",
"schema_lint",
"schema_params",
"system_exit",
"schema_description",
"actions_schema_validation",
"merge_markers",
Expand Down
37 changes: 37 additions & 0 deletions nf_core/lint/system_exit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import logging
from pathlib import Path

log = logging.getLogger(__name__)


def system_exit(self):
"""Check for System.exit calls in groovy/nextflow code

Calls to System.exit(1) should be replaced by throwing errors

This lint test looks for all calls to `System.exit`
in any file with the `.nf` or `.groovy` extension
"""
passed = []
warned = []

root_dir = Path(self.wf_path)

# Get all groovy and nf files
groovy_files = [f for f in root_dir.rglob("*.groovy")]
nf_files = [f for f in root_dir.rglob("*.nf")]
to_check = nf_files + groovy_files

for file in to_check:
try:
with file.open() as fh:
for i, l in enumerate(fh.readlines(), start=1):
if "System.exit" in l and not "System.exit(0)" in l:
warned.append(f"`System.exit` in {file.name}: _{l.strip()}_ [line {i}]")
except FileNotFoundError:
log.debug(f"Could not open file {file.name} in system_exit lint test")

if len(warned) == 0:
passed.append("No `System.exit` calls found")

return {"passed": passed, "warned": warned}
3 changes: 2 additions & 1 deletion nf_core/pipeline-template/lib/NfcoreSchema.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// This file holds several functions used to perform JSON parameter validation, help and summary rendering for the nf-core pipeline template.
//

import nextflow.Nextflow
import org.everit.json.schema.Schema
import org.everit.json.schema.loader.SchemaLoader
import org.everit.json.schema.ValidationException
Expand Down Expand Up @@ -177,7 +178,7 @@ class NfcoreSchema {
}

if (has_error) {
System.exit(1)
Nextflow.error('Exiting!')
}
}

Expand Down
5 changes: 3 additions & 2 deletions nf_core/pipeline-template/lib/WorkflowMain.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// This file holds several functions specific to the main.nf workflow in the {{ name }} pipeline
//

import nextflow.Nextflow

class WorkflowMain {

//
Expand Down Expand Up @@ -85,8 +87,7 @@ class WorkflowMain {

// Check input has been provided
if (!params.input) {
log.error "Please provide an input samplesheet to the pipeline e.g. '--input samplesheet.csv'"
System.exit(1)
Nextflow.error("Please provide an input samplesheet to the pipeline e.g. '--input samplesheet.csv'")
}
}
{% if igenomes -%}
Expand Down
8 changes: 4 additions & 4 deletions nf_core/pipeline-template/lib/WorkflowPipeline.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// This file holds several functions specific to the workflow/{{ short_name }}.nf in the {{ name }} pipeline
//

import nextflow.Nextflow
import groovy.text.SimpleTemplateEngine

class Workflow{{ short_name[0]|upper }}{{ short_name[1:] }} {
Expand All @@ -15,8 +16,7 @@ class Workflow{{ short_name[0]|upper }}{{ short_name[1:] }} {
{% endif %}

if (!params.fasta) {
log.error "Genome fasta file not specified with e.g. '--fasta genome.fa' or via a detectable config file."
System.exit(1)
Nextflow.error "Genome fasta file not specified with e.g. '--fasta genome.fa' or via a detectable config file."
}
}

Expand Down Expand Up @@ -70,12 +70,12 @@ class Workflow{{ short_name[0]|upper }}{{ short_name[1:] }} {
//
private static void genomeExistsError(params, log) {
if (params.genomes && params.genome && !params.genomes.containsKey(params.genome)) {
log.error "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" +
def error_string = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" +
" Genome '${params.genome}' not found in any config files provided to the pipeline.\n" +
" Currently, the available genome keys are:\n" +
" ${params.genomes.keySet().join(", ")}\n" +
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
System.exit(1)
Nextflow.error(error_string)
}
}
{% endif -%}}