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