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