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 | 1db467f | 2009-07-31 05:54:17 +0000 | [diff] [blame^] | 26 | import Util |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 27 | |
Daniel Dunbar | 8fe83f1 | 2009-07-25 09:42:24 +0000 | [diff] [blame] | 28 | kSystemName = platform.system() |
| 29 | |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 30 | class TestStatus: |
| 31 | Pass = 0 |
| 32 | XFail = 1 |
| 33 | Fail = 2 |
| 34 | XPass = 3 |
Daniel Dunbar | 8bf0ccd | 2009-07-25 12:47:38 +0000 | [diff] [blame] | 35 | Invalid = 4 |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 36 | |
Daniel Dunbar | 8bf0ccd | 2009-07-25 12:47:38 +0000 | [diff] [blame] | 37 | kNames = ['Pass','XFail','Fail','XPass','Invalid'] |
Daniel Dunbar | fbbb1e7 | 2009-07-02 23:58:07 +0000 | [diff] [blame] | 38 | @staticmethod |
Daniel Dunbar | 8afede2 | 2009-07-02 23:56:37 +0000 | [diff] [blame] | 39 | def getName(code): |
| 40 | return TestStatus.kNames[code] |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 41 | |
Daniel Dunbar | 1db467f | 2009-07-31 05:54:17 +0000 | [diff] [blame^] | 42 | def executeScript(cfg, script, commands, cwd, useValgrind): |
Daniel Dunbar | 10aebbb | 2009-07-25 15:26:08 +0000 | [diff] [blame] | 43 | # Write script file |
| 44 | f = open(script,'w') |
| 45 | if kSystemName == 'Windows': |
| 46 | f.write('\nif %ERRORLEVEL% NEQ 0 EXIT\n'.join(commands)) |
| 47 | else: |
| 48 | f.write(' &&\n'.join(commands)) |
| 49 | f.write('\n') |
| 50 | f.close() |
| 51 | |
| 52 | if kSystemName == 'Windows': |
| 53 | command = ['cmd','/c', script] |
| 54 | else: |
| 55 | command = ['/bin/sh', script] |
Daniel Dunbar | 9a676b7 | 2009-07-29 02:57:25 +0000 | [diff] [blame] | 56 | if useValgrind: |
| 57 | # FIXME: Running valgrind on sh is overkill. We probably could just |
| 58 | # ron on clang with no real loss. |
| 59 | command = ['valgrind', '-q', |
| 60 | '--tool=memcheck', '--leak-check=no', '--trace-children=yes', |
| 61 | '--error-exitcode=123'] + command |
Daniel Dunbar | 10aebbb | 2009-07-25 15:26:08 +0000 | [diff] [blame] | 62 | |
| 63 | p = subprocess.Popen(command, cwd=cwd, |
| 64 | stdin=subprocess.PIPE, |
| 65 | stdout=subprocess.PIPE, |
| 66 | stderr=subprocess.PIPE, |
Daniel Dunbar | 1db467f | 2009-07-31 05:54:17 +0000 | [diff] [blame^] | 67 | env=cfg.environment) |
Daniel Dunbar | 10aebbb | 2009-07-25 15:26:08 +0000 | [diff] [blame] | 68 | out,err = p.communicate() |
| 69 | exitCode = p.wait() |
| 70 | |
| 71 | # Detect Ctrl-C in subprocess. |
| 72 | if exitCode == -signal.SIGINT: |
| 73 | raise KeyboardInterrupt |
| 74 | |
| 75 | return out, err, exitCode |
| 76 | |
Daniel Dunbar | a957d99 | 2009-07-25 14:46:05 +0000 | [diff] [blame] | 77 | import StringIO |
Daniel Dunbar | 1db467f | 2009-07-31 05:54:17 +0000 | [diff] [blame^] | 78 | def runOneTest(cfg, testPath, tmpBase, clang, clangcc, useValgrind): |
Daniel Dunbar | a957d99 | 2009-07-25 14:46:05 +0000 | [diff] [blame] | 79 | # Make paths absolute. |
| 80 | tmpBase = os.path.abspath(tmpBase) |
| 81 | testPath = os.path.abspath(testPath) |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 82 | |
| 83 | # Create the output directory if it does not already exist. |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 84 | |
Daniel Dunbar | 1db467f | 2009-07-31 05:54:17 +0000 | [diff] [blame^] | 85 | Util.mkdir_p(os.path.dirname(tmpBase)) |
Daniel Dunbar | a957d99 | 2009-07-25 14:46:05 +0000 | [diff] [blame] | 86 | script = tmpBase + '.script' |
Daniel Dunbar | 8fe83f1 | 2009-07-25 09:42:24 +0000 | [diff] [blame] | 87 | if kSystemName == 'Windows': |
Daniel Dunbar | a957d99 | 2009-07-25 14:46:05 +0000 | [diff] [blame] | 88 | script += '.bat' |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 89 | |
Daniel Dunbar | a957d99 | 2009-07-25 14:46:05 +0000 | [diff] [blame] | 90 | substitutions = [('%s', testPath), |
| 91 | ('%S', os.path.dirname(testPath)), |
| 92 | ('%t', tmpBase + '.tmp'), |
| 93 | (' clang ', ' ' + clang + ' '), |
| 94 | (' clang-cc ', ' ' + clangcc + ' ')] |
Daniel Dunbar | 025f80d | 2009-07-25 11:27:37 +0000 | [diff] [blame] | 95 | |
| 96 | # Collect the test lines from the script. |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 97 | scriptLines = [] |
| 98 | xfailLines = [] |
Daniel Dunbar | a957d99 | 2009-07-25 14:46:05 +0000 | [diff] [blame] | 99 | for ln in open(testPath): |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 100 | if 'RUN:' in ln: |
Daniel Dunbar | 025f80d | 2009-07-25 11:27:37 +0000 | [diff] [blame] | 101 | # Isolate the command to run. |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 102 | index = ln.index('RUN:') |
| 103 | ln = ln[index+4:] |
Daniel Dunbar | 025f80d | 2009-07-25 11:27:37 +0000 | [diff] [blame] | 104 | |
Daniel Dunbar | 322f789 | 2009-07-25 12:23:35 +0000 | [diff] [blame] | 105 | # Strip trailing newline. |
| 106 | scriptLines.append(ln) |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 107 | elif 'XFAIL' in ln: |
| 108 | xfailLines.append(ln) |
Daniel Dunbar | 025f80d | 2009-07-25 11:27:37 +0000 | [diff] [blame] | 109 | |
| 110 | # FIXME: Support something like END, in case we need to process large |
| 111 | # files. |
Daniel Dunbar | a957d99 | 2009-07-25 14:46:05 +0000 | [diff] [blame] | 112 | |
| 113 | # Verify the script contains a run line. |
| 114 | if not scriptLines: |
| 115 | return (TestStatus.Fail, "Test has no run line!") |
Daniel Dunbar | 322f789 | 2009-07-25 12:23:35 +0000 | [diff] [blame] | 116 | |
| 117 | # Apply substitutions to the script. |
| 118 | def processLine(ln): |
| 119 | # Apply substitutions |
| 120 | for a,b in substitutions: |
| 121 | ln = ln.replace(a,b) |
| 122 | |
Daniel Dunbar | 322f789 | 2009-07-25 12:23:35 +0000 | [diff] [blame] | 123 | # Strip the trailing newline and any extra whitespace. |
| 124 | return ln.strip() |
| 125 | scriptLines = map(processLine, scriptLines) |
Daniel Dunbar | 025f80d | 2009-07-25 11:27:37 +0000 | [diff] [blame] | 126 | |
| 127 | # Validate interior lines for '&&', a lovely historical artifact. |
| 128 | for i in range(len(scriptLines) - 1): |
| 129 | ln = scriptLines[i] |
| 130 | |
| 131 | if not ln.endswith('&&'): |
Daniel Dunbar | a957d99 | 2009-07-25 14:46:05 +0000 | [diff] [blame] | 132 | return (TestStatus.Fail, |
Daniel Dunbar | 6779647 | 2009-07-27 19:01:13 +0000 | [diff] [blame] | 133 | ("MISSING \'&&\': %s\n" + |
| 134 | "FOLLOWED BY : %s\n") % (ln, scriptLines[i + 1])) |
Daniel Dunbar | 025f80d | 2009-07-25 11:27:37 +0000 | [diff] [blame] | 135 | |
| 136 | # Strip off '&&' |
| 137 | scriptLines[i] = ln[:-2] |
Daniel Dunbar | 025f80d | 2009-07-25 11:27:37 +0000 | [diff] [blame] | 138 | |
Daniel Dunbar | 1db467f | 2009-07-31 05:54:17 +0000 | [diff] [blame^] | 139 | out, err, exitCode = executeScript(cfg, script, scriptLines, |
Daniel Dunbar | 9a676b7 | 2009-07-29 02:57:25 +0000 | [diff] [blame] | 140 | os.path.dirname(testPath), |
| 141 | useValgrind) |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 142 | if xfailLines: |
Daniel Dunbar | a957d99 | 2009-07-25 14:46:05 +0000 | [diff] [blame] | 143 | ok = exitCode != 0 |
| 144 | status = (TestStatus.XPass, TestStatus.XFail)[ok] |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 145 | else: |
Daniel Dunbar | a957d99 | 2009-07-25 14:46:05 +0000 | [diff] [blame] | 146 | ok = exitCode == 0 |
| 147 | status = (TestStatus.Fail, TestStatus.Pass)[ok] |
| 148 | |
| 149 | if ok: |
| 150 | return (status,'') |
| 151 | |
| 152 | output = StringIO.StringIO() |
| 153 | print >>output, "Script:" |
| 154 | print >>output, "--" |
| 155 | print >>output, '\n'.join(scriptLines) |
| 156 | print >>output, "--" |
| 157 | print >>output, "Exit Code: %r" % exitCode |
| 158 | print >>output, "Command Output (stdout):" |
| 159 | print >>output, "--" |
| 160 | output.write(out) |
| 161 | print >>output, "--" |
| 162 | print >>output, "Command Output (stderr):" |
| 163 | print >>output, "--" |
| 164 | output.write(err) |
| 165 | print >>output, "--" |
| 166 | return (status, output.getvalue()) |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 167 | |
| 168 | def capture(args): |
Daniel Dunbar | 8fe83f1 | 2009-07-25 09:42:24 +0000 | [diff] [blame] | 169 | p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 170 | out,_ = p.communicate() |
| 171 | return out |
| 172 | |
Daniel Dunbar | 1db467f | 2009-07-31 05:54:17 +0000 | [diff] [blame^] | 173 | def inferClang(cfg): |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 174 | # Determine which clang to use. |
| 175 | clang = os.getenv('CLANG') |
| 176 | |
| 177 | # If the user set clang in the environment, definitely use that and don't |
| 178 | # try to validate. |
| 179 | if clang: |
| 180 | return clang |
| 181 | |
| 182 | # Otherwise look in the path. |
Daniel Dunbar | 1db467f | 2009-07-31 05:54:17 +0000 | [diff] [blame^] | 183 | clang = Util.which('clang', cfg.environment['PATH']) |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 184 | |
| 185 | if not clang: |
| 186 | print >>sys.stderr, "error: couldn't find 'clang' program, try setting CLANG in your environment" |
| 187 | sys.exit(1) |
| 188 | |
| 189 | return clang |
| 190 | |
Daniel Dunbar | 1db467f | 2009-07-31 05:54:17 +0000 | [diff] [blame^] | 191 | def inferClangCC(cfg, clang): |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 192 | clangcc = os.getenv('CLANGCC') |
| 193 | |
| 194 | # If the user set clang in the environment, definitely use that and don't |
| 195 | # try to validate. |
| 196 | if clangcc: |
| 197 | return clangcc |
| 198 | |
| 199 | # Otherwise try adding -cc since we expect to be looking in a build |
| 200 | # directory. |
Daniel Dunbar | 8fe83f1 | 2009-07-25 09:42:24 +0000 | [diff] [blame] | 201 | if clang.endswith('.exe'): |
| 202 | clangccName = clang[:-4] + '-cc.exe' |
| 203 | else: |
| 204 | clangccName = clang + '-cc' |
Daniel Dunbar | 1db467f | 2009-07-31 05:54:17 +0000 | [diff] [blame^] | 205 | clangcc = Util.which(clangccName, cfg.environment['PATH']) |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 206 | if not clangcc: |
| 207 | # Otherwise ask clang. |
| 208 | res = capture([clang, '-print-prog-name=clang-cc']) |
| 209 | res = res.strip() |
| 210 | if res and os.path.exists(res): |
| 211 | clangcc = res |
| 212 | |
| 213 | if not clangcc: |
| 214 | print >>sys.stderr, "error: couldn't find 'clang-cc' program, try setting CLANGCC in your environment" |
| 215 | sys.exit(1) |
| 216 | |
| 217 | return clangcc |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 218 | |
Daniel Dunbar | df08489 | 2009-07-25 09:53:43 +0000 | [diff] [blame] | 219 | def getTestOutputBase(dir, testpath): |
Daniel Dunbar | fecdd00 | 2009-07-25 12:05:55 +0000 | [diff] [blame] | 220 | """getTestOutputBase(dir, testpath) - Get the full path for temporary files |
Daniel Dunbar | df08489 | 2009-07-25 09:53:43 +0000 | [diff] [blame] | 221 | corresponding to the given test path.""" |
| 222 | |
| 223 | # Form the output base out of the test parent directory name and the test |
| 224 | # name. FIXME: Find a better way to organize test results. |
| 225 | return os.path.join(dir, |
| 226 | os.path.basename(os.path.dirname(testpath)), |
| 227 | os.path.basename(testpath)) |