testcfg.py revision b8e0da25ee8efac3bb05cd6b2730aafbd96119f4
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 test
29import os
30from os.path import join, dirname, exists
31import re
32import tempfile
33
34FLAGS_PATTERN = re.compile(r"//\s+Flags:(.*)")
35FILES_PATTERN = re.compile(r"//\s+Files:(.*)")
36SELF_SCRIPT_PATTERN = re.compile(r"//\s+Env: TEST_FILE_NAME")
37
38
39class MjsunitTestCase(test.TestCase):
40
41  def __init__(self, path, file, mode, context, config):
42    super(MjsunitTestCase, self).__init__(context, path, mode)
43    self.file = file
44    self.config = config
45    self.self_script = False
46
47  def GetLabel(self):
48    return "%s %s" % (self.mode, self.GetName())
49
50  def GetName(self):
51    return self.path[-1]
52
53  def GetCommand(self):
54    result = self.config.context.GetVmCommand(self, self.mode)
55    source = open(self.file).read()
56    flags_match = FLAGS_PATTERN.search(source)
57    if flags_match:
58      result += flags_match.group(1).strip().split()
59    additional_files = []
60    files_match = FILES_PATTERN.search(source);
61    # Accept several lines of 'Files:'
62    while True:
63      if files_match:
64        additional_files += files_match.group(1).strip().split()
65        files_match = FILES_PATTERN.search(source, files_match.end())
66      else:
67        break
68    for a_file in additional_files:
69      result.append(join(dirname(self.config.root), '..', a_file))
70    framework = join(dirname(self.config.root), 'mjsunit', 'mjsunit.js')
71    if SELF_SCRIPT_PATTERN.search(source):
72      result.append(self.CreateSelfScript())
73    result += [framework, self.file]
74    return result
75
76  def GetSource(self):
77    return open(self.file).read()
78
79  def CreateSelfScript(self):
80    (fd_self_script, self_script) = tempfile.mkstemp(suffix=".js")
81    def MakeJsConst(name, value):
82      return "var %(name)s=\'%(value)s\';\n" % \
83             {'name': name, \
84              'value': value.replace('\\', '\\\\').replace('\'', '\\\'') }
85    try:
86      os.write(fd_self_script, MakeJsConst('TEST_FILE_NAME', self.file))
87    except IOError, e:
88      test.PrintError("write() " + str(e))
89    os.close(fd_self_script)
90    self.self_script = self_script
91    return self_script
92
93  def AfterRun(self, result):
94    if self.self_script and (not result.HasPreciousOutput()):
95      test.CheckedUnlink(self.self_script)
96
97class MjsunitTestConfiguration(test.TestConfiguration):
98
99  def __init__(self, context, root):
100    super(MjsunitTestConfiguration, self).__init__(context, root)
101
102  def Ls(self, path):
103    def SelectTest(name):
104      return name.endswith('.js') and name != 'mjsunit.js'
105    return [f[:-3] for f in os.listdir(path) if SelectTest(f)]
106
107  def ListTests(self, current_path, path, mode):
108    mjsunit = [current_path + [t] for t in self.Ls(self.root)]
109    regress = [current_path + ['regress', t] for t in self.Ls(join(self.root, 'regress'))]
110    bugs = [current_path + ['bugs', t] for t in self.Ls(join(self.root, 'bugs'))]
111    third_party = [current_path + ['third_party', t] for t in self.Ls(join(self.root, 'third_party'))]
112    tools = [current_path + ['tools', t] for t in self.Ls(join(self.root, 'tools'))]
113    compiler = [current_path + ['compiler', t] for t in self.Ls(join(self.root, 'compiler'))]
114    mjsunit.sort()
115    regress.sort()
116    bugs.sort()
117    third_party.sort()
118    tools.sort()
119    compiler.sort()
120    all_tests = mjsunit + regress + bugs + third_party + tools + compiler
121    result = []
122    for test in all_tests:
123      if self.Contains(path, test):
124        file_path = join(self.root, reduce(join, test[1:], "") + ".js")
125        result.append(MjsunitTestCase(test, file_path, mode, self.context, self))
126    return result
127
128  def GetBuildRequirements(self):
129    return ['sample', 'sample=shell']
130
131  def GetTestStatus(self, sections, defs):
132    status_file = join(self.root, 'mjsunit.status')
133    if exists(status_file):
134      test.ReadConfigurationInto(status_file, sections, defs)
135
136
137
138def GetConfiguration(context, root):
139  return MjsunitTestConfiguration(context, root)
140