Home | History | Annotate | Download | only in test262
      1 # Copyright 2012 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 
     29 import hashlib
     30 import os
     31 import sys
     32 import tarfile
     33 import urllib
     34 
     35 from testrunner.local import testsuite
     36 from testrunner.objects import testcase
     37 
     38 
     39 TEST_262_ARCHIVE_REVISION = "99aac3bc1cad"  # This is the r365 revision.
     40 TEST_262_ARCHIVE_MD5 = "aadbd720ce9bdb4f8f3de066f4d7eea1"
     41 TEST_262_URL = "http://hg.ecmascript.org/tests/test262/archive/%s.tar.bz2"
     42 TEST_262_HARNESS = ["sta.js", "testBuiltInObject.js"]
     43 TEST_262_SKIP = ["intl402"]
     44 
     45 
     46 class Test262TestSuite(testsuite.TestSuite):
     47 
     48   def __init__(self, name, root):
     49     super(Test262TestSuite, self).__init__(name, root)
     50     self.testroot = os.path.join(root, "data", "test", "suite")
     51     self.harness = [os.path.join(self.root, "data", "test", "harness", f)
     52                     for f in TEST_262_HARNESS]
     53     self.harness += [os.path.join(self.root, "harness-adapt.js")]
     54 
     55   def CommonTestName(self, testcase):
     56     return testcase.path.split(os.path.sep)[-1]
     57 
     58   def ListTests(self, context):
     59     tests = []
     60     for dirname, dirs, files in os.walk(self.testroot):
     61       for dotted in [x for x in dirs if x.startswith(".")]:
     62         dirs.remove(dotted)
     63       for skipped in [x for x in dirs if x in TEST_262_SKIP]:
     64         dirs.remove(skipped)
     65       dirs.sort()
     66       files.sort()
     67       for filename in files:
     68         if filename.endswith(".js"):
     69           testname = os.path.join(dirname[len(self.testroot) + 1:],
     70                                   filename[:-3])
     71           case = testcase.TestCase(self, testname)
     72           tests.append(case)
     73     return tests
     74 
     75   def GetFlagsForTestCase(self, testcase, context):
     76     return (testcase.flags + context.mode_flags + self.harness +
     77             [os.path.join(self.testroot, testcase.path + ".js")])
     78 
     79   def GetSourceForTest(self, testcase):
     80     filename = os.path.join(self.testroot, testcase.path + ".js")
     81     with open(filename) as f:
     82       return f.read()
     83 
     84   def IsNegativeTest(self, testcase):
     85     return "@negative" in self.GetSourceForTest(testcase)
     86 
     87   def IsFailureOutput(self, output, testpath):
     88     if output.exit_code != 0:
     89       return True
     90     return "FAILED!" in output.stdout
     91 
     92   def DownloadData(self):
     93     revision = TEST_262_ARCHIVE_REVISION
     94     archive_url = TEST_262_URL % revision
     95     archive_name = os.path.join(self.root, "test262-%s.tar.bz2" % revision)
     96     directory_name = os.path.join(self.root, "data")
     97     directory_old_name = os.path.join(self.root, "data.old")
     98     if not os.path.exists(archive_name):
     99       print "Downloading test data from %s ..." % archive_url
    100       urllib.urlretrieve(archive_url, archive_name)
    101       if os.path.exists(directory_name):
    102         os.rename(directory_name, directory_old_name)
    103     if not os.path.exists(directory_name):
    104       print "Extracting test262-%s.tar.bz2 ..." % revision
    105       md5 = hashlib.md5()
    106       with open(archive_name, "rb") as f:
    107         for chunk in iter(lambda: f.read(8192), ""):
    108           md5.update(chunk)
    109       if md5.hexdigest() != TEST_262_ARCHIVE_MD5:
    110         os.remove(archive_name)
    111         raise Exception("Hash mismatch of test data file")
    112       archive = tarfile.open(archive_name, "r:bz2")
    113       if sys.platform in ("win32", "cygwin"):
    114         # Magic incantation to allow longer path names on Windows.
    115         archive.extractall(u"\\\\?\\%s" % self.root)
    116       else:
    117         archive.extractall(self.root)
    118       os.rename(os.path.join(self.root, "test262-%s" % revision),
    119                 directory_name)
    120 
    121 
    122 def GetSuite(name, root):
    123   return Test262TestSuite(name, root)
    124