fcafe3cc7ae71a1aa48ea0ac70b851abd06d2d9f
[oota-llvm.git] / utils / lit / TestFormats.py
1 import os
2
3 import Test
4 import TestRunner
5 import Util
6
7 class GoogleTest(object):
8     def __init__(self, test_sub_dir, test_suffix):
9         self.test_sub_dir = str(test_sub_dir)
10         self.test_suffix = str(test_suffix)
11
12     def getGTestTests(self, path):
13         """getGTestTests(path) - [name]
14         
15         Return the tests available in gtest executable."""
16
17         lines = Util.capture([path, '--gtest_list_tests']).split('\n')
18         nested_tests = []
19         for ln in lines:
20             if not ln.strip():
21                 continue
22
23             prefix = ''
24             index = 0
25             while ln[index*2:index*2+2] == '  ':
26                 index += 1
27             while len(nested_tests) > index:
28                 nested_tests.pop()
29             
30             ln = ln[index*2:]
31             if ln.endswith('.'):
32                 nested_tests.append(ln)
33             else:
34                 yield ''.join(nested_tests) + ln
35
36     def getTestsInDirectory(self, testSuite, path_in_suite,
37                             litConfig, localConfig):
38         source_path = testSuite.getSourcePath(path_in_suite)
39         for filename in os.listdir(source_path):
40             # Check for the one subdirectory (build directory) tests will be in.
41             if filename != self.test_sub_dir:
42                 continue
43
44             filepath = os.path.join(source_path, filename)
45             for subfilename in os.listdir(filepath):
46                 if subfilename.endswith(self.test_suffix):
47                     execpath = os.path.join(filepath, subfilename)
48
49                     # Discover the tests in this executable.
50                     for name in self.getGTestTests(execpath):
51                         testPath = path_in_suite + (filename, subfilename, name)
52                         yield Test.Test(testSuite, testPath, localConfig)
53
54     def execute(self, test, litConfig):
55         testPath,testName = os.path.split(test.getSourcePath())
56         if not os.path.exists(testPath):
57             # Handle GTest typed tests, whose name includes a '/'.
58             testPath, namePrefix = os.path.split(testPath)
59             testName = os.path.join(namePrefix, testName)
60
61         cmd = [testPath, '--gtest_filter=' + testName]
62         out, err, exitCode = TestRunner.executeCommand(cmd)
63             
64         if not exitCode:
65             return Test.PASS,''
66
67         return Test.FAIL, out + err
68
69 ###
70
71 class FileBasedTest(object):
72     def getTestsInDirectory(self, testSuite, path_in_suite,
73                             litConfig, localConfig):
74         source_path = testSuite.getSourcePath(path_in_suite)
75         for filename in os.listdir(source_path):
76             filepath = os.path.join(source_path, filename)
77             if not os.path.isdir(filepath):
78                 base,ext = os.path.splitext(filename)
79                 if ext in localConfig.suffixes:
80                     yield Test.Test(testSuite, path_in_suite + (filename,),
81                                     localConfig)
82
83 class ShTest(FileBasedTest):
84     def __init__(self, execute_external = False, require_and_and = False):
85         self.execute_external = execute_external
86         self.require_and_and = require_and_and
87
88     def execute(self, test, litConfig):
89         return TestRunner.executeShTest(test, litConfig,
90                                         self.execute_external,
91                                         self.require_and_and)
92
93 class TclTest(FileBasedTest):
94     def execute(self, test, litConfig):
95         return TestRunner.executeTclTest(test, litConfig)
96
97 ###
98
99 import re
100 import tempfile
101
102 class SyntaxCheckTest:
103     # FIXME: Refactor into generic test for running some command on a directory
104     # of inputs.
105
106     def __init__(self, compiler, dir, recursive, pattern, excludes=[], 
107                  extra_cxx_args=[]):
108         self.compiler = str(compiler)
109         self.dir = str(dir)
110         self.recursive = bool(recursive)
111         self.pattern = re.compile(pattern)
112         self.excludes = list(excludes)
113         self.extra_cxx_args = list(extra_cxx_args)
114
115     def getTestsInDirectory(self, testSuite, path_in_suite,
116                             litConfig, localConfig):
117         for dirname,subdirs,filenames in os.walk(self.dir):
118             if not self.recursive:
119                 subdirs[:] = []
120
121             if dirname.__contains__('.svn'):
122                 continue
123                 
124             for filename in filenames:
125                 if (not self.pattern.match(filename) or
126                     filename in localConfig.excludes):
127                     continue
128                 
129                 # Skip any files that were specifically excluded.
130                 excluded = False
131                 for exclude in self.excludes:
132                     if filename.__contains__(exclude):
133                       excluded = True
134                       break
135                       
136                 if excluded:
137                     continue
138
139                 path = os.path.join(dirname,filename)
140                 suffix = path[len(self.dir):]
141                 if suffix.startswith(os.sep):
142                     suffix = suffix[1:]
143                 test = Test.Test(testSuite,
144                                  path_in_suite + tuple(suffix.split(os.sep)),
145                                  localConfig)
146                 # FIXME: Hack?
147                 test.source_path = path
148                 yield test
149
150     def execute(self, test, litConfig):
151         tmp = tempfile.NamedTemporaryFile(suffix='.cpp')
152         print >>tmp, '#include "%s"' % test.source_path
153         tmp.flush()
154
155         cmd = [self.compiler, '-x', 'c++', '-fsyntax-only', tmp.name]
156         cmd.extend(self.extra_cxx_args)
157         out, err, exitCode = TestRunner.executeCommand(cmd)
158
159         diags = out + err
160         if not exitCode and not diags.strip():
161             return Test.PASS,''
162
163         return Test.FAIL, diags