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