blob: 55e0eb21ae02ffd3778ea674f9adfff2a355e836 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001# Copyright 2012 the V8 project authors. All rights reserved.
2# Redistribution and use in source and binary forms, with or without
3# modification, are permitted provided that the following conditions are
4# met:
5#
6# * Redistributions of source code must retain the above copyright
7# notice, this list of conditions and the following disclaimer.
8# * Redistributions in binary form must reproduce the above
9# copyright notice, this list of conditions and the following
10# disclaimer in the documentation and/or other materials provided
11# with the distribution.
12# * Neither the name of Google Inc. nor the names of its
13# contributors may be used to endorse or promote products derived
14# from this software without specific prior written permission.
15#
16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28
29import imp
30import os
31
32from . import commands
33from . import statusfile
34from . import utils
35from ..objects import testcase
36
Emily Bernierd0a1eb72015-03-24 16:35:39 -040037# Use this to run several variants of the tests.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000038ALL_VARIANT_FLAGS = {
39 "default": [[]],
40 "stress": [["--stress-opt", "--always-opt"]],
41 "turbofan": [["--turbo"]],
42 "turbofan_opt": [["--turbo", "--always-opt"]],
43 "nocrankshaft": [["--nocrankshaft"]],
Ben Murdoch097c5b22016-05-18 11:27:45 +010044 "ignition": [["--ignition", "--turbo"]],
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000045 "preparser": [["--min-preparse-length=0"]],
46}
Emily Bernierd0a1eb72015-03-24 16:35:39 -040047
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000048# FAST_VARIANTS implies no --always-opt.
49FAST_VARIANT_FLAGS = {
50 "default": [[]],
51 "stress": [["--stress-opt"]],
52 "turbofan": [["--turbo"]],
53 "nocrankshaft": [["--nocrankshaft"]],
Ben Murdoch097c5b22016-05-18 11:27:45 +010054 "ignition": [["--ignition", "--turbo"]],
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000055 "preparser": [["--min-preparse-length=0"]],
56}
57
58ALL_VARIANTS = set(["default", "stress", "turbofan", "turbofan_opt",
59 "nocrankshaft", "ignition", "preparser"])
60FAST_VARIANTS = set(["default", "turbofan"])
61STANDARD_VARIANT = set(["default"])
62
63
64class VariantGenerator(object):
65 def __init__(self, suite, variants):
66 self.suite = suite
67 self.all_variants = ALL_VARIANTS & variants
68 self.fast_variants = FAST_VARIANTS & variants
69 self.standard_variant = STANDARD_VARIANT & variants
70
71 def FilterVariantsByTest(self, testcase):
72 if testcase.outcomes and statusfile.OnlyStandardVariant(
73 testcase.outcomes):
74 return self.standard_variant
75 if testcase.outcomes and statusfile.OnlyFastVariants(testcase.outcomes):
76 return self.fast_variants
77 return self.all_variants
78
79 def GetFlagSets(self, testcase, variant):
80 if testcase.outcomes and statusfile.OnlyFastVariants(testcase.outcomes):
81 return FAST_VARIANT_FLAGS[variant]
82 else:
83 return ALL_VARIANT_FLAGS[variant]
84
Emily Bernierd0a1eb72015-03-24 16:35:39 -040085
Ben Murdochb8a8cc12014-11-26 15:28:44 +000086class TestSuite(object):
87
88 @staticmethod
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000089 def LoadTestSuite(root, global_init=True):
Ben Murdochb8a8cc12014-11-26 15:28:44 +000090 name = root.split(os.path.sep)[-1]
91 f = None
92 try:
93 (f, pathname, description) = imp.find_module("testcfg", [root])
94 module = imp.load_module("testcfg", f, pathname, description)
95 return module.GetSuite(name, root)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000096 except ImportError:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000097 # Use default if no testcfg is present.
98 return GoogleTestSuite(name, root)
99 finally:
100 if f:
101 f.close()
102
103 def __init__(self, name, root):
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000104 # Note: This might be called concurrently from different processes.
105 # Changing harddisk state should be done in 'SetupWorkingDirectory' below.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000106 self.name = name # string
107 self.root = root # string containing path
108 self.tests = None # list of TestCase objects
109 self.rules = None # dictionary mapping test path to list of outcomes
110 self.wildcards = None # dictionary mapping test paths to list of outcomes
111 self.total_duration = None # float, assigned on demand
112
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000113 def SetupWorkingDirectory(self):
114 # This is called once per test suite object in a multi-process setting.
115 # Multi-process-unsafe work-directory setup can go here.
116 pass
117
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000118 def shell(self):
119 return "d8"
120
121 def suffix(self):
122 return ".js"
123
124 def status_file(self):
125 return "%s/%s.status" % (self.root, self.name)
126
127 # Used in the status file and for stdout printing.
128 def CommonTestName(self, testcase):
129 if utils.IsWindows():
130 return testcase.path.replace("\\", "/")
131 else:
132 return testcase.path
133
134 def ListTests(self, context):
135 raise NotImplementedError
136
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000137 def _VariantGeneratorFactory(self):
138 """The variant generator class to be used."""
139 return VariantGenerator
140
141 def CreateVariantGenerator(self, variants):
142 """Return a generator for the testing variants of this suite.
143
144 Args:
145 variants: List of variant names to be run as specified by the test
146 runner.
147 Returns: An object of type VariantGenerator.
148 """
149 return self._VariantGeneratorFactory()(self, set(variants))
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000150
151 def DownloadData(self):
152 pass
153
154 def ReadStatusFile(self, variables):
155 (self.rules, self.wildcards) = \
156 statusfile.ReadStatusFile(self.status_file(), variables)
157
158 def ReadTestCases(self, context):
159 self.tests = self.ListTests(context)
160
161 @staticmethod
162 def _FilterFlaky(flaky, mode):
163 return (mode == "run" and not flaky) or (mode == "skip" and flaky)
164
165 @staticmethod
166 def _FilterSlow(slow, mode):
167 return (mode == "run" and not slow) or (mode == "skip" and slow)
168
169 @staticmethod
170 def _FilterPassFail(pass_fail, mode):
171 return (mode == "run" and not pass_fail) or (mode == "skip" and pass_fail)
172
173 def FilterTestCasesByStatus(self, warn_unused_rules,
174 flaky_tests="dontcare",
175 slow_tests="dontcare",
176 pass_fail_tests="dontcare"):
177 filtered = []
178 used_rules = set()
179 for t in self.tests:
180 flaky = False
181 slow = False
182 pass_fail = False
183 testname = self.CommonTestName(t)
184 if testname in self.rules:
185 used_rules.add(testname)
186 # Even for skipped tests, as the TestCase object stays around and
187 # PrintReport() uses it.
188 t.outcomes = self.rules[testname]
189 if statusfile.DoSkip(t.outcomes):
190 continue # Don't add skipped tests to |filtered|.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400191 for outcome in t.outcomes:
192 if outcome.startswith('Flags: '):
193 t.flags += outcome[7:].split()
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000194 flaky = statusfile.IsFlaky(t.outcomes)
195 slow = statusfile.IsSlow(t.outcomes)
196 pass_fail = statusfile.IsPassOrFail(t.outcomes)
197 skip = False
198 for rule in self.wildcards:
199 assert rule[-1] == '*'
200 if testname.startswith(rule[:-1]):
201 used_rules.add(rule)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000202 t.outcomes |= self.wildcards[rule]
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000203 if statusfile.DoSkip(t.outcomes):
204 skip = True
205 break # "for rule in self.wildcards"
206 flaky = flaky or statusfile.IsFlaky(t.outcomes)
207 slow = slow or statusfile.IsSlow(t.outcomes)
208 pass_fail = pass_fail or statusfile.IsPassOrFail(t.outcomes)
209 if (skip or self._FilterFlaky(flaky, flaky_tests)
210 or self._FilterSlow(slow, slow_tests)
211 or self._FilterPassFail(pass_fail, pass_fail_tests)):
212 continue # "for t in self.tests"
213 filtered.append(t)
214 self.tests = filtered
215
216 if not warn_unused_rules:
217 return
218
219 for rule in self.rules:
220 if rule not in used_rules:
221 print("Unused rule: %s -> %s" % (rule, self.rules[rule]))
222 for rule in self.wildcards:
223 if rule not in used_rules:
224 print("Unused rule: %s -> %s" % (rule, self.wildcards[rule]))
225
226 def FilterTestCasesByArgs(self, args):
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000227 """Filter test cases based on command-line arguments.
228
229 An argument with an asterisk in the end will match all test cases
230 that have the argument as a prefix. Without asterisk, only exact matches
231 will be used with the exeption of the test-suite name as argument.
232 """
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000233 filtered = []
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000234 globs = []
235 exact_matches = []
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000236 for a in args:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000237 argpath = a.split('/')
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000238 if argpath[0] != self.name:
239 continue
240 if len(argpath) == 1 or (len(argpath) == 2 and argpath[1] == '*'):
241 return # Don't filter, run all tests in this suite.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000242 path = '/'.join(argpath[1:])
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000243 if path[-1] == '*':
244 path = path[:-1]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000245 globs.append(path)
246 else:
247 exact_matches.append(path)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000248 for t in self.tests:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000249 for a in globs:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000250 if t.path.startswith(a):
251 filtered.append(t)
252 break
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000253 for a in exact_matches:
254 if t.path == a:
255 filtered.append(t)
256 break
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000257 self.tests = filtered
258
259 def GetFlagsForTestCase(self, testcase, context):
260 raise NotImplementedError
261
262 def GetSourceForTest(self, testcase):
263 return "(no source available)"
264
265 def IsFailureOutput(self, output, testpath):
266 return output.exit_code != 0
267
268 def IsNegativeTest(self, testcase):
269 return False
270
271 def HasFailed(self, testcase):
272 execution_failed = self.IsFailureOutput(testcase.output, testcase.path)
273 if self.IsNegativeTest(testcase):
274 return not execution_failed
275 else:
276 return execution_failed
277
278 def GetOutcome(self, testcase):
279 if testcase.output.HasCrashed():
280 return statusfile.CRASH
281 elif testcase.output.HasTimedOut():
282 return statusfile.TIMEOUT
283 elif self.HasFailed(testcase):
284 return statusfile.FAIL
285 else:
286 return statusfile.PASS
287
288 def HasUnexpectedOutput(self, testcase):
289 outcome = self.GetOutcome(testcase)
290 return not outcome in (testcase.outcomes or [statusfile.PASS])
291
292 def StripOutputForTransmit(self, testcase):
293 if not self.HasUnexpectedOutput(testcase):
294 testcase.output.stdout = ""
295 testcase.output.stderr = ""
296
297 def CalculateTotalDuration(self):
298 self.total_duration = 0.0
299 for t in self.tests:
300 self.total_duration += t.duration
301 return self.total_duration
302
303
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000304class StandardVariantGenerator(VariantGenerator):
305 def FilterVariantsByTest(self, testcase):
306 return self.standard_variant
307
308
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000309class GoogleTestSuite(TestSuite):
310 def __init__(self, name, root):
311 super(GoogleTestSuite, self).__init__(name, root)
312
313 def ListTests(self, context):
314 shell = os.path.abspath(os.path.join(context.shell_dir, self.shell()))
315 if utils.IsWindows():
316 shell += ".exe"
317 output = commands.Execute(context.command_prefix +
318 [shell, "--gtest_list_tests"] +
319 context.extra_flags)
320 if output.exit_code != 0:
321 print output.stdout
322 print output.stderr
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400323 raise Exception("Test executable failed to list the tests.")
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000324 tests = []
325 test_case = ''
326 for line in output.stdout.splitlines():
327 test_desc = line.strip().split()[0]
328 if test_desc.endswith('.'):
329 test_case = test_desc
330 elif test_case and test_desc:
331 test = testcase.TestCase(self, test_case + test_desc, dependency=None)
332 tests.append(test)
333 tests.sort()
334 return tests
335
336 def GetFlagsForTestCase(self, testcase, context):
337 return (testcase.flags + ["--gtest_filter=" + testcase.path] +
338 ["--gtest_random_seed=%s" % context.random_seed] +
339 ["--gtest_print_time=0"] +
340 context.mode_flags)
341
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000342 def _VariantGeneratorFactory(self):
343 return StandardVariantGenerator
344
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000345 def shell(self):
346 return self.name