OSDN Git Service

[lit] Move formats into their own subpackage.
[android-x86/external-llvm.git] / utils / lit / lit / formats / googletest.py
1 from __future__ import absolute_import
2 import os
3 import sys
4
5 import lit.Test
6 import lit.TestRunner
7 import lit.util
8
9 kIsWindows = sys.platform in ['win32', 'cygwin']
10
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)
15
16         # On Windows, assume tests will also end in '.exe'.
17         if kIsWindows:
18             self.test_suffix += '.exe'
19
20     def getGTestTests(self, path, litConfig, localConfig):
21         """getGTestTests(path) - [name]
22
23         Return the tests available in gtest executable.
24
25         Args:
26           path: String path to a gtest executable
27           litConfig: LitConfig instance
28           localConfig: TestingConfig instance"""
29
30         try:
31             lines = lit.util.capture([path, '--gtest_list_tests'],
32                                      env=localConfig.environment)
33             lines = lines.decode('ascii')
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)
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.TestRunner.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