forked from willyg302/clip.py
/
test.py
710 lines (558 loc) · 16.7 KB
/
test.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
import unittest
import contextlib
import clip
@contextlib.contextmanager
def mock_clip_input(s):
cache = clip.input
def step(_):
return s.pop(0)
clip.input = step
yield
clip.input = cache
class Stream(object):
def __init__(self):
self._writes = []
def write(self, message):
self._writes.append(message)
class BaseTest(unittest.TestCase):
'''Base class for tests in this file.
This should:
- Hold generic test apps and expected values, bound to self
- Expose generic methods useful to other tests
'''
def setUp(self):
pass
def make_kitchen_sink_app(self):
app = clip.App()
self.a = []
self.b = []
@app.main()
@clip.flag('-a', '--apple')
@clip.flag('--banana', '-b')
@clip.opt('--file', name='filename')
@clip.arg('donut')
def a(apple, banana, filename, donut):
self.a = [apple, banana, filename, donut]
@a.subcommand()
@clip.flag('-t')
@clip.flag('--long-thing')
@clip.arg('args', nargs=-1)
def b(t, long_thing, args):
self.b = [t, long_thing, args]
return app
def embed(self):
out, err = Stream(), Stream()
app = clip.App(stdout=out, stderr=err)
return app, out, err
def make_embedded_app(self):
app, out, err = self.embed()
@app.main()
@clip.opt('--to-out')
@clip.opt('--to-err')
def f(to_out, to_err):
clip.echo(to_out)
clip.echo(to_err, err=True)
return app, out, err
class TestGlobals(BaseTest):
def test_clip_globals(self):
out, err = Stream(), Stream()
cg = clip.ClipGlobals()
cg.set_stdout(out)
cg.set_stderr(err)
cg.echo('My my')
cg.echo('What have I done?', err=True)
self.assertEqual(out._writes, ['My my\n'])
self.assertEqual(err._writes, ['What have I done?\n'])
def test_exit(self):
# Standard case, custom message
try:
clip.exit('Woot!')
except clip.ClipExit as e:
self.assertEqual(e.message, 'Woot!')
# Error condition
try:
clip.exit(err=True)
except clip.ClipExit as e:
self.assertTrue(e.message.startswith('clip exiting'))
self.assertEqual(e.status, 1)
def test_confirm(self):
# All the standard accepted entries
with mock_clip_input(['y', 'n', 'Y', 'N', 'yEs', 'No', 'YES', 'no']):
for e in [True, False, True, False, True, False, True, False]:
self.assertEqual(clip.confirm('?'), e)
# Abort
with mock_clip_input(['n']):
with self.assertRaises(clip.ClipExit):
clip.confirm('?', abort=True)
# Mistaken entries
_, out, _ = self.embed()
with mock_clip_input(['', 'boop', 'PAHHHHHH', 'NO']):
self.assertFalse(clip.confirm('?'))
self.assertEqual(len(out._writes), 3)
class TestParse(BaseTest):
def test_kitchen_sink(self):
expected = {
'apple': True,
'banana': True,
'filename': 'pie.txt',
'donut': 'chocolate',
'b': {
't': False,
'long_thing': True,
'args': ['yum', 'yo']
}
}
actuals = [
'-ab --file pie.txt chocolate b --long-thing yum yo',
'chocolate -a --file pie.txt --banana b --long-thing yum yo'
]
for actual in actuals:
self.assertEqual(self.make_kitchen_sink_app().parse(actual.split()), expected)
def test_required(self):
app, _, err = self.embed()
@app.main()
@clip.opt('-o')
@clip.flag('-r', required=True)
def f(o, r):
pass
with self.assertRaises(clip.ClipExit):
app.run('-o joe')
self.assertTrue('Missing' in err._writes[0])
def test_common_errors(self):
app, _, err = self.embed()
@app.main()
def f():
pass
@f.subcommand()
@clip.opt('--tokens', nargs=4)
def nargs(tokens):
pass
@f.subcommand()
@clip.opt('--int', type=int)
@clip.arg('ints', type=int, nargs=3)
def coerce(int, ints):
pass
for e in ['nargs --tokens 1 2 3', 'coerce --int pie', 'coerce this aint right']:
with self.assertRaises(clip.ClipExit):
app.run(e)
for i, e in enumerate(['Not enough', 'Invalid type', 'Invalid type']):
self.assertTrue(e in err._writes[i])
# @TODO:
# - Test type (should be part of kitchen sink)
# - Test required/nargs (should be part of kitchen sink)
# - Test default. This includes a case where default is a function!
class TestInvoke(BaseTest):
def test_invoke(self):
self.make_kitchen_sink_app().run('-ab --file pie.txt chocolate b --long-thing yum yo')
self.assertEqual(self.a, [True, True, 'pie.txt', 'chocolate'])
self.assertEqual(self.b, [False, True, ['yum', 'yo']])
def test_reset(self):
app = self.make_kitchen_sink_app()
app.run('b a n a n a')
self.assertEqual(self.b[2], 'a n a n a'.split())
# If reset works, we should be able to run another command right away
app.run('b o o p')
self.assertEqual(self.b[2], 'o o p'.split())
def test_run(self):
app, out, _ = self.make_embedded_app()
app.run(['--to-out', 'list']).run('--to-out string')
self.assertEqual(out._writes, ['list\n', 'string\n'])
def test_version(self):
app, out, _ = self.embed()
def print_version(value):
clip.exit('Version 0.0.0')
@app.main()
@clip.flag('--version', callback=print_version, hidden=True)
def f():
clip.echo('Should not be called')
with self.assertRaises(clip.ClipExit):
app.run('--version')
self.assertEqual(out._writes, ['Version 0.0.0\n'])
class TestHelp(BaseTest):
def test_help(self):
pass
# @TODO
#self.make_kitchen_sink_app().run('b -h')
class TestInheritance(BaseTest):
def test_inheritance(self):
pass
# @TODO:
# - All inheritance cases
# - Inheritance of a param two levels up (nested subcommand)
# - Inheritance can be specified using decls, name, etc.
def test_inherits_args(self):
pass
def test_inherit_only(self):
app, out, err = self.embed()
@app.main()
@clip.flag('-s', inherit_only=True)
@clip.flag('-t')
def f(t):
clip.echo(t)
@f.subcommand(inherits=['-s', '-t'])
def sub(s, t):
clip.echo(s, err=True)
clip.echo(t, err=True)
parsed = app.parse(['-st', 'sub'])
self.assertEqual(parsed, {
't': True,
'sub': {
's': True,
't': True
}
})
app.invoke(parsed)
self.assertEqual(out._writes, ['True\n'])
self.assertEqual(err._writes, ['True\n', 'True\n'])
class TestEmbedding(BaseTest):
def test_streams(self):
app, out, err = self.make_embedded_app()
app.run('--to-out out1 --to-err err1')
self.assertEqual(out._writes, ['out1\n'])
self.assertEqual(err._writes, ['err1\n'])
def test_exit_message(self):
# Exiting should print a message
_, out, _ = self.make_embedded_app()
with self.assertRaises(clip.ClipExit):
clip.exit('Exiting!')
self.assertEqual(out._writes, ['Exiting!\n'])
def test_embedded_confirm(self):
self.cache = None
def custom_input(prompt):
self.cache = prompt
return 'y'
self.assertTrue(clip.confirm('?', default='no', input_function=custom_input))
self.assertEqual(self.cache, '? [y/N]: ')
class TestMistakes(BaseTest):
'''These are mistakes a programmer would make using clip.
'''
def test_app_mistakes(self):
# App should not define more than one main command
app = self.make_kitchen_sink_app()
with self.assertRaises(AttributeError):
@app.main()
def f():
pass
# App should define at least one main command
with self.assertRaises(AttributeError):
clip.App().run('something')
def test_command_mistakes(self):
# Giving a command a callback that is already a command
with self.assertRaises(TypeError):
app = clip.App()
@app.main()
def f():
pass
@f.subcommand()
@f.subcommand()
def sub():
pass
# Giving main an "inherits"
with self.assertRaises(AttributeError):
app = clip.App()
@app.main(inherits=['whoops'])
def f(whoops):
pass
# Inheriting a parameter that doesn't exist
with self.assertRaises(AttributeError):
app = clip.App()
@app.main()
def f():
pass
@f.subcommand(inherits=['whoops'])
def sub(whoops):
pass
def test_parameter_mistakes(self):
# If nargs != 1 and default is not a list
with self.assertRaises(TypeError):
app = clip.App()
@app.main()
@clip.opt('-a', nargs=-1, default='whoops')
def f(a):
pass
def test_argument_mistakes(self):
# Specifying more than one name for an argument
with self.assertRaises(TypeError):
app = clip.App()
@app.main()
@clip.arg('name', 'whoops')
def f(name):
pass
class TestExamples(BaseTest):
'''Test documentation examples to protect against regressions.
If any of these tests fail, then the associated docs need to be updated.
Similarly, if any of the examples are updated they should be reflected
in the tests here.
'''
def test_readme(self):
# Chef example in README and Getting Started section
app, out, err = self.embed()
@app.main(description='Hey, I em zee Svedeesh cheff!')
def chef():
pass
@chef.subcommand(description='Hefe-a zee cheff cuuk sume-a fuud')
@clip.arg('food', required=True, help='Neme-a ooff zee fuud')
@clip.opt('-c', '--count', default=1, help='Hoo mooch fuud yuoo vunt')
def cook(food, count):
clip.echo('Zee cheff veell cuuk {}'.format(' '.join([food] * count)))
@chef.subcommand(description='Tell zee cheff tu beke-a a pestry')
@clip.arg('pastry', required=True, help='Neme-a ooff zee pestry')
@clip.flag('--now', help='Iff yuoo\'re-a in a hoorry')
def bake(pastry, now):
response = 'Ookey ookey, I veell beke-a zee {} reeght evey!' if now else 'Ooh, yuoo vunt a {}?'
clip.echo(response.format(pastry))
inputs = [
'-h',
'cook -h',
'cook burger',
'cook pie -c 5',
'bake --now',
'bake cake --now'
]
for e in inputs:
try:
app.run(e)
except clip.ClipExit:
pass
self.assertEqual(out._writes, [
'''chef: Hey, I em zee Svedeesh cheff!
Usage: chef {{options}} {{subcommand}}
Options:
-h, --help Show this help message and exit
Subcommands:
cook Hefe-a zee cheff cuuk sume-a fuud
bake Tell zee cheff tu beke-a a pestry
''',
'''chef cook: Hefe-a zee cheff cuuk sume-a fuud
Usage: cook {{arguments}} {{options}}
Arguments:
food [text] Neme-a ooff zee fuud
Options:
-h, --help Show this help message and exit
-c, --count [int] Hoo mooch fuud yuoo vunt (default: 1)
''',
'Zee cheff veell cuuk burger\n',
'Zee cheff veell cuuk pie pie pie pie pie\n',
'Ookey ookey, I veell beke-a zee cake reeght evey!\n'
])
self.assertEqual(err._writes, [
'Error: Missing parameter "pastry".\n'
])
def test_getting_started(self):
# Echo example
app, out, _ = self.embed()
@app.main()
@clip.arg('words', nargs=-1)
def echo(words):
clip.echo(' '.join(words))
app.run('Hello world!').run('idk what i doinggggg hahahaa a')
self.assertEqual(out._writes, ['Hello world!\n', 'idk what i doinggggg hahahaa a\n'])
def test_commands(self):
# description
app, out, _ = self.embed()
@app.main(description='This thing does awesome stuff!')
def f():
pass
@f.subcommand(description='This is a sweet subcommand!')
def sub():
pass
for e in ['-h', 'sub -h']:
try:
app.run(e)
except clip.ClipExit:
pass
self.assertEqual(out._writes, [
'''f: This thing does awesome stuff!
Usage: f {{options}} {{subcommand}}
Options:
-h, --help Show this help message and exit
Subcommands:
sub This is a sweet subcommand!
''',
'''f sub: This is a sweet subcommand!
Usage: sub {{options}}
Options:
-h, --help Show this help message and exit
'''
])
# epilogue
app, out, _ = self.embed()
@app.main(epilogue='So long and thanks for all the fish!')
def f():
pass
try:
app.run('-h')
except clip.ClipExit:
pass
self.assertEqual(out._writes[0], '''f
Usage: f {{options}}
Options:
-h, --help Show this help message and exit
So long and thanks for all the fish!
''')
def test_parameters(self):
# nargs
app, out, err = self.embed()
@app.main()
@clip.arg('stuff', nargs=3)
def f(stuff):
clip.echo('You entered: {}'.format(stuff))
for e in ['a', 'a b', 'a b c']:
try:
app.run(e)
except clip.ClipExit:
pass
self.assertEqual(out._writes, ["You entered: ['a', 'b', 'c']\n"])
self.assertEqual(err._writes, ['Error: Not enough arguments for "stuff".\n'] * 2)
# default
app, out, _ = self.embed()
@app.main()
@clip.opt('--name', default='Joe')
def f(name):
clip.echo('Hello {}!'.format(name))
app.run('').run('--name Dave')
self.assertEqual(out._writes, ['Hello Joe!\n', 'Hello Dave!\n'])
# type
app, out, err = self.embed()
@app.main()
@clip.arg('numbers', nargs=-1, default=[1, 2, 3])
def f(numbers):
clip.echo(sum(numbers))
for e in ['2 4 6 8 10', 'wuuutttt']:
try:
app.run(e)
except clip.ClipExit:
pass
self.assertEqual(out._writes, ['30\n'])
self.assertEqual(err._writes, ['Error: Invalid type given to "numbers", expected int.\n'])
# required
app, out, err = self.embed()
@app.main()
@clip.flag('--needed', required=True)
def f(needed):
clip.echo('Ahh, I needed that.')
for e in ['', '--needed']:
try:
app.run(e)
except clip.ClipExit:
pass
self.assertEqual(out._writes, ['Ahh, I needed that.\n'])
self.assertEqual(err._writes, ['Error: Missing parameter "needed".\n'])
# callback
app, out, _ = self.embed()
def shout(value):
clip.echo(' '.join(value).upper())
@app.main()
@clip.arg('words', nargs=-1, callback=shout)
def f(words):
pass
app.run('i feel da powah!')
self.assertEqual(out._writes[0], 'I FEEL DA POWAH!\n')
# help
app, out, _ = self.embed()
@app.main()
@clip.flag('--panic', help='Don\'t do this')
def f(panic):
pass
try:
app.run('-h')
except clip.ClipExit:
pass
self.assertEqual(out._writes[0], '''f
Usage: f {{options}}
Options:
-h, --help Show this help message and exit
--panic Don't do this
''')
def test_inheriting_parameters(self):
# Calculator (here we only test the final version for expected output)
app, out, _ = self.embed()
@app.main()
@clip.arg('numbers', nargs=-1, type=int, inherit_only=True)
@clip.flag('-s', '--silent', inherit_only=True)
def calculator():
pass
@calculator.subcommand(inherits=['numbers', '-s'])
def add(numbers, silent):
if not silent:
clip.echo('Add these numbers? Okay, here we gooooo!')
clip.echo(sum(numbers))
@calculator.subcommand(inherits=['numbers', '--silent'])
def multiply(numbers, silent):
if not silent:
clip.echo('Wa-hoo, let\'s-a multiply these numbers!')
clip.echo(reduce(lambda x, y: x * y, numbers) if numbers else 0)
for e in ['add 1 3 5 7', 'multiply 1 3 5 7', 'add -s 1 3 5 7', '-s add 1 3 5 7']:
app.run(e)
self.assertEqual(out._writes, [
'Add these numbers? Okay, here we gooooo!\n', '16\n',
'Wa-hoo, let\'s-a multiply these numbers!\n', '105\n',
'16\n',
'16\n'
])
# Going deeper
app, out, _ = self.embed()
@app.main()
@clip.flag('-a')
def w(a):
clip.echo('a in w: {}'.format(a))
@w.subcommand()
@clip.flag('-b')
def x(b):
clip.echo('b in x: {}'.format(b))
@x.subcommand(inherits=['a'])
@clip.flag('-c')
def y(a, c):
clip.echo('a in y: {}'.format(a))
clip.echo('c in y: {}'.format(c))
@y.subcommand(inherits=['a', 'b', 'c'])
@clip.flag('-d')
def z(a, b, c, d):
clip.echo('All together now: {}'.format((a, b, c, d)))
app.run('-a x -b y -c z -d')
self.assertEqual(out._writes, [
'a in w: True\n',
'b in x: True\n',
'a in y: True\n',
'c in y: True\n',
'All together now: (True, True, True, True)\n'
])
def test_extending_clip(self):
# The sorting program
class Choice(clip.Option):
'''A special option that must be chosen from a list of valid values.
The default value will be the first item of the list.
'''
def __init__(self, param_decls, **attrs):
try:
self._choices = attrs.pop('choices')
except KeyError:
raise AttributeError('You must specify the choices to select from')
if not isinstance(self._choices, list) or len(self._choices) == 0:
raise AttributeError('"choices" must be a nonempty list of valid values')
attrs['nargs'] = 1
attrs['default'] = self._choices[0]
clip.Option.__init__(self, param_decls, **attrs)
def consume(self, tokens):
tokens.pop(0) # Pop the choice from the tokens array
selected = tokens.pop(0)
if selected not in self._choices:
clip.exit('Error: "{}" is not a valid choice (choose from {}).'.format(selected, ', '.join(self._choices)), True)
clip.Parameter.post_consume(self, selected)
return tokens
def choice(*param_decls, **attrs):
return clip._make_param(Choice, param_decls, **attrs)
app, out, err = self.embed()
@app.main()
@choice('-t', '--type', name='sort_type', choices=['quicksort', 'bubblesort', 'mergesort'])
def sort(sort_type):
clip.echo('You selected {}'.format(sort_type))
for e in ['', '-t spaghettisort', '-t mergesort']:
try:
app.run(e)
except clip.ClipExit:
pass
self.assertEqual(out._writes, ['You selected quicksort\n', 'You selected mergesort\n'])
self.assertEqual(err._writes, ['Error: "spaghettisort" is not a valid choice (choose from quicksort, bubblesort, mergesort).\n'])
if __name__ == '__main__':
unittest.main()