1 from __future__ import absolute_import
9 kIsWindows = sys.platform in ['win32', 'cygwin']
11 class GoogleTest(object):
12 def __init__(self, test_sub_dir, test_suffix):
13 self.test_sub_dir = os.path.normcase(str(test_sub_dir)).split(';')
14 self.test_suffix = str(test_suffix)
16 # On Windows, assume tests will also end in '.exe'.
18 self.test_suffix += '.exe'
20 def getGTestTests(self, path, litConfig, localConfig):
21 """getGTestTests(path) - [name]
23 Return the tests available in gtest executable.
26 path: String path to a gtest executable
27 litConfig: LitConfig instance
28 localConfig: TestingConfig instance"""
31 lines = lit.Util.capture([path, '--gtest_list_tests'],
32 env=localConfig.environment)
34 lines = lines.replace('\r', '')
35 lines = lines.split('\n')
37 litConfig.error("unable to discover google-tests in %r" % path)
47 while ln[index*2:index*2+2] == ' ':
49 while len(nested_tests) > index:
54 nested_tests.append(ln)
56 yield ''.join(nested_tests) + ln
58 # Note: path_in_suite should not include the executable name.
59 def getTestsInExecutable(self, testSuite, path_in_suite, execpath,
60 litConfig, localConfig):
61 if not execpath.endswith(self.test_suffix):
63 (dirname, basename) = os.path.split(execpath)
64 # Discover the tests in this executable.
65 for testname in self.getGTestTests(execpath, litConfig, localConfig):
66 testPath = path_in_suite + (basename, testname)
67 yield lit.Test.Test(testSuite, testPath, localConfig)
69 def getTestsInDirectory(self, testSuite, path_in_suite,
70 litConfig, localConfig):
71 source_path = testSuite.getSourcePath(path_in_suite)
72 for filename in os.listdir(source_path):
73 filepath = os.path.join(source_path, filename)
74 if os.path.isdir(filepath):
75 # Iterate over executables in a directory.
76 if not os.path.normcase(filename) in self.test_sub_dir:
78 dirpath_in_suite = path_in_suite + (filename, )
79 for subfilename in os.listdir(filepath):
80 execpath = os.path.join(filepath, subfilename)
81 for test in self.getTestsInExecutable(
82 testSuite, dirpath_in_suite, execpath,
83 litConfig, localConfig):
85 elif ('.' in self.test_sub_dir):
86 for test in self.getTestsInExecutable(
87 testSuite, path_in_suite, filepath,
88 litConfig, localConfig):
91 def execute(self, test, litConfig):
92 testPath,testName = os.path.split(test.getSourcePath())
93 while not os.path.exists(testPath):
94 # Handle GTest parametrized and typed tests, whose name includes
96 testPath, namePrefix = os.path.split(testPath)
97 testName = os.path.join(namePrefix, testName)
99 cmd = [testPath, '--gtest_filter=' + testName]
100 if litConfig.useValgrind:
101 cmd = litConfig.valgrindArgs + cmd
103 if litConfig.noExecute:
106 out, err, exitCode = lit.TestRunner.executeCommand(
107 cmd, env=test.config.environment)
110 return lit.Test.PASS,''
112 return lit.Test.FAIL, out + err
116 class FileBasedTest(object):
117 def getTestsInDirectory(self, testSuite, path_in_suite,
118 litConfig, localConfig):
119 source_path = testSuite.getSourcePath(path_in_suite)
120 for filename in os.listdir(source_path):
121 # Ignore dot files and excluded tests.
122 if (filename.startswith('.') or
123 filename in localConfig.excludes):
126 filepath = os.path.join(source_path, filename)
127 if not os.path.isdir(filepath):
128 base,ext = os.path.splitext(filename)
129 if ext in localConfig.suffixes:
130 yield lit.Test.Test(testSuite, path_in_suite + (filename,),
133 class ShTest(FileBasedTest):
134 def __init__(self, execute_external = False):
135 self.execute_external = execute_external
137 def execute(self, test, litConfig):
138 return lit.TestRunner.executeShTest(test, litConfig,
139 self.execute_external)
146 class OneCommandPerFileTest:
147 # FIXME: Refactor into generic test for running some command on a directory
150 def __init__(self, command, dir, recursive=False,
151 pattern=".*", useTempInput=False):
152 if isinstance(command, str):
153 self.command = [command]
155 self.command = list(command)
159 self.recursive = bool(recursive)
160 self.pattern = re.compile(pattern)
161 self.useTempInput = useTempInput
163 def getTestsInDirectory(self, testSuite, path_in_suite,
164 litConfig, localConfig):
167 dir = testSuite.getSourcePath(path_in_suite)
169 for dirname,subdirs,filenames in os.walk(dir):
170 if not self.recursive:
173 subdirs[:] = [d for d in subdirs
175 d not in localConfig.excludes)]
177 for filename in filenames:
178 if (filename.startswith('.') or
179 not self.pattern.match(filename) or
180 filename in localConfig.excludes):
183 path = os.path.join(dirname,filename)
184 suffix = path[len(dir):]
185 if suffix.startswith(os.sep):
187 test = lit.Test.Test(
188 testSuite, path_in_suite + tuple(suffix.split(os.sep)),
191 test.source_path = path
194 def createTempInput(self, tmp, test):
197 def execute(self, test, litConfig):
198 if test.config.unsupported:
199 return (lit.Test.UNSUPPORTED, 'Test is unsupported')
201 cmd = list(self.command)
203 # If using temp input, create a temporary file and hand it to the
205 if self.useTempInput:
206 tmp = tempfile.NamedTemporaryFile(suffix='.cpp')
207 self.createTempInput(tmp, test)
210 elif hasattr(test, 'source_path'):
211 cmd.append(test.source_path)
213 cmd.append(test.getSourcePath())
215 out, err, exitCode = lit.TestRunner.executeCommand(cmd)
218 if not exitCode and not diags.strip():
219 return lit.Test.PASS,''
221 # Try to include some useful information.
222 report = """Command: %s\n""" % ' '.join(["'%s'" % a
224 if self.useTempInput:
225 report += """Temporary File: %s\n""" % tmp.name
226 report += "--\n%s--\n""" % open(tmp.name).read()
227 report += """Output:\n--\n%s--""" % diags
229 return lit.Test.FAIL, report