-
Notifications
You must be signed in to change notification settings - Fork 37
/
run-tests.py
executable file
·134 lines (104 loc) · 3.81 KB
/
run-tests.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
#!/usr/bin/env python3
import argparse
import os
import os.path as path
import subprocess
import signal
import sys
import time
THIS_DIR = path.abspath(path.dirname(__file__))
if sys.platform == 'win32' or sys.platform == 'cygwin':
TESTS_EXE = 'tests.exe'
else:
TESTS_EXE = 'tests'
MOCK_SERVERS_DIR = path.join(THIS_DIR, 'test', 'mock-servers')
MOCK_SERVERS = [
('mock-compression-server.js', 'mock-compression-server-log'),
('mock-disconnect-server.js', 'mock-disconnect-server-log'),
('mock-kick-server.js', 'mock-kick-server-log'),
('mock-timeout-server.js', 'mock-timeout-server-log'),
('mock-destroy-socket-server.js', 'mock-destroy-socket-server-log'),
('mock-kill-client-server.js', 'mock-kill-client-server-log'),
]
mock_server_processes = []
file_descriptors = []
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--node-path', dest='node_path', help='Where is node located')
parser.add_argument('--tests-dir', dest='tests_dir',
help='Where the test executable is located')
parser.add_argument('--only-deps', dest='only_deps', action='store_true')
parser.add_argument('--leak-check', dest='leak_check', action='store_true')
return parser.parse_known_args()
def kill_all_servers():
for p in mock_server_processes: p.terminate()
def close_file_descriptors():
for fd in file_descriptors: fd.close()
def signal_handler(signal, frame):
kill_all_servers()
close_file_descriptors()
def start_mock_servers(node_path):
os.chdir(MOCK_SERVERS_DIR)
for (s, l) in MOCK_SERVERS:
print('Starting {}...'.format(s))
fd = open(path.join(MOCK_SERVERS_DIR, l), 'wb')
if node_path:
process = subprocess.Popen(
[node_path, s], stdout=fd, stderr=fd,
)
else:
process = subprocess.Popen(
['node', s], stdout=fd, stderr=fd,
)
mock_server_processes.append(process)
file_descriptors.append(fd)
os.chdir(THIS_DIR)
def ensure_tests_executable(tests_dir):
tests_path = path.join(tests_dir, TESTS_EXE)
if not path.exists(tests_path):
print('Tests executable {} does not exist.'.format(tests_path))
exit(1)
def run_tests(tests_dir):
os.chdir(tests_dir)
if sys.platform == 'win32' or sys.platform == 'cygwin':
args = '{} {}'.format(TESTS_EXE, ' '.join(sys.argv))
else:
args = './{} {}'.format(TESTS_EXE, ' '.join(sys.argv))
code = subprocess.call(args, shell=True)
os.chdir(THIS_DIR)
return code
def run_leak_check(tests_dir):
os.chdir(tests_dir)
args = 'valgrind --tool=memcheck --leak-check=full --xml=yes --xml-file=leaks.xml ./{} --no-fork {}'.format(TESTS_EXE, ' '.join(sys.argv))
code = subprocess.call(args, shell=True)
os.chdir(THIS_DIR)
return code
def main():
args, unknown_args = parse_args()
sys.argv = unknown_args
if args.node_path and not path.exists(args.node_path):
print("Node path does not exist.")
exit(1)
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)
if args.only_deps:
start_mock_servers(args.node_path)
input()
else:
if args.tests_dir == None:
print('You need to pass the test executable directory')
exit(1)
ensure_tests_executable(args.tests_dir)
start_mock_servers(args.node_path)
time.sleep(1)
if args.leak_check:
if sys.platform != 'linux':
raise Exception('Leak check can only be run under linux')
code = run_leak_check(args.tests_dir)
else:
code = run_tests(args.tests_dir)
kill_all_servers()
close_file_descriptors()
exit(code)
if __name__ == '__main__':
main()