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)
33 lines = lines.decode('ascii')
35 lines = lines.replace('\r', '')
36 lines = lines.split('\n')
38 litConfig.error("unable to discover google-tests in %r" % path)
48 while ln[index*2:index*2+2] == ' ':
50 while len(nested_tests) > index:
55 nested_tests.append(ln)
57 yield ''.join(nested_tests) + ln
59 # Note: path_in_suite should not include the executable name.
60 def getTestsInExecutable(self, testSuite, path_in_suite, execpath,
61 litConfig, localConfig):
62 if not execpath.endswith(self.test_suffix):
64 (dirname, basename) = os.path.split(execpath)
65 # Discover the tests in this executable.
66 for testname in self.getGTestTests(execpath, litConfig, localConfig):
67 testPath = path_in_suite + (basename, testname)
68 yield lit.Test.Test(testSuite, testPath, localConfig)
70 def getTestsInDirectory(self, testSuite, path_in_suite,
71 litConfig, localConfig):
72 source_path = testSuite.getSourcePath(path_in_suite)
73 for filename in os.listdir(source_path):
74 filepath = os.path.join(source_path, filename)
75 if os.path.isdir(filepath):
76 # Iterate over executables in a directory.
77 if not os.path.normcase(filename) in self.test_sub_dir:
79 dirpath_in_suite = path_in_suite + (filename, )
80 for subfilename in os.listdir(filepath):
81 execpath = os.path.join(filepath, subfilename)
82 for test in self.getTestsInExecutable(
83 testSuite, dirpath_in_suite, execpath,
84 litConfig, localConfig):
86 elif ('.' in self.test_sub_dir):
87 for test in self.getTestsInExecutable(
88 testSuite, path_in_suite, filepath,
89 litConfig, localConfig):
92 def execute(self, test, litConfig):
93 testPath,testName = os.path.split(test.getSourcePath())
94 while not os.path.exists(testPath):
95 # Handle GTest parametrized and typed tests, whose name includes
97 testPath, namePrefix = os.path.split(testPath)
98 testName = os.path.join(namePrefix, testName)
100 cmd = [testPath, '--gtest_filter=' + testName]
101 if litConfig.useValgrind:
102 cmd = litConfig.valgrindArgs + cmd
104 if litConfig.noExecute:
105 return lit.Test.PASS, ''
107 out, err, exitCode = lit.TestRunner.executeCommand(
108 cmd, env=test.config.environment)
111 return lit.Test.PASS,''
113 return lit.Test.FAIL, out + err
117 class FileBasedTest(object):
118 def getTestsInDirectory(self, testSuite, path_in_suite,
119 litConfig, localConfig):
120 source_path = testSuite.getSourcePath(path_in_suite)
121 for filename in os.listdir(source_path):
122 # Ignore dot files and excluded tests.
123 if (filename.startswith('.') or
124 filename in localConfig.excludes):
127 filepath = os.path.join(source_path, filename)
128 if not os.path.isdir(filepath):
129 base,ext = os.path.splitext(filename)
130 if ext in localConfig.suffixes:
131 yield lit.Test.Test(testSuite, path_in_suite + (filename,),
134 class ShTest(FileBasedTest):
135 def __init__(self, execute_external = False):
136 self.execute_external = execute_external
138 def execute(self, test, litConfig):
139 return lit.TestRunner.executeShTest(test, litConfig,
140 self.execute_external)
147 class OneCommandPerFileTest:
148 # FIXME: Refactor into generic test for running some command on a directory
151 def __init__(self, command, dir, recursive=False,
152 pattern=".*", useTempInput=False):
153 if isinstance(command, str):
154 self.command = [command]
156 self.command = list(command)
160 self.recursive = bool(recursive)
161 self.pattern = re.compile(pattern)
162 self.useTempInput = useTempInput
164 def getTestsInDirectory(self, testSuite, path_in_suite,
165 litConfig, localConfig):
168 dir = testSuite.getSourcePath(path_in_suite)
170 for dirname,subdirs,filenames in os.walk(dir):
171 if not self.recursive:
174 subdirs[:] = [d for d in subdirs
176 d not in localConfig.excludes)]
178 for filename in filenames:
179 if (filename.startswith('.') or
180 not self.pattern.match(filename) or
181 filename in localConfig.excludes):
184 path = os.path.join(dirname,filename)
185 suffix = path[len(dir):]
186 if suffix.startswith(os.sep):
188 test = lit.Test.Test(
189 testSuite, path_in_suite + tuple(suffix.split(os.sep)),
192 test.source_path = path
195 def createTempInput(self, tmp, test):
198 def execute(self, test, litConfig):
199 if test.config.unsupported:
200 return (lit.Test.UNSUPPORTED, 'Test is unsupported')
202 cmd = list(self.command)
204 # If using temp input, create a temporary file and hand it to the
206 if self.useTempInput:
207 tmp = tempfile.NamedTemporaryFile(suffix='.cpp')
208 self.createTempInput(tmp, test)
211 elif hasattr(test, 'source_path'):
212 cmd.append(test.source_path)
214 cmd.append(test.getSourcePath())
216 out, err, exitCode = lit.TestRunner.executeCommand(cmd)
219 if not exitCode and not diags.strip():
220 return lit.Test.PASS,''
222 # Try to include some useful information.
223 report = """Command: %s\n""" % ' '.join(["'%s'" % a
225 if self.useTempInput:
226 report += """Temporary File: %s\n""" % tmp.name
227 report += "--\n%s--\n""" % open(tmp.name).read()
228 report += """Output:\n--\n%s--""" % diags
230 return lit.Test.FAIL, report