Skip to content

Commit 0e66136

Browse files
tcloseghisvail
andauthored
BUG: Fix when optional output template files is set to False (#709)
* added test to catch type error when optional outputs with file templates being set to False * add test to catch case where optional output-file is passed False to disable it * fixed output file collection for optional file outputs set to False * Remove unused branch case --------- Co-authored-by: Ghislain Vaillant <[email protected]>
1 parent 6f12146 commit 0e66136

File tree

2 files changed

+102
-3
lines changed

2 files changed

+102
-3
lines changed

pydra/engine/specs.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -452,9 +452,12 @@ def collect_additional_outputs(self, inputs, output_dir, outputs):
452452
input_value = getattr(inputs, fld.name, attr.NOTHING)
453453
if input_value is not attr.NOTHING:
454454
if TypeParser.contains_type(FileSet, fld.type):
455-
label = f"output field '{fld.name}' of {self}"
456-
input_value = TypeParser(fld.type, label=label).coerce(input_value)
457-
additional_out[fld.name] = input_value
455+
if input_value is not False:
456+
label = f"output field '{fld.name}' of {self}"
457+
input_value = TypeParser(fld.type, label=label).coerce(
458+
input_value
459+
)
460+
additional_out[fld.name] = input_value
458461
elif (
459462
fld.default is None or fld.default == attr.NOTHING
460463
) and not fld.metadata: # TODO: is it right?

pydra/engine/tests/test_shelltask.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4347,6 +4347,102 @@ def change_name(file):
43474347
# res = shelly(plugin="cf")
43484348

43494349

4350+
def test_shell_cmd_optional_output_file1(tmp_path):
4351+
"""
4352+
Test to see that 'unused' doesn't complain about not having an output passed to it
4353+
"""
4354+
my_cp_spec = SpecInfo(
4355+
name="Input",
4356+
fields=[
4357+
(
4358+
"input",
4359+
attr.ib(
4360+
type=File, metadata={"argstr": "", "help_string": "input file"}
4361+
),
4362+
),
4363+
(
4364+
"output",
4365+
attr.ib(
4366+
type=Path,
4367+
metadata={
4368+
"argstr": "",
4369+
"output_file_template": "out.txt",
4370+
"help_string": "output file",
4371+
},
4372+
),
4373+
),
4374+
(
4375+
"unused",
4376+
attr.ib(
4377+
type=ty.Union[Path, bool],
4378+
default=False,
4379+
metadata={
4380+
"argstr": "--not-used",
4381+
"output_file_template": "out.txt",
4382+
"help_string": "dummy output",
4383+
},
4384+
),
4385+
),
4386+
],
4387+
bases=(ShellSpec,),
4388+
)
4389+
4390+
my_cp = ShellCommandTask(
4391+
name="my_cp",
4392+
executable="cp",
4393+
input_spec=my_cp_spec,
4394+
)
4395+
file1 = tmp_path / "file1.txt"
4396+
file1.write_text("foo")
4397+
result = my_cp(input=file1, unused=False)
4398+
assert result.output.output.fspath.read_text() == "foo"
4399+
4400+
4401+
def test_shell_cmd_optional_output_file2(tmp_path):
4402+
"""
4403+
Test to see that 'unused' doesn't complain about not having an output passed to it
4404+
"""
4405+
my_cp_spec = SpecInfo(
4406+
name="Input",
4407+
fields=[
4408+
(
4409+
"input",
4410+
attr.ib(
4411+
type=File, metadata={"argstr": "", "help_string": "input file"}
4412+
),
4413+
),
4414+
(
4415+
"output",
4416+
attr.ib(
4417+
type=ty.Union[Path, bool],
4418+
default=False,
4419+
metadata={
4420+
"argstr": "",
4421+
"output_file_template": "out.txt",
4422+
"help_string": "dummy output",
4423+
},
4424+
),
4425+
),
4426+
],
4427+
bases=(ShellSpec,),
4428+
)
4429+
4430+
my_cp = ShellCommandTask(
4431+
name="my_cp",
4432+
executable="cp",
4433+
input_spec=my_cp_spec,
4434+
)
4435+
file1 = tmp_path / "file1.txt"
4436+
file1.write_text("foo")
4437+
result = my_cp(input=file1, output=True)
4438+
assert result.output.output.fspath.read_text() == "foo"
4439+
4440+
file2 = tmp_path / "file2.txt"
4441+
file2.write_text("bar")
4442+
with pytest.raises(RuntimeError):
4443+
my_cp(input=file2, output=False)
4444+
4445+
43504446
def test_shell_cmd_non_existing_outputs_1(tmp_path):
43514447
"""Checking that non existing output files do not return a phantom path,
43524448
but return NOTHING instead"""

0 commit comments

Comments
 (0)