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