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