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