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. |
Daniel Dunbar | 3b2505b | 2009-07-25 12:57:15 +0000 | [diff] [blame] | 27 | kChildEnv = {} |
| 28 | for var in ('PATH', 'SYSTEMROOT'): |
| 29 | kChildEnv[var] = os.environ.get(var, '') |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 30 | |
Daniel Dunbar | 8fe83f1 | 2009-07-25 09:42:24 +0000 | [diff] [blame] | 31 | kSystemName = platform.system() |
| 32 | |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 33 | class TestStatus: |
| 34 | Pass = 0 |
| 35 | XFail = 1 |
| 36 | Fail = 2 |
| 37 | XPass = 3 |
Daniel Dunbar | 8bf0ccd | 2009-07-25 12:47:38 +0000 | [diff] [blame] | 38 | Invalid = 4 |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 39 | |
Daniel Dunbar | 8bf0ccd | 2009-07-25 12:47:38 +0000 | [diff] [blame] | 40 | kNames = ['Pass','XFail','Fail','XPass','Invalid'] |
Daniel Dunbar | fbbb1e7 | 2009-07-02 23:58:07 +0000 | [diff] [blame] | 41 | @staticmethod |
Daniel Dunbar | 8afede2 | 2009-07-02 23:56:37 +0000 | [diff] [blame] | 42 | def getName(code): |
| 43 | return TestStatus.kNames[code] |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 44 | |
| 45 | def mkdir_p(path): |
| 46 | if not path: |
| 47 | pass |
| 48 | elif os.path.exists(path): |
| 49 | pass |
| 50 | else: |
| 51 | parent = os.path.dirname(path) |
| 52 | if parent != path: |
| 53 | mkdir_p(parent) |
| 54 | try: |
| 55 | os.mkdir(path) |
| 56 | except OSError,e: |
| 57 | if e.errno != errno.EEXIST: |
| 58 | raise |
| 59 | |
Daniel Dunbar | 9a676b7 | 2009-07-29 02:57:25 +0000 | [diff] [blame^] | 60 | def executeScript(script, commands, cwd, useValgrind): |
Daniel Dunbar | 10aebbb | 2009-07-25 15:26:08 +0000 | [diff] [blame] | 61 | # Write script file |
| 62 | f = open(script,'w') |
| 63 | if kSystemName == 'Windows': |
| 64 | f.write('\nif %ERRORLEVEL% NEQ 0 EXIT\n'.join(commands)) |
| 65 | else: |
| 66 | f.write(' &&\n'.join(commands)) |
| 67 | f.write('\n') |
| 68 | f.close() |
| 69 | |
| 70 | if kSystemName == 'Windows': |
| 71 | command = ['cmd','/c', script] |
| 72 | else: |
| 73 | command = ['/bin/sh', script] |
Daniel Dunbar | 9a676b7 | 2009-07-29 02:57:25 +0000 | [diff] [blame^] | 74 | if useValgrind: |
| 75 | # FIXME: Running valgrind on sh is overkill. We probably could just |
| 76 | # ron on clang with no real loss. |
| 77 | command = ['valgrind', '-q', |
| 78 | '--tool=memcheck', '--leak-check=no', '--trace-children=yes', |
| 79 | '--error-exitcode=123'] + command |
Daniel Dunbar | 10aebbb | 2009-07-25 15:26:08 +0000 | [diff] [blame] | 80 | |
| 81 | p = subprocess.Popen(command, cwd=cwd, |
| 82 | stdin=subprocess.PIPE, |
| 83 | stdout=subprocess.PIPE, |
| 84 | stderr=subprocess.PIPE, |
| 85 | env=kChildEnv) |
| 86 | out,err = p.communicate() |
| 87 | exitCode = p.wait() |
| 88 | |
| 89 | # Detect Ctrl-C in subprocess. |
| 90 | if exitCode == -signal.SIGINT: |
| 91 | raise KeyboardInterrupt |
| 92 | |
| 93 | return out, err, exitCode |
| 94 | |
Daniel Dunbar | a957d99 | 2009-07-25 14:46:05 +0000 | [diff] [blame] | 95 | import StringIO |
Daniel Dunbar | 9a676b7 | 2009-07-29 02:57:25 +0000 | [diff] [blame^] | 96 | def runOneTest(testPath, tmpBase, clang, clangcc, useValgrind): |
Daniel Dunbar | a957d99 | 2009-07-25 14:46:05 +0000 | [diff] [blame] | 97 | # Make paths absolute. |
| 98 | tmpBase = os.path.abspath(tmpBase) |
| 99 | testPath = os.path.abspath(testPath) |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 100 | |
| 101 | # Create the output directory if it does not already exist. |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 102 | |
Daniel Dunbar | a957d99 | 2009-07-25 14:46:05 +0000 | [diff] [blame] | 103 | mkdir_p(os.path.dirname(tmpBase)) |
| 104 | script = tmpBase + '.script' |
Daniel Dunbar | 8fe83f1 | 2009-07-25 09:42:24 +0000 | [diff] [blame] | 105 | if kSystemName == 'Windows': |
Daniel Dunbar | a957d99 | 2009-07-25 14:46:05 +0000 | [diff] [blame] | 106 | script += '.bat' |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 107 | |
Daniel Dunbar | a957d99 | 2009-07-25 14:46:05 +0000 | [diff] [blame] | 108 | substitutions = [('%s', testPath), |
| 109 | ('%S', os.path.dirname(testPath)), |
| 110 | ('%t', tmpBase + '.tmp'), |
| 111 | (' clang ', ' ' + clang + ' '), |
| 112 | (' clang-cc ', ' ' + clangcc + ' ')] |
Daniel Dunbar | 025f80d | 2009-07-25 11:27:37 +0000 | [diff] [blame] | 113 | |
| 114 | # Collect the test lines from the script. |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 115 | scriptLines = [] |
| 116 | xfailLines = [] |
Daniel Dunbar | a957d99 | 2009-07-25 14:46:05 +0000 | [diff] [blame] | 117 | for ln in open(testPath): |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 118 | if 'RUN:' in ln: |
Daniel Dunbar | 025f80d | 2009-07-25 11:27:37 +0000 | [diff] [blame] | 119 | # Isolate the command to run. |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 120 | index = ln.index('RUN:') |
| 121 | ln = ln[index+4:] |
Daniel Dunbar | 025f80d | 2009-07-25 11:27:37 +0000 | [diff] [blame] | 122 | |
Daniel Dunbar | 322f789 | 2009-07-25 12:23:35 +0000 | [diff] [blame] | 123 | # Strip trailing newline. |
| 124 | scriptLines.append(ln) |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 125 | elif 'XFAIL' in ln: |
| 126 | xfailLines.append(ln) |
Daniel Dunbar | 025f80d | 2009-07-25 11:27:37 +0000 | [diff] [blame] | 127 | |
| 128 | # FIXME: Support something like END, in case we need to process large |
| 129 | # files. |
Daniel Dunbar | a957d99 | 2009-07-25 14:46:05 +0000 | [diff] [blame] | 130 | |
| 131 | # Verify the script contains a run line. |
| 132 | if not scriptLines: |
| 133 | return (TestStatus.Fail, "Test has no run line!") |
Daniel Dunbar | 322f789 | 2009-07-25 12:23:35 +0000 | [diff] [blame] | 134 | |
| 135 | # Apply substitutions to the script. |
| 136 | def processLine(ln): |
| 137 | # Apply substitutions |
| 138 | for a,b in substitutions: |
| 139 | ln = ln.replace(a,b) |
| 140 | |
Daniel Dunbar | 322f789 | 2009-07-25 12:23:35 +0000 | [diff] [blame] | 141 | # Strip the trailing newline and any extra whitespace. |
| 142 | return ln.strip() |
| 143 | scriptLines = map(processLine, scriptLines) |
Daniel Dunbar | 025f80d | 2009-07-25 11:27:37 +0000 | [diff] [blame] | 144 | |
| 145 | # Validate interior lines for '&&', a lovely historical artifact. |
| 146 | for i in range(len(scriptLines) - 1): |
| 147 | ln = scriptLines[i] |
| 148 | |
| 149 | if not ln.endswith('&&'): |
Daniel Dunbar | a957d99 | 2009-07-25 14:46:05 +0000 | [diff] [blame] | 150 | return (TestStatus.Fail, |
Daniel Dunbar | 6779647 | 2009-07-27 19:01:13 +0000 | [diff] [blame] | 151 | ("MISSING \'&&\': %s\n" + |
| 152 | "FOLLOWED BY : %s\n") % (ln, scriptLines[i + 1])) |
Daniel Dunbar | 025f80d | 2009-07-25 11:27:37 +0000 | [diff] [blame] | 153 | |
| 154 | # Strip off '&&' |
| 155 | scriptLines[i] = ln[:-2] |
Daniel Dunbar | 025f80d | 2009-07-25 11:27:37 +0000 | [diff] [blame] | 156 | |
Daniel Dunbar | 10aebbb | 2009-07-25 15:26:08 +0000 | [diff] [blame] | 157 | out, err, exitCode = executeScript(script, scriptLines, |
Daniel Dunbar | 9a676b7 | 2009-07-29 02:57:25 +0000 | [diff] [blame^] | 158 | os.path.dirname(testPath), |
| 159 | useValgrind) |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 160 | if xfailLines: |
Daniel Dunbar | a957d99 | 2009-07-25 14:46:05 +0000 | [diff] [blame] | 161 | ok = exitCode != 0 |
| 162 | status = (TestStatus.XPass, TestStatus.XFail)[ok] |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 163 | else: |
Daniel Dunbar | a957d99 | 2009-07-25 14:46:05 +0000 | [diff] [blame] | 164 | ok = exitCode == 0 |
| 165 | status = (TestStatus.Fail, TestStatus.Pass)[ok] |
| 166 | |
| 167 | if ok: |
| 168 | return (status,'') |
| 169 | |
| 170 | output = StringIO.StringIO() |
| 171 | print >>output, "Script:" |
| 172 | print >>output, "--" |
| 173 | print >>output, '\n'.join(scriptLines) |
| 174 | print >>output, "--" |
| 175 | print >>output, "Exit Code: %r" % exitCode |
| 176 | print >>output, "Command Output (stdout):" |
| 177 | print >>output, "--" |
| 178 | output.write(out) |
| 179 | print >>output, "--" |
| 180 | print >>output, "Command Output (stderr):" |
| 181 | print >>output, "--" |
| 182 | output.write(err) |
| 183 | print >>output, "--" |
| 184 | return (status, output.getvalue()) |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 185 | |
| 186 | def capture(args): |
Daniel Dunbar | 8fe83f1 | 2009-07-25 09:42:24 +0000 | [diff] [blame] | 187 | p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 188 | out,_ = p.communicate() |
| 189 | return out |
| 190 | |
| 191 | def which(command): |
Daniel Dunbar | 6779647 | 2009-07-27 19:01:13 +0000 | [diff] [blame] | 192 | # FIXME: Take configuration object. |
| 193 | |
Daniel Dunbar | 8fe83f1 | 2009-07-25 09:42:24 +0000 | [diff] [blame] | 194 | # Check for absolute match first. |
| 195 | if os.path.exists(command): |
| 196 | return command |
| 197 | |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 198 | # Would be nice if Python had a lib function for this. |
Daniel Dunbar | 6779647 | 2009-07-27 19:01:13 +0000 | [diff] [blame] | 199 | paths = kChildEnv['PATH'] |
Daniel Dunbar | 8fe83f1 | 2009-07-25 09:42:24 +0000 | [diff] [blame] | 200 | if not paths: |
| 201 | paths = os.defpath |
| 202 | |
| 203 | # Get suffixes to search. |
| 204 | pathext = os.environ.get('PATHEXT', '').split(os.pathsep) |
| 205 | |
| 206 | # Search the paths... |
| 207 | for path in paths.split(os.pathsep): |
| 208 | for ext in pathext: |
| 209 | p = os.path.join(path, command + ext) |
| 210 | if os.path.exists(p): |
| 211 | return p |
| 212 | |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 213 | return None |
| 214 | |
| 215 | def inferClang(): |
| 216 | # Determine which clang to use. |
| 217 | clang = os.getenv('CLANG') |
| 218 | |
| 219 | # If the user set clang in the environment, definitely use that and don't |
| 220 | # try to validate. |
| 221 | if clang: |
| 222 | return clang |
| 223 | |
| 224 | # Otherwise look in the path. |
| 225 | clang = which('clang') |
| 226 | |
| 227 | if not clang: |
| 228 | print >>sys.stderr, "error: couldn't find 'clang' program, try setting CLANG in your environment" |
| 229 | sys.exit(1) |
| 230 | |
| 231 | return clang |
| 232 | |
| 233 | def inferClangCC(clang): |
| 234 | clangcc = os.getenv('CLANGCC') |
| 235 | |
| 236 | # If the user set clang in the environment, definitely use that and don't |
| 237 | # try to validate. |
| 238 | if clangcc: |
| 239 | return clangcc |
| 240 | |
| 241 | # Otherwise try adding -cc since we expect to be looking in a build |
| 242 | # directory. |
Daniel Dunbar | 8fe83f1 | 2009-07-25 09:42:24 +0000 | [diff] [blame] | 243 | if clang.endswith('.exe'): |
| 244 | clangccName = clang[:-4] + '-cc.exe' |
| 245 | else: |
| 246 | clangccName = clang + '-cc' |
| 247 | clangcc = which(clangccName) |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 248 | if not clangcc: |
| 249 | # Otherwise ask clang. |
| 250 | res = capture([clang, '-print-prog-name=clang-cc']) |
| 251 | res = res.strip() |
| 252 | if res and os.path.exists(res): |
| 253 | clangcc = res |
| 254 | |
| 255 | if not clangcc: |
| 256 | print >>sys.stderr, "error: couldn't find 'clang-cc' program, try setting CLANGCC in your environment" |
| 257 | sys.exit(1) |
| 258 | |
| 259 | return clangcc |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 260 | |
Daniel Dunbar | df08489 | 2009-07-25 09:53:43 +0000 | [diff] [blame] | 261 | def getTestOutputBase(dir, testpath): |
Daniel Dunbar | fecdd00 | 2009-07-25 12:05:55 +0000 | [diff] [blame] | 262 | """getTestOutputBase(dir, testpath) - Get the full path for temporary files |
Daniel Dunbar | df08489 | 2009-07-25 09:53:43 +0000 | [diff] [blame] | 263 | corresponding to the given test path.""" |
| 264 | |
| 265 | # Form the output base out of the test parent directory name and the test |
| 266 | # name. FIXME: Find a better way to organize test results. |
| 267 | return os.path.join(dir, |
| 268 | os.path.basename(os.path.dirname(testpath)), |
| 269 | os.path.basename(testpath)) |
| 270 | |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 271 | def main(): |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 272 | global options |
| 273 | from optparse import OptionParser |
| 274 | parser = OptionParser("usage: %prog [options] {tests}") |
| 275 | parser.add_option("", "--clang", dest="clang", |
| 276 | help="Program to use as \"clang\"", |
| 277 | action="store", default=None) |
| 278 | parser.add_option("", "--clang-cc", dest="clangcc", |
| 279 | help="Program to use as \"clang-cc\"", |
| 280 | action="store", default=None) |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 281 | (opts, args) = parser.parse_args() |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 282 | |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 283 | if not args: |
| 284 | parser.error('No tests specified') |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 285 | |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 286 | if opts.clang is None: |
| 287 | opts.clang = inferClang() |
| 288 | if opts.clangcc is None: |
| 289 | opts.clangcc = inferClangCC(opts.clang) |
| 290 | |
| 291 | for path in args: |
Daniel Dunbar | a957d99 | 2009-07-25 14:46:05 +0000 | [diff] [blame] | 292 | base = getTestOutputBase('Output', path) + '.out' |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 293 | |
Daniel Dunbar | a957d99 | 2009-07-25 14:46:05 +0000 | [diff] [blame] | 294 | status,output = runOneTest(path, base, opts.clang, opts.clangcc) |
| 295 | print '%s: %s' % (TestStatus.getName(status).upper(), path) |
| 296 | if status == TestStatus.Fail or status == TestStatus.XPass: |
| 297 | print "%s TEST '%s' FAILED %s" % ('*'*20, path, '*'*20) |
| 298 | sys.stdout.write(output) |
| 299 | print "*" * 20 |
| 300 | sys.exit(1) |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 301 | |
Daniel Dunbar | a957d99 | 2009-07-25 14:46:05 +0000 | [diff] [blame] | 302 | sys.exit(0) |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 303 | |
| 304 | if __name__=='__main__': |
| 305 | main() |