-
Notifications
You must be signed in to change notification settings - Fork 1
/
unittestt.py
213 lines (180 loc) · 7.44 KB
/
unittestt.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#! /usr/bin/env python
# encoding: utf-8
# Copyright (c)2011, Hideyuki Tanaka
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
#
# * Neither the name of Hideyuki Tanaka nor the names of other
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import os, sys
from waflib.TaskGen import before, after, feature
from waflib import Options, Task, Utils, Logs, Errors
def configure(conf):
if conf.check_cfg(path = 'gtest-config',
args = '--cppflags --cxxflags --ldflags --libs',
package = '',
uselib_store = 'GTEST'):
t = conf.env.LIB_GTEST
conf.env.LIB_GTEST = []
for l in t:
if l == 'gtest':
l = 'gtest_main'
conf.env.LIB_GTEST.append(l)
def options(opt):
opt.add_option('--check', action = 'store_true', default = False,
help = 'Execute unit tests')
opt.add_option('--checkall', action = 'store_true', default = False,
help = 'Execute all unit tests')
opt.add_option('--checkone', action = 'store', default = False,
help = 'Execute specified unit test')
opt.add_option('--checkfilter', action = 'store', default = False,
help = 'Execute unit tests sprcified by pattern')
def match_filter(filt, targ):
if isinstance(filt, str):
(pat, _, _) = filt.partition('.')
if pat == '*':
return True
return pat == targ
return False
@feature('testt', 'gtest')
@before('process_rule')
def test_remover(self):
if not Options.options.check and not Options.options.checkall and self.target != Options.options.checkone and not match_filter(Options.options.checkfilter, self.target):
self.meths[:] = []
@feature('gtest')
@before('apply_core')
def gtest_attach(self):
if not self.env.HAVE_GTEST:
Logs.error('gtest is not found')
self.meths[:] = []
return
if not hasattr(self, 'use'):
self.use = 'GTEST'
elif isinstance(self.use, str):
self.use += ' GTEST'
else:
self.use.append('GTEST')
@feature('testt', 'gtest')
@after('apply_link')
def make_test(self):
if not 'cprogram' in self.features and not 'cxxprogram' in self.features:
Logs.error('test cannot be executed %s'%self)
return
self.default_install_path = None
self.create_task('utest', self.link_task.outputs)
import threading
testlock = threading.Lock()
class utest(Task.Task):
"""
Execute a unit test
"""
color = 'PINK'
ext_in = ['.bin']
vars = []
def runnable_status(self):
stat = super(utest, self).runnable_status()
if stat != Task.SKIP_ME:
return stat
if Options.options.checkall:
return Task.RUN_ME
if Options.options.checkone == self.generator.name:
return Task.RUN_ME
if isinstance(Options.options.checkfilter, str):
if match_filter(Options.options.checkfilter, self.generator.name):
return Task.RUN_ME
return stat
def run(self):
"""
Execute the test. The execution is always successful, but the results
are stored on ``self.generator.bld.utest_results`` for postprocessing.
"""
status = 0
filename = self.inputs[0].abspath()
self.ut_exec = getattr(self, 'ut_exec', [filename])
if getattr(self.generator, 'ut_fun', None):
self.generator.ut_fun(self)
try:
fu = getattr(self.generator.bld, 'all_test_paths')
except AttributeError:
fu = os.environ.copy()
self.generator.bld.all_test_paths = fu
lst = []
for g in self.generator.bld.groups:
for tg in g:
if getattr(tg, 'link_task', None):
lst.append(tg.link_task.outputs[0].parent.abspath())
def add_path(dct, path, var):
dct[var] = os.pathsep.join(Utils.to_list(path) + [os.environ.get(var, '')])
if sys.platform == 'win32':
add_path(fu, lst, 'PATH')
elif sys.platform == 'darwin':
add_path(fu, lst, 'DYLD_LIBRARY_PATH')
add_path(fu, lst, 'LD_LIBRARY_PATH')
else:
add_path(fu, lst, 'LD_LIBRARY_PATH')
if isinstance(Options.options.checkfilter, str):
(_, _, filt) = Options.options.checkfilter.partition('.')
if filt != "":
self.ut_exec += ['--gtest_filter=' + filt]
cwd = getattr(self.generator, 'ut_cwd', '') or self.inputs[0].parent.abspath()
proc = Utils.subprocess.Popen(self.ut_exec, cwd=cwd, env=fu, stderr=Utils.subprocess.PIPE, stdout=Utils.subprocess.PIPE)
(stdout, stderr) = proc.communicate()
tup = (filename, proc.returncode, stdout, stderr)
self.generator.utest_result = tup
testlock.acquire()
try:
bld = self.generator.bld
Logs.debug("ut: %r", tup)
try:
bld.utest_results.append(tup)
except AttributeError:
bld.utest_results = [tup]
a = getattr(self.generator.bld, 'added_post_fun', False)
if not a:
self.generator.bld.add_post_fun(summary)
self.generator.bld.added_post_fun = True
finally:
testlock.release()
def summary(bld):
lst = getattr(bld, 'utest_results', [])
if not lst: return
total = len(lst)
fail = len([x for x in lst if x[1]])
Logs.pprint('CYAN', 'test summary')
Logs.pprint('CYAN', ' tests that pass %d/%d' % (total-fail, total))
for (f, code, out, err) in lst:
if not code:
Logs.pprint('GREEN', ' %s' % f)
if isinstance(Options.options.checkfilter, str):
print(out)
if fail>0:
Logs.pprint('RED', ' tests that fail %d/%d' % (fail, total))
for (f, code, out, err) in lst:
if code:
Logs.pprint('RED', ' %s' % f)
print(out.decode())
raise Errors.WafError('test failed')