source: src/tests/test.py @ 209383b

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

Tests: moved debug flags to settings to eliminate parameters and functools

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