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()) |
| 56 | |
| 57 | cmd = [testPath, '--gtest_filter=' + testName] |
| 58 | out, err, exitCode = TestRunner.executeCommand(cmd) |
| 59 | |
| 60 | if not exitCode: |
| 61 | return Test.PASS,'' |
| 62 | |
| 63 | return Test.FAIL, out + err |
| 64 | |
| 65 | ### |
| 66 | |
| 67 | class FileBasedTest(object): |
| 68 | def getTestsInDirectory(self, testSuite, path_in_suite, |
| 69 | litConfig, localConfig): |
| 70 | source_path = testSuite.getSourcePath(path_in_suite) |
| 71 | for filename in os.listdir(source_path): |
| 72 | filepath = os.path.join(source_path, filename) |
| 73 | if not os.path.isdir(filepath): |
| 74 | base,ext = os.path.splitext(filename) |
| 75 | if ext in localConfig.suffixes: |
| 76 | yield Test.Test(testSuite, path_in_suite + (filename,), |
| 77 | localConfig) |
| 78 | |
| 79 | class ShTest(FileBasedTest): |
| 80 | def __init__(self, execute_external = False, require_and_and = False): |
| 81 | self.execute_external = execute_external |
| 82 | self.require_and_and = require_and_and |
| 83 | |
| 84 | def execute(self, test, litConfig): |
| 85 | return TestRunner.executeShTest(test, litConfig, |
| 86 | self.execute_external, |
| 87 | self.require_and_and) |
| 88 | |
| 89 | class TclTest(FileBasedTest): |
| 90 | def execute(self, test, litConfig): |
| 91 | return TestRunner.executeTclTest(test, litConfig) |
Daniel Dunbar | 7c74866 | 2009-09-16 01:34:52 +0000 | [diff] [blame^] | 92 | |
| 93 | ### |
| 94 | |
| 95 | import re |
| 96 | import tempfile |
| 97 | |
| 98 | class SyntaxCheckTest: |
| 99 | # FIXME: Refactor into generic test for running some command on a directory |
| 100 | # of inputs. |
| 101 | |
| 102 | def __init__(self, compiler, dir, recursive, pattern, extra_cxx_args=[]): |
| 103 | self.compiler = str(compiler) |
| 104 | self.dir = str(dir) |
| 105 | self.recursive = bool(recursive) |
| 106 | self.pattern = re.compile(pattern) |
| 107 | self.extra_cxx_args = list(extra_cxx_args) |
| 108 | |
| 109 | def getTestsInDirectory(self, testSuite, path_in_suite, |
| 110 | litConfig, localConfig): |
| 111 | for dirname,subdirs,filenames in os.walk(self.dir): |
| 112 | if not self.recursive: |
| 113 | subdirs[:] = [] |
| 114 | |
| 115 | for filename in filenames: |
| 116 | if (not self.pattern.match(filename) or |
| 117 | filename in localConfig.excludes): |
| 118 | continue |
| 119 | |
| 120 | path = os.path.join(dirname,filename) |
| 121 | suffix = path[len(self.dir):] |
| 122 | if suffix.startswith(os.sep): |
| 123 | suffix = suffix[1:] |
| 124 | test = Test.Test(testSuite, |
| 125 | path_in_suite + tuple(suffix.split(os.sep)), |
| 126 | localConfig) |
| 127 | # FIXME: Hack? |
| 128 | test.source_path = path |
| 129 | yield test |
| 130 | |
| 131 | def execute(self, test, litConfig): |
| 132 | tmp = tempfile.NamedTemporaryFile(suffix='.cpp') |
| 133 | print >>tmp, '#include "%s"' % test.source_path |
| 134 | tmp.flush() |
| 135 | |
| 136 | cmd = [self.compiler, '-x', 'c++', '-fsyntax-only', tmp.name] |
| 137 | cmd.extend(self.extra_cxx_args) |
| 138 | out, err, exitCode = TestRunner.executeCommand(cmd) |
| 139 | |
| 140 | diags = out + err |
| 141 | if not exitCode and not diags.strip(): |
| 142 | return Test.PASS,'' |
| 143 | |
| 144 | return Test.FAIL, diags |