-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathtest_script_tasks.py
406 lines (352 loc) · 12.5 KB
/
test_script_tasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import difflib
no_venv = {"POETRY_VIRTUALENVS_CREATE": "false"}
def test_script_task_with_hard_coded_args(run_poe_subproc, projects, esc_prefix):
result = run_poe_subproc("static-args-test", project="scripts", env=no_venv)
assert result.capture == "Poe => static-args-test\n"
assert result.stdout == (
"str: pat a cake\n"
"int: 1\n"
"float: 1.2\n"
"bool: False\n"
"bool: True\n"
"ellipsis: Ellipsis\n"
"str: 0x63\n"
"str: 7\n"
"tuple: ('first', ('second', 'lol'))\n"
"bool: True\n"
"spread => set: {1, 2, 3}\n"
"thing => str: stuff\n"
"data1 => bytes: b'stuff'\n"
"data2 => bytes: b'BCD'\n"
"eight => tuple: (8, 8.0, 8)\n"
)
assert result.stderr == ""
def test_call_attr_func_with_exec(run_poe_subproc):
result = run_poe_subproc("call_attrs", project="scripts", env=no_venv)
assert result.capture == "Poe => call_attrs\n"
assert result.stdout == "task!\n"
assert result.stderr == ""
def test_print_result(run_poe_subproc):
result = run_poe_subproc("print-script-result", project="scripts", env=no_venv)
assert result.capture == "Poe => print-script-result\n"
assert result.stdout == "determining most random number...\n7\n"
assert result.stderr == ""
def test_dont_print_result(run_poe_subproc):
result = run_poe_subproc("dont-print-script-result", project="scripts", env=no_venv)
assert result.capture == "Poe => dont-print-script-result\n"
assert result.stdout == "determining most random number...\n"
assert result.stderr == ""
def test_automatic_kwargs_from_args(run_poe_subproc):
result = run_poe_subproc("greet", project="scripts", env=no_venv)
assert result.capture == "Poe => greet\n"
assert result.stdout == "I'm sorry Dave\n"
assert result.stderr == ""
def test_script_task_with_cli_args(run_poe_subproc, is_windows):
result = run_poe_subproc(
"greet-passed-args",
"--greeting=hello",
"--user=nat",
project="scripts",
env=no_venv,
)
assert result.capture == "Poe => greet-passed-args --greeting=hello --user=nat\n", [
li
for li in difflib.ndiff(
result.capture, "Poe => greet-passed-args --greeting=hello --user=nat\n"
)
if li[0] != " "
]
if is_windows:
assert result.stdout == "hello nat \\U0001f44b None\n"
else:
assert result.stdout == "hello nat 👋 None\n"
assert result.stderr == ""
def test_script_task_with_args_optional(run_poe_subproc, projects, is_windows):
named_args_project_path = projects["scripts"]
result = run_poe_subproc(
"greet-passed-args",
"--greeting=hello",
"--user=nat",
f"--optional=welcome to {named_args_project_path}",
project="scripts",
env=no_venv,
)
assert result.capture == (
f"Poe => greet-passed-args --greeting=hello --user=nat "
f"'--optional=welcome to {named_args_project_path}'\n"
)
if is_windows:
assert (
result.stdout
== f"hello nat \\U0001f44b welcome to {named_args_project_path}\n"
)
else:
assert result.stdout == f"hello nat 👋 welcome to {named_args_project_path}\n"
assert result.stderr == ""
def test_script_task_default_arg(run_poe_subproc):
result = run_poe_subproc("greet-full-args", project="scripts", env=no_venv)
assert result.capture == "Poe => greet-full-args\n"
# hi is the default value for --greeting
assert result.stdout == "hi None None None\n"
assert result.stderr == ""
def test_script_task_include_boolean_flag_and_numeric_args(run_poe_subproc):
result = run_poe_subproc(
"greet-full-args",
"--greeting=hello",
"--user=nat",
"--upper",
"--age=42",
"--height=1.23",
project="scripts",
env=no_venv,
)
assert result.capture == (
"Poe => greet-full-args --greeting=hello --user=nat --upper --age=42 "
"--height=1.23\n"
)
assert result.stdout == "HELLO NAT 1.23 42\n"
assert result.stderr == ""
def test_script_task_with_short_args(run_poe_subproc):
result = run_poe_subproc(
"greet-full-args",
"-g=Ciao",
"--user=toni",
"-a",
"109",
"-h=1.09",
project="scripts",
env=no_venv,
)
assert result.capture == (
"Poe => greet-full-args -g=Ciao --user=toni -a 109 -h=1.09\n"
)
assert result.stdout == "Ciao toni 1.09 109\n"
assert result.stderr == ""
def test_wrong_args_passed(run_poe_subproc):
base_error = (
"usage: poe greet-full-args [--greeting GREETING] [--user USER] [--upper]\n"
" [--age AGE] [--height USER_HEIGHT]\n"
"poe greet-full-args: error:"
)
result = run_poe_subproc(
"greet-full-args", "--age=lol", project="scripts", env=no_venv
)
assert result.capture == ""
assert result.stdout == ""
assert result.stderr == (
f"{base_error} argument --age/-a: invalid int value: 'lol'\n"
)
result = run_poe_subproc("greet-full-args", "--age", project="scripts", env=no_venv)
assert result.capture == ""
assert result.stdout == ""
assert result.stderr == (f"{base_error} argument --age/-a: expected one argument\n")
result = run_poe_subproc(
"greet-full-args", "--age 3 2 1", project="scripts", env=no_venv
)
assert result.capture == ""
assert result.stdout == ""
assert result.stderr == (f"{base_error} unrecognized arguments: --age 3 2 1\n")
result = run_poe_subproc(
"greet-full-args", "--potato", project="scripts", env=no_venv
)
assert result.capture == ""
assert result.stdout == ""
assert result.stderr == (f"{base_error} unrecognized arguments: --potato\n")
def test_required_args(run_poe_subproc):
result = run_poe_subproc(
"greet-strict",
"--greeting=yo",
"--name",
"dude",
project="scripts",
env=no_venv,
)
assert result.capture == "Poe => greet-strict --greeting=yo --name dude\n"
assert result.stdout == "yo dude\n"
assert result.stderr == ""
result = run_poe_subproc("greet-strict", project="scripts", env=no_venv)
assert result.capture == ""
assert result.stdout == ""
assert result.stderr == (
"usage: poe greet-strict --greeting GREETING --name NAME\npoe greet-strict: "
"error: the following arguments are required: --greeting, --name\n"
)
def test_script_task_bad_type(run_poe_subproc, projects):
result = run_poe_subproc(
f'-C={projects["scripts/bad_type"]}',
"bad-type",
"--greeting=hello",
)
assert (
"Error: Invalid argument 'greeting' declared in task 'bad-type'\n"
" | Option 'type' must be one of "
"('string', 'float', 'integer', 'boolean')\n"
) in result.capture
assert result.stdout == ""
assert result.stderr == ""
def test_script_task_bad_content(run_poe_subproc, projects):
result = run_poe_subproc(
f'-C={projects["scripts/bad_content"]}',
"bad-content",
"--greeting=hello",
)
assert (
"Error: Invalid task 'bad-type'\n"
" | Invalid callable reference 'dummy_package:main[greeting]'\n"
" | (expected something like `module:callable` or `module:callable()`)\n"
) in result.capture
assert result.stdout == ""
assert result.stderr == ""
def test_script_with_positional_args(run_poe_subproc):
result = run_poe_subproc(
"greet-positional", "help!", "Santa", project="scripts", env=no_venv
)
assert result.capture == "Poe => greet-positional 'help!' Santa\n"
assert result.stdout == "help! Santa\n"
assert result.stderr == ""
# Omission of optional positional arg
result = run_poe_subproc(
"greet-positional", "Santa", project="scripts", env=no_venv
)
assert result.capture == "Poe => greet-positional Santa\n"
assert result.stdout == "yo Santa\n"
assert result.stderr == ""
# Omission of required positional arg
result = run_poe_subproc("greet-positional", project="scripts", env=no_venv)
assert result.capture == ""
assert result.stdout == ""
assert result.stderr == (
"usage: poe greet-positional [--upper] [greeting] user\n"
"poe greet-positional: error: the following arguments are required: user\n"
)
# Too many positional args
result = run_poe_subproc(
"greet-positional", "plop", "plop", "plop", project="scripts", env=no_venv
)
assert result.capture == ""
assert result.stdout == ""
assert result.stderr == (
"usage: poe greet-positional [--upper] [greeting] user\n"
"poe greet-positional: error: unrecognized arguments: plop\n"
)
def test_script_with_positional_args_and_options(run_poe_subproc):
result = run_poe_subproc(
"greet-positional", "help!", "Santa", "--upper", project="scripts", env=no_venv
)
assert result.capture == "Poe => greet-positional 'help!' Santa --upper\n"
assert result.stdout == "HELP! SANTA\n"
assert result.stderr == ""
result = run_poe_subproc(
"greet-positional", "--upper", "help!", "Santa", project="scripts", env=no_venv
)
assert result.capture == "Poe => greet-positional --upper 'help!' Santa\n"
assert result.stdout == "HELP! SANTA\n"
assert result.stderr == ""
def test_script_with_multi_value_args(run_poe_subproc):
# Test with all args
result = run_poe_subproc(
"multiple-value-args",
"hey",
"1",
"2",
"3",
"--widgets",
"cow",
"dog",
"--engines",
"v2",
"v8",
project="scripts",
env=no_venv,
)
assert (
result.capture
== "Poe => multiple-value-args hey 1 2 3 --widgets cow dog --engines v2 v8\n"
)
assert result.stdout == (
"args ('hey', [1, 2, 3])\n"
"kwargs {'widgets': ['cow', 'dog'], 'engines': ['v2', 'v8']}\n"
)
assert result.stderr == ""
# Test with some args
result = run_poe_subproc(
"multiple-value-args", "1", "--engines", "v2", project="scripts", env=no_venv
)
assert result.capture == "Poe => multiple-value-args 1 --engines v2\n"
assert result.stdout == (
"args ('1', [])\nkwargs {'widgets': None, 'engines': ['v2']}\n"
)
assert result.stderr == ""
# Test with minimal args
result = run_poe_subproc(
"multiple-value-args", "--engines", "v2", project="scripts", env=no_venv
)
assert result.capture == "Poe => multiple-value-args --engines v2\n"
assert result.stdout == (
"args (None, [])\nkwargs {'widgets': None, 'engines': ['v2']}\n"
)
assert result.stderr == ""
# Not enough values for option: 0
result = run_poe_subproc(
"multiple-value-args",
"--engines",
"v2",
"--widgets",
project="scripts",
env=no_venv,
)
assert result.capture == ""
assert result.stdout == ""
assert (
"poe multiple-value-args: error: argument --widgets: expected 2 arguments"
in result.stderr
)
# Too many values for option: 3
result = run_poe_subproc(
"multiple-value-args",
"bloop", # without the first arg, dong gets read an positional
"--engines",
"v2",
"--widgets",
"ding",
"dang",
"dong",
project="scripts",
env=no_venv,
)
assert result.capture == ""
assert result.stdout == ""
# accomodate difference in argparse output between python versions
assert (
"poe multiple-value-args: error: argument second: invalid int value: 'dong'"
in result.stderr
) or (
"poe multiple-value-args: error: unrecognized arguments: dong" in result.stderr
)
# wrong type for multiple values
result = run_poe_subproc(
"multiple-value-args",
"1",
"wrong",
"--engines",
"v2",
project="scripts",
env=no_venv,
)
assert result.capture == ""
assert result.stdout == ""
assert (
"poe multiple-value-args: error: argument second: invalid int value: 'wrong'"
in result.stderr
)
def test_async_script_task(run_poe_subproc, projects):
result = run_poe_subproc(
"async-task",
"--a=foo",
"--b=bar",
project="scripts",
env=no_venv,
)
assert result.capture == "Poe => async-task --a=foo --b=bar\n"
assert result.stdout == "I'm an async task! () {'a': 'foo', 'b': 'bar'}\n"
assert result.stderr == ""