-
Notifications
You must be signed in to change notification settings - Fork 26
/
runtest.py
executable file
·54 lines (46 loc) · 1.42 KB
/
runtest.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
#!/usr/bin/env python3
LINEWIDTH = 80
COLORIZED = True
import os
import shlex
import subprocess
import sys
def main():
# Store arguments
testname = sys.argv[1]
logfile = sys.argv[2]
cmds = sys.argv[3:]
# Run test and save results
with open(logfile, 'w') as f:
cmd = ' '.join(map(shlex.quote, cmds))
f.write("# Command executed: ")
f.write(cmd)
f.write('\n')
p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True, shell=True)
out, _ = p.communicate()
f.write(out)
returncode = p.returncode
# Check returncode to set status
if returncode == 0:
status = True
statustext = 'PASS'
else:
status = False
statustext = 'FAIL'
# Add coloring if desired
use_colors = COLORIZED
if 'COLORIZED' in os.environ:
use_colors = True if os.environ['COLORIZED'] == '1' else False
if use_colors and sys.stdout.isatty():
if status:
statustext = '\033[32m' + statustext + '\033[0m'
else:
statustext = '\033[31m' + statustext + '\033[0m'
# Print status
print("{t} {d} {s}".format(t=testname,
d=(LINEWIDTH-len(testname)-4-2)*'.',
s=statustext))
if __name__ == '__main__':
sys.exit(main())