Skip to content

Commit

Permalink
debugged test_helpers_file
Browse files Browse the repository at this point in the history
  • Loading branch information
tclose committed Jan 30, 2025
1 parent 2da62fd commit ff8069f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 33 deletions.
2 changes: 1 addition & 1 deletion pydra/design/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ def remaining_positions(
If multiple fields have the same position
"""
if num_args is None:
num_args = len(args)
num_args = len(args) - 1 # Subtract 1 for the 'additional_args' field
# Check for multiple positions
positions = defaultdict(list)
for arg in args:
Expand Down
9 changes: 5 additions & 4 deletions pydra/engine/helpers_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,9 @@ def template_update(
for field in list_fields(definition)
if isinstance(field, shell.outarg)
and field.path_template
and getattr(definition, field.name) is not False
and getattr(definition, field.name)
and all(
getattr(definition, required_field) is not None
for required_field in field.requires
getattr(definition, required_field) for required_field in field.requires
)
]

Expand Down Expand Up @@ -164,7 +163,7 @@ def template_update_single(
raise TypeError(
f"type of '{field.name}' is Path, consider using Union[Path, bool]"
)
if inp_val_set is not attr.NOTHING and not is_lazy(inp_val_set):
if inp_val_set is not None and not is_lazy(inp_val_set):
inp_val_set = TypeParser(ty.Union[OUTPUT_TEMPLATE_TYPES])(inp_val_set)
elif spec_type == "output":
if not TypeParser.contains_type(FileSet, field.type):
Expand Down Expand Up @@ -252,6 +251,8 @@ def _string_template_formatting(field, template, definition, input_values):
if fld_name not in input_values:
raise AttributeError(f"{fld_name} is not provided in the input")
fld_value = input_values[fld_name]
if isinstance(fld_value, Path): # Remove path
fld_value = fld_value.name
if fld_value is attr.NOTHING:
# if value is NOTHING, nothing should be added to the command
return attr.NOTHING
Expand Down
31 changes: 7 additions & 24 deletions pydra/engine/specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -945,27 +945,14 @@ def _command_args(
pos_args.append(self._command_shelltask_executable(field, value))
elif name == "additional_args":
continue
elif name == "args":
pos_val = self._command_shelltask_args(field, value)
if pos_val:
pos_args.append(pos_val)
else:
if name in modified_inputs:
pos_val = self._command_pos_args(
field=field,
value=value,
inputs=inputs,
root=root,
output_dir=output_dir,
)
else:
pos_val = self._command_pos_args(
field=field,
value=value,
output_dir=output_dir,
inputs=inputs,
root=root,
)
pos_val = self._command_pos_args(
field=field,
value=value,
inputs=inputs,
root=root,
output_dir=output_dir,
)
if pos_val:
pos_args.append(pos_val)

Expand Down Expand Up @@ -1024,10 +1011,6 @@ def _command_pos_args(

self._positions_provided.append(field.position)

# Shift non-negatives up to allow executable to be 0
# Shift negatives down to allow args to be -1
field.position += 1 if field.position >= 0 else -1

if value and isinstance(value, str):
if root: # values from templates
value = value.replace(str(output_dir), f"{root}{output_dir}")
Expand Down
13 changes: 9 additions & 4 deletions pydra/engine/tests/test_helpers_file.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import typing as ty
import sys
import os
from pathlib import Path
from unittest.mock import Mock
import pytest
from fileformats.generic import File
from pydra.engine.specs import ShellDef, ShellOutputs
from pydra.design import shell
from ..helpers_file import (
from pydra.engine.helpers import list_fields
from pydra.engine.helpers_file import (
ensure_list,
MountIndentifier,
copy_nested_files,
Expand Down Expand Up @@ -376,18 +378,21 @@ class Outputs(ShellOutputs):
defn = MyCommand(in_file=filename)
assert defn.cmdline == f"my {filename}"
defn.optional = True
assert defn.cmdline == f"my {filename} --opt 'file.out'"
file_out_path = os.path.join(os.getcwd(), "file.out")
if " " in file_out_path:
file_out_path = f"'{file_out_path}'"
assert defn.cmdline == f"my {filename} --opt {file_out_path}"
defn.optional = False
assert defn.cmdline == f"my {filename}"
defn.optional = "custom-file-out.txt"
assert defn.cmdline == f"my {filename} --opt custom-file-out.txt"


def test_template_formatting(tmp_path):
def test_template_formatting(tmp_path: Path):
field = Mock()
field.name = "grad"
field.argstr = "--grad"
field.metadata = {"output_file_template": ("{in_file}.bvec", "{in_file}.bval")}
field.path_template = ("{in_file}.bvec", "{in_file}.bval")
inputs = Mock()
inputs_dict = {"in_file": "/a/b/c/file.txt", "grad": True}

Expand Down

0 comments on commit ff8069f

Please sign in to comment.