1# Copyright 2008 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
28import itertools
29import os
30import re
31
32from testrunner.local import testsuite
33from testrunner.local import utils
34from testrunner.objects import testcase
35
36
37FLAGS_PATTERN = re.compile(r"//\s+Flags:(.*)")
38
39
40class MessageTestSuite(testsuite.TestSuite):
41  def __init__(self, name, root):
42    super(MessageTestSuite, self).__init__(name, root)
43
44  def ListTests(self, context):
45    tests = []
46    for dirname, dirs, files in os.walk(self.root):
47      for dotted in [x for x in dirs if x.startswith('.')]:
48        dirs.remove(dotted)
49      dirs.sort()
50      files.sort()
51      for filename in files:
52        if filename.endswith(".js"):
53          testname = os.path.join(dirname[len(self.root) + 1:], filename[:-3])
54          test = testcase.TestCase(self, testname)
55          tests.append(test)
56    return tests
57
58  def GetFlagsForTestCase(self, testcase, context):
59    source = self.GetSourceForTest(testcase)
60    result = []
61    flags_match = re.findall(FLAGS_PATTERN, source)
62    for match in flags_match:
63      result += match.strip().split()
64    result += context.mode_flags
65    result.append(os.path.join(self.root, testcase.path + ".js"))
66    return testcase.flags + result
67
68  def GetSourceForTest(self, testcase):
69    filename = os.path.join(self.root, testcase.path + self.suffix())
70    with open(filename) as f:
71      return f.read()
72
73  def _IgnoreLine(self, string):
74    """Ignore empty lines, valgrind output and Android output."""
75    if not string: return True
76    return (string.startswith("==") or string.startswith("**") or
77            string.startswith("ANDROID") or
78            # These five patterns appear in normal Native Client output.
79            string.startswith("DEBUG MODE ENABLED") or
80            string.startswith("tools/nacl-run.py") or
81            string.find("BYPASSING ALL ACL CHECKS") > 0 or
82            string.find("Native Client module will be loaded") > 0 or
83            string.find("NaClHostDescOpen:") > 0)
84
85  def IsFailureOutput(self, output, testpath):
86    expected_path = os.path.join(self.root, testpath + ".out")
87    expected_lines = []
88    # Can't use utils.ReadLinesFrom() here because it strips whitespace.
89    with open(expected_path) as f:
90      for line in f:
91        if line.startswith("#") or not line.strip(): continue
92        expected_lines.append(line)
93    raw_lines = output.stdout.splitlines()
94    actual_lines = [ s for s in raw_lines if not self._IgnoreLine(s) ]
95    env = { "basename": os.path.basename(testpath + ".js") }
96    if len(expected_lines) != len(actual_lines):
97      return True
98    for (expected, actual) in itertools.izip_longest(
99        expected_lines, actual_lines, fillvalue=''):
100      pattern = re.escape(expected.rstrip() % env)
101      pattern = pattern.replace("\\*", ".*")
102      pattern = "^%s$" % pattern
103      if not re.match(pattern, actual):
104        return True
105    return False
106
107  def StripOutputForTransmit(self, testcase):
108    pass
109
110
111def GetSuite(name, root):
112  return MessageTestSuite(name, root)
113