-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
executable file
·131 lines (104 loc) · 3.72 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
#!/usr/bin/env python
"""run all the test cases"""
import argparse
import fnmatch
import logging
import os
import re
import subprocess
import sys
from contextlib import contextmanager
class Logger(object):
def __init__(self, file_stream):
self.file_stream = file_stream
self.current_test = 'null'
def error(self, text):
text = '%s: %s' % (self.current_test, text)
self._write('ERROR: %s' % text)
logging.error(text)
def info(self, text):
self._write('INFO: %s' % text)
logging.info(text)
def log_new_test(self, filename, folder):
folder = os.path.basename(os.path.normpath(folder))
filename = os.path.splitext(filename)[0]
self.current_test = '.'.join([folder, filename])
text = '%s: running' % self.current_test
self.info(text)
def success(self):
self.info('%s: success' % self.current_test)
def print_command_output(self, output, display_on_screen=False):
self._write(output)
if display_on_screen:
print('\033[30;1m%s\033[0m' % output)
def _write(self, text):
self.file_stream.write(text + '\n')
@contextmanager
def pushd(directory):
"""Context manager to temporally change working directory."""
cwd = os.getcwd()
try:
os.chdir(directory)
yield
finally:
os.chdir(cwd)
def run_test(filename, directory, environmet):
filename = './' + filename
with pushd(directory):
return subprocess.check_output(
filename,
env=environmet,
stderr=subprocess.STDOUT).decode('utf-8')
def source_walk(root, file_pattern):
root = os.path.abspath(root)
regex = re.compile(fnmatch.translate(file_pattern))
for path, _, files in os.walk(root):
files[:] = [f for f in files if regex.match(f) is not None]
for filename in files:
yield filename, path
def do_the_test():
argparser = argparse.ArgumentParser(description=__doc__)
argparser.add_argument(
'--script',
metavar='FILENAME',
dest='script_filename',
default='*.sh',
help='filename of the script to run on each subfolder (can have wild cards)')
argparser.add_argument(
'--log-file',
metavar='FILEPATH',
dest='log_file',
default='./test.log',
help='log file path')
argparser.add_argument(
'configure_pyz_path',
help='path to the configure.pyz to test')
argparser.add_argument(
'testdir',
help='directory containing test cases')
args = argparser.parse_args()
loglevel = logging.DEBUG
logging.basicConfig(format='%(levelname)s: %(message)s', level=loglevel)
if not os.path.isdir(args.testdir):
logging.critical('"%s" is not a directory', args.testdir)
return
with open(args.log_file, 'w+') as file_stream:
logger = Logger(file_stream)
environmet = dict(os.environ)
environmet['CONFIGURE_PYZ'] = os.path.abspath(args.configure_pyz_path)
for filename, path in source_walk(args.testdir, args.script_filename):
try:
logger.log_new_test(filename, path)
output = run_test(filename, path, environmet)
logger.print_command_output(output)
logger.success()
except subprocess.CalledProcessError as exception:
logger.print_command_output(exception.output, display_on_screen=True)
logger.error(exception)
sys.exit(1)
except Exception as exception:
logger.error(exception)
sys.exit(2)
logger.info("every test terminated successfully")
if __name__ == '__main__':
do_the_test()