• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1from __future__ import print_function
2# Copyright 2008 the V8 project authors. All rights reserved.
3# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions are
5# met:
6#
7#     * Redistributions of source code must retain the above copyright
8#       notice, this list of conditions and the following disclaimer.
9#     * Redistributions in binary form must reproduce the above
10#       copyright notice, this list of conditions and the following
11#       disclaimer in the documentation and/or other materials provided
12#       with the distribution.
13#     * Neither the name of Google Inc. nor the names of its
14#       contributors may be used to endorse or promote products derived
15#       from this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29import test
30import os
31from os.path import join, exists, basename, isdir
32import re
33from functools import reduce
34
35FLAGS_PATTERN = re.compile(r"//\s+Flags:(.*)")
36
37class MessageTestCase(test.TestCase):
38
39  def __init__(self, path, file, expected, arch, mode, context, config):
40    super(MessageTestCase, self).__init__(context, path, arch, mode)
41    self.file = file
42    self.expected = expected
43    self.config = config
44    self.arch = arch
45    self.mode = mode
46
47  def IgnoreLine(self, str):
48    """Ignore empty lines and valgrind output."""
49    if not str.strip(): return True
50    else: return str.startswith('==') or str.startswith('**')
51
52  def IsFailureOutput(self, output):
53    f = open(self.expected)
54    # Skip initial '#' comment and spaces
55    #for line in f:
56    #  if (not line.startswith('#')) and (not line.strip()):
57    #    break
58    # Convert output lines to regexps that we can match
59    env = { 'basename': basename(self.file) }
60    patterns = [ ]
61    for line in f:
62      if not line.strip():
63        continue
64      pattern = re.escape(line.rstrip() % env)
65      pattern = pattern.replace('\\*', '.*')
66      pattern = '^%s$' % pattern
67      patterns.append(pattern)
68    # Compare actual output with the expected
69    raw_lines = (output.stdout + output.stderr).split('\n')
70    outlines = [ s for s in raw_lines if not self.IgnoreLine(s) ]
71    if len(outlines) != len(patterns):
72      print("length differs.")
73      print("expect=%d" % len(patterns))
74      print("actual=%d" % len(outlines))
75      print("patterns:")
76      for i in range(len(patterns)):
77        print("pattern = %s" % patterns[i])
78      print("outlines:")
79      for i in range(len(outlines)):
80        print("outline = %s" % outlines[i])
81      return True
82    for i in range(len(patterns)):
83      if not re.match(patterns[i], outlines[i]):
84        print("match failed")
85        print("line=%d" % i)
86        print("expect=%s" % patterns[i])
87        print("actual=%s" % outlines[i])
88        return True
89    return False
90
91  def GetLabel(self):
92    return "%s %s" % (self.mode, self.GetName())
93
94  def GetName(self):
95    return self.path[-1]
96
97  def GetCommand(self):
98    result = [self.config.context.GetVm(self.arch, self.mode)]
99    source = open(self.file).read()
100    flags_match = FLAGS_PATTERN.search(source)
101    if flags_match:
102      result += flags_match.group(1).strip().split()
103    result.append(self.file)
104    return result
105
106  def GetSource(self):
107    return (open(self.file).read()
108          + "\n--- expected output ---\n"
109          + open(self.expected).read())
110
111
112class MessageTestConfiguration(test.TestConfiguration):
113  def Ls(self, path):
114    if isdir(path):
115      return [f for f in os.listdir(path)
116              if f.endswith('.js') or f.endswith('.mjs')]
117    else:
118      return []
119
120  def ListTests(self, current_path, path, arch, mode):
121    all_tests = [current_path + [t] for t in self.Ls(self.root)]
122    result = []
123    for tst in all_tests:
124      if self.Contains(path, tst):
125        file_path = join(self.root, reduce(join, tst[1:], ''))
126        output_path = file_path[:file_path.rfind('.')] + '.out'
127        if not exists(output_path):
128          raise Exception("Could not find %s" % output_path)
129        result.append(MessageTestCase(tst, file_path, output_path,
130                                      arch, mode, self.context, self))
131    return result
132
133  def GetBuildRequirements(self):
134    return ['sample', 'sample=shell']
135
136
137def GetConfiguration(context, root):
138  return MessageTestConfiguration(context, root, 'message')
139