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