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 self.parallel = True 47 48 def IgnoreLine(self, str): 49 """Ignore empty lines and valgrind output.""" 50 if not str.strip(): return True 51 else: return str.startswith('==') or str.startswith('**') 52 53 def IsFailureOutput(self, output): 54 f = open(self.expected) 55 # Skip initial '#' comment and spaces 56 #for line in f: 57 # if (not line.startswith('#')) and (not line.strip()): 58 # break 59 # Convert output lines to regexps that we can match 60 env = { 'basename': basename(self.file) } 61 patterns = [ ] 62 for line in f: 63 if not line.strip(): 64 continue 65 pattern = re.escape(line.rstrip() % env) 66 pattern = pattern.replace('\\*', '.*') 67 pattern = '^%s$' % pattern 68 patterns.append(pattern) 69 # Compare actual output with the expected 70 raw_lines = (output.stdout + output.stderr).split('\n') 71 outlines = [ s for s in raw_lines if not self.IgnoreLine(s) ] 72 if len(outlines) != len(patterns): 73 print("length differs.") 74 print("expect=%d" % len(patterns)) 75 print("actual=%d" % len(outlines)) 76 print("patterns:") 77 for i in range(len(patterns)): 78 print("pattern = %s" % patterns[i]) 79 print("outlines:") 80 for i in range(len(outlines)): 81 print("outline = %s" % outlines[i]) 82 return True 83 for i in range(len(patterns)): 84 if not re.match(patterns[i], outlines[i]): 85 print("match failed") 86 print("line=%d" % i) 87 print("expect=%s" % patterns[i]) 88 print("actual=%s" % outlines[i]) 89 return True 90 return False 91 92 def GetLabel(self): 93 return "%s %s" % (self.mode, self.GetName()) 94 95 def GetName(self): 96 return self.path[-1] 97 98 def GetCommand(self): 99 result = [self.config.context.GetVm(self.arch, self.mode)] 100 source = open(self.file).read() 101 flags_match = FLAGS_PATTERN.search(source) 102 if flags_match: 103 result += flags_match.group(1).strip().split() 104 result.append(self.file) 105 return result 106 107 def GetSource(self): 108 return (open(self.file).read() 109 + "\n--- expected output ---\n" 110 + open(self.expected).read()) 111 112 113class MessageTestConfiguration(test.TestConfiguration): 114 def Ls(self, path): 115 if isdir(path): 116 return [f for f in os.listdir(path) 117 if f.endswith('.js') or f.endswith('.mjs')] 118 else: 119 return [] 120 121 def ListTests(self, current_path, path, arch, mode): 122 all_tests = [current_path + [t] for t in self.Ls(self.root)] 123 result = [] 124 for tst in all_tests: 125 if self.Contains(path, tst): 126 file_path = join(self.root, reduce(join, tst[1:], '')) 127 output_path = file_path[:file_path.rfind('.')] + '.out' 128 if not exists(output_path): 129 raise Exception("Could not find %s" % output_path) 130 result.append(MessageTestCase(tst, file_path, output_path, 131 arch, mode, self.context, self)) 132 return result 133 134 def GetBuildRequirements(self): 135 return ['sample', 'sample=shell'] 136 137 138def GetConfiguration(context, root): 139 return MessageTestConfiguration(context, root, 'message') 140