1from __future__ import absolute_import 2import os 3import sys 4 5import lit.Test 6import lit.TestRunner 7import lit.util 8from .base import TestFormat 9 10kIsWindows = sys.platform in ['win32', 'cygwin'] 11 12class GoogleTest(TestFormat): 13 def __init__(self, test_sub_dir, test_suffix): 14 self.test_sub_dir = os.path.normcase(str(test_sub_dir)).split(';') 15 self.test_suffix = str(test_suffix) 16 17 # On Windows, assume tests will also end in '.exe'. 18 if kIsWindows: 19 self.test_suffix += '.exe' 20 21 def getGTestTests(self, path, litConfig, localConfig): 22 """getGTestTests(path) - [name] 23 24 Return the tests available in gtest executable. 25 26 Args: 27 path: String path to a gtest executable 28 litConfig: LitConfig instance 29 localConfig: TestingConfig instance""" 30 31 try: 32 lines = lit.util.capture([path, '--gtest_list_tests'], 33 env=localConfig.environment) 34 if kIsWindows: 35 lines = lines.replace('\r', '') 36 lines = lines.split('\n') 37 except: 38 litConfig.error("unable to discover google-tests in %r" % path) 39 raise StopIteration 40 41 nested_tests = [] 42 for ln in lines: 43 if not ln.strip(): 44 continue 45 46 prefix = '' 47 index = 0 48 while ln[index*2:index*2+2] == ' ': 49 index += 1 50 while len(nested_tests) > index: 51 nested_tests.pop() 52 53 ln = ln[index*2:] 54 if ln.endswith('.'): 55 nested_tests.append(ln) 56 else: 57 yield ''.join(nested_tests) + ln 58 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): 63 return 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, file_path=execpath) 69 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: 78 continue 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): 85 yield test 86 elif ('.' in self.test_sub_dir): 87 for test in self.getTestsInExecutable( 88 testSuite, path_in_suite, filepath, 89 litConfig, localConfig): 90 yield test 91 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 96 # some '/'s. 97 testPath, namePrefix = os.path.split(testPath) 98 testName = os.path.join(namePrefix, testName) 99 100 cmd = [testPath, '--gtest_filter=' + testName] 101 if litConfig.useValgrind: 102 cmd = litConfig.valgrindArgs + cmd 103 104 if litConfig.noExecute: 105 return lit.Test.PASS, '' 106 107 out, err, exitCode = lit.util.executeCommand( 108 cmd, env=test.config.environment) 109 110 if not exitCode: 111 return lit.Test.PASS,'' 112 113 return lit.Test.FAIL, out + err 114