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 | class Test: |
---|
19 | def __init__(self, name, path): |
---|
20 | self.name, self.path = name, path |
---|
21 | |
---|
22 | def getMachineType(): |
---|
23 | with open('Makefile') as file: |
---|
24 | makefile = file.read() |
---|
25 | m = re.search("CFA_FLAGS\s*=\s*-m(.*)", makefile) |
---|
26 | return m.group(1) |
---|
27 | |
---|
28 | def listTests(): |
---|
29 | machineType = getMachineType() |
---|
30 | |
---|
31 | generic_list = map(lambda fname: Test(fname, fname), |
---|
32 | [splitext(f)[0] for f in listdir('./.expect') |
---|
33 | if not f.startswith('.') and f.endswith('.txt') |
---|
34 | ]) |
---|
35 | |
---|
36 | typed_list = map(lambda fname: Test( fname, "%s/%s" % (machineType, fname) ), |
---|
37 | [splitext(f)[0] for f in listdir("./.expect/%s" % machineType) |
---|
38 | if not f.startswith('.') and f.endswith('.txt') |
---|
39 | ]) |
---|
40 | |
---|
41 | return generic_list + typed_list |
---|
42 | |
---|
43 | def sh(cmd, dry_run = False, print2stdout = True): |
---|
44 | if dry_run : |
---|
45 | print("cmd: %s" % cmd) |
---|
46 | return 0, None |
---|
47 | else : |
---|
48 | proc = Popen(cmd, stdout=None if print2stdout else PIPE, stderr=STDOUT, shell=True) |
---|
49 | out, err = proc.communicate() |
---|
50 | return proc.returncode, out |
---|
51 | |
---|
52 | def file_replace(fname, pat, s_after): |
---|
53 | # first, see if the pattern is even in the file. |
---|
54 | with open(fname) as f: |
---|
55 | if not any(re.search(pat, line) for line in f): |
---|
56 | return # pattern does not occur in file so we are done. |
---|
57 | |
---|
58 | # pattern is in the file, so perform replace operation. |
---|
59 | with open(fname) as f: |
---|
60 | out_fname = fname + ".tmp" |
---|
61 | out = open(out_fname, "w") |
---|
62 | for line in f: |
---|
63 | out.write(re.sub(pat, s_after, line)) |
---|
64 | out.close() |
---|
65 | os.rename(out_fname, fname) |
---|
66 | |
---|
67 | def fix_MakeLevel(file) : |
---|
68 | if environ.get('MAKELEVEL') : |
---|
69 | file_replace(file, "make\[%i\]" % int(environ.get('MAKELEVEL')), 'make' ) |
---|
70 | |
---|
71 | def fileContainsOnly(file, text) : |
---|
72 | with open(file) as f: |
---|
73 | ff = f.read().strip() |
---|
74 | result = ff == text.strip() |
---|
75 | # |
---|
76 | # print("Comparing :\n\t'%s'\nWith:\n\t'%s'" % (ff, text)) |
---|
77 | # print("Result is : \n\t", end="") |
---|
78 | # print(result) |
---|
79 | |
---|
80 | return result; |
---|
81 | |
---|
82 | def fileIsExecutable(file) : |
---|
83 | try : |
---|
84 | fileinfo = os.stat(file) |
---|
85 | return bool(fileinfo.st_mode & stat.S_IXUSR) |
---|
86 | except Exception as inst: |
---|
87 | print(type(inst)) # the exception instance |
---|
88 | print(inst.args) # arguments stored in .args |
---|
89 | print(inst) |
---|
90 | return False |
---|
91 | |
---|
92 | def filterTests(testname) : |
---|
93 | found = [test for test in allTests if test.name == testname] |
---|
94 | return (found[0] if len(found) == 1 else Test(testname, testname) ) |
---|
95 | |
---|
96 | ################################################################################ |
---|
97 | # running test functions |
---|
98 | ################################################################################ |
---|
99 | def run_test_instance(test, generate, dry_run): |
---|
100 | |
---|
101 | out_file = (".out/%s.log" % test.name) if not generate else (".expect/%s.txt" % test.path) |
---|
102 | |
---|
103 | sh("rm -f %s" % out_file, dry_run) |
---|
104 | sh("rm -f %s > /dev/null 2>&1" % test.name, dry_run) |
---|
105 | |
---|
106 | # build, skipping to next test on error |
---|
107 | make_ret, _ = sh("%s %s 2> %s 1> /dev/null" % (make_cmd, test.name, out_file), dry_run) |
---|
108 | |
---|
109 | if make_ret == 0 : |
---|
110 | # fetch optional input |
---|
111 | stdinput = "< .in/%s.txt" % test.name if isfile(".in/%s.txt" % test.path) else "" |
---|
112 | |
---|
113 | if fileIsExecutable(test.name) : |
---|
114 | # run test |
---|
115 | sh("./%s %s > %s 2>&1" % (test.name, stdinput, out_file), dry_run) |
---|
116 | else : |
---|
117 | # simply cat the result into the output |
---|
118 | sh("cat %s > %s" % (test.name, out_file), dry_run) |
---|
119 | |
---|
120 | retcode = 0 |
---|
121 | error = None |
---|
122 | |
---|
123 | fix_MakeLevel(out_file) |
---|
124 | |
---|
125 | if generate : |
---|
126 | if not dry_run and fileContainsOnly(out_file, "make: *** No rule to make target `%s'. Stop." % test.name) : |
---|
127 | retcode = 1; |
---|
128 | error = "\t\tNo make target for test %s!" % test |
---|
129 | sh("rm %s" % out_file, False) |
---|
130 | |
---|
131 | else : |
---|
132 | # diff the output of the files |
---|
133 | diff_cmd = ("diff --old-group-format='\t\tmissing lines :\n" |
---|
134 | "%%<' \\\n" |
---|
135 | "--new-group-format='\t\tnew lines :\n" |
---|
136 | "%%>' \\\n" |
---|
137 | "--unchanged-group-format='%%=' \\" |
---|
138 | "--changed-group-format='\t\texpected :\n" |
---|
139 | "%%<\n" |
---|
140 | "\t\tgot :\n" |
---|
141 | "%%>' \\\n" |
---|
142 | "--new-line-format='\t\t%%dn\t%%L' \\\n" |
---|
143 | "--old-line-format='\t\t%%dn\t%%L' \\\n" |
---|
144 | "--unchanged-line-format='' \\\n" |
---|
145 | ".expect/%s.txt .out/%s.log") |
---|
146 | |
---|
147 | retcode, error = sh(diff_cmd % (test.path, test.name), dry_run, False) |
---|
148 | |
---|
149 | # clean the executable |
---|
150 | sh("rm -f %s > /dev/null 2>&1" % test.name, dry_run) |
---|
151 | |
---|
152 | return retcode, error |
---|
153 | |
---|
154 | def run_tests(tests, generate, dry_run) : |
---|
155 | sh("%s clean > /dev/null 2>&1" % make_cmd, dry_run) |
---|
156 | sh('mkdir -p .out .expect', dry_run) |
---|
157 | |
---|
158 | if generate : |
---|
159 | print( "Regenerate tests for: " ) |
---|
160 | |
---|
161 | failed = False; |
---|
162 | for t in tests: |
---|
163 | print("%20s " % t.name, end="") |
---|
164 | sys.stdout.flush() |
---|
165 | test_failed, error = run_test_instance(t, generate, dry_run) |
---|
166 | failed = test_failed or failed |
---|
167 | |
---|
168 | if generate : |
---|
169 | failed_txt = "ERROR" |
---|
170 | success_txt = "Done" |
---|
171 | else : |
---|
172 | failed_txt = "FAILED" |
---|
173 | success_txt = "PASSED" |
---|
174 | |
---|
175 | print(failed_txt if test_failed else success_txt) |
---|
176 | if error : |
---|
177 | print(error, file=sys.stderr) |
---|
178 | |
---|
179 | sh("%s clean > /dev/null 2>&1" % make_cmd, dry_run) |
---|
180 | |
---|
181 | return 1 if failed else 0 |
---|
182 | |
---|
183 | ################################################################################ |
---|
184 | # main loop |
---|
185 | ################################################################################ |
---|
186 | parser = argparse.ArgumentParser(description='Script which runs cforall tests') |
---|
187 | parser.add_argument('--dry-run', help='Don\'t run the tests, only output the commands', action='store_true') |
---|
188 | parser.add_argument('--list', help='List all test available', action='store_true') |
---|
189 | parser.add_argument('--all', help='Run all test available', action='store_true') |
---|
190 | parser.add_argument('--regenerate-expected', help='Regenerate the .expect by running the specified tets, can be used with --all option', action='store_true') |
---|
191 | parser.add_argument('tests', metavar='test', type=str, nargs='*', help='a list of tests to run') |
---|
192 | |
---|
193 | options = parser.parse_args() |
---|
194 | |
---|
195 | if (len(options.tests) > 0 and options.all and not options.list) \ |
---|
196 | or (len(options.tests) == 0 and not options.all and not options.list) : |
---|
197 | print('ERROR: must have option \'--all\' or non-empty test list', file=sys.stderr) |
---|
198 | parser.print_help() |
---|
199 | sys.exit(1) |
---|
200 | |
---|
201 | allTests = listTests() |
---|
202 | |
---|
203 | if options.all or options.list : |
---|
204 | tests = allTests |
---|
205 | |
---|
206 | else : |
---|
207 | tests = [] |
---|
208 | |
---|
209 | if options.regenerate_expected : |
---|
210 | tests = map(filterTests, options.tests) |
---|
211 | |
---|
212 | else : |
---|
213 | for testname in options.tests: |
---|
214 | test = [t for t in allTests if t.name == testname] |
---|
215 | |
---|
216 | if len(test) != 0 : |
---|
217 | tests.append( test[0] ) |
---|
218 | else : |
---|
219 | print('ERROR: No expected file for test %s, ignoring it' % testname, file=sys.stderr) |
---|
220 | |
---|
221 | if len(tests) == 0 : |
---|
222 | print('ERROR: No valid test to run', file=sys.stderr) |
---|
223 | sys.exit(1) |
---|
224 | |
---|
225 | tests.sort(key=lambda t: t.name) |
---|
226 | |
---|
227 | make_flags = environ.get('MAKEFLAGS') |
---|
228 | make_cmd = "make" if make_flags and "-j" in make_flags else "make -j8" |
---|
229 | |
---|
230 | if options.list : |
---|
231 | print("\n".join(map(lambda t: "%s (%s)" % (t.name, t.path), tests))) |
---|
232 | |
---|
233 | else : |
---|
234 | sys.exit( run_tests(tests, options.regenerate_expected, options.dry_run) ) |
---|