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