From cfb72f8fa3e9264bec88deb41d6dadbd4d9e9f31 Mon Sep 17 00:00:00 2001 From: Nicolas Vannieuwkerke Date: Tue, 29 Oct 2024 16:30:02 +0100 Subject: [PATCH] add decent error messages for wrongly used file-path-pattern --- CHANGELOG.md | 1 + .../FormatDirectoryPathEvaluator.groovy | 3 + .../FormatFilePathEvaluator.groovy | 3 + .../FormatPathEvaluator.groovy | 3 + .../validation/ValidateParametersTest.groovy | 56 ++++++++++++++++++- 5 files changed, 64 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 10258c26..1b457c3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ 1. Fixed a bug in `samplesheetToList` that caused output mixing when the function was used more than once in channel operators. 2. Added a missing depencency for email format validation. +3. All path formats (with exception to `file-path-pattern`) will now give a proper error message when a `file-path-pattern` has been used. ## Improvements diff --git a/plugins/nf-schema/src/main/nextflow/validation/CustomEvaluators/FormatDirectoryPathEvaluator.groovy b/plugins/nf-schema/src/main/nextflow/validation/CustomEvaluators/FormatDirectoryPathEvaluator.groovy index 6ed6cbff..3b9d65c9 100644 --- a/plugins/nf-schema/src/main/nextflow/validation/CustomEvaluators/FormatDirectoryPathEvaluator.groovy +++ b/plugins/nf-schema/src/main/nextflow/validation/CustomEvaluators/FormatDirectoryPathEvaluator.groovy @@ -33,6 +33,9 @@ class FormatDirectoryPathEvaluator implements Evaluator { // Actual validation logic def Path file = Nextflow.file(value) as Path + if (file instanceof List) { + return Evaluator.Result.failure("'${value}' is not a directory, but a file path pattern" as String) + } if (file.exists() && !file.isDirectory()) { return Evaluator.Result.failure("'${value}' is not a directory, but a file" as String) } diff --git a/plugins/nf-schema/src/main/nextflow/validation/CustomEvaluators/FormatFilePathEvaluator.groovy b/plugins/nf-schema/src/main/nextflow/validation/CustomEvaluators/FormatFilePathEvaluator.groovy index 3b761537..6bfcebcb 100644 --- a/plugins/nf-schema/src/main/nextflow/validation/CustomEvaluators/FormatFilePathEvaluator.groovy +++ b/plugins/nf-schema/src/main/nextflow/validation/CustomEvaluators/FormatFilePathEvaluator.groovy @@ -33,6 +33,9 @@ class FormatFilePathEvaluator implements Evaluator { // Actual validation logic def Path file = Nextflow.file(value) as Path + if (file instanceof List) { + return Evaluator.Result.failure("'${value}' is not a file, but a file path pattern" as String) + } if (file.exists() && file.isDirectory()) { return Evaluator.Result.failure("'${value}' is not a file, but a directory" as String) } diff --git a/plugins/nf-schema/src/main/nextflow/validation/CustomEvaluators/FormatPathEvaluator.groovy b/plugins/nf-schema/src/main/nextflow/validation/CustomEvaluators/FormatPathEvaluator.groovy index bb51f517..e72945ed 100644 --- a/plugins/nf-schema/src/main/nextflow/validation/CustomEvaluators/FormatPathEvaluator.groovy +++ b/plugins/nf-schema/src/main/nextflow/validation/CustomEvaluators/FormatPathEvaluator.groovy @@ -33,6 +33,9 @@ class FormatPathEvaluator implements Evaluator { // Actual validation logic def Path file = Nextflow.file(value) as Path + if (file instanceof List) { + return Evaluator.Result.failure("'${value}' is not a path, but a file path pattern" as String) + } return Evaluator.Result.success() } } \ No newline at end of file diff --git a/plugins/nf-schema/src/test/nextflow/validation/ValidateParametersTest.groovy b/plugins/nf-schema/src/test/nextflow/validation/ValidateParametersTest.groovy index dd2cbb54..2591e355 100644 --- a/plugins/nf-schema/src/test/nextflow/validation/ValidateParametersTest.groovy +++ b/plugins/nf-schema/src/test/nextflow/validation/ValidateParametersTest.groovy @@ -1097,7 +1097,7 @@ class ValidateParametersTest extends Dsl2Spec{ given: def schema = Path.of('src/testResources/nextflow_schema.json').toAbsolutePath().toString() def SCRIPT = """ - params.input = 'src/testResource/samplesheet.csv' + params.input = 'src/testResources/samplesheet.csv' params.outdir = 'src/testResources/testDir' params.email = "test@domain.com" include { validateParameters } from 'plugin/nf-schema' @@ -1122,7 +1122,7 @@ class ValidateParametersTest extends Dsl2Spec{ given: def schema = Path.of('src/testResources/nextflow_schema.json').toAbsolutePath().toString() def SCRIPT = """ - params.input = 'src/testResource/samplesheet.csv' + params.input = 'src/testResources/samplesheet.csv' params.outdir = 'src/testResources/testDir' params.email = "thisisnotanemail" include { validateParameters } from 'plugin/nf-schema' @@ -1145,4 +1145,56 @@ class ValidateParametersTest extends Dsl2Spec{ !stdout } + def 'should give an error when a file-path-pattern is used with a file-path format' () { + given: + def schema = Path.of('src/testResources/nextflow_schema.json').toAbsolutePath().toString() + def SCRIPT = """ + params.input = 'src/testResources/*.csv' + params.outdir = 'src/testResources/testDir' + include { validateParameters } from 'plugin/nf-schema' + + validateParameters(parameters_schema: '$schema') + """ + + when: + def config = [:] + def result = new MockScriptRunner(config).setScript(SCRIPT).execute() + def stdout = capture + .toString() + .readLines() + .findResults {it.contains('WARN nextflow.validation.SchemaValidator') || it.startsWith('* --') ? it : null } + + + then: + def error = thrown(SchemaValidationException) + error.message.contains("* --input (src/testResources/*.csv): 'src/testResources/*.csv' is not a file, but a file path pattern") + !stdout + } + + def 'should give an error when a file-path-pattern is used with a directory-path format' () { + given: + def schema = Path.of('src/testResources/nextflow_schema.json').toAbsolutePath().toString() + def SCRIPT = """ + params.input = 'src/testResources/samplesheet.csv' + params.outdir = 'src/testResources/testDi*' + include { validateParameters } from 'plugin/nf-schema' + + validateParameters(parameters_schema: '$schema') + """ + + when: + def config = [:] + def result = new MockScriptRunner(config).setScript(SCRIPT).execute() + def stdout = capture + .toString() + .readLines() + .findResults {it.contains('WARN nextflow.validation.SchemaValidator') || it.startsWith('* --') ? it : null } + + + then: + def error = thrown(SchemaValidationException) + error.message.contains("* --outdir (src/testResources/testDi*): 'src/testResources/testDi*' is not a directory, but a file path pattern") + !stdout + } + } \ No newline at end of file