|
1 | 1 | import typing as ty
|
2 | 2 | import sys
|
3 | 3 | from pathlib import Path
|
| 4 | +import attr |
4 | 5 | import pytest
|
5 | 6 | from fileformats.generic import File
|
| 7 | +from ..specs import SpecInfo, ShellSpec |
| 8 | +from ..task import ShellCommandTask |
6 | 9 | from ..helpers_file import (
|
7 | 10 | ensure_list,
|
8 | 11 | MountIndentifier,
|
@@ -343,3 +346,55 @@ def test_cifs_check():
|
343 | 346 | with MountIndentifier.patch_table(fake_table):
|
344 | 347 | for target, expected in cifs_targets:
|
345 | 348 | assert MountIndentifier.on_cifs(target) is expected
|
| 349 | + |
| 350 | + |
| 351 | +def test_output_template(tmp_path): |
| 352 | + filename = str(tmp_path / "file.txt") |
| 353 | + with open(filename, "w") as f: |
| 354 | + f.write("hello from pydra") |
| 355 | + in_file = File(filename) |
| 356 | + |
| 357 | + my_input_spec = SpecInfo( |
| 358 | + name="Input", |
| 359 | + fields=[ |
| 360 | + ( |
| 361 | + "in_file", |
| 362 | + attr.ib( |
| 363 | + type=File, |
| 364 | + metadata={ |
| 365 | + "mandatory": True, |
| 366 | + "position": 1, |
| 367 | + "argstr": "", |
| 368 | + "help_string": "input file", |
| 369 | + }, |
| 370 | + ), |
| 371 | + ), |
| 372 | + ( |
| 373 | + "optional", |
| 374 | + attr.ib( |
| 375 | + type=ty.Union[Path, bool], |
| 376 | + default=False, |
| 377 | + metadata={ |
| 378 | + "position": 2, |
| 379 | + "argstr": "--opt", |
| 380 | + "output_file_template": "{in_file}.out", |
| 381 | + "help_string": "optional file output", |
| 382 | + }, |
| 383 | + ), |
| 384 | + ), |
| 385 | + ], |
| 386 | + bases=(ShellSpec,), |
| 387 | + ) |
| 388 | + |
| 389 | + class MyCommand(ShellCommandTask): |
| 390 | + executable = "my" |
| 391 | + input_spec = my_input_spec |
| 392 | + |
| 393 | + task = MyCommand(in_file=filename) |
| 394 | + assert task.cmdline == f"my {filename}" |
| 395 | + task.inputs.optional = True |
| 396 | + assert task.cmdline == f"my {filename} --opt {task.output_dir}/file.out" |
| 397 | + task.inputs.optional = False |
| 398 | + assert task.cmdline == f"my {filename}" |
| 399 | + task.inputs.optional = "custom-file-out.txt" |
| 400 | + assert task.cmdline == f"my {filename} --opt custom-file-out.txt" |
0 commit comments