[lit] Rename lit.{TestFormats,Util} to their aliased names {formats,util}.
[oota-llvm.git] / utils / lit / lit / formats.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
114
115 ###
116
117 class FileBasedTest(object):
118     def getTestsInDirectory(self, testSuite, path_in_suite,
119                             litConfig, localConfig):
120         source_path = testSuite.getSourcePath(path_in_suite)
121         for filename in os.listdir(source_path):
122             # Ignore dot files and excluded tests.
123             if (filename.startswith('.') or
124                 filename in localConfig.excludes):
125                 continue
126
127             filepath = os.path.join(source_path, filename)
128             if not os.path.isdir(filepath):
129                 base,ext = os.path.splitext(filename)
130                 if ext in localConfig.suffixes:
131                     yield lit.Test.Test(testSuite, path_in_suite + (filename,),
132                                         localConfig)
133
134 class ShTest(FileBasedTest):
135     def __init__(self, execute_external = False):
136         self.execute_external = execute_external
137
138     def execute(self, test, litConfig):
139         return lit.TestRunner.executeShTest(test, litConfig,
140                                             self.execute_external)
141
142 ###
143
144 import re
145 import tempfile
146
147 class OneCommandPerFileTest:
148     # FIXME: Refactor into generic test for running some command on a directory
149     # of inputs.
150
151     def __init__(self, command, dir, recursive=False,
152                  pattern=".*", useTempInput=False):
153         if isinstance(command, str):
154             self.command = [command]
155         else:
156             self.command = list(command)
157         if dir is not None:
158             dir = str(dir)
159         self.dir = dir
160         self.recursive = bool(recursive)
161         self.pattern = re.compile(pattern)
162         self.useTempInput = useTempInput
163
164     def getTestsInDirectory(self, testSuite, path_in_suite,
165                             litConfig, localConfig):
166         dir = self.dir
167         if dir is None:
168             dir = testSuite.getSourcePath(path_in_suite)
169
170         for dirname,subdirs,filenames in os.walk(dir):
171             if not self.recursive:
172                 subdirs[:] = []
173
174             subdirs[:] = [d for d in subdirs
175                           if (d != '.svn' and
176                               d not in localConfig.excludes)]
177
178             for filename in filenames:
179                 if (filename.startswith('.') or
180                     not self.pattern.match(filename) or
181                     filename in localConfig.excludes):
182                     continue
183
184                 path = os.path.join(dirname,filename)
185                 suffix = path[len(dir):]
186                 if suffix.startswith(os.sep):
187                     suffix = suffix[1:]
188                 test = lit.Test.Test(
189                     testSuite, path_in_suite + tuple(suffix.split(os.sep)),
190                     localConfig)
191                 # FIXME: Hack?
192                 test.source_path = path
193                 yield test
194
195     def createTempInput(self, tmp, test):
196         abstract
197
198     def execute(self, test, litConfig):
199         if test.config.unsupported:
200             return (lit.Test.UNSUPPORTED, 'Test is unsupported')
201
202         cmd = list(self.command)
203
204         # If using temp input, create a temporary file and hand it to the
205         # subclass.
206         if self.useTempInput:
207             tmp = tempfile.NamedTemporaryFile(suffix='.cpp')
208             self.createTempInput(tmp, test)
209             tmp.flush()
210             cmd.append(tmp.name)
211         elif hasattr(test, 'source_path'):
212             cmd.append(test.source_path)
213         else:
214             cmd.append(test.getSourcePath())
215
216         out, err, exitCode = lit.TestRunner.executeCommand(cmd)
217
218         diags = out + err
219         if not exitCode and not diags.strip():
220             return lit.Test.PASS,''
221
222         # Try to include some useful information.
223         report = """Command: %s\n""" % ' '.join(["'%s'" % a
224                                                  for a in cmd])
225         if self.useTempInput:
226             report += """Temporary File: %s\n""" % tmp.name
227             report += "--\n%s--\n""" % open(tmp.name).read()
228         report += """Output:\n--\n%s--""" % diags
229
230         return lit.Test.FAIL, report