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)
12 def getGTestTests(self, path, litConfig, localConfig):
13 """getGTestTests(path) - [name]
15 Return the tests available in gtest executable.
18 path: String path to a gtest executable
19 litConfig: LitConfig instance
20 localConfig: TestingConfig instance"""
23 lines = Util.capture([path, '--gtest_list_tests'],
24 env=localConfig.environment).split('\n')
26 litConfig.error("unable to discover google-tests in %r" % path)
36 while ln[index*2:index*2+2] == ' ':
38 while len(nested_tests) > index:
43 nested_tests.append(ln)
45 yield ''.join(nested_tests) + ln
47 def getTestsInDirectory(self, testSuite, path_in_suite,
48 litConfig, localConfig):
49 source_path = testSuite.getSourcePath(path_in_suite)
50 for filename in os.listdir(source_path):
51 # Check for the one subdirectory (build directory) tests will be in.
52 if filename != self.test_sub_dir:
55 filepath = os.path.join(source_path, filename)
56 for subfilename in os.listdir(filepath):
57 if subfilename.endswith(self.test_suffix):
58 execpath = os.path.join(filepath, subfilename)
60 # Discover the tests in this executable.
61 for name in self.getGTestTests(execpath, litConfig,
63 testPath = path_in_suite + (filename, subfilename, name)
64 yield Test.Test(testSuite, testPath, localConfig)
66 def execute(self, test, litConfig):
67 testPath,testName = os.path.split(test.getSourcePath())
68 while not os.path.exists(testPath):
69 # Handle GTest parametrized and typed tests, whose name includes
71 testPath, namePrefix = os.path.split(testPath)
72 testName = os.path.join(namePrefix, testName)
74 cmd = [testPath, '--gtest_filter=' + testName]
75 if litConfig.useValgrind:
76 cmd = litConfig.valgrindArgs + cmd
78 out, err, exitCode = TestRunner.executeCommand(
79 cmd, env=test.config.environment)
84 return Test.FAIL, out + err
88 class FileBasedTest(object):
89 def getTestsInDirectory(self, testSuite, path_in_suite,
90 litConfig, localConfig):
91 source_path = testSuite.getSourcePath(path_in_suite)
92 for filename in os.listdir(source_path):
94 if filename.startswith('.'):
97 filepath = os.path.join(source_path, filename)
98 if not os.path.isdir(filepath):
99 base,ext = os.path.splitext(filename)
100 if ext in localConfig.suffixes:
101 yield Test.Test(testSuite, path_in_suite + (filename,),
104 class ShTest(FileBasedTest):
105 def __init__(self, execute_external = False):
106 self.execute_external = execute_external
108 def execute(self, test, litConfig):
109 return TestRunner.executeShTest(test, litConfig,
110 self.execute_external)
112 class TclTest(FileBasedTest):
113 def execute(self, test, litConfig):
114 return TestRunner.executeTclTest(test, litConfig)
121 class OneCommandPerFileTest:
122 # FIXME: Refactor into generic test for running some command on a directory
125 def __init__(self, command, dir, recursive=False,
126 pattern=".*", useTempInput=False):
127 if isinstance(command, str):
128 self.command = [command]
130 self.command = list(command)
132 self.recursive = bool(recursive)
133 self.pattern = re.compile(pattern)
134 self.useTempInput = useTempInput
136 def getTestsInDirectory(self, testSuite, path_in_suite,
137 litConfig, localConfig):
138 for dirname,subdirs,filenames in os.walk(self.dir):
139 if not self.recursive:
142 subdirs[:] = [d for d in subdirs
144 d not in localConfig.excludes)]
146 for filename in filenames:
147 if (filename.startswith('.') or
148 not self.pattern.match(filename) or
149 filename in localConfig.excludes):
152 path = os.path.join(dirname,filename)
153 suffix = path[len(self.dir):]
154 if suffix.startswith(os.sep):
156 test = Test.Test(testSuite,
157 path_in_suite + tuple(suffix.split(os.sep)),
160 test.source_path = path
163 def createTempInput(self, tmp, test):
166 def execute(self, test, litConfig):
167 if test.config.unsupported:
168 return (Test.UNSUPPORTED, 'Test is unsupported')
170 cmd = list(self.command)
172 # If using temp input, create a temporary file and hand it to the
174 if self.useTempInput:
175 tmp = tempfile.NamedTemporaryFile(suffix='.cpp')
176 self.createTempInput(tmp, test)
180 cmd.append(test.source_path)
182 out, err, exitCode = TestRunner.executeCommand(cmd)
185 if not exitCode and not diags.strip():
188 # Try to include some useful information.
189 report = """Command: %s\n""" % ' '.join(["'%s'" % a
191 if self.useTempInput:
192 report += """Temporary File: %s\n""" % tmp.name
193 report += "--\n%s--\n""" % open(tmp.name).read()
194 report += """Output:\n--\n%s--""" % diags
196 return Test.FAIL, report
198 class SyntaxCheckTest(OneCommandPerFileTest):
199 def __init__(self, compiler, dir, extra_cxx_args=[], *args, **kwargs):
200 cmd = [compiler, '-x', 'c++', '-fsyntax-only'] + extra_cxx_args
201 OneCommandPerFileTest.__init__(self, cmd, dir,
202 useTempInput=1, *args, **kwargs)
204 def createTempInput(self, tmp, test):
205 print >>tmp, '#include "%s"' % test.source_path