Eli Friedman | 77a1fe9 | 2009-07-10 20:15:12 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 2 | # |
| 3 | # TestRunner.py - This script is used to run arbitrary unit tests. Unit |
| 4 | # tests must contain the command used to run them in the input file, starting |
| 5 | # immediately after a "RUN:" string. |
| 6 | # |
| 7 | # This runner recognizes and replaces the following strings in the command: |
| 8 | # |
| 9 | # %s - Replaced with the input name of the program, or the program to |
| 10 | # execute, as appropriate. |
Daniel Dunbar | 4d8076a | 2009-04-10 19:49:21 +0000 | [diff] [blame] | 11 | # %S - Replaced with the directory where the input resides. |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 12 | # %llvmgcc - llvm-gcc command |
| 13 | # %llvmgxx - llvm-g++ command |
| 14 | # %prcontext - prcontext.tcl script |
| 15 | # %t - temporary file name (derived from testcase name) |
| 16 | # |
| 17 | |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 18 | import errno |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 19 | import os |
Daniel Dunbar | 8fe83f1 | 2009-07-25 09:42:24 +0000 | [diff] [blame] | 20 | import platform |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 21 | import re |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 22 | import signal |
| 23 | import subprocess |
| 24 | import sys |
| 25 | |
Daniel Dunbar | 78a88e7 | 2009-07-25 10:14:19 +0000 | [diff] [blame] | 26 | # Increase determinism by explicitly choosing the environment. |
| 27 | kChildEnv = { 'PATH' : os.environ.get('PATH','') } |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 28 | |
Daniel Dunbar | 8fe83f1 | 2009-07-25 09:42:24 +0000 | [diff] [blame] | 29 | kSystemName = platform.system() |
| 30 | |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 31 | class TestStatus: |
| 32 | Pass = 0 |
| 33 | XFail = 1 |
| 34 | Fail = 2 |
| 35 | XPass = 3 |
| 36 | NoRunLine = 4 |
| 37 | Invalid = 5 |
| 38 | |
| 39 | kNames = ['Pass','XFail','Fail','XPass','NoRunLine','Invalid'] |
Daniel Dunbar | fbbb1e7 | 2009-07-02 23:58:07 +0000 | [diff] [blame] | 40 | @staticmethod |
Daniel Dunbar | 8afede2 | 2009-07-02 23:56:37 +0000 | [diff] [blame] | 41 | def getName(code): |
| 42 | return TestStatus.kNames[code] |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 43 | |
| 44 | def mkdir_p(path): |
| 45 | if not path: |
| 46 | pass |
| 47 | elif os.path.exists(path): |
| 48 | pass |
| 49 | else: |
| 50 | parent = os.path.dirname(path) |
| 51 | if parent != path: |
| 52 | mkdir_p(parent) |
| 53 | try: |
| 54 | os.mkdir(path) |
| 55 | except OSError,e: |
| 56 | if e.errno != errno.EEXIST: |
| 57 | raise |
| 58 | |
| 59 | def remove(path): |
| 60 | try: |
| 61 | os.remove(path) |
| 62 | except OSError: |
| 63 | pass |
| 64 | |
| 65 | def cat(path, output): |
| 66 | f = open(path) |
| 67 | output.writelines(f) |
| 68 | f.close() |
| 69 | |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 70 | def runOneTest(FILENAME, SUBST, OUTPUT, TESTNAME, CLANG, CLANGCC, |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 71 | useValgrind=False, |
| 72 | useDGCompat=False, |
| 73 | useScript=None, |
| 74 | output=sys.stdout): |
Nuno Lopes | a7afc45 | 2009-07-11 18:34:43 +0000 | [diff] [blame] | 75 | OUTPUT = os.path.abspath(OUTPUT) |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 76 | if useValgrind: |
| 77 | VG_OUTPUT = '%s.vg'%(OUTPUT,) |
Nuno Lopes | a7afc45 | 2009-07-11 18:34:43 +0000 | [diff] [blame] | 78 | os.system('rm -f %s.*'%(VG_OUTPUT)) |
| 79 | VALGRIND = 'valgrind -q --tool=memcheck --leak-check=full --trace-children=yes --log-file=%s.%%p'%(VG_OUTPUT) |
| 80 | CLANG = '%s %s'%(VALGRIND, CLANG) |
| 81 | CLANGCC = '%s %s'%(VALGRIND, CLANGCC) |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 82 | |
| 83 | # Create the output directory if it does not already exist. |
| 84 | mkdir_p(os.path.dirname(OUTPUT)) |
| 85 | |
| 86 | # FIXME |
| 87 | #ulimit -t 40 |
| 88 | |
| 89 | # FIXME: Load script once |
| 90 | # FIXME: Support "short" script syntax |
| 91 | |
| 92 | if useScript: |
| 93 | scriptFile = useScript |
| 94 | else: |
| 95 | # See if we have a per-dir test script. |
| 96 | dirScriptFile = os.path.join(os.path.dirname(FILENAME), 'test.script') |
| 97 | if os.path.exists(dirScriptFile): |
| 98 | scriptFile = dirScriptFile |
| 99 | else: |
| 100 | scriptFile = FILENAME |
| 101 | |
| 102 | # Verify the script contains a run line. |
| 103 | for ln in open(scriptFile): |
| 104 | if 'RUN:' in ln: |
| 105 | break |
| 106 | else: |
| 107 | print >>output, "******************** TEST '%s' HAS NO RUN LINE! ********************"%(TESTNAME,) |
| 108 | output.flush() |
| 109 | return TestStatus.NoRunLine |
| 110 | |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 111 | FILENAME = os.path.abspath(FILENAME) |
| 112 | SCRIPT = OUTPUT + '.script' |
Daniel Dunbar | 8fe83f1 | 2009-07-25 09:42:24 +0000 | [diff] [blame] | 113 | if kSystemName == 'Windows': |
| 114 | SCRIPT += '.bat' |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 115 | TEMPOUTPUT = OUTPUT + '.tmp' |
| 116 | |
| 117 | substitutions = [('%s',SUBST), |
Daniel Dunbar | 4d8076a | 2009-04-10 19:49:21 +0000 | [diff] [blame] | 118 | ('%S',os.path.dirname(SUBST)), |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 119 | ('%llvmgcc','llvm-gcc -emit-llvm -w'), |
| 120 | ('%llvmgxx','llvm-g++ -emit-llvm -w'), |
| 121 | ('%prcontext','prcontext.tcl'), |
| 122 | ('%t',TEMPOUTPUT), |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 123 | (' clang ', ' ' + CLANG + ' '), |
| 124 | (' clang-cc ', ' ' + CLANGCC + ' ')] |
Daniel Dunbar | 025f80d | 2009-07-25 11:27:37 +0000 | [diff] [blame] | 125 | |
| 126 | # Collect the test lines from the script. |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 127 | scriptLines = [] |
| 128 | xfailLines = [] |
| 129 | for ln in open(scriptFile): |
| 130 | if 'RUN:' in ln: |
Daniel Dunbar | 025f80d | 2009-07-25 11:27:37 +0000 | [diff] [blame] | 131 | # Isolate the command to run. |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 132 | index = ln.index('RUN:') |
| 133 | ln = ln[index+4:] |
Daniel Dunbar | 025f80d | 2009-07-25 11:27:37 +0000 | [diff] [blame] | 134 | |
Daniel Dunbar | 322f789 | 2009-07-25 12:23:35 +0000 | [diff] [blame^] | 135 | # Strip trailing newline. |
| 136 | scriptLines.append(ln) |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 137 | elif 'XFAIL' in ln: |
| 138 | xfailLines.append(ln) |
Daniel Dunbar | 025f80d | 2009-07-25 11:27:37 +0000 | [diff] [blame] | 139 | |
| 140 | # FIXME: Support something like END, in case we need to process large |
| 141 | # files. |
Daniel Dunbar | 322f789 | 2009-07-25 12:23:35 +0000 | [diff] [blame^] | 142 | |
| 143 | # Apply substitutions to the script. |
| 144 | def processLine(ln): |
| 145 | # Apply substitutions |
| 146 | for a,b in substitutions: |
| 147 | ln = ln.replace(a,b) |
| 148 | |
| 149 | if useDGCompat: |
| 150 | ln = re.sub(r'\{(.*)\}', r'"\1"', ln) |
| 151 | |
| 152 | # Strip the trailing newline and any extra whitespace. |
| 153 | return ln.strip() |
| 154 | scriptLines = map(processLine, scriptLines) |
Daniel Dunbar | 025f80d | 2009-07-25 11:27:37 +0000 | [diff] [blame] | 155 | |
| 156 | # Validate interior lines for '&&', a lovely historical artifact. |
| 157 | for i in range(len(scriptLines) - 1): |
| 158 | ln = scriptLines[i] |
| 159 | |
| 160 | if not ln.endswith('&&'): |
| 161 | print >>output, "MISSING \'&&\': %s" % ln |
| 162 | print >>output, "FOLLOWED BY : %s" % scriptLines[i + 1] |
| 163 | return TestStatus.Fail |
| 164 | |
| 165 | # Strip off '&&' |
| 166 | scriptLines[i] = ln[:-2] |
Daniel Dunbar | 025f80d | 2009-07-25 11:27:37 +0000 | [diff] [blame] | 167 | |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 168 | if xfailLines: |
| 169 | print >>output, "XFAILED '%s':"%(TESTNAME,) |
| 170 | output.writelines(xfailLines) |
| 171 | |
| 172 | # Write script file |
| 173 | f = open(SCRIPT,'w') |
Daniel Dunbar | 025f80d | 2009-07-25 11:27:37 +0000 | [diff] [blame] | 174 | f.write(' &&\n'.join(scriptLines)) |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 175 | f.close() |
| 176 | |
| 177 | outputFile = open(OUTPUT,'w') |
| 178 | p = None |
| 179 | try: |
Daniel Dunbar | 8fe83f1 | 2009-07-25 09:42:24 +0000 | [diff] [blame] | 180 | if kSystemName == 'Windows': |
| 181 | command = ['cmd','/c', SCRIPT] |
| 182 | else: |
| 183 | command = ['/bin/sh', SCRIPT] |
| 184 | |
| 185 | p = subprocess.Popen(command, |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 186 | cwd=os.path.dirname(FILENAME), |
Daniel Dunbar | 02cee15 | 2009-07-13 21:24:28 +0000 | [diff] [blame] | 187 | stdin=subprocess.PIPE, |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 188 | stdout=subprocess.PIPE, |
Daniel Dunbar | 78a88e7 | 2009-07-25 10:14:19 +0000 | [diff] [blame] | 189 | stderr=subprocess.PIPE, |
| 190 | env=kChildEnv) |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 191 | out,err = p.communicate() |
| 192 | outputFile.write(out) |
| 193 | outputFile.write(err) |
| 194 | SCRIPT_STATUS = p.wait() |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 195 | |
| 196 | # Detect Ctrl-C in subprocess. |
| 197 | if SCRIPT_STATUS == -signal.SIGINT: |
| 198 | raise KeyboardInterrupt |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 199 | except KeyboardInterrupt: |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 200 | raise |
| 201 | outputFile.close() |
| 202 | |
| 203 | if xfailLines: |
| 204 | SCRIPT_STATUS = not SCRIPT_STATUS |
| 205 | |
| 206 | if useValgrind: |
Daniel Dunbar | 8fe83f1 | 2009-07-25 09:42:24 +0000 | [diff] [blame] | 207 | if kSystemName == 'Windows': |
| 208 | raise NotImplementedError,'Cannot run valgrind on windows' |
| 209 | else: |
| 210 | VG_OUTPUT = capture(['/bin/sh','-c','cat %s.*'%(VG_OUTPUT)]) |
Nuno Lopes | a7afc45 | 2009-07-11 18:34:43 +0000 | [diff] [blame] | 211 | VG_STATUS = len(VG_OUTPUT) |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 212 | else: |
| 213 | VG_STATUS = 0 |
| 214 | |
| 215 | if SCRIPT_STATUS or VG_STATUS: |
| 216 | print >>output, "******************** TEST '%s' FAILED! ********************"%(TESTNAME,) |
| 217 | print >>output, "Command: " |
| 218 | output.writelines(scriptLines) |
| 219 | if not SCRIPT_STATUS: |
| 220 | print >>output, "Output:" |
| 221 | else: |
| 222 | print >>output, "Incorrect Output:" |
| 223 | cat(OUTPUT, output) |
| 224 | if VG_STATUS: |
| 225 | print >>output, "Valgrind Output:" |
Nuno Lopes | a7afc45 | 2009-07-11 18:34:43 +0000 | [diff] [blame] | 226 | print >>output, VG_OUTPUT |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 227 | print >>output, "******************** TEST '%s' FAILED! ********************"%(TESTNAME,) |
| 228 | output.flush() |
| 229 | if xfailLines: |
| 230 | return TestStatus.XPass |
| 231 | else: |
| 232 | return TestStatus.Fail |
| 233 | |
| 234 | if xfailLines: |
| 235 | return TestStatus.XFail |
| 236 | else: |
| 237 | return TestStatus.Pass |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 238 | |
| 239 | def capture(args): |
Daniel Dunbar | 8fe83f1 | 2009-07-25 09:42:24 +0000 | [diff] [blame] | 240 | p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 241 | out,_ = p.communicate() |
| 242 | return out |
| 243 | |
| 244 | def which(command): |
Daniel Dunbar | 8fe83f1 | 2009-07-25 09:42:24 +0000 | [diff] [blame] | 245 | # Check for absolute match first. |
| 246 | if os.path.exists(command): |
| 247 | return command |
| 248 | |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 249 | # Would be nice if Python had a lib function for this. |
Daniel Dunbar | 8fe83f1 | 2009-07-25 09:42:24 +0000 | [diff] [blame] | 250 | paths = os.environ.get('PATH') |
| 251 | if not paths: |
| 252 | paths = os.defpath |
| 253 | |
| 254 | # Get suffixes to search. |
| 255 | pathext = os.environ.get('PATHEXT', '').split(os.pathsep) |
| 256 | |
| 257 | # Search the paths... |
| 258 | for path in paths.split(os.pathsep): |
| 259 | for ext in pathext: |
| 260 | p = os.path.join(path, command + ext) |
| 261 | if os.path.exists(p): |
| 262 | return p |
| 263 | |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 264 | return None |
| 265 | |
| 266 | def inferClang(): |
| 267 | # Determine which clang to use. |
| 268 | clang = os.getenv('CLANG') |
| 269 | |
| 270 | # If the user set clang in the environment, definitely use that and don't |
| 271 | # try to validate. |
| 272 | if clang: |
| 273 | return clang |
| 274 | |
| 275 | # Otherwise look in the path. |
| 276 | clang = which('clang') |
| 277 | |
| 278 | if not clang: |
| 279 | print >>sys.stderr, "error: couldn't find 'clang' program, try setting CLANG in your environment" |
| 280 | sys.exit(1) |
| 281 | |
| 282 | return clang |
| 283 | |
| 284 | def inferClangCC(clang): |
| 285 | clangcc = os.getenv('CLANGCC') |
| 286 | |
| 287 | # If the user set clang in the environment, definitely use that and don't |
| 288 | # try to validate. |
| 289 | if clangcc: |
| 290 | return clangcc |
| 291 | |
| 292 | # Otherwise try adding -cc since we expect to be looking in a build |
| 293 | # directory. |
Daniel Dunbar | 8fe83f1 | 2009-07-25 09:42:24 +0000 | [diff] [blame] | 294 | if clang.endswith('.exe'): |
| 295 | clangccName = clang[:-4] + '-cc.exe' |
| 296 | else: |
| 297 | clangccName = clang + '-cc' |
| 298 | clangcc = which(clangccName) |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 299 | if not clangcc: |
| 300 | # Otherwise ask clang. |
| 301 | res = capture([clang, '-print-prog-name=clang-cc']) |
| 302 | res = res.strip() |
| 303 | if res and os.path.exists(res): |
| 304 | clangcc = res |
| 305 | |
| 306 | if not clangcc: |
| 307 | print >>sys.stderr, "error: couldn't find 'clang-cc' program, try setting CLANGCC in your environment" |
| 308 | sys.exit(1) |
| 309 | |
| 310 | return clangcc |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 311 | |
Daniel Dunbar | df08489 | 2009-07-25 09:53:43 +0000 | [diff] [blame] | 312 | def getTestOutputBase(dir, testpath): |
Daniel Dunbar | fecdd00 | 2009-07-25 12:05:55 +0000 | [diff] [blame] | 313 | """getTestOutputBase(dir, testpath) - Get the full path for temporary files |
Daniel Dunbar | df08489 | 2009-07-25 09:53:43 +0000 | [diff] [blame] | 314 | corresponding to the given test path.""" |
| 315 | |
| 316 | # Form the output base out of the test parent directory name and the test |
| 317 | # name. FIXME: Find a better way to organize test results. |
| 318 | return os.path.join(dir, |
| 319 | os.path.basename(os.path.dirname(testpath)), |
| 320 | os.path.basename(testpath)) |
| 321 | |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 322 | def main(): |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 323 | global options |
| 324 | from optparse import OptionParser |
| 325 | parser = OptionParser("usage: %prog [options] {tests}") |
| 326 | parser.add_option("", "--clang", dest="clang", |
| 327 | help="Program to use as \"clang\"", |
| 328 | action="store", default=None) |
| 329 | parser.add_option("", "--clang-cc", dest="clangcc", |
| 330 | help="Program to use as \"clang-cc\"", |
| 331 | action="store", default=None) |
| 332 | parser.add_option("", "--vg", dest="useValgrind", |
| 333 | help="Run tests under valgrind", |
| 334 | action="store_true", default=False) |
| 335 | parser.add_option("", "--dg", dest="useDGCompat", |
| 336 | help="Use llvm dejagnu compatibility mode", |
| 337 | action="store_true", default=False) |
| 338 | (opts, args) = parser.parse_args() |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 339 | |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 340 | if not args: |
| 341 | parser.error('No tests specified') |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 342 | |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 343 | if opts.clang is None: |
| 344 | opts.clang = inferClang() |
| 345 | if opts.clangcc is None: |
| 346 | opts.clangcc = inferClangCC(opts.clang) |
| 347 | |
| 348 | for path in args: |
| 349 | command = path |
Daniel Dunbar | fecdd00 | 2009-07-25 12:05:55 +0000 | [diff] [blame] | 350 | output = getTestOutputBase('Output', path) + '.out' |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 351 | testname = path |
| 352 | |
| 353 | res = runOneTest(path, command, output, testname, |
| 354 | opts.clang, opts.clangcc, |
| 355 | useValgrind=opts.useValgrind, |
| 356 | useDGCompat=opts.useDGCompat, |
| 357 | useScript=os.getenv("TEST_SCRIPT")) |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 358 | |
| 359 | sys.exit(res == TestStatus.Fail or res == TestStatus.XPass) |
| 360 | |
| 361 | if __name__=='__main__': |
| 362 | main() |