| [945047e] | 1 | #!/usr/bin/python | 
|---|
| [efc15918] | 2 | from __future__ import print_function | 
|---|
|  | 3 |  | 
|---|
| [c07d724] | 4 | from pybin.tools import * | 
|---|
| [0ad0c55] | 5 | from pybin.test_run import * | 
|---|
| [bacc36c] | 6 | from pybin import settings | 
|---|
| [efc15918] | 7 |  | 
|---|
|  | 8 | import argparse | 
|---|
| [122cac7] | 9 | import re | 
|---|
| [efc15918] | 10 | import sys | 
|---|
|  | 11 |  | 
|---|
|  | 12 | ################################################################################ | 
|---|
|  | 13 | #               help functions | 
|---|
|  | 14 | ################################################################################ | 
|---|
| [f1231f2] | 15 |  | 
|---|
| [209383b] | 16 | def findTests(): | 
|---|
| [0ad0c55] | 17 | expected = [] | 
|---|
| [f1231f2] | 18 |  | 
|---|
| [f3b9efc] | 19 | def matchTest(path): | 
|---|
| [bacc36c] | 20 | match = re.search("(\.[\w\/\-_]*)\/.expect\/([\w\-_]+)(\.[\w\-_]+)?\.txt", path) | 
|---|
|  | 21 | if match : | 
|---|
|  | 22 | test = Test() | 
|---|
|  | 23 | test.name = match.group(2) | 
|---|
|  | 24 | test.path = match.group(1) | 
|---|
|  | 25 | test.arch = match.group(3)[1:] if match.group(3) else None | 
|---|
| [f3b9efc] | 26 | if settings.arch.match(test.arch): | 
|---|
|  | 27 | expected.append(test) | 
|---|
| [f803a75] | 28 |  | 
|---|
| [f3b9efc] | 29 | pathWalk( matchTest ) | 
|---|
| [c07d724] | 30 |  | 
|---|
| [0ad0c55] | 31 | return expected | 
|---|
| [efc15918] | 32 |  | 
|---|
| [be65cca] | 33 | # reads the directory ./.expect and indentifies the tests | 
|---|
| [0ad0c55] | 34 | def listTests( includes, excludes ): | 
|---|
| [bacc36c] | 35 | includes = [canonicalPath( i ) for i in includes] if includes else None | 
|---|
|  | 36 | excludes = [canonicalPath( i ) for i in excludes] if excludes else None | 
|---|
| [be65cca] | 37 |  | 
|---|
|  | 38 | # tests directly in the .expect folder will always be processed | 
|---|
| [209383b] | 39 | test_list = findTests() | 
|---|
| [be65cca] | 40 |  | 
|---|
| [0ad0c55] | 41 | # if we have a limited number of includes, filter by them | 
|---|
|  | 42 | if includes: | 
|---|
|  | 43 | test_list = [x for x in test_list if | 
|---|
| [a85e44c] | 44 | x.target().startswith( tuple(includes) ) | 
|---|
| [0ad0c55] | 45 | ] | 
|---|
| [be65cca] | 46 |  | 
|---|
| [0ad0c55] | 47 | # # if we have a folders to excludes, filter by them | 
|---|
|  | 48 | if excludes: | 
|---|
|  | 49 | test_list = [x for x in test_list if not | 
|---|
| [a85e44c] | 50 | x.target().startswith( tuple(excludes) ) | 
|---|
| [0ad0c55] | 51 | ] | 
|---|
| [f1231f2] | 52 |  | 
|---|
| [0ad0c55] | 53 | return test_list | 
|---|
| [efc15918] | 54 |  | 
|---|
| [c07d724] | 55 | # from the found tests, filter all the valid tests/desired tests | 
|---|
|  | 56 | def validTests( options ): | 
|---|
|  | 57 | tests = [] | 
|---|
|  | 58 |  | 
|---|
|  | 59 | # if we are regenerating the tests we need to find the information of the | 
|---|
|  | 60 | # already existing tests and create new info for the new tests | 
|---|
|  | 61 | if options.regenerate_expected : | 
|---|
|  | 62 | for testname in options.tests : | 
|---|
| [16a63a78] | 63 | testname = canonicalPath( testname ) | 
|---|
| [bacc36c] | 64 | if Test.valid_name(testname): | 
|---|
|  | 65 | found = [test for test in allTests if test.target() == testname] | 
|---|
|  | 66 | tests.append( found[0] if len(found) == 1 else Test.from_target(testname) ) | 
|---|
| [c07d724] | 67 | else : | 
|---|
| [bacc36c] | 68 | print('ERROR: "%s", tests are not allowed to end with a C/C++/CFA extension, ignoring it' % testname, file=sys.stderr) | 
|---|
| [c07d724] | 69 |  | 
|---|
|  | 70 | else : | 
|---|
|  | 71 | # otherwise we only need to validate that all tests are present in the complete list | 
|---|
|  | 72 | for testname in options.tests: | 
|---|
| [bacc36c] | 73 | test = [t for t in allTests if pathCmp( t.target(), testname )] | 
|---|
| [c07d724] | 74 |  | 
|---|
| [bacc36c] | 75 | if test : | 
|---|
| [c07d724] | 76 | tests.append( test[0] ) | 
|---|
|  | 77 | else : | 
|---|
|  | 78 | print('ERROR: No expected file for test %s, ignoring it' % testname, file=sys.stderr) | 
|---|
|  | 79 |  | 
|---|
|  | 80 | # make sure we have at least some test to run | 
|---|
| [f3b9efc] | 81 | if not tests : | 
|---|
| [c07d724] | 82 | print('ERROR: No valid test to run', file=sys.stderr) | 
|---|
|  | 83 | sys.exit(1) | 
|---|
|  | 84 |  | 
|---|
|  | 85 | return tests | 
|---|
|  | 86 |  | 
|---|
|  | 87 | # parses the option | 
|---|
|  | 88 | def getOptions(): | 
|---|
|  | 89 | # create a parser with the arguments for the tests script | 
|---|
|  | 90 | parser = argparse.ArgumentParser(description='Script which runs cforall tests') | 
|---|
|  | 91 | parser.add_argument('--debug', help='Run all tests in debug or release', type=yes_no, default='no') | 
|---|
| [bacc36c] | 92 | parser.add_argument('--arch', help='Test for specific architecture', type=str, default='') | 
|---|
| [c07d724] | 93 | parser.add_argument('--dry-run', help='Don\'t run the tests, only output the commands', action='store_true') | 
|---|
|  | 94 | parser.add_argument('--list', help='List all test available', action='store_true') | 
|---|
|  | 95 | parser.add_argument('--all', help='Run all test available', action='store_true') | 
|---|
|  | 96 | parser.add_argument('--regenerate-expected', help='Regenerate the .expect by running the specified tets, can be used with --all option', action='store_true') | 
|---|
|  | 97 | parser.add_argument('-j', '--jobs', help='Number of tests to run simultaneously', type=int, default='8') | 
|---|
|  | 98 | parser.add_argument('--list-comp', help='List all valide arguments', action='store_true') | 
|---|
| [0ad0c55] | 99 | parser.add_argument('-I','--include', help='Directory of test to include, can be used multiple time, All  if omitted', action='append') | 
|---|
|  | 100 | parser.add_argument('-E','--exclude', help='Directory of test to exclude, can be used multiple time, None if omitted', action='append') | 
|---|
| [c07d724] | 101 | parser.add_argument('tests', metavar='test', type=str, nargs='*', help='a list of tests to run') | 
|---|
|  | 102 |  | 
|---|
|  | 103 | options =  parser.parse_args() | 
|---|
|  | 104 |  | 
|---|
|  | 105 | # script must have at least some tests to run or be listing | 
|---|
|  | 106 | listing    = options.list or options.list_comp | 
|---|
|  | 107 | all_tests  = options.all | 
|---|
|  | 108 | some_tests = len(options.tests) > 0 | 
|---|
| [0ad0c55] | 109 | some_dirs  = len(options.include) > 0 if options.include else 0 | 
|---|
| [c07d724] | 110 |  | 
|---|
|  | 111 | # check that exactly one of the booleans is set to true | 
|---|
| [0ad0c55] | 112 | if not sum( (listing, all_tests, some_tests, some_dirs) ) > 0 : | 
|---|
|  | 113 | print('ERROR: must have option \'--all\', \'--list\', \'--include\', \'-I\' or non-empty test list', file=sys.stderr) | 
|---|
| [c07d724] | 114 | parser.print_help() | 
|---|
|  | 115 | sys.exit(1) | 
|---|
|  | 116 |  | 
|---|
|  | 117 | return options | 
|---|
|  | 118 |  | 
|---|
| [efc15918] | 119 | ################################################################################ | 
|---|
|  | 120 | #               running test functions | 
|---|
|  | 121 | ################################################################################ | 
|---|
| [c07d724] | 122 | # logic to run a single test and return the result (No handling of printing or other test framework logic) | 
|---|
| [209383b] | 123 | def run_single_test(test): | 
|---|
| [3c1d702] | 124 |  | 
|---|
| [c07d724] | 125 | # find the output file based on the test name and options flag | 
|---|
| [bacc36c] | 126 | out_file = test.target_output() | 
|---|
|  | 127 | err_file = test.error_log() | 
|---|
|  | 128 | cmp_file = test.expect() | 
|---|
|  | 129 | in_file  = test.input() | 
|---|
| [0ad0c55] | 130 |  | 
|---|
|  | 131 | # prepare the proper directories | 
|---|
| [bacc36c] | 132 | test.prepare() | 
|---|
| [efc15918] | 133 |  | 
|---|
| [c07d724] | 134 | # remove any outputs from the previous tests to prevent side effects | 
|---|
| [bacc36c] | 135 | rm( (out_file, err_file, test.target()) ) | 
|---|
| [6a1bdfd] | 136 |  | 
|---|
| [c07d724] | 137 | # build, skipping to next test on error | 
|---|
| [bacc36c] | 138 | make_ret, _ = make( test.target(), | 
|---|
|  | 139 | redirects  = "2> %s 1> /dev/null" % out_file, | 
|---|
|  | 140 | error_file = err_file | 
|---|
|  | 141 | ) | 
|---|
| [efc15918] | 142 |  | 
|---|
| [c07d724] | 143 | # if the make command succeds continue otherwise skip to diff | 
|---|
| [bacc36c] | 144 | if make_ret == 0 or settings.dry_run: | 
|---|
|  | 145 | if settings.dry_run or fileIsExecutable(test.target()) : | 
|---|
| [c07d724] | 146 | # run test | 
|---|
| [bacc36c] | 147 | retcode, _ = sh("timeout 60 %s > %s 2>&1" % (test.target(), out_file), input = in_file) | 
|---|
| [9fcb5e4] | 148 | else : | 
|---|
| [c07d724] | 149 | # simply cat the result into the output | 
|---|
| [f3b9efc] | 150 | retcode, _ = sh("cat %s > %s" % (test.target(), out_file)) | 
|---|
| [0ad0c55] | 151 | else: | 
|---|
| [f3b9efc] | 152 | retcode, _ = sh("mv %s %s" % (err_file, out_file)) | 
|---|
| [122cac7] | 153 |  | 
|---|
| [0ad0c55] | 154 |  | 
|---|
| [c2d5e28] | 155 | if retcode == 0: | 
|---|
| [bacc36c] | 156 | if settings.generating : | 
|---|
| [c2d5e28] | 157 | # if we are ounly generating the output we still need to check that the test actually exists | 
|---|
| [bacc36c] | 158 | if not settings.dry_run and fileContainsOnly(out_file, "make: *** No rule to make target `%s'.  Stop." % test.target()) : | 
|---|
| [c2d5e28] | 159 | retcode = 1; | 
|---|
| [0ad0c55] | 160 | error = "\t\tNo make target for test %s!" % test.target() | 
|---|
| [c2d5e28] | 161 | sh("rm %s" % out_file, False) | 
|---|
| [f3b9efc] | 162 | else: | 
|---|
|  | 163 | error = None | 
|---|
| [c2d5e28] | 164 | else : | 
|---|
|  | 165 | # fetch return code and error from the diff command | 
|---|
| [bacc36c] | 166 | retcode, error = diff(cmp_file, out_file) | 
|---|
| [ac032b5] | 167 |  | 
|---|
|  | 168 | else: | 
|---|
|  | 169 | with open (out_file, "r") as myfile: | 
|---|
|  | 170 | error = myfile.read() | 
|---|
|  | 171 |  | 
|---|
| [b5f9829] | 172 |  | 
|---|
| [c07d724] | 173 | # clean the executable | 
|---|
| [bacc36c] | 174 | sh("rm -f %s > /dev/null 2>&1" % test.target()) | 
|---|
| [efc15918] | 175 |  | 
|---|
| [472ca32] | 176 | return retcode, error | 
|---|
| [efc15918] | 177 |  | 
|---|
| [c07d724] | 178 | # run a single test and handle the errors, outputs, printing, exception handling, etc. | 
|---|
| [209383b] | 179 | def run_test_worker(t) : | 
|---|
| [ced2e989] | 180 |  | 
|---|
| [bacc36c] | 181 | with SignalHandling(): | 
|---|
|  | 182 | # print formated name | 
|---|
|  | 183 | name_txt = "%20s  " % t.name | 
|---|
| [ced2e989] | 184 |  | 
|---|
| [209383b] | 185 | retcode, error = run_single_test(t) | 
|---|
| [0a1a680] | 186 |  | 
|---|
| [bacc36c] | 187 | # update output based on current action | 
|---|
|  | 188 | result_txt = TestResult.toString( retcode ) | 
|---|
|  | 189 |  | 
|---|
|  | 190 | #print result with error if needed | 
|---|
|  | 191 | text = name_txt + result_txt | 
|---|
|  | 192 | out = sys.stdout | 
|---|
|  | 193 | if error : | 
|---|
|  | 194 | text = text + "\n" + error | 
|---|
|  | 195 | out = sys.stderr | 
|---|
|  | 196 |  | 
|---|
|  | 197 | print(text, file = out) | 
|---|
|  | 198 | sys.stdout.flush() | 
|---|
|  | 199 | sys.stderr.flush() | 
|---|
| [9fcb5e4] | 200 |  | 
|---|
| [c2d5e28] | 201 | return retcode != TestResult.SUCCESS | 
|---|
| [ced2e989] | 202 |  | 
|---|
| [911348cd] | 203 | # run the given list of tests with the given parameters | 
|---|
| [209383b] | 204 | def run_tests(tests, jobs) : | 
|---|
| [911348cd] | 205 | # clean the sandbox from previous commands | 
|---|
| [bacc36c] | 206 | make('clean', redirects = '> /dev/null 2>&1') | 
|---|
| [efc15918] | 207 |  | 
|---|
| [c07d724] | 208 | # create the executor for our jobs and handle the signal properly | 
|---|
| [bacc36c] | 209 | pool = setupPool(jobs) | 
|---|
| [c07d724] | 210 |  | 
|---|
|  | 211 | # for each test to run | 
|---|
| [ced2e989] | 212 | try : | 
|---|
| [bacc36c] | 213 | results = pool.map_async( | 
|---|
| [209383b] | 214 | run_test_worker, | 
|---|
| [bacc36c] | 215 | tests, | 
|---|
|  | 216 | chunksize = 1 | 
|---|
|  | 217 | ).get(7200) | 
|---|
| [ced2e989] | 218 | except KeyboardInterrupt: | 
|---|
|  | 219 | pool.terminate() | 
|---|
|  | 220 | print("Tests interrupted by user") | 
|---|
|  | 221 | sys.exit(1) | 
|---|
| [efc15918] | 222 |  | 
|---|
| [c07d724] | 223 | # clean the workspace | 
|---|
| [bacc36c] | 224 | make('clean', redirects = '> /dev/null 2>&1') | 
|---|
| [efc15918] | 225 |  | 
|---|
| [ced2e989] | 226 | for failed in results: | 
|---|
|  | 227 | if failed : | 
|---|
|  | 228 | return 1 | 
|---|
|  | 229 |  | 
|---|
|  | 230 | return 0 | 
|---|
| [efc15918] | 231 |  | 
|---|
| [6a1bdfd] | 232 |  | 
|---|
| [efc15918] | 233 | ################################################################################ | 
|---|
|  | 234 | #               main loop | 
|---|
|  | 235 | ################################################################################ | 
|---|
| [c07d724] | 236 | if __name__ == "__main__": | 
|---|
|  | 237 | #always run from same folder | 
|---|
| [f803a75] | 238 | chdir() | 
|---|
|  | 239 |  | 
|---|
| [c07d724] | 240 | # parse the command line arguments | 
|---|
|  | 241 | options = getOptions() | 
|---|
| [f1231f2] | 242 |  | 
|---|
| [bacc36c] | 243 | # init global settings | 
|---|
|  | 244 | settings.init( options ) | 
|---|
|  | 245 |  | 
|---|
| [c07d724] | 246 | # fetch the liest of all valid tests | 
|---|
| [0ad0c55] | 247 | allTests = listTests( options.include, options.exclude ) | 
|---|
| [f1231f2] | 248 |  | 
|---|
| [c07d724] | 249 | # if user wants all tests than no other treatement of the test list is required | 
|---|
| [0ad0c55] | 250 | if options.all or options.list or options.list_comp or options.include : | 
|---|
| [c07d724] | 251 | tests = allTests | 
|---|
| [0534c3c] | 252 |  | 
|---|
| [f3b9efc] | 253 | #otherwise we need to validate that the test list that was entered is valid | 
|---|
| [c07d724] | 254 | else : | 
|---|
|  | 255 | tests = validTests( options ) | 
|---|
| [0534c3c] | 256 |  | 
|---|
| [c07d724] | 257 | # sort the test alphabetically for convenience | 
|---|
| [f3b9efc] | 258 | tests.sort(key=lambda t: (t.arch if t.arch else '') + t.target()) | 
|---|
| [f1231f2] | 259 |  | 
|---|
| [c07d724] | 260 | # users may want to simply list the tests | 
|---|
|  | 261 | if options.list_comp : | 
|---|
| [f3b9efc] | 262 | print("-h --help --debug --dry-run --list --arch --all --regenerate-expected -j --jobs ", end='') | 
|---|
| [0ad0c55] | 263 | print(" ".join(map(lambda t: "%s" % (t.target()), tests))) | 
|---|
| [911348cd] | 264 |  | 
|---|
| [c07d724] | 265 | elif options.list : | 
|---|
| [f3b9efc] | 266 | print("Listing for %s:%s"% (settings.arch.string, settings.debug.string)) | 
|---|
| [0ad0c55] | 267 | print("\n".join(map(lambda t: "%s" % (t.toString()), tests))) | 
|---|
| [911348cd] | 268 |  | 
|---|
| [b98c913] | 269 | else : | 
|---|
| [bacc36c] | 270 | options.jobs, forceJobs = jobCount( options, tests ) | 
|---|
|  | 271 | settings.updateMakeCmd(forceJobs, options.jobs) | 
|---|
| [b98c913] | 272 |  | 
|---|
| [bacc36c] | 273 | print('%s (%s:%s) on %i cores' % ( | 
|---|
|  | 274 | 'Regenerate tests' if settings.generating else 'Running', | 
|---|
| [f3b9efc] | 275 | settings.arch.string, | 
|---|
|  | 276 | settings.debug.string, | 
|---|
| [bacc36c] | 277 | options.jobs | 
|---|
|  | 278 | )) | 
|---|
| [efc15918] | 279 |  | 
|---|
| [c07d724] | 280 | # otherwise run all tests and make sure to return the correct error code | 
|---|
| [209383b] | 281 | sys.exit( run_tests(tests, options.jobs) ) | 
|---|