From 6f313a500efec4ddf0936460fccaee1c5b1f1215 Mon Sep 17 00:00:00 2001 From: Jake Fennick Date: Wed, 12 Jun 2024 13:57:10 -1000 Subject: [PATCH] recursively insert output format --- cwltool/command_line_tool.py | 15 +++++++++++++-- tests/output_2D_file_format.cwl | 29 +++++++++++++++++++++++++++++ tests/test_2D.py | 17 +++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 tests/output_2D_file_format.cwl create mode 100644 tests/test_2D.py diff --git a/cwltool/command_line_tool.py b/cwltool/command_line_tool.py index b11bb4bc37..4afd84228c 100644 --- a/cwltool/command_line_tool.py +++ b/cwltool/command_line_tool.py @@ -1495,8 +1495,19 @@ def collect_output( ) primary["format"] = format_eval else: - for primary in aslist(result): - primary["format"] = format_field + + def recursively_insert(j_dict: Any, key: Any, val: Any) -> Any: + """Recursively inserts a value into any dictionaries""" + if isinstance(j_dict, List): + return [recursively_insert(x, key, val) for x in j_dict] + if isinstance(j_dict, Dict): + if j_dict.get("class") == "File": + j_dict[key] = val + else: + return {x: recursively_insert(y, key, val) for x, y in j_dict.items()} + return j_dict + + result = recursively_insert(result, "format", format_field) # Ensure files point to local references outside of the run environment adjustFileObjs(result, revmap) diff --git a/tests/output_2D_file_format.cwl b/tests/output_2D_file_format.cwl new file mode 100644 index 0000000000..6211110615 --- /dev/null +++ b/tests/output_2D_file_format.cwl @@ -0,0 +1,29 @@ +#!/usr/bin/env cwl-runner +cwlVersion: v1.0 + +class: CommandLineTool + +baseCommand: 'true' + +requirements: + InlineJavascriptRequirement: {} + +inputs: {} + +outputs: + output_array: + type: {"type": "array", "items": {"type": "array", "items": "File"}} + outputBinding: + outputEval: | + ${ + var out2d = []; + for (var i = 0; i < 2; i++) { + var out1d = []; + for (var j = 0; j < 2; j++) { + out1d.push({"class": "File", "location": "../../filename.txt"}); + } + out2d.push(out1d); + } + return out2d; + } + format: some_format diff --git a/tests/test_2D.py b/tests/test_2D.py new file mode 100644 index 0000000000..305840bb91 --- /dev/null +++ b/tests/test_2D.py @@ -0,0 +1,17 @@ +import subprocess +import sys + +from .util import get_data + + +def test_output_2D_file_format() -> None: + """Test format tag for 2D output arrays.""" + + params = [ + sys.executable, + "-m", + "cwltool", + get_data("tests/output_2D_file_format.cwl"), + ] + + assert subprocess.check_call(params) == 0