source: src/tests/test.py @ 6f9bc09

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since 6f9bc09 was d142ec5, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Fixed handling of jobs parameter in test.py

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