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 # Note: path_in_suite should not include the executable name.
58 def getTestsInExecutable(self, testSuite, path_in_suite, execpath,
59 litConfig, localConfig):
60 if not execpath.endswith(self.test_suffix):
62 (dirname, basename) = os.path.split(execpath)
63 # Discover the tests in this executable.
64 for testname in self.getGTestTests(execpath, litConfig, localConfig):
65 testPath = path_in_suite + (basename, testname)
66 yield Test.Test(testSuite, testPath, localConfig)
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 os.path.isdir(filepath):
74 # Iterate over executables in a directory.
75 if not os.path.normcase(filename) in self.test_sub_dir:
77 dirpath_in_suite = path_in_suite + (filename, )
78 for subfilename in os.listdir(filepath):
79 execpath = os.path.join(filepath, subfilename)
80 for test in self.getTestsInExecutable(
81 testSuite, dirpath_in_suite, execpath,
82 litConfig, localConfig):
84 elif ('.' in self.test_sub_dir):
85 for test in self.getTestsInExecutable(
86 testSuite, path_in_suite, filepath,
87 litConfig, localConfig):
90 def execute(self, test, litConfig):
91 testPath,testName = os.path.split(test.getSourcePath())
92 while not os.path.exists(testPath):
93 # Handle GTest parametrized and typed tests, whose name includes
95 testPath, namePrefix = os.path.split(testPath)
96 testName = os.path.join(namePrefix, testName)
98 cmd = [testPath, '--gtest_filter=' + testName]
99 if litConfig.useValgrind:
100 cmd = litConfig.valgrindArgs + cmd
102 out, err, exitCode = TestRunner.executeCommand(
103 cmd, env=test.config.environment)
108 return Test.FAIL, out + err
112 class FileBasedTest(object):
113 def getTestsInDirectory(self, testSuite, path_in_suite,
114 litConfig, localConfig):
115 source_path = testSuite.getSourcePath(path_in_suite)
116 for filename in os.listdir(source_path):
117 # Ignore dot files and excluded tests.
118 if (filename.startswith('.') or
119 filename in localConfig.excludes):
122 filepath = os.path.join(source_path, filename)
123 if not os.path.isdir(filepath):
124 base,ext = os.path.splitext(filename)
125 if ext in localConfig.suffixes:
126 yield Test.Test(testSuite, path_in_suite + (filename,),
129 class ShTest(FileBasedTest):
130 def __init__(self, execute_external = False):
131 self.execute_external = execute_external
133 def execute(self, test, litConfig):
134 return TestRunner.executeShTest(test, litConfig,
135 self.execute_external)
142 class OneCommandPerFileTest:
143 # FIXME: Refactor into generic test for running some command on a directory
146 def __init__(self, command, dir, recursive=False,
147 pattern=".*", useTempInput=False):
148 if isinstance(command, str):
149 self.command = [command]
151 self.command = list(command)
155 self.recursive = bool(recursive)
156 self.pattern = re.compile(pattern)
157 self.useTempInput = useTempInput
159 def getTestsInDirectory(self, testSuite, path_in_suite,
160 litConfig, localConfig):
163 dir = testSuite.getSourcePath(path_in_suite)
165 for dirname,subdirs,filenames in os.walk(dir):
166 if not self.recursive:
169 subdirs[:] = [d for d in subdirs
171 d not in localConfig.excludes)]
173 for filename in filenames:
174 if (filename.startswith('.') or
175 not self.pattern.match(filename) or
176 filename in localConfig.excludes):
179 path = os.path.join(dirname,filename)
180 suffix = path[len(dir):]
181 if suffix.startswith(os.sep):
183 test = Test.Test(testSuite,
184 path_in_suite + tuple(suffix.split(os.sep)),
187 test.source_path = path
190 def createTempInput(self, tmp, test):
193 def execute(self, test, litConfig):
194 if test.config.unsupported:
195 return (Test.UNSUPPORTED, 'Test is unsupported')
197 cmd = list(self.command)
199 # If using temp input, create a temporary file and hand it to the
201 if self.useTempInput:
202 tmp = tempfile.NamedTemporaryFile(suffix='.cpp')
203 self.createTempInput(tmp, test)
206 elif hasattr(test, 'source_path'):
207 cmd.append(test.source_path)
209 cmd.append(test.getSourcePath())
211 out, err, exitCode = TestRunner.executeCommand(cmd)
214 if not exitCode and not diags.strip():
217 # Try to include some useful information.
218 report = """Command: %s\n""" % ' '.join(["'%s'" % a
220 if self.useTempInput:
221 report += """Temporary File: %s\n""" % tmp.name
222 report += "--\n%s--\n""" % open(tmp.name).read()
223 report += """Output:\n--\n%s--""" % diags
225 return Test.FAIL, report