lit: Add LitTestCase and lit.load_test_suite, for adapting lit based suites for
[oota-llvm.git] / utils / lit / 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, litConfig, localConfig):
13         """getGTestTests(path) - [name]
14
15         Return the tests available in gtest executable.
16
17         Args:
18           path: String path to a gtest executable
19           litConfig: LitConfig instance
20           localConfig: TestingConfig instance"""
21
22         try:
23             lines = Util.capture([path, '--gtest_list_tests'],
24                                  env=localConfig.environment).split('\n')
25         except:
26             litConfig.error("unable to discover google-tests in %r" % path)
27             raise StopIteration
28
29         nested_tests = []
30         for ln in lines:
31             if not ln.strip():
32                 continue
33
34             prefix = ''
35             index = 0
36             while ln[index*2:index*2+2] == '  ':
37                 index += 1
38             while len(nested_tests) > index:
39                 nested_tests.pop()
40             
41             ln = ln[index*2:]
42             if ln.endswith('.'):
43                 nested_tests.append(ln)
44             else:
45                 yield ''.join(nested_tests) + ln
46
47     def getTestsInDirectory(self, testSuite, path_in_suite,
48                             litConfig, localConfig):
49         source_path = testSuite.getSourcePath(path_in_suite)
50         for filename in os.listdir(source_path):
51             # Check for the one subdirectory (build directory) tests will be in.
52             if filename != self.test_sub_dir:
53                 continue
54
55             filepath = os.path.join(source_path, filename)
56             for subfilename in os.listdir(filepath):
57                 if subfilename.endswith(self.test_suffix):
58                     execpath = os.path.join(filepath, subfilename)
59
60                     # Discover the tests in this executable.
61                     for name in self.getGTestTests(execpath, litConfig,
62                                                    localConfig):
63                         testPath = path_in_suite + (filename, subfilename, name)
64                         yield Test.Test(testSuite, testPath, localConfig)
65
66     def execute(self, test, litConfig):
67         testPath,testName = os.path.split(test.getSourcePath())
68         while not os.path.exists(testPath):
69             # Handle GTest parametrized and typed tests, whose name includes
70             # some '/'s.
71             testPath, namePrefix = os.path.split(testPath)
72             testName = os.path.join(namePrefix, testName)
73
74         cmd = [testPath, '--gtest_filter=' + testName]
75         if litConfig.useValgrind:
76             cmd = litConfig.valgrindArgs + cmd
77
78         out, err, exitCode = TestRunner.executeCommand(
79             cmd, env=test.config.environment)
80             
81         if not exitCode:
82             return Test.PASS,''
83
84         return Test.FAIL, out + err
85
86 ###
87
88 class FileBasedTest(object):
89     def getTestsInDirectory(self, testSuite, path_in_suite,
90                             litConfig, localConfig):
91         source_path = testSuite.getSourcePath(path_in_suite)
92         for filename in os.listdir(source_path):
93             # Ignore dot files and excluded tests.
94             if (filename.startswith('.') or
95                 filename in localConfig.excludes):
96                 continue
97
98             filepath = os.path.join(source_path, filename)
99             if not os.path.isdir(filepath):
100                 base,ext = os.path.splitext(filename)
101                 if ext in localConfig.suffixes:
102                     yield Test.Test(testSuite, path_in_suite + (filename,),
103                                     localConfig)
104
105 class ShTest(FileBasedTest):
106     def __init__(self, execute_external = False):
107         self.execute_external = execute_external
108
109     def execute(self, test, litConfig):
110         return TestRunner.executeShTest(test, litConfig,
111                                         self.execute_external)
112
113 class TclTest(FileBasedTest):
114     def execute(self, test, litConfig):
115         return TestRunner.executeTclTest(test, litConfig)
116
117 ###
118
119 import re
120 import tempfile
121
122 class OneCommandPerFileTest:
123     # FIXME: Refactor into generic test for running some command on a directory
124     # of inputs.
125
126     def __init__(self, command, dir, recursive=False,
127                  pattern=".*", useTempInput=False):
128         if isinstance(command, str):
129             self.command = [command]
130         else:
131             self.command = list(command)
132         self.dir = str(dir)
133         self.recursive = bool(recursive)
134         self.pattern = re.compile(pattern)
135         self.useTempInput = useTempInput
136
137     def getTestsInDirectory(self, testSuite, path_in_suite,
138                             litConfig, localConfig):
139         for dirname,subdirs,filenames in os.walk(self.dir):
140             if not self.recursive:
141                 subdirs[:] = []
142
143             subdirs[:] = [d for d in subdirs
144                           if (d != '.svn' and
145                               d not in localConfig.excludes)]
146
147             for filename in filenames:
148                 if (filename.startswith('.') or
149                     not self.pattern.match(filename) or
150                     filename in localConfig.excludes):
151                     continue
152
153                 path = os.path.join(dirname,filename)
154                 suffix = path[len(self.dir):]
155                 if suffix.startswith(os.sep):
156                     suffix = suffix[1:]
157                 test = Test.Test(testSuite,
158                                  path_in_suite + tuple(suffix.split(os.sep)),
159                                  localConfig)
160                 # FIXME: Hack?
161                 test.source_path = path
162                 yield test
163
164     def createTempInput(self, tmp, test):
165         abstract
166
167     def execute(self, test, litConfig):
168         if test.config.unsupported:
169             return (Test.UNSUPPORTED, 'Test is unsupported')
170
171         cmd = list(self.command)
172
173         # If using temp input, create a temporary file and hand it to the
174         # subclass.
175         if self.useTempInput:
176             tmp = tempfile.NamedTemporaryFile(suffix='.cpp')
177             self.createTempInput(tmp, test)
178             tmp.flush()
179             cmd.append(tmp.name)
180         else:
181             cmd.append(test.source_path)
182
183         out, err, exitCode = TestRunner.executeCommand(cmd)
184
185         diags = out + err
186         if not exitCode and not diags.strip():
187             return Test.PASS,''
188
189         # Try to include some useful information.
190         report = """Command: %s\n""" % ' '.join(["'%s'" % a
191                                                  for a in cmd])
192         if self.useTempInput:
193             report += """Temporary File: %s\n""" % tmp.name
194             report += "--\n%s--\n""" % open(tmp.name).read()
195         report += """Output:\n--\n%s--""" % diags
196
197         return Test.FAIL, report
198
199 class SyntaxCheckTest(OneCommandPerFileTest):
200     def __init__(self, compiler, dir, extra_cxx_args=[], *args, **kwargs):
201         cmd = [compiler, '-x', 'c++', '-fsyntax-only'] + extra_cxx_args
202         OneCommandPerFileTest.__init__(self, cmd, dir,
203                                        useTempInput=1, *args, **kwargs)
204
205     def createTempInput(self, tmp, test):
206         print >>tmp, '#include "%s"' % test.source_path