blob: f067baedca49d08a82f61b36338a9b36f5ced8f3 [file] [log] [blame]
Daniel Dunbar54ec2322009-09-14 02:38:46 +00001import os
2
3import Test
4import TestRunner
5import Util
6
7class 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())
Jeffrey Yasskin10d36042009-11-16 22:41:33 +000056 while not os.path.exists(testPath):
57 # Handle GTest parametrized and typed tests, whose name includes
58 # some '/'s.
Jeffrey Yasskin35ec32d2009-10-18 02:05:42 +000059 testPath, namePrefix = os.path.split(testPath)
60 testName = os.path.join(namePrefix, testName)
Daniel Dunbar54ec2322009-09-14 02:38:46 +000061
62 cmd = [testPath, '--gtest_filter=' + testName]
63 out, err, exitCode = TestRunner.executeCommand(cmd)
64
65 if not exitCode:
66 return Test.PASS,''
67
68 return Test.FAIL, out + err
69
70###
71
72class FileBasedTest(object):
73 def getTestsInDirectory(self, testSuite, path_in_suite,
74 litConfig, localConfig):
75 source_path = testSuite.getSourcePath(path_in_suite)
76 for filename in os.listdir(source_path):
77 filepath = os.path.join(source_path, filename)
78 if not os.path.isdir(filepath):
79 base,ext = os.path.splitext(filename)
80 if ext in localConfig.suffixes:
81 yield Test.Test(testSuite, path_in_suite + (filename,),
82 localConfig)
83
84class ShTest(FileBasedTest):
Daniel Dunbar6ff773b2009-11-08 09:07:13 +000085 def __init__(self, execute_external = False):
Daniel Dunbar54ec2322009-09-14 02:38:46 +000086 self.execute_external = execute_external
Daniel Dunbar54ec2322009-09-14 02:38:46 +000087
88 def execute(self, test, litConfig):
89 return TestRunner.executeShTest(test, litConfig,
Daniel Dunbar6ff773b2009-11-08 09:07:13 +000090 self.execute_external)
Daniel Dunbar54ec2322009-09-14 02:38:46 +000091
92class TclTest(FileBasedTest):
93 def execute(self, test, litConfig):
94 return TestRunner.executeTclTest(test, litConfig)
Daniel Dunbar8d2aa382009-09-16 01:34:52 +000095
96###
97
98import re
99import tempfile
100
Daniel Dunbar9a61bd502009-11-15 08:10:29 +0000101class OneCommandPerFileTest:
Daniel Dunbar8d2aa382009-09-16 01:34:52 +0000102 # FIXME: Refactor into generic test for running some command on a directory
103 # of inputs.
104
Daniel Dunbar9a61bd502009-11-15 08:10:29 +0000105 def __init__(self, command, dir, recursive=False,
106 pattern=".*", useTempInput=False):
107 if isinstance(command, str):
108 self.command = [command]
109 else:
110 self.command = list(command)
Daniel Dunbar8d2aa382009-09-16 01:34:52 +0000111 self.dir = str(dir)
112 self.recursive = bool(recursive)
113 self.pattern = re.compile(pattern)
Daniel Dunbar9a61bd502009-11-15 08:10:29 +0000114 self.useTempInput = useTempInput
Daniel Dunbar8d2aa382009-09-16 01:34:52 +0000115
116 def getTestsInDirectory(self, testSuite, path_in_suite,
117 litConfig, localConfig):
118 for dirname,subdirs,filenames in os.walk(self.dir):
119 if not self.recursive:
120 subdirs[:] = []
121
Daniel Dunbard4764712009-11-15 07:22:58 +0000122 if dirname == '.svn' or dirname in localConfig.excludes:
Douglas Gregor86a947d2009-11-05 22:58:04 +0000123 continue
Daniel Dunbard4764712009-11-15 07:22:58 +0000124
Daniel Dunbar8d2aa382009-09-16 01:34:52 +0000125 for filename in filenames:
126 if (not self.pattern.match(filename) or
127 filename in localConfig.excludes):
128 continue
129
130 path = os.path.join(dirname,filename)
131 suffix = path[len(self.dir):]
132 if suffix.startswith(os.sep):
133 suffix = suffix[1:]
134 test = Test.Test(testSuite,
135 path_in_suite + tuple(suffix.split(os.sep)),
136 localConfig)
137 # FIXME: Hack?
138 test.source_path = path
139 yield test
140
Daniel Dunbar9a61bd502009-11-15 08:10:29 +0000141 def createTempInput(self, tmp, test):
142 abstract
143
Daniel Dunbar8d2aa382009-09-16 01:34:52 +0000144 def execute(self, test, litConfig):
Daniel Dunbard4764712009-11-15 07:22:58 +0000145 if test.config.unsupported:
146 return (Test.UNSUPPORTED, 'Test is unsupported')
147
Daniel Dunbar9a61bd502009-11-15 08:10:29 +0000148 cmd = list(self.command)
Daniel Dunbar8d2aa382009-09-16 01:34:52 +0000149
Daniel Dunbar9a61bd502009-11-15 08:10:29 +0000150 # If using temp input, create a temporary file and hand it to the
151 # subclass.
152 if self.useTempInput:
153 tmp = tempfile.NamedTemporaryFile(suffix='.cpp')
154 self.createTempInput(tmp, test)
155 tmp.flush()
156 cmd.append(tmp.name)
157 else:
158 cmd.append(test.source_path)
159
Daniel Dunbar8d2aa382009-09-16 01:34:52 +0000160 out, err, exitCode = TestRunner.executeCommand(cmd)
161
162 diags = out + err
163 if not exitCode and not diags.strip():
164 return Test.PASS,''
165
Daniel Dunbar9a61bd502009-11-15 08:10:29 +0000166 # Try to include some useful information.
167 report = """Command: %s\n""" % ' '.join(["'%s'" % a
168 for a in cmd])
169 if self.useTempInput:
170 report += """Temporary File: %s\n""" % tmp.name
171 report += "--\n%s--\n""" % open(tmp.name).read()
172 report += """Output:\n--\n%s--""" % diags
173
174 return Test.FAIL, report
175
176class SyntaxCheckTest(OneCommandPerFileTest):
177 def __init__(self, compiler, dir, extra_cxx_args=[], *args, **kwargs):
178 cmd = [compiler, '-x', 'c++', '-fsyntax-only'] + extra_cxx_args
179 OneCommandPerFileTest.__init__(self, cmd, dir,
180 useTempInput=1, *args, **kwargs)
181
182 def createTempInput(self, tmp, test):
183 print >>tmp, '#include "%s"' % test.source_path