comparison env/lib/python3.9/site-packages/cwltool/tests/test_toolargparse.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 import argparse
2 from io import StringIO
3 from pathlib import Path
4 from typing import Callable
5
6 import pytest
7
8 import cwltool.executors
9 from cwltool.argparser import generate_parser
10 from cwltool.context import LoadingContext
11 from cwltool.load_tool import load_tool
12 from cwltool.main import main
13
14 from .util import get_data, needs_docker
15
16 script_a = """
17 #!/usr/bin/env cwl-runner
18 cwlVersion: v1.0
19 class: CommandLineTool
20 doc: "This tool is developed for SMC-RNA Challenge for detecting gene fusions (STAR fusion)"
21 inputs:
22 #Give it a list of input files
23 - id: input
24 type: File
25 inputBinding:
26 position: 0
27 outputs:
28 - id: output
29 type: File
30 outputBinding:
31 glob: test.txt
32 stdout: test.txt
33 baseCommand: [cat]
34 """
35
36 script_b = """
37 #!/usr/bin/env cwl-runner
38 cwlVersion: v1.0
39 class: CommandLineTool
40 inputs:
41 - id: bdg
42 type: "boolean"
43 outputs:
44 - id: output
45 type: File
46 outputBinding:
47 glob: foo
48 baseCommand:
49 - echo
50 - "ff"
51 stdout: foo
52 """
53
54 script_c = """
55 #!/usr/bin/env cwl-runner
56
57 cwlVersion: v1.0
58 class: ExpressionTool
59
60 inputs:
61 foo:
62 type:
63 type: record
64 fields:
65 one: File
66 two: string
67
68 expression: $(inputs.foo.two)
69
70 outputs: []
71 """
72
73 scripts_argparse_params = [
74 ("help", script_a, lambda x: ["--debug", x, "--input", get_data("tests/echo.cwl")]),
75 ("boolean", script_b, lambda x: [x, "--help"]),
76 ("help with c", script_c, lambda x: [x, "--help"]),
77 (
78 "foo with c",
79 script_c,
80 lambda x: [x, "--foo.one", get_data("tests/echo.cwl"), "--foo.two", "test"],
81 ),
82 ]
83
84
85 @needs_docker
86 @pytest.mark.parametrize("name,script_contents,params", scripts_argparse_params)
87 def test_argparse(
88 name: str, script_contents: str, params: Callable[[str], str], tmp_path: Path
89 ) -> None:
90 script_name = tmp_path / "script"
91 try:
92 with script_name.open(mode="w") as script:
93 script.write(script_contents)
94
95 my_params = ["--outdir", str(tmp_path / "outdir")]
96 my_params.extend(params(script.name))
97 assert main(my_params) == 0, name
98
99 except SystemExit as err:
100 assert err.code == 0, name
101
102
103 def test_dont_require_inputs(tmp_path: Path) -> None:
104 stream = StringIO()
105
106 script_name = tmp_path / "script"
107 try:
108 with script_name.open(mode="w") as script:
109 script.write(script_a)
110
111 assert (
112 main(
113 argsl=["--debug", str(script_name), "--input", str(script_name)],
114 executor=cwltool.executors.NoopJobExecutor(),
115 stdout=stream,
116 )
117 == 0
118 )
119 assert (
120 main(
121 argsl=["--debug", str(script_name)],
122 executor=cwltool.executors.NoopJobExecutor(),
123 stdout=stream,
124 )
125 == 2
126 )
127 assert (
128 main(
129 argsl=["--debug", str(script_name)],
130 executor=cwltool.executors.NoopJobExecutor(),
131 input_required=False,
132 stdout=stream,
133 )
134 == 0
135 )
136
137 except SystemExit as err:
138 assert err.code == 0, script_name if script else None
139
140
141 def test_argparser_with_doc() -> None:
142 """The `desription` field is set if `doc` field is provided."""
143 loadingContext = LoadingContext()
144 tool = load_tool(get_data("tests/with_doc.cwl"), loadingContext)
145 p = argparse.ArgumentParser()
146 parser = generate_parser(p, tool, {}, [], False)
147 assert parser.description is not None
148
149
150 def test_argparser_without_doc() -> None:
151 """The `desription` field is None if `doc` field is not provided."""
152 loadingContext = LoadingContext()
153 tool = load_tool(get_data("tests/without_doc.cwl"), loadingContext)
154 p = argparse.ArgumentParser()
155 parser = generate_parser(p, tool, {}, [], False)
156 assert parser.description is None