-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrun_tests.py
153 lines (116 loc) · 4.56 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"""
Run all or some tests.
By default, we use 'nosetests' but 'unittest discover' can be used instead. See help.
This script is fragile. It should be tested on different platforms.
"""
from __future__ import print_function
import os
import sys
import subprocess
import shutil
import distutils
import argparse
try:
import nose
except ImportError:
print("You need to install nose to run the tests.")
sys.exit(-1)
def make_parser():
"""
Create a comment line argument parser.
Returns:
The command line parser.
"""
parser = argparse.ArgumentParser(description='%s: run all or some tests for the CySparse library' % os.path.basename(sys.argv[0]))
parser.add_argument("-r", "--rebuild", help="Rebuild from scratch the CySparse library", action='store_true', required=False)
parser.add_argument("-b", "--build", help="Build (if needed) CySparse library with new code", action='store_true', required=False)
parser.add_argument("-p", "--pattern", help="Run tests with a given filename pattern", required=False)
parser.add_argument("-v", "--verbose", help="Add some context on the console", action='store_true', required=False)
parser.add_argument("-n", "--dont_use_nose", help="Use unittest discover instead of nosetests", action='store_true', required=False)
return parser
def clean_lib():
subprocess.call(['python', 'clean.py'])
def generate_lib():
subprocess.call(['python', 'generate_code.py','-r'])
subprocess.call(['python', 'setup.py', 'build'])
def launch_nosetests(pattern=None, verbose=False, use_libraries=None):
current_dir = os.getcwd()
os.chdir(lib_dir)
commands_list = ['nosetests']
if verbose:
commands_list.append('--verbosity=2')
if pattern is not None:
commands_list.append('-p')
commands_list.append(pattern)
# # do we exclude some libraries?
# # TODO: this is very fragile...
# if use_libraries is not None:
# # SuiteSparse
# if not use_libraries['use_suitesparse']:
# commands_list.append('--exclude-dir')
# commands_list.append(os.path.sep.join(['tests', 'cysparse', 'linalg', 'suitesparse']))
#
# # MUMPS
# if not use_libraries['use_suitesparse']:
# commands_list.append('--exclude-dir')
# commands_list.append(os.path.sep.join(['tests', 'cysparse', 'linalg', 'mumps']))
commands_list.append('tests')
if verbose:
print("launch command: '%s':" % " ".join(commands_list))
result = subprocess.call(commands_list)
os.chdir(current_dir)
return result
def launch_unittest(pattern=None, verbose=False):
current_dir = os.getcwd()
os.chdir(lib_dir)
commands_list = ['python', '-m', 'unittest', 'discover', 'tests', '-c']
if verbose:
commands_list.append('-v')
if pattern is not None:
commands_list.append('-p')
commands_list.append(pattern)
if verbose:
print("launch command: '%s':" % " ".join(commands_list))
result = subprocess.call(commands_list)
os.chdir(current_dir)
return result
if __name__ == "__main__":
# line arguments
parser = make_parser()
arg_options = parser.parse_args()
platform = distutils.util.get_platform()
python_version = sys.version
lib_dir = "build" + os.path.sep + "lib." + platform + "-" + python_version[0:3]
destination_dir = lib_dir + os.path.sep + "tests"
if arg_options.verbose:
print("Deleting test directory %s... " % destination_dir,)
# clean libxxx/tests because shutil.copytree only copies non existing directories
shutil.rmtree(destination_dir, ignore_errors=True)
if arg_options.verbose:
print("done")
print("copying tests into test directory %s..." % destination_dir,)
shutil.copytree("tests", destination_dir, symlinks=False, ignore=None)
if arg_options.verbose:
print("done")
if arg_options.rebuild:
if arg_options.verbose:
print("Cleaning lib...",)
clean_lib()
if arg_options.verbose:
print("done")
print("Generating lib...",)
generate_lib()
if arg_options.verbose:
print("done")
elif arg_options.build:
if arg_options.verbose:
print("Generating lib...",)
generate_lib()
if arg_options.verbose:
print("done")
result = -1
if arg_options.dont_use_nose:
result = launch_unittest(arg_options.pattern, arg_options.verbose)
else:
result = launch_nosetests(arg_options.pattern, arg_options.verbose)
sys.exit(result)