Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 1 | import errno |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 2 | import os |
Daniel Dunbar | 8fe83f1 | 2009-07-25 09:42:24 +0000 | [diff] [blame] | 3 | import platform |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 4 | import re |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 5 | import signal |
| 6 | import subprocess |
| 7 | import sys |
| 8 | |
Daniel Dunbar | 1db467f | 2009-07-31 05:54:17 +0000 | [diff] [blame] | 9 | import Util |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 10 | |
Daniel Dunbar | 8fe83f1 | 2009-07-25 09:42:24 +0000 | [diff] [blame] | 11 | kSystemName = platform.system() |
| 12 | |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 13 | class TestStatus: |
| 14 | Pass = 0 |
| 15 | XFail = 1 |
| 16 | Fail = 2 |
| 17 | XPass = 3 |
Daniel Dunbar | 8bf0ccd | 2009-07-25 12:47:38 +0000 | [diff] [blame] | 18 | Invalid = 4 |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 19 | |
Daniel Dunbar | 8bf0ccd | 2009-07-25 12:47:38 +0000 | [diff] [blame] | 20 | kNames = ['Pass','XFail','Fail','XPass','Invalid'] |
Daniel Dunbar | fbbb1e7 | 2009-07-02 23:58:07 +0000 | [diff] [blame] | 21 | @staticmethod |
Daniel Dunbar | 8afede2 | 2009-07-02 23:56:37 +0000 | [diff] [blame] | 22 | def getName(code): |
| 23 | return TestStatus.kNames[code] |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 24 | |
Daniel Dunbar | 5928ccd | 2009-08-01 04:06:02 +0000 | [diff] [blame^] | 25 | def executeScript(cfg, script, commands, cwd): |
Daniel Dunbar | 10aebbb | 2009-07-25 15:26:08 +0000 | [diff] [blame] | 26 | # Write script file |
| 27 | f = open(script,'w') |
| 28 | if kSystemName == 'Windows': |
| 29 | f.write('\nif %ERRORLEVEL% NEQ 0 EXIT\n'.join(commands)) |
| 30 | else: |
| 31 | f.write(' &&\n'.join(commands)) |
| 32 | f.write('\n') |
| 33 | f.close() |
| 34 | |
| 35 | if kSystemName == 'Windows': |
| 36 | command = ['cmd','/c', script] |
| 37 | else: |
| 38 | command = ['/bin/sh', script] |
Daniel Dunbar | 5928ccd | 2009-08-01 04:06:02 +0000 | [diff] [blame^] | 39 | if cfg.useValgrind: |
Daniel Dunbar | 9a676b7 | 2009-07-29 02:57:25 +0000 | [diff] [blame] | 40 | # FIXME: Running valgrind on sh is overkill. We probably could just |
Daniel Dunbar | 5928ccd | 2009-08-01 04:06:02 +0000 | [diff] [blame^] | 41 | # run on clang with no real loss. |
Daniel Dunbar | 9a676b7 | 2009-07-29 02:57:25 +0000 | [diff] [blame] | 42 | command = ['valgrind', '-q', |
| 43 | '--tool=memcheck', '--leak-check=no', '--trace-children=yes', |
| 44 | '--error-exitcode=123'] + command |
Daniel Dunbar | 10aebbb | 2009-07-25 15:26:08 +0000 | [diff] [blame] | 45 | |
| 46 | p = subprocess.Popen(command, cwd=cwd, |
| 47 | stdin=subprocess.PIPE, |
| 48 | stdout=subprocess.PIPE, |
| 49 | stderr=subprocess.PIPE, |
Daniel Dunbar | 1db467f | 2009-07-31 05:54:17 +0000 | [diff] [blame] | 50 | env=cfg.environment) |
Daniel Dunbar | 10aebbb | 2009-07-25 15:26:08 +0000 | [diff] [blame] | 51 | out,err = p.communicate() |
| 52 | exitCode = p.wait() |
| 53 | |
| 54 | # Detect Ctrl-C in subprocess. |
| 55 | if exitCode == -signal.SIGINT: |
| 56 | raise KeyboardInterrupt |
| 57 | |
| 58 | return out, err, exitCode |
| 59 | |
Daniel Dunbar | a957d99 | 2009-07-25 14:46:05 +0000 | [diff] [blame] | 60 | import StringIO |
Daniel Dunbar | 5928ccd | 2009-08-01 04:06:02 +0000 | [diff] [blame^] | 61 | def runOneTest(cfg, testPath, tmpBase): |
Daniel Dunbar | a957d99 | 2009-07-25 14:46:05 +0000 | [diff] [blame] | 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 | 1db467f | 2009-07-31 05:54:17 +0000 | [diff] [blame] | 68 | Util.mkdir_p(os.path.dirname(tmpBase)) |
Daniel Dunbar | a957d99 | 2009-07-25 14:46:05 +0000 | [diff] [blame] | 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'), |
Daniel Dunbar | 5928ccd | 2009-08-01 04:06:02 +0000 | [diff] [blame^] | 76 | (' clang ', ' ' + cfg.clang + ' '), |
| 77 | (' clang-cc ', ' ' + cfg.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, |
Daniel Dunbar | 6779647 | 2009-07-27 19:01:13 +0000 | [diff] [blame] | 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 | 1db467f | 2009-07-31 05:54:17 +0000 | [diff] [blame] | 122 | out, err, exitCode = executeScript(cfg, script, scriptLines, |
Daniel Dunbar | 5928ccd | 2009-08-01 04:06:02 +0000 | [diff] [blame^] | 123 | os.path.dirname(testPath)) |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 124 | if xfailLines: |
Daniel Dunbar | a957d99 | 2009-07-25 14:46:05 +0000 | [diff] [blame] | 125 | ok = exitCode != 0 |
| 126 | status = (TestStatus.XPass, TestStatus.XFail)[ok] |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 127 | else: |
Daniel Dunbar | a957d99 | 2009-07-25 14:46:05 +0000 | [diff] [blame] | 128 | ok = exitCode == 0 |
| 129 | status = (TestStatus.Fail, TestStatus.Pass)[ok] |
| 130 | |
| 131 | if ok: |
| 132 | return (status,'') |
| 133 | |
| 134 | output = StringIO.StringIO() |
| 135 | print >>output, "Script:" |
| 136 | print >>output, "--" |
| 137 | print >>output, '\n'.join(scriptLines) |
| 138 | print >>output, "--" |
| 139 | print >>output, "Exit Code: %r" % exitCode |
| 140 | print >>output, "Command Output (stdout):" |
| 141 | print >>output, "--" |
| 142 | output.write(out) |
| 143 | print >>output, "--" |
| 144 | print >>output, "Command Output (stderr):" |
| 145 | print >>output, "--" |
| 146 | output.write(err) |
| 147 | print >>output, "--" |
| 148 | return (status, output.getvalue()) |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 149 | |
| 150 | def capture(args): |
Daniel Dunbar | 8fe83f1 | 2009-07-25 09:42:24 +0000 | [diff] [blame] | 151 | p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 152 | out,_ = p.communicate() |
| 153 | return out |
| 154 | |
Daniel Dunbar | 1db467f | 2009-07-31 05:54:17 +0000 | [diff] [blame] | 155 | def inferClang(cfg): |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 156 | # Determine which clang to use. |
| 157 | clang = os.getenv('CLANG') |
| 158 | |
| 159 | # If the user set clang in the environment, definitely use that and don't |
| 160 | # try to validate. |
| 161 | if clang: |
| 162 | return clang |
| 163 | |
| 164 | # Otherwise look in the path. |
Daniel Dunbar | 1db467f | 2009-07-31 05:54:17 +0000 | [diff] [blame] | 165 | clang = Util.which('clang', cfg.environment['PATH']) |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 166 | |
| 167 | if not clang: |
| 168 | print >>sys.stderr, "error: couldn't find 'clang' program, try setting CLANG in your environment" |
| 169 | sys.exit(1) |
| 170 | |
| 171 | return clang |
| 172 | |
Daniel Dunbar | 1db467f | 2009-07-31 05:54:17 +0000 | [diff] [blame] | 173 | def inferClangCC(cfg, clang): |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 174 | clangcc = os.getenv('CLANGCC') |
| 175 | |
| 176 | # If the user set clang in the environment, definitely use that and don't |
| 177 | # try to validate. |
| 178 | if clangcc: |
| 179 | return clangcc |
| 180 | |
| 181 | # Otherwise try adding -cc since we expect to be looking in a build |
| 182 | # directory. |
Daniel Dunbar | 8fe83f1 | 2009-07-25 09:42:24 +0000 | [diff] [blame] | 183 | if clang.endswith('.exe'): |
| 184 | clangccName = clang[:-4] + '-cc.exe' |
| 185 | else: |
| 186 | clangccName = clang + '-cc' |
Daniel Dunbar | 1db467f | 2009-07-31 05:54:17 +0000 | [diff] [blame] | 187 | clangcc = Util.which(clangccName, cfg.environment['PATH']) |
Daniel Dunbar | 69e07a7 | 2009-06-17 21:33:37 +0000 | [diff] [blame] | 188 | if not clangcc: |
| 189 | # Otherwise ask clang. |
| 190 | res = capture([clang, '-print-prog-name=clang-cc']) |
| 191 | res = res.strip() |
| 192 | if res and os.path.exists(res): |
| 193 | clangcc = res |
| 194 | |
| 195 | if not clangcc: |
| 196 | print >>sys.stderr, "error: couldn't find 'clang-cc' program, try setting CLANGCC in your environment" |
| 197 | sys.exit(1) |
| 198 | |
| 199 | return clangcc |
Daniel Dunbar | 6fc0bdf | 2009-03-06 22:20:40 +0000 | [diff] [blame] | 200 | |
Daniel Dunbar | df08489 | 2009-07-25 09:53:43 +0000 | [diff] [blame] | 201 | def getTestOutputBase(dir, testpath): |
Daniel Dunbar | fecdd00 | 2009-07-25 12:05:55 +0000 | [diff] [blame] | 202 | """getTestOutputBase(dir, testpath) - Get the full path for temporary files |
Daniel Dunbar | df08489 | 2009-07-25 09:53:43 +0000 | [diff] [blame] | 203 | corresponding to the given test path.""" |
| 204 | |
| 205 | # Form the output base out of the test parent directory name and the test |
| 206 | # name. FIXME: Find a better way to organize test results. |
| 207 | return os.path.join(dir, |
| 208 | os.path.basename(os.path.dirname(testpath)), |
| 209 | os.path.basename(testpath)) |