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