1 | #!/usr/bin/python |
---|
2 | from __future__ import print_function |
---|
3 | |
---|
4 | from pybin.tools import * |
---|
5 | from pybin.test_run import * |
---|
6 | from pybin import settings |
---|
7 | |
---|
8 | import argparse |
---|
9 | import re |
---|
10 | import sys |
---|
11 | import time |
---|
12 | |
---|
13 | ################################################################################ |
---|
14 | # help functions |
---|
15 | ################################################################################ |
---|
16 | |
---|
17 | def findTests(): |
---|
18 | expected = [] |
---|
19 | |
---|
20 | def matchTest(path): |
---|
21 | match = re.search("^%s\/([\w\/\-_]*).expect\/([\w\-_]+)(\.[\w\-_]+)?\.txt$" % settings.SRCDIR, path) |
---|
22 | if match : |
---|
23 | test = Test() |
---|
24 | test.name = match.group(2) |
---|
25 | test.path = match.group(1) |
---|
26 | test.arch = match.group(3)[1:] if match.group(3) else None |
---|
27 | if settings.arch.match(test.arch): |
---|
28 | expected.append(test) |
---|
29 | |
---|
30 | pathWalk( matchTest ) |
---|
31 | |
---|
32 | return expected |
---|
33 | |
---|
34 | # reads the directory ./.expect and indentifies the tests |
---|
35 | def listTests( includes, excludes ): |
---|
36 | # tests directly in the .expect folder will always be processed |
---|
37 | test_list = findTests() |
---|
38 | |
---|
39 | # if we have a limited number of includes, filter by them |
---|
40 | if includes: |
---|
41 | test_list = [x for x in test_list if |
---|
42 | x.target().startswith( tuple(includes) ) |
---|
43 | ] |
---|
44 | |
---|
45 | # # if we have a folders to excludes, filter by them |
---|
46 | if excludes: |
---|
47 | test_list = [x for x in test_list if not |
---|
48 | x.target().startswith( tuple(excludes) ) |
---|
49 | ] |
---|
50 | |
---|
51 | return test_list |
---|
52 | |
---|
53 | # from the found tests, filter all the valid tests/desired tests |
---|
54 | def validTests( options ): |
---|
55 | tests = [] |
---|
56 | |
---|
57 | # if we are regenerating the tests we need to find the information of the |
---|
58 | # already existing tests and create new info for the new tests |
---|
59 | if options.regenerate_expected : |
---|
60 | for testname in options.tests : |
---|
61 | testname = canonicalPath( testname ) |
---|
62 | if Test.valid_name(testname): |
---|
63 | found = [test for test in allTests if canonicalPath( test.target() ) == testname] |
---|
64 | tests.append( found[0] if len(found) == 1 else Test.from_target(testname) ) |
---|
65 | else : |
---|
66 | print('ERROR: "%s", tests are not allowed to end with a C/C++/CFA extension, ignoring it' % testname, file=sys.stderr) |
---|
67 | |
---|
68 | else : |
---|
69 | # otherwise we only need to validate that all tests are present in the complete list |
---|
70 | for testname in options.tests: |
---|
71 | test = [t for t in allTests if pathCmp( t.target(), testname )] |
---|
72 | |
---|
73 | if test : |
---|
74 | tests.append( test[0] ) |
---|
75 | else : |
---|
76 | print('ERROR: No expected file for test %s, ignoring it' % testname, file=sys.stderr) |
---|
77 | |
---|
78 | return tests |
---|
79 | |
---|
80 | # parses the option |
---|
81 | def getOptions(): |
---|
82 | # create a parser with the arguments for the tests script |
---|
83 | parser = argparse.ArgumentParser(description='Script which runs cforall tests') |
---|
84 | parser.add_argument('--debug', help='Run all tests in debug or release', type=yes_no, default='yes') |
---|
85 | parser.add_argument('--install', help='Run all tests based on installed binaries or tree binaries', type=yes_no, default='no') |
---|
86 | parser.add_argument('--arch', help='Test for specific architecture', type=str, default='') |
---|
87 | parser.add_argument('--timeout', help='Maximum duration in seconds after a single test is considered to have timed out', type=int, default=60) |
---|
88 | parser.add_argument('--global-timeout', help='Maximum cumulative duration in seconds after the ALL tests are considered to have timed out', type=int, default=7200) |
---|
89 | parser.add_argument('--dry-run', help='Don\'t run the tests, only output the commands', action='store_true') |
---|
90 | parser.add_argument('--list', help='List all test available', action='store_true') |
---|
91 | parser.add_argument('--all', help='Run all test available', action='store_true') |
---|
92 | parser.add_argument('--regenerate-expected', help='Regenerate the .expect by running the specified tets, can be used with --all option', action='store_true') |
---|
93 | parser.add_argument('-j', '--jobs', help='Number of tests to run simultaneously', type=int) |
---|
94 | parser.add_argument('--list-comp', help='List all valide arguments', action='store_true') |
---|
95 | parser.add_argument('-I','--include', help='Directory of test to include, can be used multiple time, All if omitted', action='append') |
---|
96 | parser.add_argument('-E','--exclude', help='Directory of test to exclude, can be used multiple time, None if omitted', action='append') |
---|
97 | parser.add_argument('tests', metavar='test', type=str, nargs='*', help='a list of tests to run') |
---|
98 | |
---|
99 | try: |
---|
100 | options = parser.parse_args() |
---|
101 | except: |
---|
102 | print('ERROR: invalid arguments', file=sys.stderr) |
---|
103 | parser.print_help(sys.stderr) |
---|
104 | sys.exit(1) |
---|
105 | |
---|
106 | # script must have at least some tests to run or be listing |
---|
107 | listing = options.list or options.list_comp |
---|
108 | all_tests = options.all |
---|
109 | some_tests = len(options.tests) > 0 |
---|
110 | some_dirs = len(options.include) > 0 if options.include else 0 |
---|
111 | |
---|
112 | # check that exactly one of the booleans is set to true |
---|
113 | if not sum( (listing, all_tests, some_tests, some_dirs) ) > 0 : |
---|
114 | print('ERROR: must have option \'--all\', \'--list\', \'--include\', \'-I\' or non-empty test list', file=sys.stderr) |
---|
115 | parser.print_help() |
---|
116 | sys.exit(1) |
---|
117 | |
---|
118 | return options |
---|
119 | |
---|
120 | ################################################################################ |
---|
121 | # running test functions |
---|
122 | ################################################################################ |
---|
123 | def success(val): |
---|
124 | return val == 0 or settings.dry_run |
---|
125 | |
---|
126 | def isExe(file): |
---|
127 | return settings.dry_run or fileIsExecutable(file) |
---|
128 | |
---|
129 | def noRule(file, target): |
---|
130 | return not settings.dry_run and fileContainsOnly(file, "make: *** No rule to make target `%s'. Stop." % target) |
---|
131 | |
---|
132 | # logic to run a single test and return the result (No handling of printing or other test framework logic) |
---|
133 | def run_single_test(test): |
---|
134 | |
---|
135 | # find the output file based on the test name and options flag |
---|
136 | exe_file = test.target_executable(); |
---|
137 | out_file = test.target_output() |
---|
138 | err_file = test.error_log() |
---|
139 | cmp_file = test.expect() |
---|
140 | in_file = test.input() |
---|
141 | |
---|
142 | # prepare the proper directories |
---|
143 | test.prepare() |
---|
144 | |
---|
145 | # build, skipping to next test on error |
---|
146 | with Timed() as comp_dur: |
---|
147 | make_ret, _ = make( test.target(), redirects = ("2> %s 1> /dev/null" % out_file), error_file = err_file ) |
---|
148 | |
---|
149 | # if the make command succeds continue otherwise skip to diff |
---|
150 | run_dur = None |
---|
151 | if success(make_ret): |
---|
152 | with Timed() as run_dur: |
---|
153 | if isExe(exe_file): |
---|
154 | # run test |
---|
155 | retcode = run(exe_file, out_file, in_file) |
---|
156 | else : |
---|
157 | # simply cat the result into the output |
---|
158 | retcode = cat(exe_file, out_file) |
---|
159 | else: |
---|
160 | retcode = mv(err_file, out_file) |
---|
161 | |
---|
162 | if success(retcode): |
---|
163 | if settings.generating : |
---|
164 | # if we are ounly generating the output we still need to check that the test actually exists |
---|
165 | if noRule(out_file, test.target()) : |
---|
166 | retcode = 1 |
---|
167 | error = "\t\tNo make target for test %s!" % test.target() |
---|
168 | rm(out_file) |
---|
169 | else: |
---|
170 | error = None |
---|
171 | else : |
---|
172 | # fetch return code and error from the diff command |
---|
173 | retcode, error = diff(cmp_file, out_file) |
---|
174 | |
---|
175 | else: |
---|
176 | with open (out_file, "r") as myfile: |
---|
177 | error = myfile.read() |
---|
178 | |
---|
179 | ret, info = coreInfo(exe_file) |
---|
180 | error = error + info |
---|
181 | |
---|
182 | |
---|
183 | |
---|
184 | # clean the executable |
---|
185 | rm(exe_file) |
---|
186 | |
---|
187 | return retcode, error, [comp_dur.duration, run_dur.duration if run_dur else None] |
---|
188 | |
---|
189 | # run a single test and handle the errors, outputs, printing, exception handling, etc. |
---|
190 | def run_test_worker(t) : |
---|
191 | |
---|
192 | with SignalHandling(): |
---|
193 | # print formated name |
---|
194 | name_txt = "%24s " % t.name |
---|
195 | |
---|
196 | retcode, error, duration = run_single_test(t) |
---|
197 | |
---|
198 | # update output based on current action |
---|
199 | result_txt = TestResult.toString( retcode, duration ) |
---|
200 | |
---|
201 | #print result with error if needed |
---|
202 | text = name_txt + result_txt |
---|
203 | out = sys.stdout |
---|
204 | if error : |
---|
205 | text = text + "\n" + error |
---|
206 | out = sys.stderr |
---|
207 | |
---|
208 | print(text, file = out) |
---|
209 | sys.stdout.flush() |
---|
210 | sys.stderr.flush() |
---|
211 | |
---|
212 | return retcode != TestResult.SUCCESS |
---|
213 | |
---|
214 | # run the given list of tests with the given parameters |
---|
215 | def run_tests(tests, jobs) : |
---|
216 | # clean the sandbox from previous commands |
---|
217 | make('clean', redirects = '> /dev/null 2>&1') |
---|
218 | |
---|
219 | # create the executor for our jobs and handle the signal properly |
---|
220 | pool = setupPool(jobs) |
---|
221 | |
---|
222 | # for each test to run |
---|
223 | try : |
---|
224 | results = pool.map_async( |
---|
225 | run_test_worker, |
---|
226 | tests, |
---|
227 | chunksize = 1 |
---|
228 | ).get(settings.timeout.total) |
---|
229 | except KeyboardInterrupt: |
---|
230 | pool.terminate() |
---|
231 | print("Tests interrupted by user") |
---|
232 | sys.exit(1) |
---|
233 | |
---|
234 | # clean the workspace |
---|
235 | make('clean', redirects = '> /dev/null 2>&1') |
---|
236 | |
---|
237 | for failed in results: |
---|
238 | if failed : |
---|
239 | return 1 |
---|
240 | |
---|
241 | return 0 |
---|
242 | |
---|
243 | |
---|
244 | ################################################################################ |
---|
245 | # main loop |
---|
246 | ################################################################################ |
---|
247 | if __name__ == "__main__": |
---|
248 | |
---|
249 | # parse the command line arguments |
---|
250 | options = getOptions() |
---|
251 | |
---|
252 | # init global settings |
---|
253 | settings.init( options ) |
---|
254 | |
---|
255 | # fetch the liest of all valid tests |
---|
256 | allTests = listTests( options.include, options.exclude ) |
---|
257 | |
---|
258 | |
---|
259 | # if user wants all tests than no other treatement of the test list is required |
---|
260 | if options.all or options.list or options.list_comp or options.include : |
---|
261 | tests = allTests |
---|
262 | |
---|
263 | #otherwise we need to validate that the test list that was entered is valid |
---|
264 | else : |
---|
265 | tests = validTests( options ) |
---|
266 | |
---|
267 | # make sure we have at least some test to run |
---|
268 | if not tests : |
---|
269 | print('ERROR: No valid test to run', file=sys.stderr) |
---|
270 | sys.exit(1) |
---|
271 | |
---|
272 | |
---|
273 | # sort the test alphabetically for convenience |
---|
274 | tests.sort(key=lambda t: (t.arch if t.arch else '') + t.target()) |
---|
275 | |
---|
276 | # users may want to simply list the tests |
---|
277 | if options.list_comp : |
---|
278 | print("-h --help --debug --dry-run --list --arch --all --regenerate-expected --install --timeout --global-timeout -j --jobs ", end='') |
---|
279 | print(" ".join(map(lambda t: "%s" % (t.target()), tests))) |
---|
280 | |
---|
281 | elif options.list : |
---|
282 | print("Listing for %s:%s"% (settings.arch.string, settings.debug.string)) |
---|
283 | fancy_print("\n".join(map(lambda t: "%s" % (t.toString()), tests))) |
---|
284 | |
---|
285 | else : |
---|
286 | # check the build configuration works |
---|
287 | settings.validate() |
---|
288 | |
---|
289 | options.jobs, forceJobs = jobCount( options, tests ) |
---|
290 | settings.updateMakeCmd(forceJobs, options.jobs) |
---|
291 | |
---|
292 | print('%s (%s:%s) on %i cores' % ( |
---|
293 | 'Regenerate tests' if settings.generating else 'Running', |
---|
294 | settings.arch.string, |
---|
295 | settings.debug.string, |
---|
296 | options.jobs |
---|
297 | )) |
---|
298 | |
---|
299 | # otherwise run all tests and make sure to return the correct error code |
---|
300 | sys.exit( run_tests(tests, options.jobs) ) |
---|