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 out, err, exitCode = lit.TestRunner.executeCommand(
104 cmd, env=test.config.environment)
107 return lit.Test.PASS,''
109 return lit.Test.FAIL, out + err
113 class FileBasedTest(object):
114 def getTestsInDirectory(self, testSuite, path_in_suite,
115 litConfig, localConfig):
116 source_path = testSuite.getSourcePath(path_in_suite)
117 for filename in os.listdir(source_path):
118 # Ignore dot files and excluded tests.
119 if (filename.startswith('.') or
120 filename in localConfig.excludes):
123 filepath = os.path.join(source_path, filename)
124 if not os.path.isdir(filepath):
125 base,ext = os.path.splitext(filename)
126 if ext in localConfig.suffixes:
127 yield lit.Test.Test(testSuite, path_in_suite + (filename,),
130 class ShTest(FileBasedTest):
131 def __init__(self, execute_external = False):
132 self.execute_external = execute_external
134 def execute(self, test, litConfig):
135 return lit.TestRunner.executeShTest(test, litConfig,
136 self.execute_external)
143 class OneCommandPerFileTest:
144 # FIXME: Refactor into generic test for running some command on a directory
147 def __init__(self, command, dir, recursive=False,
148 pattern=".*", useTempInput=False):
149 if isinstance(command, str):
150 self.command = [command]
152 self.command = list(command)
156 self.recursive = bool(recursive)
157 self.pattern = re.compile(pattern)
158 self.useTempInput = useTempInput
160 def getTestsInDirectory(self, testSuite, path_in_suite,
161 litConfig, localConfig):
164 dir = testSuite.getSourcePath(path_in_suite)
166 for dirname,subdirs,filenames in os.walk(dir):
167 if not self.recursive:
170 subdirs[:] = [d for d in subdirs
172 d not in localConfig.excludes)]
174 for filename in filenames:
175 if (filename.startswith('.') or
176 not self.pattern.match(filename) or
177 filename in localConfig.excludes):
180 path = os.path.join(dirname,filename)
181 suffix = path[len(dir):]
182 if suffix.startswith(os.sep):
184 test = lit.Test.Test(
185 testSuite, path_in_suite + tuple(suffix.split(os.sep)),
188 test.source_path = path
191 def createTempInput(self, tmp, test):
194 def execute(self, test, litConfig):
195 if test.config.unsupported:
196 return (lit.Test.UNSUPPORTED, 'Test is unsupported')
198 cmd = list(self.command)
200 # If using temp input, create a temporary file and hand it to the
202 if self.useTempInput:
203 tmp = tempfile.NamedTemporaryFile(suffix='.cpp')
204 self.createTempInput(tmp, test)
207 elif hasattr(test, 'source_path'):
208 cmd.append(test.source_path)
210 cmd.append(test.getSourcePath())
212 out, err, exitCode = lit.TestRunner.executeCommand(cmd)
215 if not exitCode and not diags.strip():
216 return lit.Test.PASS,''
218 # Try to include some useful information.
219 report = """Command: %s\n""" % ' '.join(["'%s'" % a
221 if self.useTempInput:
222 report += """Temporary File: %s\n""" % tmp.name
223 report += "--\n%s--\n""" % open(tmp.name).read()
224 report += """Output:\n--\n%s--""" % diags
226 return lit.Test.FAIL, report