source: src/tests/test.py@ aedfd91

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since aedfd91 was 20340c2, checked in by Thierry Delisle <tdelisle@…>, 9 years ago

test now uses compiler to know to run 32-bit or 64-bit tests

  • Property mode set to 100755
File size: 10.0 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
[a43e1d7]11import stat
[efc15918]12import sys
13
14################################################################################
15# help functions
16################################################################################
[f1231f2]17
[911348cd]18# Test class that defines what a test is
[f1231f2]19class Test:
20 def __init__(self, name, path):
21 self.name, self.path = name, path
22
[911348cd]23# parses the Makefile to find the machine type (32-bit / 64-bit)
[f1231f2]24def getMachineType():
[20340c2]25 sh('echo "int main() { return 0; }" > .dummy.c')
26 sh("make .dummy", print2stdout=False)
27 _, out = sh("file .dummy", print2stdout=False)
28 sh("rm -f .dummy.c > /dev/null 2>&1")
29 sh("rm -f .dummy > /dev/null 2>&1")
30 return re.search("ELF\s([0-9]+)-bit", out).group(1)
[f1231f2]31
[911348cd]32# reads the directory ./.expect and indentifies the tests
[efc15918]33def listTests():
[f1231f2]34 machineType = getMachineType()
35
[20340c2]36 print(machineType)
37
[911348cd]38 # tests directly in the .expect folder will always be processed
[f1231f2]39 generic_list = map(lambda fname: Test(fname, fname),
40 [splitext(f)[0] for f in listdir('./.expect')
[0534c3c]41 if not f.startswith('.') and f.endswith('.txt')
[f1231f2]42 ])
[efc15918]43
[911348cd]44 # tests in the machineType folder will be ran only for the corresponding compiler
[f1231f2]45 typed_list = map(lambda fname: Test( fname, "%s/%s" % (machineType, fname) ),
46 [splitext(f)[0] for f in listdir("./.expect/%s" % machineType)
47 if not f.startswith('.') and f.endswith('.txt')
48 ])
49
[911348cd]50 # append both lists to get
[f1231f2]51 return generic_list + typed_list
[efc15918]52
[911348cd]53# helper functions to run terminal commands
[472ca32]54def sh(cmd, dry_run = False, print2stdout = True):
[911348cd]55 if dry_run : # if this is a dry_run, only print the commands that would be ran
[efc15918]56 print("cmd: %s" % cmd)
[472ca32]57 return 0, None
[911348cd]58 else : # otherwise create a pipe and run the desired command
[472ca32]59 proc = Popen(cmd, stdout=None if print2stdout else PIPE, stderr=STDOUT, shell=True)
60 out, err = proc.communicate()
61 return proc.returncode, out
[efc15918]62
[911348cd]63# helper function to replace patterns in a file
[122cac7]64def file_replace(fname, pat, s_after):
65 # first, see if the pattern is even in the file.
66 with open(fname) as f:
67 if not any(re.search(pat, line) for line in f):
68 return # pattern does not occur in file so we are done.
69
70 # pattern is in the file, so perform replace operation.
71 with open(fname) as f:
72 out_fname = fname + ".tmp"
73 out = open(out_fname, "w")
74 for line in f:
75 out.write(re.sub(pat, s_after, line))
76 out.close()
77 os.rename(out_fname, fname)
78
[911348cd]79# tests output may differ depending on the depth of the makefile
[122cac7]80def fix_MakeLevel(file) :
81 if environ.get('MAKELEVEL') :
82 file_replace(file, "make\[%i\]" % int(environ.get('MAKELEVEL')), 'make' )
83
[911348cd]84# helper function to check if a files contains only a spacific string
[84d4d6f]85def fileContainsOnly(file, text) :
86 with open(file) as f:
87 ff = f.read().strip()
88 result = ff == text.strip()
89
90 return result;
91
[911348cd]92# check whether or not a file is executable
[a43e1d7]93def fileIsExecutable(file) :
94 try :
95 fileinfo = os.stat(file)
96 return bool(fileinfo.st_mode & stat.S_IXUSR)
97 except Exception as inst:
98 print(type(inst)) # the exception instance
99 print(inst.args) # arguments stored in .args
100 print(inst)
101 return False
[122cac7]102
[911348cd]103# find the test data for a given test name
[f1231f2]104def filterTests(testname) :
105 found = [test for test in allTests if test.name == testname]
106 return (found[0] if len(found) == 1 else Test(testname, testname) )
107
[efc15918]108################################################################################
109# running test functions
110################################################################################
[3c1d702]111def run_test_instance(test, generate, dry_run):
112
[911348cd]113 # find the output file based on the test name and options flag
[f1231f2]114 out_file = (".out/%s.log" % test.name) if not generate else (".expect/%s.txt" % test.path)
[3c1d702]115
[911348cd]116 # remove any outputs from the previous tests to prevent side effects
[3c1d702]117 sh("rm -f %s" % out_file, dry_run)
[f1231f2]118 sh("rm -f %s > /dev/null 2>&1" % test.name, dry_run)
[efc15918]119
120 # build, skipping to next test on error
[f1231f2]121 make_ret, _ = sh("%s %s 2> %s 1> /dev/null" % (make_cmd, test.name, out_file), dry_run)
[efc15918]122
[911348cd]123 # if the make command succeds continue otherwise skip to diff
[3c1d702]124 if make_ret == 0 :
125 # fetch optional input
[e28d0f5]126 stdinput = "< .in/%s.txt" % test.name if isfile(".in/%s.txt" % test.path) else ""
[efc15918]127
[f1231f2]128 if fileIsExecutable(test.name) :
[a43e1d7]129 # run test
[f1231f2]130 sh("./%s %s > %s 2>&1" % (test.name, stdinput, out_file), dry_run)
[a43e1d7]131 else :
132 # simply cat the result into the output
[f1231f2]133 sh("cat %s > %s" % (test.name, out_file), dry_run)
[efc15918]134
[3c1d702]135 retcode = 0
[472ca32]136 error = None
[122cac7]137
[911348cd]138 # fix output to prevent make depth to cause issues
[122cac7]139 fix_MakeLevel(out_file)
140
[84d4d6f]141 if generate :
[911348cd]142 # if we are ounly generating the output we still need to check that the test actually exists
[f1231f2]143 if not dry_run and fileContainsOnly(out_file, "make: *** No rule to make target `%s'. Stop." % test.name) :
[84d4d6f]144 retcode = 1;
145 error = "\t\tNo make target for test %s!" % test
146 sh("rm %s" % out_file, False)
147
148 else :
[3c1d702]149 # diff the output of the files
[472ca32]150 diff_cmd = ("diff --old-group-format='\t\tmissing lines :\n"
151 "%%<' \\\n"
152 "--new-group-format='\t\tnew lines :\n"
153 "%%>' \\\n"
154 "--unchanged-group-format='%%=' \\"
155 "--changed-group-format='\t\texpected :\n"
156 "%%<\n"
157 "\t\tgot :\n"
158 "%%>' \\\n"
159 "--new-line-format='\t\t%%dn\t%%L' \\\n"
160 "--old-line-format='\t\t%%dn\t%%L' \\\n"
161 "--unchanged-line-format='' \\\n"
162 ".expect/%s.txt .out/%s.log")
163
[911348cd]164 # fetch return code and error from the diff command
[f1231f2]165 retcode, error = sh(diff_cmd % (test.path, test.name), dry_run, False)
[efc15918]166
167 # clean the executable
[f1231f2]168 sh("rm -f %s > /dev/null 2>&1" % test.name, dry_run)
[efc15918]169
[472ca32]170 return retcode, error
[efc15918]171
[911348cd]172# run the given list of tests with the given parameters
173def run_tests(tests, generate, dry_run, jobs) :
174 # clean the sandbox from previous commands
[74358c3]175 sh("%s clean > /dev/null 2>&1" % make_cmd, dry_run)
[911348cd]176
177 #make sure the required folder are present
[3c1d702]178 sh('mkdir -p .out .expect', dry_run)
179
180 if generate :
[ebcd82b]181 print( "Regenerate tests for: " )
[efc15918]182
183 failed = False;
[911348cd]184 # for eeach test to run
[efc15918]185 for t in tests:
[911348cd]186 # print formated name
[52c97dd]187 name_txt = "%20s " % t.name
[911348cd]188
189 #run the test instance and collect the result
[472ca32]190 test_failed, error = run_test_instance(t, generate, dry_run)
[911348cd]191
192 # aggregate test suite result
[efc15918]193 failed = test_failed or failed
194
[911348cd]195 # update output based on current action
[84d4d6f]196 if generate :
197 failed_txt = "ERROR"
198 success_txt = "Done"
[ebcd82b]199 else :
[84d4d6f]200 failed_txt = "FAILED"
201 success_txt = "PASSED"
202
[911348cd]203 #print result with error if needed
[52c97dd]204 text = name_txt + (failed_txt if test_failed else success_txt)
205 out = sys.stdout
[84d4d6f]206 if error :
[52c97dd]207 text = text + "\n" + error
208 out = sys.stderr
209
210 print(text, file = out);
[1d57215]211 sys.stdout.flush()
212 sys.stderr.flush()
213
[efc15918]214
[911348cd]215 #clean the workspace
[74358c3]216 sh("%s clean > /dev/null 2>&1" % make_cmd, dry_run)
[efc15918]217
[b70b6fc]218 return 1 if failed else 0
[efc15918]219
220################################################################################
221# main loop
222################################################################################
[911348cd]223# create a parser with the arguments for the tests script
[efc15918]224parser = argparse.ArgumentParser(description='Script which runs cforall tests')
225parser.add_argument('--dry-run', help='Don\'t run the tests, only output the commands', action='store_true')
[0534c3c]226parser.add_argument('--list', help='List all test available', action='store_true')
[efc15918]227parser.add_argument('--all', help='Run all test available', action='store_true')
[0534c3c]228parser.add_argument('--regenerate-expected', help='Regenerate the .expect by running the specified tets, can be used with --all option', action='store_true')
[52c97dd]229parser.add_argument('-j', '--jobs', help='Number of tests to run simultaneously', type=int, default='8')
[efc15918]230parser.add_argument('tests', metavar='test', type=str, nargs='*', help='a list of tests to run')
231
[911348cd]232# parse the command line arguments
[efc15918]233options = parser.parse_args()
234
[911348cd]235# script must have at least some tests to run
[0534c3c]236if (len(options.tests) > 0 and options.all and not options.list) \
237or (len(options.tests) == 0 and not options.all and not options.list) :
[3c1d702]238 print('ERROR: must have option \'--all\' or non-empty test list', file=sys.stderr)
239 parser.print_help()
240 sys.exit(1)
[efc15918]241
[911348cd]242# fetch the liest of all valid tests
[0534c3c]243allTests = listTests()
244
[911348cd]245# if user wants all tests than no other treatement of the test list is required
[0534c3c]246if options.all or options.list :
247 tests = allTests
248
249else :
[911348cd]250 #otherwise we need to validate that the test list that was entered is valid
[0534c3c]251 tests = []
[f1231f2]252
[911348cd]253 # if we are regenerating the tests we need to find the information of the
254 # already existing tests and create new info for the new tests
[f1231f2]255 if options.regenerate_expected :
256 tests = map(filterTests, options.tests)
257
258 else :
[911348cd]259 # otherwise we only need to validate that all tests are present in the complete list
[f1231f2]260 for testname in options.tests:
261 test = [t for t in allTests if t.name == testname]
262
263 if len(test) != 0 :
264 tests.append( test[0] )
265 else :
266 print('ERROR: No expected file for test %s, ignoring it' % testname, file=sys.stderr)
[0534c3c]267
[911348cd]268 # make sure we have at least some test to run
[0534c3c]269 if len(tests) == 0 :
270 print('ERROR: No valid test to run', file=sys.stderr)
271 sys.exit(1)
272
[911348cd]273# sort the test alphabetically for convenience
[f1231f2]274tests.sort(key=lambda t: t.name)
275
[911348cd]276# check if the user already passed in a number of jobs for multi-threading
[7bd045d]277make_flags = environ.get('MAKEFLAGS')
[52c97dd]278make_has_max_jobs = re.search("(-j|--jobs)\s*([0-9]+)", make_flags) if make_flags else None
279make_max_jobs = make_has_max_jobs.group(2) if make_has_max_jobs else None
280make_cmd = "make" if make_flags and "-j" in make_flags else "make -j8"
[911348cd]281
282# make sure we have a valid number of jobs that corresponds to user input
283options.jobs = int(make_max_jobs) if make_max_jobs else options.jobs
284if options.jobs <= 0 :
285 print('ERROR: Invalid number of jobs', file=sys.stderr)
286 sys.exit(1)
287
288# users may want to simply list the tests
[0534c3c]289if options.list :
[f1231f2]290 print("\n".join(map(lambda t: "%s (%s)" % (t.name, t.path), tests)))
[efc15918]291
[0534c3c]292else :
[911348cd]293 # otherwise run all tests and make sure to return the correct error code
[52c97dd]294 sys.exit( run_tests(tests, options.regenerate_expected, options.dry_run, options.jobs) )
Note: See TracBrowser for help on using the repository browser.