-
Notifications
You must be signed in to change notification settings - Fork 27
/
test.py
executable file
·62 lines (43 loc) · 1.57 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
#!/usr/bin/env python
"""Test output of various incantations of example.sh
"""
import unittest
import subprocess
from textwrap import dedent
def run(script, *args):
cmd = [script] + list(args)
if hasattr(subprocess, 'check_output'):
# python 2.7+
output = subprocess.check_output(cmd, universal_newlines=True).strip()
else:
# python 2.6
output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0].strip()
return output
def strip(val):
return dedent(val).strip()
class TestEverything(unittest.TestCase):
def test01(self):
output = run('./example.sh', 'infile', 'outfile')
expected = strip("""
required infile: infile
required outfile: outfile
the answer: 42
do the thing? no, do not do it
arg with multiple values: []
""")
self.assertEqual(output, expected)
def test02(self):
output = run('./example.sh', 'infile', 'outfile', '-m', 'one fish', 'two fish')
self.assertTrue('arg with multiple values: [one fish] [two fish]' in output)
def test03(self):
output = run('./example.sh', 'infile', 'outfile', '-d')
self.assertTrue('yes, do it' in output)
def test04(self):
output = run('./example.sh', 'infile', 'outfile', '-a', '0')
self.assertTrue('the answer: 0' in output)
def test05(self):
# argparse.bash prints a script template
output = run('./argparse.bash')
self.assertTrue(output.startswith('#!/usr/bin/env bash'))
if __name__ == '__main__':
unittest.main()