|
1 | 1 | import os
|
2 | 2 | import tempfile
|
3 |
| -from pathlib import Path |
4 | 3 | import attrs
|
5 |
| -import pydra.engine |
| 4 | +from pathlib import Path |
| 5 | +import pytest |
| 6 | +import cloudpickle as cp |
6 | 7 | from pydra.mark import shell_task, shell_arg, shell_out
|
7 | 8 |
|
8 | 9 |
|
9 |
| -def test_shell_task_full(): |
10 |
| - @attrs.define(kw_only=True, slots=False) |
11 |
| - class LsInputSpec(pydra.specs.ShellSpec): |
12 |
| - directory: os.PathLike = shell_arg( |
13 |
| - help_string="the directory to list the contents of", |
14 |
| - argstr="", |
15 |
| - mandatory=True, |
16 |
| - ) |
17 |
| - hidden: bool = shell_arg(help_string=("display hidden FS objects"), argstr="-a") |
18 |
| - long_format: bool = shell_arg( |
19 |
| - help_string=( |
20 |
| - "display properties of FS object, such as permissions, size and timestamps " |
21 |
| - ), |
22 |
| - argstr="-l", |
23 |
| - ) |
24 |
| - human_readable: bool = shell_arg( |
25 |
| - help_string="display file sizes in human readable form", |
26 |
| - argstr="-h", |
27 |
| - requires=["long_format"], |
28 |
| - ) |
29 |
| - complete_date: bool = shell_arg( |
30 |
| - help_string="Show complete date in long format", |
31 |
| - argstr="-T", |
32 |
| - requires=["long_format"], |
33 |
| - xor=["date_format_str"], |
34 |
| - ) |
35 |
| - date_format_str: str = shell_arg( |
36 |
| - help_string="format string for ", |
37 |
| - argstr="-D", |
38 |
| - requires=["long_format"], |
39 |
| - xor=["complete_date"], |
40 |
| - ) |
| 10 | +def list_entries(stdout): |
| 11 | + return stdout.split("\n")[:-1] |
41 | 12 |
|
42 |
| - def list_outputs(stdout): |
43 |
| - return stdout.split("\n")[:-1] |
44 | 13 |
|
45 |
| - @attrs.define(kw_only=True, slots=False) |
46 |
| - class LsOutputSpec(pydra.specs.ShellOutSpec): |
47 |
| - entries: list = shell_out( |
48 |
| - help_string="list of entries returned by ls command", callable=list_outputs |
49 |
| - ) |
| 14 | +@pytest.fixture |
| 15 | +def tmpdir(): |
| 16 | + return Path(tempfile.mkdtemp()) |
50 | 17 |
|
51 |
| - class Ls(pydra.engine.ShellCommandTask): |
52 |
| - """Task definition for the `ls` command line tool""" |
53 | 18 |
|
54 |
| - executable = "ls" |
| 19 | +@pytest.fixture(params=["static", "dynamic"]) |
| 20 | +def Ls(request): |
| 21 | + if request.param == "static": |
55 | 22 |
|
56 |
| - input_spec = pydra.specs.SpecInfo( |
57 |
| - name="LsInput", |
58 |
| - bases=(LsInputSpec,), |
59 |
| - ) |
| 23 | + @shell_task |
| 24 | + class Ls: |
| 25 | + executable = "ls" |
60 | 26 |
|
61 |
| - output_spec = pydra.specs.SpecInfo( |
62 |
| - name="LsOutput", |
63 |
| - bases=(LsOutputSpec,), |
| 27 | + class Inputs: |
| 28 | + directory: os.PathLike = shell_arg( |
| 29 | + help_string="the directory to list the contents of", |
| 30 | + argstr="", |
| 31 | + mandatory=True, |
| 32 | + ) |
| 33 | + hidden: bool = shell_arg( |
| 34 | + help_string=("display hidden FS objects"), |
| 35 | + argstr="-a", |
| 36 | + default=False, |
| 37 | + ) |
| 38 | + long_format: bool = shell_arg( |
| 39 | + help_string=( |
| 40 | + "display properties of FS object, such as permissions, size and " |
| 41 | + "timestamps " |
| 42 | + ), |
| 43 | + default=False, |
| 44 | + argstr="-l", |
| 45 | + ) |
| 46 | + human_readable: bool = shell_arg( |
| 47 | + help_string="display file sizes in human readable form", |
| 48 | + argstr="-h", |
| 49 | + default=False, |
| 50 | + requires=["long_format"], |
| 51 | + ) |
| 52 | + complete_date: bool = shell_arg( |
| 53 | + help_string="Show complete date in long format", |
| 54 | + argstr="-T", |
| 55 | + default=False, |
| 56 | + requires=["long_format"], |
| 57 | + xor=["date_format_str"], |
| 58 | + ) |
| 59 | + date_format_str: str = shell_arg( |
| 60 | + help_string="format string for ", |
| 61 | + argstr="-D", |
| 62 | + default=None, |
| 63 | + requires=["long_format"], |
| 64 | + xor=["complete_date"], |
| 65 | + ) |
| 66 | + |
| 67 | + class Outputs: |
| 68 | + entries: list = shell_out( |
| 69 | + help_string="list of entries returned by ls command", |
| 70 | + callable=list_entries, |
| 71 | + ) |
| 72 | + |
| 73 | + elif request.param == "dynamic": |
| 74 | + Ls = shell_task( |
| 75 | + "Ls", |
| 76 | + executable="ls", |
| 77 | + input_fields={ |
| 78 | + "directory": { |
| 79 | + "type": os.PathLike, |
| 80 | + "help_string": "the directory to list the contents of", |
| 81 | + "argstr": "", |
| 82 | + "mandatory": True, |
| 83 | + }, |
| 84 | + "hidden": { |
| 85 | + "type": bool, |
| 86 | + "help_string": "display hidden FS objects", |
| 87 | + "argstr": "-a", |
| 88 | + }, |
| 89 | + "long_format": { |
| 90 | + "type": bool, |
| 91 | + "help_string": ( |
| 92 | + "display properties of FS object, such as permissions, size and " |
| 93 | + "timestamps " |
| 94 | + ), |
| 95 | + "argstr": "-l", |
| 96 | + }, |
| 97 | + "human_readable": { |
| 98 | + "type": bool, |
| 99 | + "help_string": "display file sizes in human readable form", |
| 100 | + "argstr": "-h", |
| 101 | + "requires": ["long_format"], |
| 102 | + }, |
| 103 | + "complete_date": { |
| 104 | + "type": bool, |
| 105 | + "help_string": "Show complete date in long format", |
| 106 | + "argstr": "-T", |
| 107 | + "requires": ["long_format"], |
| 108 | + "xor": ["date_format_str"], |
| 109 | + }, |
| 110 | + "date_format_str": { |
| 111 | + "type": str, |
| 112 | + "help_string": "format string for ", |
| 113 | + "argstr": "-D", |
| 114 | + "requires": ["long_format"], |
| 115 | + "xor": ["complete_date"], |
| 116 | + }, |
| 117 | + }, |
| 118 | + output_fields={ |
| 119 | + "entries": { |
| 120 | + "type": list, |
| 121 | + "help_string": "list of entries returned by ls command", |
| 122 | + "callable": list_entries, |
| 123 | + } |
| 124 | + }, |
64 | 125 | )
|
65 | 126 |
|
66 |
| - tmpdir = Path(tempfile.mkdtemp()) |
| 127 | + else: |
| 128 | + assert False |
| 129 | + |
| 130 | + return Ls |
| 131 | + |
| 132 | + |
| 133 | +def test_shell_task_fields(Ls): |
| 134 | + assert [a.name for a in attrs.fields(Ls.Inputs)] == [ |
| 135 | + "executable", |
| 136 | + "args", |
| 137 | + "directory", |
| 138 | + "hidden", |
| 139 | + "long_format", |
| 140 | + "human_readable", |
| 141 | + "complete_date", |
| 142 | + "date_format_str", |
| 143 | + ] |
| 144 | + |
| 145 | + assert [a.name for a in attrs.fields(Ls.Outputs)] == [ |
| 146 | + "return_code", |
| 147 | + "stdout", |
| 148 | + "stderr", |
| 149 | + "entries", |
| 150 | + ] |
| 151 | + |
| 152 | + |
| 153 | +def test_shell_task_pickle_roundtrip(Ls, tmpdir): |
| 154 | + pkl_file = tmpdir / "ls.pkl" |
| 155 | + with open(pkl_file, "wb") as f: |
| 156 | + cp.dump(Ls, f) |
| 157 | + |
| 158 | + with open(pkl_file, "rb") as f: |
| 159 | + RereadLs = cp.load(f) |
| 160 | + |
| 161 | + assert RereadLs is Ls |
| 162 | + |
| 163 | + |
| 164 | +@pytest.mark.xfail( |
| 165 | + reason=( |
| 166 | + "Need to change relationship between Inputs/Outputs and input_spec/output_spec " |
| 167 | + "for the task to run" |
| 168 | + ) |
| 169 | +) |
| 170 | +def test_shell_task_init(Ls, tmpdir): |
| 171 | + inputs = Ls.Inputs(directory=tmpdir) |
| 172 | + assert inputs.directory == tmpdir |
| 173 | + assert not inputs.hidden |
| 174 | + outputs = Ls.Outputs(entries=[]) |
| 175 | + assert outputs.entries == [] |
| 176 | + |
| 177 | + |
| 178 | +@pytest.mark.xfail( |
| 179 | + reason=( |
| 180 | + "Need to change relationship between Inputs/Outputs and input_spec/output_spec " |
| 181 | + "for the task to run" |
| 182 | + ) |
| 183 | +) |
| 184 | +def test_shell_task_run(Ls, tmpdir): |
67 | 185 | Path.touch(tmpdir / "a")
|
68 | 186 | Path.touch(tmpdir / "b")
|
69 | 187 | Path.touch(tmpdir / "c")
|
|
0 commit comments