Skip to content

Commit 3c3a1e5

Browse files
committed
Better errors for expressions in input format fields
1 parent 6af9932 commit 3c3a1e5

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed

Diff for: cwltool/builder.py

+18-4
Original file line numberDiff line numberDiff line change
@@ -555,18 +555,32 @@ def addsf(
555555
if isinstance(eval_format, str):
556556
evaluated_format: Union[str, List[str]] = eval_format
557557
elif isinstance(eval_format, MutableSequence):
558-
for entry in eval_format:
558+
for index, entry in enumerate(eval_format):
559+
message = None
559560
if not isinstance(entry, str):
560-
raise SourceLine(
561-
schema, "format", WorkflowException, debug
562-
).makeError(
561+
message = (
563562
"An expression in the 'format' field must "
564563
"evaluate to a string, or list of strings. "
565564
"However a non-string item was received: "
566565
f"'{entry}' of type '{type(entry)}'. "
567566
f"The expression was '{schema['format']}' and "
568567
f"its fully evaluated result is '{eval_format}'."
569568
)
569+
if expression.needs_parsing(entry):
570+
message = (
571+
"For inputs, 'format' field can either "
572+
"contain a single CWL Expression or CWL Parameter "
573+
"Reference, a single format string, or a list of "
574+
"format strings. But the list cannot contain CWL "
575+
"Expressions or CWL Parameter References. List "
576+
f"entry number {index+1} contains the following "
577+
"unallowed CWL Parameter Reference or Expression: "
578+
f"'{entry}'."
579+
)
580+
if message:
581+
raise SourceLine(
582+
schema["format"], index, WorkflowException, debug
583+
).makeError(message)
570584
evaluated_format = cast(List[str], eval_format)
571585
else:
572586
raise SourceLine(

Diff for: tests/test_examples.py

+20
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,26 @@ def test_format_expr_error() -> None:
827827
)
828828

829829

830+
def test_format_expr_error2() -> None:
831+
"""Better format expression error, for a list of formats."""
832+
error_code, _, stderr = get_main_output(
833+
[
834+
get_data("tests/wf/bad_formattest2.cwl"),
835+
get_data("tests/wf/formattest-job.json"),
836+
]
837+
)
838+
assert error_code != 0
839+
stderr = re.sub(r"\s\s+", " ", stderr)
840+
assert (
841+
"bad_formattest2.cwl:14:9: For inputs, 'format' field can either contain "
842+
"a single CWL Expression or CWL Parameter Reference, a single format string, "
843+
"or a list of format strings. But the list cannot contain CWL Expressions "
844+
"or CWL Parameter References. List entry number 1 contains the following "
845+
"unallowed CWL Parameter Reference or Expression: '${ return "
846+
'["http://edamontology.org/format_2330", 42];}' in stderr
847+
)
848+
849+
830850
def test_static_checker() -> None:
831851
# check that the static checker raises exception when a source type
832852
# mismatches its sink type.

Diff for: tests/wf/bad_formattest2.cwl

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env cwl-runner
2+
$namespaces:
3+
edam: "http://edamontology.org/"
4+
cwlVersion: v1.0
5+
class: CommandLineTool
6+
requirements:
7+
InlineJavascriptRequirement: {}
8+
doc: "Reverse each line using the `rev` command"
9+
inputs:
10+
input:
11+
type: File
12+
inputBinding: {}
13+
format:
14+
- |
15+
${ return ["http://edamontology.org/format_2330", 42];}
16+
17+
outputs:
18+
output:
19+
type: File
20+
outputBinding:
21+
glob: output.txt
22+
format: |
23+
${return "http://edamontology.org/format_2330";}
24+
25+
baseCommand: rev
26+
stdout: output.txt

0 commit comments

Comments
 (0)