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
29import hashlib
30import os
31import shutil
32import sys
33import tarfile
34
35from testrunner.local import testsuite
36from testrunner.local import utils
37from testrunner.objects import testcase
38
39
40TEST_262_ARCHIVE_REVISION = "fbba29f"  # This is the r365 revision.
41TEST_262_ARCHIVE_MD5 = "e1ff0db438cc12de8fb6da80621b4ef6"
42TEST_262_URL = "https://github.com/tc39/test262/tarball/%s"
43TEST_262_HARNESS = ["sta.js", "testBuiltInObject.js", "testIntl.js"]
44
45
46class 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      if context.noi18n and "intl402" in dirs:
64        dirs.remove("intl402")
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, "tc39-test262-%s.tar.gz" % 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      utils.URLRetrieve(archive_url, archive_name)
101      if os.path.exists(directory_name):
102        if os.path.exists(directory_old_name):
103          shutil.rmtree(directory_old_name)
104        os.rename(directory_name, directory_old_name)
105    if not os.path.exists(directory_name):
106      print "Extracting test262-%s.tar.gz ..." % revision
107      md5 = hashlib.md5()
108      with open(archive_name, "rb") as f:
109        for chunk in iter(lambda: f.read(8192), ""):
110          md5.update(chunk)
111      if md5.hexdigest() != TEST_262_ARCHIVE_MD5:
112        os.remove(archive_name)
113        raise Exception("Hash mismatch of test data file")
114      archive = tarfile.open(archive_name, "r:gz")
115      if sys.platform in ("win32", "cygwin"):
116        # Magic incantation to allow longer path names on Windows.
117        archive.extractall(u"\\\\?\\%s" % self.root)
118      else:
119        archive.extractall(self.root)
120      os.rename(os.path.join(self.root, "tc39-test262-%s" % revision),
121                directory_name)
122
123
124def GetSuite(name, root):
125  return Test262TestSuite(name, root)
126