source: src/tests/test.py @ 3f3b731d

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 3f3b731d was 945047e, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

fixed interpreter for test.py

  • Property mode set to 100755
File size: 5.7 KB
RevLine 
[945047e]1#!/usr/bin/python
[efc15918]2from __future__ import print_function
3
[122cac7]4from os import listdir, environ
[0534c3c]5from os.path import isfile, join, splitext
[efc15918]6from subprocess import Popen, PIPE, STDOUT
7
8import argparse
[122cac7]9import os
10import re
[efc15918]11import sys
12
13################################################################################
14#               help functions
15################################################################################
16def listTests():
[0534c3c]17        list = [splitext(f)[0] for f in listdir('./.expect')
18                if not f.startswith('.') and f.endswith('.txt')
19                ]
[efc15918]20
21        return list
22
[472ca32]23def sh(cmd, dry_run = False, print2stdout = True):
[efc15918]24        if dry_run :
25                print("cmd: %s" % cmd)
[472ca32]26                return 0, None
[efc15918]27        else :
[472ca32]28                proc = Popen(cmd, stdout=None if print2stdout else PIPE, stderr=STDOUT, shell=True)
29                out, err = proc.communicate()
30                return proc.returncode, out
[efc15918]31
[122cac7]32def file_replace(fname, pat, s_after):
33    # first, see if the pattern is even in the file.
34    with open(fname) as f:
35        if not any(re.search(pat, line) for line in f):
36            return # pattern does not occur in file so we are done.
37
38    # pattern is in the file, so perform replace operation.
39    with open(fname) as f:
40        out_fname = fname + ".tmp"
41        out = open(out_fname, "w")
42        for line in f:
43            out.write(re.sub(pat, s_after, line))
44        out.close()
45        os.rename(out_fname, fname)
46
47def fix_MakeLevel(file) :
48        if environ.get('MAKELEVEL') :
49                file_replace(file, "make\[%i\]" % int(environ.get('MAKELEVEL')), 'make' )
50
[84d4d6f]51def fileContainsOnly(file, text) :
52        with open(file) as f:
53                ff = f.read().strip()
54                result = ff == text.strip()
55                #
56                # print("Comparing :\n\t'%s'\nWith:\n\t'%s'" % (ff, text))
57                # print("Result is : \n\t", end="")
58                # print(result)
59
60                return result;
61
[122cac7]62
[efc15918]63################################################################################
64#               running test functions
65################################################################################
[3c1d702]66def run_test_instance(test, generate, dry_run):
67
68        out_file = (".out/%s.log" % test) if not generate else (".expect/%s.txt" % test)
69
70        sh("rm -f %s" % out_file, dry_run)
[245510f]71        sh("rm -f %s > /dev/null 2>&1" % test, dry_run)
[efc15918]72
73        # build, skipping to next test on error
[472ca32]74        make_ret, _ = sh("%s %s 2> %s 1> /dev/null" % (make_cmd, test, out_file), dry_run)
[efc15918]75
[3c1d702]76        if make_ret == 0 :
77                # fetch optional input
78                stdinput = "< .in/%s.txt" % test if isfile(".in/%s.txt" % test) else ""
[efc15918]79
[3c1d702]80                # run test
81                sh("./%s %s > %s 2>&1" % (test, stdinput, out_file), dry_run)
[efc15918]82
[3c1d702]83        retcode = 0
[472ca32]84        error = None
[122cac7]85
86        fix_MakeLevel(out_file)
87
[84d4d6f]88        if generate :
89                if not dry_run and fileContainsOnly(out_file, "make: *** No rule to make target `%s'.  Stop." % test) :
90                        retcode = 1;
91                        error = "\t\tNo make target for test %s!" % test
92                        sh("rm %s" % out_file, False)
93
94        else :
[3c1d702]95                # diff the output of the files
[472ca32]96                diff_cmd = ("diff --old-group-format='\t\tmissing lines :\n"
97                                        "%%<' \\\n"
98                                        "--new-group-format='\t\tnew lines :\n"
99                                        "%%>' \\\n"
100                                        "--unchanged-group-format='%%=' \\"
101                                        "--changed-group-format='\t\texpected :\n"
102                                        "%%<\n"
103                                        "\t\tgot :\n"
104                                        "%%>' \\\n"
105                                        "--new-line-format='\t\t%%dn\t%%L' \\\n"
106                                        "--old-line-format='\t\t%%dn\t%%L' \\\n"
107                                        "--unchanged-line-format='' \\\n"
108                                        ".expect/%s.txt .out/%s.log")
109
110                retcode, error = sh(diff_cmd % (test, test), dry_run, False)
[efc15918]111
112        # clean the executable
[245510f]113        sh("rm -f %s > /dev/null 2>&1" % test, dry_run)
[efc15918]114
[472ca32]115        return retcode, error
[efc15918]116
[3c1d702]117def run_tests(tests, generate, dry_run) :
[74358c3]118        sh("%s clean > /dev/null 2>&1" % make_cmd, dry_run)
[3c1d702]119        sh('mkdir -p .out .expect', dry_run)
120
121        if generate :
[ebcd82b]122                print( "Regenerate tests for: " )
[efc15918]123
124        failed = False;
125        for t in tests:
[ebcd82b]126                print("%20s  " % t, end="")
[efc15918]127                sys.stdout.flush()
[472ca32]128                test_failed, error = run_test_instance(t, generate, dry_run)
[efc15918]129                failed = test_failed or failed
130
[84d4d6f]131                if generate :
132                        failed_txt = "ERROR"
133                        success_txt = "Done"
[ebcd82b]134                else :
[84d4d6f]135                        failed_txt = "FAILED"
136                        success_txt = "PASSED"
137
138                print(failed_txt if test_failed else success_txt)
139                if error :
140                        print(error, file=sys.stderr)
[efc15918]141
[74358c3]142        sh("%s clean > /dev/null 2>&1" % make_cmd, dry_run)
[efc15918]143
[b70b6fc]144        return 1 if failed else 0
[efc15918]145
146################################################################################
147#               main loop
148################################################################################
149parser = argparse.ArgumentParser(description='Script which runs cforall tests')
150parser.add_argument('--dry-run', help='Don\'t run the tests, only output the commands', action='store_true')
[0534c3c]151parser.add_argument('--list', help='List all test available', action='store_true')
[efc15918]152parser.add_argument('--all', help='Run all test available', action='store_true')
[0534c3c]153parser.add_argument('--regenerate-expected', help='Regenerate the .expect by running the specified tets, can be used with --all option', action='store_true')
[efc15918]154parser.add_argument('tests', metavar='test', type=str, nargs='*', help='a list of tests to run')
155
156options = parser.parse_args()
157
[0534c3c]158if (len(options.tests) > 0  and     options.all and not options.list) \
159or (len(options.tests) == 0 and not options.all and not options.list) :
[3c1d702]160        print('ERROR: must have option \'--all\' or non-empty test list', file=sys.stderr)
161        parser.print_help()
162        sys.exit(1)
[efc15918]163
[0534c3c]164allTests = listTests()
165
166if options.all or options.list :
167        tests = allTests
168
169else :
170        tests = []
171        for test in options.tests:
[177a5ce]172                if test in allTests or options.regenerate_expected :
[0534c3c]173                        tests.append(test)
174                else :
175                        print('ERROR: No expected file for test %s, ignoring it' % test, file=sys.stderr)
176
177        if len(tests) == 0 :
178                print('ERROR: No valid test to run', file=sys.stderr)
179                sys.exit(1)
180
[74358c3]181tests.sort()
[7bd045d]182make_flags = environ.get('MAKEFLAGS')
183make_cmd = "make" if make_flags and "-j" in make_flags else "make -j8"
[74358c3]184
[0534c3c]185if options.list :
186        print("\n".join(tests))
[efc15918]187
[0534c3c]188else :
189        sys.exit( run_tests(tests, options.regenerate_expected, options.dry_run) )
Note: See TracBrowser for help on using the repository browser.