1 | #!/usr/bin/python |
---|
2 | from __future__ import print_function |
---|
3 | |
---|
4 | from os import listdir, environ |
---|
5 | from os.path import isfile, join, splitext |
---|
6 | from subprocess import Popen, PIPE, STDOUT |
---|
7 | |
---|
8 | import argparse |
---|
9 | import os |
---|
10 | import re |
---|
11 | import stat |
---|
12 | import sys |
---|
13 | |
---|
14 | ################################################################################ |
---|
15 | # help functions |
---|
16 | ################################################################################ |
---|
17 | |
---|
18 | # Test class that defines what a test is |
---|
19 | class Test: |
---|
20 | def __init__(self, name, path): |
---|
21 | self.name, self.path = name, path |
---|
22 | |
---|
23 | # parses the Makefile to find the machine type (32-bit / 64-bit) |
---|
24 | def getMachineType(): |
---|
25 | sh('echo "int main() { return 0; }" > .dummy.c') |
---|
26 | sh("make .dummy", print2stdout=False) |
---|
27 | _, out = sh("file .dummy", print2stdout=False) |
---|
28 | sh("rm -f .dummy.c > /dev/null 2>&1") |
---|
29 | sh("rm -f .dummy > /dev/null 2>&1") |
---|
30 | return re.search("ELF\s([0-9]+)-bit", out).group(1) |
---|
31 | |
---|
32 | # reads the directory ./.expect and indentifies the tests |
---|
33 | def listTests(): |
---|
34 | machineType = getMachineType() |
---|
35 | |
---|
36 | # tests directly in the .expect folder will always be processed |
---|
37 | generic_list = map(lambda fname: Test(fname, fname), |
---|
38 | [splitext(f)[0] for f in listdir('./.expect') |
---|
39 | if not f.startswith('.') and f.endswith('.txt') |
---|
40 | ]) |
---|
41 | |
---|
42 | # tests in the machineType folder will be ran only for the corresponding compiler |
---|
43 | typed_list = map(lambda fname: Test( fname, "%s/%s" % (machineType, fname) ), |
---|
44 | [splitext(f)[0] for f in listdir("./.expect/%s" % machineType) |
---|
45 | if not f.startswith('.') and f.endswith('.txt') |
---|
46 | ]) |
---|
47 | |
---|
48 | # append both lists to get |
---|
49 | return generic_list + typed_list |
---|
50 | |
---|
51 | # helper functions to run terminal commands |
---|
52 | def sh(cmd, dry_run = False, print2stdout = True): |
---|
53 | if dry_run : # if this is a dry_run, only print the commands that would be ran |
---|
54 | print("cmd: %s" % cmd) |
---|
55 | return 0, None |
---|
56 | else : # otherwise create a pipe and run the desired command |
---|
57 | proc = Popen(cmd, stdout=None if print2stdout else PIPE, stderr=STDOUT, shell=True) |
---|
58 | out, err = proc.communicate() |
---|
59 | return proc.returncode, out |
---|
60 | |
---|
61 | # helper function to replace patterns in a file |
---|
62 | def file_replace(fname, pat, s_after): |
---|
63 | # first, see if the pattern is even in the file. |
---|
64 | with open(fname) as f: |
---|
65 | if not any(re.search(pat, line) for line in f): |
---|
66 | return # pattern does not occur in file so we are done. |
---|
67 | |
---|
68 | # pattern is in the file, so perform replace operation. |
---|
69 | with open(fname) as f: |
---|
70 | out_fname = fname + ".tmp" |
---|
71 | out = open(out_fname, "w") |
---|
72 | for line in f: |
---|
73 | out.write(re.sub(pat, s_after, line)) |
---|
74 | out.close() |
---|
75 | os.rename(out_fname, fname) |
---|
76 | |
---|
77 | # tests output may differ depending on the depth of the makefile |
---|
78 | def fix_MakeLevel(file) : |
---|
79 | if environ.get('MAKELEVEL') : |
---|
80 | file_replace(file, "make\[%i\]" % int(environ.get('MAKELEVEL')), 'make' ) |
---|
81 | |
---|
82 | # helper function to check if a files contains only a spacific string |
---|
83 | def fileContainsOnly(file, text) : |
---|
84 | with open(file) as f: |
---|
85 | ff = f.read().strip() |
---|
86 | result = ff == text.strip() |
---|
87 | |
---|
88 | return result; |
---|
89 | |
---|
90 | # check whether or not a file is executable |
---|
91 | def fileIsExecutable(file) : |
---|
92 | try : |
---|
93 | fileinfo = os.stat(file) |
---|
94 | return bool(fileinfo.st_mode & stat.S_IXUSR) |
---|
95 | except Exception as inst: |
---|
96 | print(type(inst)) # the exception instance |
---|
97 | print(inst.args) # arguments stored in .args |
---|
98 | print(inst) |
---|
99 | return False |
---|
100 | |
---|
101 | # find the test data for a given test name |
---|
102 | def filterTests(testname) : |
---|
103 | found = [test for test in allTests if test.name == testname] |
---|
104 | return (found[0] if len(found) == 1 else Test(testname, testname) ) |
---|
105 | |
---|
106 | ################################################################################ |
---|
107 | # running test functions |
---|
108 | ################################################################################ |
---|
109 | def run_test_instance(test, generate, dry_run): |
---|
110 | |
---|
111 | # find the output file based on the test name and options flag |
---|
112 | out_file = (".out/%s.log" % test.name) if not generate else (".expect/%s.txt" % test.path) |
---|
113 | |
---|
114 | # remove any outputs from the previous tests to prevent side effects |
---|
115 | sh("rm -f %s" % out_file, dry_run) |
---|
116 | sh("rm -f %s > /dev/null 2>&1" % test.name, dry_run) |
---|
117 | |
---|
118 | # build, skipping to next test on error |
---|
119 | make_ret, _ = sh("%s %s 2> %s 1> /dev/null" % (make_cmd, test.name, out_file), dry_run) |
---|
120 | |
---|
121 | # if the make command succeds continue otherwise skip to diff |
---|
122 | if make_ret == 0 : |
---|
123 | # fetch optional input |
---|
124 | stdinput = "< .in/%s.txt" % test.name if isfile(".in/%s.txt" % test.path) else "" |
---|
125 | |
---|
126 | if fileIsExecutable(test.name) : |
---|
127 | # run test |
---|
128 | sh("./%s %s > %s 2>&1" % (test.name, stdinput, out_file), dry_run) |
---|
129 | else : |
---|
130 | # simply cat the result into the output |
---|
131 | sh("cat %s > %s" % (test.name, out_file), dry_run) |
---|
132 | |
---|
133 | retcode = 0 |
---|
134 | error = None |
---|
135 | |
---|
136 | # fix output to prevent make depth to cause issues |
---|
137 | fix_MakeLevel(out_file) |
---|
138 | |
---|
139 | if generate : |
---|
140 | # if we are ounly generating the output we still need to check that the test actually exists |
---|
141 | if not dry_run and fileContainsOnly(out_file, "make: *** No rule to make target `%s'. Stop." % test.name) : |
---|
142 | retcode = 1; |
---|
143 | error = "\t\tNo make target for test %s!" % test |
---|
144 | sh("rm %s" % out_file, False) |
---|
145 | |
---|
146 | else : |
---|
147 | # diff the output of the files |
---|
148 | diff_cmd = ("diff --old-group-format='\t\tmissing lines :\n" |
---|
149 | "%%<' \\\n" |
---|
150 | "--new-group-format='\t\tnew lines :\n" |
---|
151 | "%%>' \\\n" |
---|
152 | "--unchanged-group-format='%%=' \\" |
---|
153 | "--changed-group-format='\t\texpected :\n" |
---|
154 | "%%<\n" |
---|
155 | "\t\tgot :\n" |
---|
156 | "%%>' \\\n" |
---|
157 | "--new-line-format='\t\t%%dn\t%%L' \\\n" |
---|
158 | "--old-line-format='\t\t%%dn\t%%L' \\\n" |
---|
159 | "--unchanged-line-format='' \\\n" |
---|
160 | ".expect/%s.txt .out/%s.log") |
---|
161 | |
---|
162 | # fetch return code and error from the diff command |
---|
163 | retcode, error = sh(diff_cmd % (test.path, test.name), dry_run, False) |
---|
164 | |
---|
165 | # clean the executable |
---|
166 | sh("rm -f %s > /dev/null 2>&1" % test.name, dry_run) |
---|
167 | |
---|
168 | return retcode, error |
---|
169 | |
---|
170 | # run the given list of tests with the given parameters |
---|
171 | def run_tests(tests, generate, dry_run, jobs) : |
---|
172 | # clean the sandbox from previous commands |
---|
173 | sh("%s clean > /dev/null 2>&1" % make_cmd, dry_run) |
---|
174 | |
---|
175 | #make sure the required folder are present |
---|
176 | sh('mkdir -p .out .expect', dry_run) |
---|
177 | |
---|
178 | if generate : |
---|
179 | print( "Regenerate tests for: " ) |
---|
180 | |
---|
181 | failed = False; |
---|
182 | # for eeach test to run |
---|
183 | for t in tests: |
---|
184 | # print formated name |
---|
185 | name_txt = "%20s " % t.name |
---|
186 | |
---|
187 | #run the test instance and collect the result |
---|
188 | test_failed, error = run_test_instance(t, generate, dry_run) |
---|
189 | |
---|
190 | # aggregate test suite result |
---|
191 | failed = test_failed or failed |
---|
192 | |
---|
193 | # update output based on current action |
---|
194 | if generate : |
---|
195 | failed_txt = "ERROR" |
---|
196 | success_txt = "Done" |
---|
197 | else : |
---|
198 | failed_txt = "FAILED" |
---|
199 | success_txt = "PASSED" |
---|
200 | |
---|
201 | #print result with error if needed |
---|
202 | text = name_txt + (failed_txt if test_failed else success_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 | |
---|
213 | #clean the workspace |
---|
214 | sh("%s clean > /dev/null 2>&1" % make_cmd, dry_run) |
---|
215 | |
---|
216 | return 1 if failed else 0 |
---|
217 | |
---|
218 | ################################################################################ |
---|
219 | # main loop |
---|
220 | ################################################################################ |
---|
221 | # create a parser with the arguments for the tests script |
---|
222 | parser = argparse.ArgumentParser(description='Script which runs cforall tests') |
---|
223 | parser.add_argument('--dry-run', help='Don\'t run the tests, only output the commands', action='store_true') |
---|
224 | parser.add_argument('--list', help='List all test available', action='store_true') |
---|
225 | parser.add_argument('--all', help='Run all test available', action='store_true') |
---|
226 | parser.add_argument('--regenerate-expected', help='Regenerate the .expect by running the specified tets, can be used with --all option', action='store_true') |
---|
227 | parser.add_argument('-j', '--jobs', help='Number of tests to run simultaneously', type=int, default='8') |
---|
228 | parser.add_argument('tests', metavar='test', type=str, nargs='*', help='a list of tests to run') |
---|
229 | |
---|
230 | # parse the command line arguments |
---|
231 | options = parser.parse_args() |
---|
232 | |
---|
233 | # script must have at least some tests to run |
---|
234 | if (len(options.tests) > 0 and options.all and not options.list) \ |
---|
235 | or (len(options.tests) == 0 and not options.all and not options.list) : |
---|
236 | print('ERROR: must have option \'--all\' or non-empty test list', file=sys.stderr) |
---|
237 | parser.print_help() |
---|
238 | sys.exit(1) |
---|
239 | |
---|
240 | # fetch the liest of all valid tests |
---|
241 | allTests = listTests() |
---|
242 | |
---|
243 | # if user wants all tests than no other treatement of the test list is required |
---|
244 | if options.all or options.list : |
---|
245 | tests = allTests |
---|
246 | |
---|
247 | else : |
---|
248 | #otherwise we need to validate that the test list that was entered is valid |
---|
249 | tests = [] |
---|
250 | |
---|
251 | # if we are regenerating the tests we need to find the information of the |
---|
252 | # already existing tests and create new info for the new tests |
---|
253 | if options.regenerate_expected : |
---|
254 | tests = map(filterTests, options.tests) |
---|
255 | |
---|
256 | else : |
---|
257 | # otherwise we only need to validate that all tests are present in the complete list |
---|
258 | for testname in options.tests: |
---|
259 | test = [t for t in allTests if t.name == testname] |
---|
260 | |
---|
261 | if len(test) != 0 : |
---|
262 | tests.append( test[0] ) |
---|
263 | else : |
---|
264 | print('ERROR: No expected file for test %s, ignoring it' % testname, file=sys.stderr) |
---|
265 | |
---|
266 | # make sure we have at least some test to run |
---|
267 | if len(tests) == 0 : |
---|
268 | print('ERROR: No valid test to run', file=sys.stderr) |
---|
269 | sys.exit(1) |
---|
270 | |
---|
271 | # sort the test alphabetically for convenience |
---|
272 | tests.sort(key=lambda t: t.name) |
---|
273 | |
---|
274 | # check if the user already passed in a number of jobs for multi-threading |
---|
275 | make_flags = environ.get('MAKEFLAGS') |
---|
276 | make_has_max_jobs = re.search("(-j|--jobs)\s*([0-9]+)", make_flags) if make_flags else None |
---|
277 | make_max_jobs = make_has_max_jobs.group(2) if make_has_max_jobs else None |
---|
278 | make_cmd = "make" if make_flags and "-j" in make_flags else "make -j8" |
---|
279 | |
---|
280 | # make sure we have a valid number of jobs that corresponds to user input |
---|
281 | options.jobs = int(make_max_jobs) if make_max_jobs else options.jobs |
---|
282 | if options.jobs <= 0 : |
---|
283 | print('ERROR: Invalid number of jobs', file=sys.stderr) |
---|
284 | sys.exit(1) |
---|
285 | |
---|
286 | # users may want to simply list the tests |
---|
287 | if options.list : |
---|
288 | print("\n".join(map(lambda t: "%s (%s)" % (t.name, t.path), tests))) |
---|
289 | |
---|
290 | else : |
---|
291 | # otherwise run all tests and make sure to return the correct error code |
---|
292 | sys.exit( run_tests(tests, options.regenerate_expected, options.dry_run, options.jobs) ) |
---|