1
+ #!/usr/bin/env python
2
+ import sys
3
+ import subprocess
4
+
5
+ failed = False
6
+
7
+ _bin = './target/release/claptests'
8
+ cmds = {'help short: ' : ['{} -h | wc -l' .format (_bin ), ['21' ]],
9
+ 'help long: ' : ['{} --help | wc -l' .format (_bin ), ['21' ]],
10
+ 'help subcmd: ' : ['{} help | wc -l' .format (_bin ), ['21' ]],
11
+ 'flag short: ' : ['{} -f' .format (_bin ), ['flag present 1 times' ,
12
+ 'option NOT present' ,
13
+ 'positional NOT present' ,
14
+ 'subcmd NOT present' ]],
15
+ 'flag long: ' : ['{} --flag' .format (_bin ), ['flag present 1 times' ,
16
+ 'option NOT present' ,
17
+ 'positional NOT present' ,
18
+ 'subcmd NOT present' ]],
19
+ 'positional: ' : ['{} some' .format (_bin ), ['flag NOT present' ,
20
+ 'option NOT present' ,
21
+ 'positional present with value: some' ,
22
+ 'subcmd NOT present' ]],
23
+ 'option short: ' : ['{} -o some' .format (_bin ), ['flag NOT present' ,
24
+ 'option present with value: some' ,
25
+ 'An option: some' ,
26
+ 'positional NOT present' ,
27
+ 'subcmd NOT present' ]],
28
+ 'option long: ' : ['{} --option some' .format (_bin ), ['flag NOT present' ,
29
+ 'option present with value: some' ,
30
+ 'An option: some' ,
31
+ 'positional NOT present' ,
32
+ 'subcmd NOT present' ]],
33
+ 'option long=: ' : ['{} --option=some' .format (_bin ), ['flag NOT present' ,
34
+ 'option present with value: some' ,
35
+ 'An option: some' ,
36
+ 'positional NOT present' ,
37
+ 'subcmd NOT present' ]]}
38
+
39
+ def pass_fail (name , check , good ):
40
+ global failed
41
+ print (name , end = '' )
42
+ if len (good ) == 1 :
43
+ if check == good :
44
+ print ('Pass' )
45
+ return
46
+ failed = True
47
+ print ('Fail' )
48
+ return
49
+ _failed = False
50
+ for i , line in enumerate (check ):
51
+ if line == good [i ]:
52
+ continue
53
+ _failed = True
54
+ if _failed :
55
+ failed = True
56
+ print ('Fail' )
57
+ return
58
+ print ('Pass' )
59
+
60
+
61
+ def main ():
62
+ for cmd , cmd_v in cmds .items ():
63
+ with subprocess .Popen (cmd_v [0 ], shell = True , stdout = subprocess .PIPE , universal_newlines = True ) as proc :
64
+ out = proc .communicate ()[0 ].strip ()
65
+ out = out .split ('\n ' )
66
+ pass_fail (cmd , out , cmd_v [1 ])
67
+ if failed :
68
+ print ('One or more tests failed' )
69
+ return 1
70
+ print ('All tests passed!' )
71
+
72
+ if __name__ == '__main__' :
73
+ sys .exit (main ())
0 commit comments