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 | |
| 100 | class SyntaxCheckTest: |
| 101 | # FIXME: Refactor into generic test for running some command on a directory |
| 102 | # of inputs. |
| 103 | |
Douglas Gregor | 489b833 | 2009-11-05 22:58:04 +0000 | [diff] [blame] | 104 | def __init__(self, compiler, dir, recursive, pattern, excludes=[], |
| 105 | extra_cxx_args=[]): |
Daniel Dunbar | 7c74866 | 2009-09-16 01:34:52 +0000 | [diff] [blame] | 106 | self.compiler = str(compiler) |
| 107 | self.dir = str(dir) |
| 108 | self.recursive = bool(recursive) |
| 109 | self.pattern = re.compile(pattern) |
Douglas Gregor | 489b833 | 2009-11-05 22:58:04 +0000 | [diff] [blame] | 110 | self.excludes = list(excludes) |
Daniel Dunbar | 7c74866 | 2009-09-16 01:34:52 +0000 | [diff] [blame] | 111 | self.extra_cxx_args = list(extra_cxx_args) |
| 112 | |
| 113 | def getTestsInDirectory(self, testSuite, path_in_suite, |
| 114 | litConfig, localConfig): |
| 115 | for dirname,subdirs,filenames in os.walk(self.dir): |
| 116 | if not self.recursive: |
| 117 | subdirs[:] = [] |
| 118 | |
Douglas Gregor | 489b833 | 2009-11-05 22:58:04 +0000 | [diff] [blame] | 119 | if dirname.__contains__('.svn'): |
| 120 | continue |
| 121 | |
Daniel Dunbar | 7c74866 | 2009-09-16 01:34:52 +0000 | [diff] [blame] | 122 | for filename in filenames: |
| 123 | if (not self.pattern.match(filename) or |
| 124 | filename in localConfig.excludes): |
| 125 | continue |
Douglas Gregor | 489b833 | 2009-11-05 22:58:04 +0000 | [diff] [blame] | 126 | |
| 127 | # Skip any files that were specifically excluded. |
| 128 | excluded = False |
| 129 | for exclude in self.excludes: |
| 130 | if filename.__contains__(exclude): |
| 131 | excluded = True |
| 132 | break |
| 133 | |
| 134 | if excluded: |
| 135 | continue |
Daniel Dunbar | 7c74866 | 2009-09-16 01:34:52 +0000 | [diff] [blame] | 136 | |
| 137 | path = os.path.join(dirname,filename) |
| 138 | suffix = path[len(self.dir):] |
| 139 | if suffix.startswith(os.sep): |
| 140 | suffix = suffix[1:] |
| 141 | test = Test.Test(testSuite, |
| 142 | path_in_suite + tuple(suffix.split(os.sep)), |
| 143 | localConfig) |
| 144 | # FIXME: Hack? |
| 145 | test.source_path = path |
| 146 | yield test |
| 147 | |
| 148 | def execute(self, test, litConfig): |
| 149 | tmp = tempfile.NamedTemporaryFile(suffix='.cpp') |
| 150 | print >>tmp, '#include "%s"' % test.source_path |
| 151 | tmp.flush() |
| 152 | |
| 153 | cmd = [self.compiler, '-x', 'c++', '-fsyntax-only', tmp.name] |
| 154 | cmd.extend(self.extra_cxx_args) |
| 155 | out, err, exitCode = TestRunner.executeCommand(cmd) |
| 156 | |
| 157 | diags = out + err |
| 158 | if not exitCode and not diags.strip(): |
| 159 | return Test.PASS,'' |
| 160 | |
| 161 | return Test.FAIL, diags |