Daniel Dunbar | 54ec232 | 2009-09-14 02:38:46 +0000 | [diff] [blame] | 1 | import os |
| 2 | |
| 3 | import Test |
| 4 | import TestRunner |
| 5 | import Util |
| 6 | |
| 7 | class GoogleTest(object): |
| 8 | def __init__(self, test_sub_dir, test_suffix): |
| 9 | self.test_sub_dir = str(test_sub_dir) |
| 10 | self.test_suffix = str(test_suffix) |
| 11 | |
| 12 | def getGTestTests(self, path): |
| 13 | """getGTestTests(path) - [name] |
| 14 | |
| 15 | Return the tests available in gtest executable.""" |
| 16 | |
| 17 | lines = Util.capture([path, '--gtest_list_tests']).split('\n') |
| 18 | nested_tests = [] |
| 19 | for ln in lines: |
| 20 | if not ln.strip(): |
| 21 | continue |
| 22 | |
| 23 | prefix = '' |
| 24 | index = 0 |
| 25 | while ln[index*2:index*2+2] == ' ': |
| 26 | index += 1 |
| 27 | while len(nested_tests) > index: |
| 28 | nested_tests.pop() |
| 29 | |
| 30 | ln = ln[index*2:] |
| 31 | if ln.endswith('.'): |
| 32 | nested_tests.append(ln) |
| 33 | else: |
| 34 | yield ''.join(nested_tests) + ln |
| 35 | |
| 36 | def getTestsInDirectory(self, testSuite, path_in_suite, |
| 37 | litConfig, localConfig): |
| 38 | source_path = testSuite.getSourcePath(path_in_suite) |
| 39 | for filename in os.listdir(source_path): |
| 40 | # Check for the one subdirectory (build directory) tests will be in. |
| 41 | if filename != self.test_sub_dir: |
| 42 | continue |
| 43 | |
| 44 | filepath = os.path.join(source_path, filename) |
| 45 | for subfilename in os.listdir(filepath): |
| 46 | if subfilename.endswith(self.test_suffix): |
| 47 | execpath = os.path.join(filepath, subfilename) |
| 48 | |
| 49 | # Discover the tests in this executable. |
| 50 | for name in self.getGTestTests(execpath): |
| 51 | testPath = path_in_suite + (filename, subfilename, name) |
| 52 | yield Test.Test(testSuite, testPath, localConfig) |
| 53 | |
| 54 | def execute(self, test, litConfig): |
| 55 | testPath,testName = os.path.split(test.getSourcePath()) |
Jeffrey Yasskin | 10d3604 | 2009-11-16 22:41:33 +0000 | [diff] [blame^] | 56 | while not os.path.exists(testPath): |
| 57 | # Handle GTest parametrized and typed tests, whose name includes |
| 58 | # some '/'s. |
Jeffrey Yasskin | 35ec32d | 2009-10-18 02:05:42 +0000 | [diff] [blame] | 59 | testPath, namePrefix = os.path.split(testPath) |
| 60 | testName = os.path.join(namePrefix, testName) |
Daniel Dunbar | 54ec232 | 2009-09-14 02:38:46 +0000 | [diff] [blame] | 61 | |
| 62 | cmd = [testPath, '--gtest_filter=' + testName] |
| 63 | out, err, exitCode = TestRunner.executeCommand(cmd) |
| 64 | |
| 65 | if not exitCode: |
| 66 | return Test.PASS,'' |
| 67 | |
| 68 | return Test.FAIL, out + err |
| 69 | |
| 70 | ### |
| 71 | |
| 72 | class FileBasedTest(object): |
| 73 | def getTestsInDirectory(self, testSuite, path_in_suite, |
| 74 | litConfig, localConfig): |
| 75 | source_path = testSuite.getSourcePath(path_in_suite) |
| 76 | for filename in os.listdir(source_path): |
| 77 | filepath = os.path.join(source_path, filename) |
| 78 | if not os.path.isdir(filepath): |
| 79 | base,ext = os.path.splitext(filename) |
| 80 | if ext in localConfig.suffixes: |
| 81 | yield Test.Test(testSuite, path_in_suite + (filename,), |
| 82 | localConfig) |
| 83 | |
| 84 | class ShTest(FileBasedTest): |
Daniel Dunbar | 6ff773b | 2009-11-08 09:07:13 +0000 | [diff] [blame] | 85 | def __init__(self, execute_external = False): |
Daniel Dunbar | 54ec232 | 2009-09-14 02:38:46 +0000 | [diff] [blame] | 86 | self.execute_external = execute_external |
Daniel Dunbar | 54ec232 | 2009-09-14 02:38:46 +0000 | [diff] [blame] | 87 | |
| 88 | def execute(self, test, litConfig): |
| 89 | return TestRunner.executeShTest(test, litConfig, |
Daniel Dunbar | 6ff773b | 2009-11-08 09:07:13 +0000 | [diff] [blame] | 90 | self.execute_external) |
Daniel Dunbar | 54ec232 | 2009-09-14 02:38:46 +0000 | [diff] [blame] | 91 | |
| 92 | class TclTest(FileBasedTest): |
| 93 | def execute(self, test, litConfig): |
| 94 | return TestRunner.executeTclTest(test, litConfig) |
Daniel Dunbar | 8d2aa38 | 2009-09-16 01:34:52 +0000 | [diff] [blame] | 95 | |
| 96 | ### |
| 97 | |
| 98 | import re |
| 99 | import tempfile |
| 100 | |
Daniel Dunbar | 9a61bd50 | 2009-11-15 08:10:29 +0000 | [diff] [blame] | 101 | class OneCommandPerFileTest: |
Daniel Dunbar | 8d2aa38 | 2009-09-16 01:34:52 +0000 | [diff] [blame] | 102 | # FIXME: Refactor into generic test for running some command on a directory |
| 103 | # of inputs. |
| 104 | |
Daniel Dunbar | 9a61bd50 | 2009-11-15 08:10:29 +0000 | [diff] [blame] | 105 | def __init__(self, command, dir, recursive=False, |
| 106 | pattern=".*", useTempInput=False): |
| 107 | if isinstance(command, str): |
| 108 | self.command = [command] |
| 109 | else: |
| 110 | self.command = list(command) |
Daniel Dunbar | 8d2aa38 | 2009-09-16 01:34:52 +0000 | [diff] [blame] | 111 | self.dir = str(dir) |
| 112 | self.recursive = bool(recursive) |
| 113 | self.pattern = re.compile(pattern) |
Daniel Dunbar | 9a61bd50 | 2009-11-15 08:10:29 +0000 | [diff] [blame] | 114 | self.useTempInput = useTempInput |
Daniel Dunbar | 8d2aa38 | 2009-09-16 01:34:52 +0000 | [diff] [blame] | 115 | |
| 116 | def getTestsInDirectory(self, testSuite, path_in_suite, |
| 117 | litConfig, localConfig): |
| 118 | for dirname,subdirs,filenames in os.walk(self.dir): |
| 119 | if not self.recursive: |
| 120 | subdirs[:] = [] |
| 121 | |
Daniel Dunbar | d476471 | 2009-11-15 07:22:58 +0000 | [diff] [blame] | 122 | if dirname == '.svn' or dirname in localConfig.excludes: |
Douglas Gregor | 86a947d | 2009-11-05 22:58:04 +0000 | [diff] [blame] | 123 | continue |
Daniel Dunbar | d476471 | 2009-11-15 07:22:58 +0000 | [diff] [blame] | 124 | |
Daniel Dunbar | 8d2aa38 | 2009-09-16 01:34:52 +0000 | [diff] [blame] | 125 | for filename in filenames: |
| 126 | if (not self.pattern.match(filename) or |
| 127 | filename in localConfig.excludes): |
| 128 | continue |
| 129 | |
| 130 | path = os.path.join(dirname,filename) |
| 131 | suffix = path[len(self.dir):] |
| 132 | if suffix.startswith(os.sep): |
| 133 | suffix = suffix[1:] |
| 134 | test = Test.Test(testSuite, |
| 135 | path_in_suite + tuple(suffix.split(os.sep)), |
| 136 | localConfig) |
| 137 | # FIXME: Hack? |
| 138 | test.source_path = path |
| 139 | yield test |
| 140 | |
Daniel Dunbar | 9a61bd50 | 2009-11-15 08:10:29 +0000 | [diff] [blame] | 141 | def createTempInput(self, tmp, test): |
| 142 | abstract |
| 143 | |
Daniel Dunbar | 8d2aa38 | 2009-09-16 01:34:52 +0000 | [diff] [blame] | 144 | def execute(self, test, litConfig): |
Daniel Dunbar | d476471 | 2009-11-15 07:22:58 +0000 | [diff] [blame] | 145 | if test.config.unsupported: |
| 146 | return (Test.UNSUPPORTED, 'Test is unsupported') |
| 147 | |
Daniel Dunbar | 9a61bd50 | 2009-11-15 08:10:29 +0000 | [diff] [blame] | 148 | cmd = list(self.command) |
Daniel Dunbar | 8d2aa38 | 2009-09-16 01:34:52 +0000 | [diff] [blame] | 149 | |
Daniel Dunbar | 9a61bd50 | 2009-11-15 08:10:29 +0000 | [diff] [blame] | 150 | # If using temp input, create a temporary file and hand it to the |
| 151 | # subclass. |
| 152 | if self.useTempInput: |
| 153 | tmp = tempfile.NamedTemporaryFile(suffix='.cpp') |
| 154 | self.createTempInput(tmp, test) |
| 155 | tmp.flush() |
| 156 | cmd.append(tmp.name) |
| 157 | else: |
| 158 | cmd.append(test.source_path) |
| 159 | |
Daniel Dunbar | 8d2aa38 | 2009-09-16 01:34:52 +0000 | [diff] [blame] | 160 | out, err, exitCode = TestRunner.executeCommand(cmd) |
| 161 | |
| 162 | diags = out + err |
| 163 | if not exitCode and not diags.strip(): |
| 164 | return Test.PASS,'' |
| 165 | |
Daniel Dunbar | 9a61bd50 | 2009-11-15 08:10:29 +0000 | [diff] [blame] | 166 | # Try to include some useful information. |
| 167 | report = """Command: %s\n""" % ' '.join(["'%s'" % a |
| 168 | for a in cmd]) |
| 169 | if self.useTempInput: |
| 170 | report += """Temporary File: %s\n""" % tmp.name |
| 171 | report += "--\n%s--\n""" % open(tmp.name).read() |
| 172 | report += """Output:\n--\n%s--""" % diags |
| 173 | |
| 174 | return Test.FAIL, report |
| 175 | |
| 176 | class SyntaxCheckTest(OneCommandPerFileTest): |
| 177 | def __init__(self, compiler, dir, extra_cxx_args=[], *args, **kwargs): |
| 178 | cmd = [compiler, '-x', 'c++', '-fsyntax-only'] + extra_cxx_args |
| 179 | OneCommandPerFileTest.__init__(self, cmd, dir, |
| 180 | useTempInput=1, *args, **kwargs) |
| 181 | |
| 182 | def createTempInput(self, tmp, test): |
| 183 | print >>tmp, '#include "%s"' % test.source_path |