8 kIsWindows = sys.platform in ['win32', 'cygwin']
10 class GoogleTest(object):
11 def __init__(self, test_sub_dir, test_suffix):
12 self.test_sub_dir = os.path.normcase(str(test_sub_dir)).split(';')
13 self.test_suffix = str(test_suffix)
15 # On Windows, assume tests will also end in '.exe'.
17 self.test_suffix += '.exe'
19 def getGTestTests(self, path, litConfig, localConfig):
20 """getGTestTests(path) - [name]
22 Return the tests available in gtest executable.
25 path: String path to a gtest executable
26 litConfig: LitConfig instance
27 localConfig: TestingConfig instance"""
30 lines = Util.capture([path, '--gtest_list_tests'],
31 env=localConfig.environment)
33 lines = lines.replace('\r', '')
34 lines = lines.split('\n')
36 litConfig.error("unable to discover google-tests in %r" % path)
46 while ln[index*2:index*2+2] == ' ':
48 while len(nested_tests) > index:
53 nested_tests.append(ln)
55 yield ''.join(nested_tests) + ln
57 def getTestsInExecutable(self, testSuite, path_in_suite, execpath,
58 litConfig, localConfig):
59 if not execpath.endswith(self.test_suffix):
61 (dirname, basename) = os.path.split(execpath)
62 # Discover the tests in this executable.
63 for testname in self.getGTestTests(execpath, litConfig, localConfig):
64 testPath = path_in_suite + (dirname, basename, testname)
65 yield Test.Test(testSuite, testPath, localConfig)
67 def getTestsInDirectory(self, testSuite, path_in_suite,
68 litConfig, localConfig):
69 source_path = testSuite.getSourcePath(path_in_suite)
70 for filename in os.listdir(source_path):
71 filepath = os.path.join(source_path, filename)
72 if os.path.isdir(filepath):
73 # Iterate over executables in a directory.
74 if not os.path.normcase(filename) in self.test_sub_dir:
76 for subfilename in os.listdir(filepath):
77 execpath = os.path.join(filepath, subfilename)
78 for test in self.getTestsInExecutable(
79 testSuite, path_in_suite, execpath,
80 litConfig, localConfig):
82 elif ('.' in self.test_sub_dir):
83 for test in self.getTestsInExecutable(
84 testSuite, path_in_suite, filepath,
85 litConfig, localConfig):
88 def execute(self, test, litConfig):
89 testPath,testName = os.path.split(test.getSourcePath())
90 while not os.path.exists(testPath):
91 # Handle GTest parametrized and typed tests, whose name includes
93 testPath, namePrefix = os.path.split(testPath)
94 testName = os.path.join(namePrefix, testName)
96 cmd = [testPath, '--gtest_filter=' + testName]
97 if litConfig.useValgrind:
98 cmd = litConfig.valgrindArgs + cmd
100 out, err, exitCode = TestRunner.executeCommand(
101 cmd, env=test.config.environment)
106 return Test.FAIL, out + err
110 class FileBasedTest(object):
111 def getTestsInDirectory(self, testSuite, path_in_suite,
112 litConfig, localConfig):
113 source_path = testSuite.getSourcePath(path_in_suite)
114 for filename in os.listdir(source_path):
115 # Ignore dot files and excluded tests.
116 if (filename.startswith('.') or
117 filename in localConfig.excludes):
120 filepath = os.path.join(source_path, filename)
121 if not os.path.isdir(filepath):
122 base,ext = os.path.splitext(filename)
123 if ext in localConfig.suffixes:
124 yield Test.Test(testSuite, path_in_suite + (filename,),
127 class ShTest(FileBasedTest):
128 def __init__(self, execute_external = False):
129 self.execute_external = execute_external
131 def execute(self, test, litConfig):
132 return TestRunner.executeShTest(test, litConfig,
133 self.execute_external)
140 class OneCommandPerFileTest:
141 # FIXME: Refactor into generic test for running some command on a directory
144 def __init__(self, command, dir, recursive=False,
145 pattern=".*", useTempInput=False):
146 if isinstance(command, str):
147 self.command = [command]
149 self.command = list(command)
153 self.recursive = bool(recursive)
154 self.pattern = re.compile(pattern)
155 self.useTempInput = useTempInput
157 def getTestsInDirectory(self, testSuite, path_in_suite,
158 litConfig, localConfig):
161 dir = testSuite.getSourcePath(path_in_suite)
163 for dirname,subdirs,filenames in os.walk(dir):
164 if not self.recursive:
167 subdirs[:] = [d for d in subdirs
169 d not in localConfig.excludes)]
171 for filename in filenames:
172 if (filename.startswith('.') or
173 not self.pattern.match(filename) or
174 filename in localConfig.excludes):
177 path = os.path.join(dirname,filename)
178 suffix = path[len(dir):]
179 if suffix.startswith(os.sep):
181 test = Test.Test(testSuite,
182 path_in_suite + tuple(suffix.split(os.sep)),
185 test.source_path = path
188 def createTempInput(self, tmp, test):
191 def execute(self, test, litConfig):
192 if test.config.unsupported:
193 return (Test.UNSUPPORTED, 'Test is unsupported')
195 cmd = list(self.command)
197 # If using temp input, create a temporary file and hand it to the
199 if self.useTempInput:
200 tmp = tempfile.NamedTemporaryFile(suffix='.cpp')
201 self.createTempInput(tmp, test)
204 elif hasattr(test, 'source_path'):
205 cmd.append(test.source_path)
207 cmd.append(test.getSourcePath())
209 out, err, exitCode = TestRunner.executeCommand(cmd)
212 if not exitCode and not diags.strip():
215 # Try to include some useful information.
216 report = """Command: %s\n""" % ' '.join(["'%s'" % a
218 if self.useTempInput:
219 report += """Temporary File: %s\n""" % tmp.name
220 report += "--\n%s--\n""" % open(tmp.name).read()
221 report += """Output:\n--\n%s--""" % diags
223 return Test.FAIL, report
225 class SyntaxCheckTest(OneCommandPerFileTest):
226 def __init__(self, compiler, dir, extra_cxx_args=[], *args, **kwargs):
227 cmd = [compiler, '-x', 'c++', '-fsyntax-only'] + extra_cxx_args
228 OneCommandPerFileTest.__init__(self, cmd, dir,
229 useTempInput=1, *args, **kwargs)
231 def createTempInput(self, tmp, test):
232 print >>tmp, '#include "%s"' % test.source_path