source: tests/test.py @ 5b993e0

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 5b993e0 was 5b993e0, checked in by tdelisle <tdelisle@…>, 5 years ago

Updated test scripts to use python3

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