Daniel Dunbar | 00a4244 | 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 | 6bccb4c | 2009-10-18 02:05:42 +0000 | [diff] [blame] | 56 | if not os.path.exists(testPath): |
| 57 | # Handle GTest typed tests, whose name includes a '/'. |
| 58 | testPath, namePrefix = os.path.split(testPath) |
| 59 | testName = os.path.join(namePrefix, testName) |
Daniel Dunbar | 00a4244 | 2009-09-14 02:38:46 +0000 | [diff] [blame] | 60 | |
| 61 | cmd = [testPath, '--gtest_filter=' + testName] |
| 62 | out, err, exitCode = TestRunner.executeCommand(cmd) |
| 63 | |
| 64 | if not exitCode: |
| 65 | return Test.PASS,'' |
| 66 | |
| 67 | return Test.FAIL, out + err |
| 68 | |
| 69 | ### |
| 70 | |
| 71 | class FileBasedTest(object): |
| 72 | def getTestsInDirectory(self, testSuite, path_in_suite, |
| 73 | litConfig, localConfig): |
| 74 | source_path = testSuite.getSourcePath(path_in_suite) |
| 75 | for filename in os.listdir(source_path): |
| 76 | filepath = os.path.join(source_path, filename) |
| 77 | if not os.path.isdir(filepath): |
| 78 | base,ext = os.path.splitext(filename) |
| 79 | if ext in localConfig.suffixes: |
| 80 | yield Test.Test(testSuite, path_in_suite + (filename,), |
| 81 | localConfig) |
| 82 | |
| 83 | class ShTest(FileBasedTest): |
Daniel Dunbar | ee504b8 | 2009-11-08 09:07:13 +0000 | [diff] [blame] | 84 | def __init__(self, execute_external = False): |
Daniel Dunbar | 00a4244 | 2009-09-14 02:38:46 +0000 | [diff] [blame] | 85 | self.execute_external = execute_external |
Daniel Dunbar | 00a4244 | 2009-09-14 02:38:46 +0000 | [diff] [blame] | 86 | |
| 87 | def execute(self, test, litConfig): |
| 88 | return TestRunner.executeShTest(test, litConfig, |
Daniel Dunbar | ee504b8 | 2009-11-08 09:07:13 +0000 | [diff] [blame] | 89 | self.execute_external) |
Daniel Dunbar | 00a4244 | 2009-09-14 02:38:46 +0000 | [diff] [blame] | 90 | |
| 91 | class TclTest(FileBasedTest): |
| 92 | def execute(self, test, litConfig): |
| 93 | return TestRunner.executeTclTest(test, litConfig) |
Daniel Dunbar | 7c74866 | 2009-09-16 01:34:52 +0000 | [diff] [blame] | 94 | |
| 95 | ### |
| 96 | |
| 97 | import re |
| 98 | import tempfile |
| 99 | |
Daniel Dunbar | 2cb097d | 2009-11-15 08:10:29 +0000 | [diff] [blame^] | 100 | class OneCommandPerFileTest: |
Daniel Dunbar | 7c74866 | 2009-09-16 01:34:52 +0000 | [diff] [blame] | 101 | # FIXME: Refactor into generic test for running some command on a directory |
| 102 | # of inputs. |
| 103 | |
Daniel Dunbar | 2cb097d | 2009-11-15 08:10:29 +0000 | [diff] [blame^] | 104 | def __init__(self, command, dir, recursive=False, |
| 105 | pattern=".*", useTempInput=False): |
| 106 | if isinstance(command, str): |
| 107 | self.command = [command] |
| 108 | else: |
| 109 | self.command = list(command) |
Daniel Dunbar | 7c74866 | 2009-09-16 01:34:52 +0000 | [diff] [blame] | 110 | self.dir = str(dir) |
| 111 | self.recursive = bool(recursive) |
| 112 | self.pattern = re.compile(pattern) |
Daniel Dunbar | 2cb097d | 2009-11-15 08:10:29 +0000 | [diff] [blame^] | 113 | self.useTempInput = useTempInput |
Daniel Dunbar | 7c74866 | 2009-09-16 01:34:52 +0000 | [diff] [blame] | 114 | |
| 115 | def getTestsInDirectory(self, testSuite, path_in_suite, |
| 116 | litConfig, localConfig): |
| 117 | for dirname,subdirs,filenames in os.walk(self.dir): |
| 118 | if not self.recursive: |
| 119 | subdirs[:] = [] |
| 120 | |
Daniel Dunbar | 048bac3 | 2009-11-15 07:22:58 +0000 | [diff] [blame] | 121 | if dirname == '.svn' or dirname in localConfig.excludes: |
Douglas Gregor | 489b833 | 2009-11-05 22:58:04 +0000 | [diff] [blame] | 122 | continue |
Daniel Dunbar | 048bac3 | 2009-11-15 07:22:58 +0000 | [diff] [blame] | 123 | |
Daniel Dunbar | 7c74866 | 2009-09-16 01:34:52 +0000 | [diff] [blame] | 124 | for filename in filenames: |
| 125 | if (not self.pattern.match(filename) or |
| 126 | filename in localConfig.excludes): |
| 127 | continue |
| 128 | |
| 129 | path = os.path.join(dirname,filename) |
| 130 | suffix = path[len(self.dir):] |
| 131 | if suffix.startswith(os.sep): |
| 132 | suffix = suffix[1:] |
| 133 | test = Test.Test(testSuite, |
| 134 | path_in_suite + tuple(suffix.split(os.sep)), |
| 135 | localConfig) |
| 136 | # FIXME: Hack? |
| 137 | test.source_path = path |
| 138 | yield test |
| 139 | |
Daniel Dunbar | 2cb097d | 2009-11-15 08:10:29 +0000 | [diff] [blame^] | 140 | def createTempInput(self, tmp, test): |
| 141 | abstract |
| 142 | |
Daniel Dunbar | 7c74866 | 2009-09-16 01:34:52 +0000 | [diff] [blame] | 143 | def execute(self, test, litConfig): |
Daniel Dunbar | 048bac3 | 2009-11-15 07:22:58 +0000 | [diff] [blame] | 144 | if test.config.unsupported: |
| 145 | return (Test.UNSUPPORTED, 'Test is unsupported') |
| 146 | |
Daniel Dunbar | 2cb097d | 2009-11-15 08:10:29 +0000 | [diff] [blame^] | 147 | cmd = list(self.command) |
Daniel Dunbar | 7c74866 | 2009-09-16 01:34:52 +0000 | [diff] [blame] | 148 | |
Daniel Dunbar | 2cb097d | 2009-11-15 08:10:29 +0000 | [diff] [blame^] | 149 | # If using temp input, create a temporary file and hand it to the |
| 150 | # subclass. |
| 151 | if self.useTempInput: |
| 152 | tmp = tempfile.NamedTemporaryFile(suffix='.cpp') |
| 153 | self.createTempInput(tmp, test) |
| 154 | tmp.flush() |
| 155 | cmd.append(tmp.name) |
| 156 | else: |
| 157 | cmd.append(test.source_path) |
| 158 | |
Daniel Dunbar | 7c74866 | 2009-09-16 01:34:52 +0000 | [diff] [blame] | 159 | out, err, exitCode = TestRunner.executeCommand(cmd) |
| 160 | |
| 161 | diags = out + err |
| 162 | if not exitCode and not diags.strip(): |
| 163 | return Test.PASS,'' |
| 164 | |
Daniel Dunbar | 2cb097d | 2009-11-15 08:10:29 +0000 | [diff] [blame^] | 165 | # Try to include some useful information. |
| 166 | report = """Command: %s\n""" % ' '.join(["'%s'" % a |
| 167 | for a in cmd]) |
| 168 | if self.useTempInput: |
| 169 | report += """Temporary File: %s\n""" % tmp.name |
| 170 | report += "--\n%s--\n""" % open(tmp.name).read() |
| 171 | report += """Output:\n--\n%s--""" % diags |
| 172 | |
| 173 | return Test.FAIL, report |
| 174 | |
| 175 | class SyntaxCheckTest(OneCommandPerFileTest): |
| 176 | def __init__(self, compiler, dir, extra_cxx_args=[], *args, **kwargs): |
| 177 | cmd = [compiler, '-x', 'c++', '-fsyntax-only'] + extra_cxx_args |
| 178 | OneCommandPerFileTest.__init__(self, cmd, dir, |
| 179 | useTempInput=1, *args, **kwargs) |
| 180 | |
| 181 | def createTempInput(self, tmp, test): |
| 182 | print >>tmp, '#include "%s"' % test.source_path |