blob: cc40a309dc68715de49f6a103b114b4fda747c32 [file] [log] [blame]
Daniel Dunbar00a42442009-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 Yasskin6bccb4c2009-10-18 02:05:42 +000056 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)
Daniel Dunbar00a42442009-09-14 02:38:46 +000060
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
71class 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
83class ShTest(FileBasedTest):
Daniel Dunbaree504b82009-11-08 09:07:13 +000084 def __init__(self, execute_external = False):
Daniel Dunbar00a42442009-09-14 02:38:46 +000085 self.execute_external = execute_external
Daniel Dunbar00a42442009-09-14 02:38:46 +000086
87 def execute(self, test, litConfig):
88 return TestRunner.executeShTest(test, litConfig,
Daniel Dunbaree504b82009-11-08 09:07:13 +000089 self.execute_external)
Daniel Dunbar00a42442009-09-14 02:38:46 +000090
91class TclTest(FileBasedTest):
92 def execute(self, test, litConfig):
93 return TestRunner.executeTclTest(test, litConfig)
Daniel Dunbar7c748662009-09-16 01:34:52 +000094
95###
96
97import re
98import tempfile
99
Daniel Dunbar2cb097d2009-11-15 08:10:29 +0000100class OneCommandPerFileTest:
Daniel Dunbar7c748662009-09-16 01:34:52 +0000101 # FIXME: Refactor into generic test for running some command on a directory
102 # of inputs.
103
Daniel Dunbar2cb097d2009-11-15 08:10:29 +0000104 def __init__(self, command, dir, recursive=False,
105 pattern=".*", useTempInput=False):
106 if isinstance(command, str):
107 self.command = [command]
108 else:
109 self.command = list(command)
Daniel Dunbar7c748662009-09-16 01:34:52 +0000110 self.dir = str(dir)
111 self.recursive = bool(recursive)
112 self.pattern = re.compile(pattern)
Daniel Dunbar2cb097d2009-11-15 08:10:29 +0000113 self.useTempInput = useTempInput
Daniel Dunbar7c748662009-09-16 01:34:52 +0000114
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
Daniel Dunbar048bac32009-11-15 07:22:58 +0000121 if dirname == '.svn' or dirname in localConfig.excludes:
Douglas Gregor489b8332009-11-05 22:58:04 +0000122 continue
Daniel Dunbar048bac32009-11-15 07:22:58 +0000123
Daniel Dunbar7c748662009-09-16 01:34:52 +0000124 for filename in filenames:
125 if (not self.pattern.match(filename) or
126 filename in localConfig.excludes):
127 continue
128
129 path = os.path.join(dirname,filename)
130 suffix = path[len(self.dir):]
131 if suffix.startswith(os.sep):
132 suffix = suffix[1:]
133 test = Test.Test(testSuite,
134 path_in_suite + tuple(suffix.split(os.sep)),
135 localConfig)
136 # FIXME: Hack?
137 test.source_path = path
138 yield test
139
Daniel Dunbar2cb097d2009-11-15 08:10:29 +0000140 def createTempInput(self, tmp, test):
141 abstract
142
Daniel Dunbar7c748662009-09-16 01:34:52 +0000143 def execute(self, test, litConfig):
Daniel Dunbar048bac32009-11-15 07:22:58 +0000144 if test.config.unsupported:
145 return (Test.UNSUPPORTED, 'Test is unsupported')
146
Daniel Dunbar2cb097d2009-11-15 08:10:29 +0000147 cmd = list(self.command)
Daniel Dunbar7c748662009-09-16 01:34:52 +0000148
Daniel Dunbar2cb097d2009-11-15 08:10:29 +0000149 # If using temp input, create a temporary file and hand it to the
150 # subclass.
151 if self.useTempInput:
152 tmp = tempfile.NamedTemporaryFile(suffix='.cpp')
153 self.createTempInput(tmp, test)
154 tmp.flush()
155 cmd.append(tmp.name)
156 else:
157 cmd.append(test.source_path)
158
Daniel Dunbar7c748662009-09-16 01:34:52 +0000159 out, err, exitCode = TestRunner.executeCommand(cmd)
160
161 diags = out + err
162 if not exitCode and not diags.strip():
163 return Test.PASS,''
164
Daniel Dunbar2cb097d2009-11-15 08:10:29 +0000165 # Try to include some useful information.
166 report = """Command: %s\n""" % ' '.join(["'%s'" % a
167 for a in cmd])
168 if self.useTempInput:
169 report += """Temporary File: %s\n""" % tmp.name
170 report += "--\n%s--\n""" % open(tmp.name).read()
171 report += """Output:\n--\n%s--""" % diags
172
173 return Test.FAIL, report
174
175class SyntaxCheckTest(OneCommandPerFileTest):
176 def __init__(self, compiler, dir, extra_cxx_args=[], *args, **kwargs):
177 cmd = [compiler, '-x', 'c++', '-fsyntax-only'] + extra_cxx_args
178 OneCommandPerFileTest.__init__(self, cmd, dir,
179 useTempInput=1, *args, **kwargs)
180
181 def createTempInput(self, tmp, test):
182 print >>tmp, '#include "%s"' % test.source_path