source: src/tests/test.py@ 9c791dd

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 9c791dd was a43e1d7, checked in by Thierry Delisle <tdelisle@…>, 9 years ago

test script should now support make target that do not produce executables

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