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