source: src/tests/test.py @ 30e81fb

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 30e81fb 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
Line 
1#!/usr/bin/python
2from __future__ import print_function
3
4from os import listdir, environ
5from os.path import isfile, join, splitext
6from subprocess import Popen, PIPE, STDOUT
7
8import argparse
9import os
10import re
11import sys
12
13################################################################################
14#               help functions
15################################################################################
16def listTests():
17        list = [splitext(f)[0] for f in listdir('./.expect')
18                if not f.startswith('.') and f.endswith('.txt')
19                ]
20
21        return list
22
23def sh(cmd, dry_run = False, print2stdout = True):
24        if dry_run :
25                print("cmd: %s" % cmd)
26                return 0, None
27        else :
28                proc = Popen(cmd, stdout=None if print2stdout else PIPE, stderr=STDOUT, shell=True)
29                out, err = proc.communicate()
30                return proc.returncode, out
31
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
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
62
63################################################################################
64#               running test functions
65################################################################################
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)
71        sh("rm -f %s > /dev/null 2>&1" % test, dry_run)
72
73        # build, skipping to next test on error
74        make_ret, _ = sh("%s %s 2> %s 1> /dev/null" % (make_cmd, test, out_file), dry_run)
75
76        if make_ret == 0 :
77                # fetch optional input
78                stdinput = "< .in/%s.txt" % test if isfile(".in/%s.txt" % test) else ""
79
80                # run test
81                sh("./%s %s > %s 2>&1" % (test, stdinput, out_file), dry_run)
82
83        retcode = 0
84        error = None
85
86        fix_MakeLevel(out_file)
87
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 :
95                # diff the output of the files
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)
111
112        # clean the executable
113        sh("rm -f %s > /dev/null 2>&1" % test, dry_run)
114
115        return retcode, error
116
117def run_tests(tests, generate, dry_run) :
118        sh("%s clean > /dev/null 2>&1" % make_cmd, dry_run)
119        sh('mkdir -p .out .expect', dry_run)
120
121        if generate :
122                print( "Regenerate tests for: " )
123
124        failed = False;
125        for t in tests:
126                print("%20s  " % t, end="")
127                sys.stdout.flush()
128                test_failed, error = run_test_instance(t, generate, dry_run)
129                failed = test_failed or failed
130
131                if generate :
132                        failed_txt = "ERROR"
133                        success_txt = "Done"
134                else :
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)
141
142        sh("%s clean > /dev/null 2>&1" % make_cmd, dry_run)
143
144        return 1 if failed else 0
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')
151parser.add_argument('--list', help='List all test available', action='store_true')
152parser.add_argument('--all', help='Run all test available', action='store_true')
153parser.add_argument('--regenerate-expected', help='Regenerate the .expect by running the specified tets, can be used with --all option', action='store_true')
154parser.add_argument('tests', metavar='test', type=str, nargs='*', help='a list of tests to run')
155
156options = parser.parse_args()
157
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) :
160        print('ERROR: must have option \'--all\' or non-empty test list', file=sys.stderr)
161        parser.print_help()
162        sys.exit(1)
163
164allTests = listTests()
165
166if options.all or options.list :
167        tests = allTests
168
169else :
170        tests = []
171        for test in options.tests:
172                if test in allTests or options.regenerate_expected :
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
181tests.sort()
182make_flags = environ.get('MAKEFLAGS')
183make_cmd = "make" if make_flags and "-j" in make_flags else "make -j8"
184
185if options.list :
186        print("\n".join(tests))
187
188else :
189        sys.exit( run_tests(tests, options.regenerate_expected, options.dry_run) )
Note: See TracBrowser for help on using the repository browser.