|
| 1 | +import attr |
| 2 | +import typing as ty |
| 3 | +import os, sys |
| 4 | +import pytest |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | + |
| 8 | +from ..task import ShellCommandTask |
| 9 | +from ..submitter import Submitter |
| 10 | +from ..core import Workflow |
| 11 | +from ..specs import ShellOutSpec, ShellSpec, SpecInfo, File |
| 12 | +from .utils import result_no_submitter, result_submitter, use_validator |
| 13 | + |
| 14 | +interf_input_spec = SpecInfo( |
| 15 | + name="Input", fields=[("test", ty.Any, {"help_string": "test"})], bases=(ShellSpec,) |
| 16 | +) |
| 17 | +interf_output_spec = SpecInfo( |
| 18 | + name="Output", fields=[("test_out", File, "*.txt")], bases=(ShellOutSpec,) |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +class Interf_1(ShellCommandTask): |
| 23 | + """class with customized input/output specs""" |
| 24 | + |
| 25 | + input_spec = interf_input_spec |
| 26 | + output_spec = interf_output_spec |
| 27 | + |
| 28 | + |
| 29 | +class Interf_2(ShellCommandTask): |
| 30 | + """class with customized input/output specs and executables""" |
| 31 | + |
| 32 | + input_spec = interf_input_spec |
| 33 | + output_spec = interf_output_spec |
| 34 | + executable = "testing command" |
| 35 | + |
| 36 | + |
| 37 | +class TouchInterf(ShellCommandTask): |
| 38 | + """class with customized input and executables""" |
| 39 | + |
| 40 | + input_spec = SpecInfo( |
| 41 | + name="Input", |
| 42 | + fields=[ |
| 43 | + ( |
| 44 | + "new_file", |
| 45 | + str, |
| 46 | + { |
| 47 | + "help_string": "new_file", |
| 48 | + "argstr": "", |
| 49 | + "output_file_template": "{new_file}", |
| 50 | + }, |
| 51 | + ) |
| 52 | + ], |
| 53 | + bases=(ShellSpec,), |
| 54 | + ) |
| 55 | + executable = "touch" |
| 56 | + |
| 57 | + |
| 58 | +def test_interface_specs_1(): |
| 59 | + """testing if class input/output spec are set properly""" |
| 60 | + task = Interf_1(executable="ls") |
| 61 | + assert task.input_spec == interf_input_spec |
| 62 | + assert task.output_spec == interf_output_spec |
| 63 | + |
| 64 | + |
| 65 | +def test_interface_specs_2(): |
| 66 | + """testing if class input/output spec are overwritten properly by the user's specs""" |
| 67 | + my_input_spec = SpecInfo( |
| 68 | + name="Input", |
| 69 | + fields=[("my_inp", ty.Any, {"help_string": "my inp"})], |
| 70 | + bases=(ShellSpec,), |
| 71 | + ) |
| 72 | + my_output_spec = SpecInfo( |
| 73 | + name="Output", fields=[("my_out", File, "*.txt")], bases=(ShellOutSpec,) |
| 74 | + ) |
| 75 | + task = Interf_1(input_spec=my_input_spec, output_spec=my_output_spec) |
| 76 | + assert task.input_spec == my_input_spec |
| 77 | + assert task.output_spec == my_output_spec |
| 78 | + |
| 79 | + |
| 80 | +def test_interface_executable_1(): |
| 81 | + """testing if the class executable is properly set and used in the command line""" |
| 82 | + task = Interf_2() |
| 83 | + assert task.executable == "testing command" |
| 84 | + assert task.inputs.executable == "testing command" |
| 85 | + assert task.cmdline == "testing command" |
| 86 | + |
| 87 | + |
| 88 | +def test_interface_executable_2(): |
| 89 | + """testing if the class executable is overwritten by the user's input (and if the warning is raised)""" |
| 90 | + # warning that the user changes the executable from the one that is set as a class attribute |
| 91 | + with pytest.warns(UserWarning, match="changing the executable"): |
| 92 | + task = Interf_2(executable="i want a different command") |
| 93 | + assert task.executable == "testing command" |
| 94 | + # task.executable stays the same, but input.executable is changed, so the cmd is changed |
| 95 | + assert task.inputs.executable == "i want a different command" |
| 96 | + assert task.cmdline == "i want a different command" |
| 97 | + |
| 98 | + |
| 99 | +def test_interface_run_1(): |
| 100 | + """testing execution of a simple interf with customized input and executable""" |
| 101 | + task = TouchInterf(new_file="hello.txt") |
| 102 | + assert task.cmdline == "touch hello.txt" |
| 103 | + res = task() |
| 104 | + assert res.output.new_file.exists() |
0 commit comments