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 lines = lines.decode('ascii') 35 if kIsWindows: 36 lines = lines.replace('\r', '') 37 lines = lines.split('\n') 38 except: 39 litConfig.error("unable to discover google-tests in %r" % path) 40 raise StopIteration 41 42 nested_tests = [] 43 for ln in lines: 44 if not ln.strip(): 45 continue 46 47 prefix = '' 48 index = 0 49 while ln[index*2:index*2+2] == ' ': 50 index += 1 51 while len(nested_tests) > index: 52 nested_tests.pop() 53 54 ln = ln[index*2:] 55 if ln.endswith('.'): 56 nested_tests.append(ln) 57 else: 58 yield ''.join(nested_tests) + ln 59 60 # Note: path_in_suite should not include the executable name. 61 def getTestsInExecutable(self, testSuite, path_in_suite, execpath, 62 litConfig, localConfig): 63 if not execpath.endswith(self.test_suffix): 64 return 65 (dirname, basename) = os.path.split(execpath) 66 # Discover the tests in this executable. 67 for testname in self.getGTestTests(execpath, litConfig, localConfig): 68 testPath = path_in_suite + (basename, testname) 69 yield lit.Test.Test(testSuite, testPath, localConfig, file_path=execpath) 70 71 def getTestsInDirectory(self, testSuite, path_in_suite, 72 litConfig, localConfig): 73 source_path = testSuite.getSourcePath(path_in_suite) 74 for filename in os.listdir(source_path): 75 filepath = os.path.join(source_path, filename) 76 if os.path.isdir(filepath): 77 # Iterate over executables in a directory. 78 if not os.path.normcase(filename) in self.test_sub_dir: 79 continue 80 dirpath_in_suite = path_in_suite + (filename, ) 81 for subfilename in os.listdir(filepath): 82 execpath = os.path.join(filepath, subfilename) 83 for test in self.getTestsInExecutable( 84 testSuite, dirpath_in_suite, execpath, 85 litConfig, localConfig): 86 yield test 87 elif ('.' in self.test_sub_dir): 88 for test in self.getTestsInExecutable( 89 testSuite, path_in_suite, filepath, 90 litConfig, localConfig): 91 yield test 92 93 def execute(self, test, litConfig): 94 testPath,testName = os.path.split(test.getSourcePath()) 95 while not os.path.exists(testPath): 96 # Handle GTest parametrized and typed tests, whose name includes 97 # some '/'s. 98 testPath, namePrefix = os.path.split(testPath) 99 testName = os.path.join(namePrefix, testName) 100 101 cmd = [testPath, '--gtest_filter=' + testName] 102 if litConfig.useValgrind: 103 cmd = litConfig.valgrindArgs + cmd 104 105 if litConfig.noExecute: 106 return lit.Test.PASS, '' 107 108 out, err, exitCode = lit.util.executeCommand( 109 cmd, env=test.config.environment) 110 111 if not exitCode: 112 return lit.Test.PASS,'' 113 114 return lit.Test.FAIL, out + err 115