• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2
3# Copyright JS Foundation and other contributors, http://js.foundation
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17from __future__ import print_function
18import argparse
19import os
20import shutil
21import subprocess
22import sys
23
24import util
25
26def get_platform_cmd_prefix():
27    if sys.platform == 'win32':
28        return ['cmd', '/S', '/C']
29    return ['python2']  # The official test262.py isn't python3 compatible, but has python shebang.
30
31
32def get_arguments():
33    execution_runtime = os.environ.get('RUNTIME', '')
34    parser = argparse.ArgumentParser()
35    parser.add_argument('--runtime', metavar='FILE', default=execution_runtime,
36                        help='Execution runtime (e.g. qemu)')
37    parser.add_argument('--engine', metavar='FILE', required=True,
38                        help='JerryScript binary to run tests with')
39    parser.add_argument('--test-dir', metavar='DIR', required=True,
40                        help='Directory contains test262 test suite')
41    group = parser.add_mutually_exclusive_group(required=True)
42    group.add_argument('--es51', action='store_true',
43                       help='Run test262 ES5.1 version')
44    group.add_argument('--es2015', action='store_true',
45                       help='Run test262 ES2015 version')
46
47    args = parser.parse_args()
48
49    if args.es2015:
50        args.test_dir = os.path.join(args.test_dir, 'es2015')
51    else:
52        args.test_dir = os.path.join(args.test_dir, 'es51')
53
54    return args
55
56
57def prepare_test262_test_suite(args):
58    if os.path.isdir(os.path.join(args.test_dir, '.git')):
59        return 0
60
61    return_code = subprocess.call(['git', 'clone', '--no-checkout',
62                                   'https://gitee.com/Han00000000/test262.git', args.test_dir])
63    if return_code:
64        print('Cloning test262 repository failed.')
65        return return_code
66
67    if args.es2015:
68        git_hash = 'fd44cd73dfbce0b515a2474b7cd505d6176a9eb5'
69    else:
70        git_hash = 'es5-tests'
71
72    return_code = subprocess.call(['git', 'checkout', git_hash], cwd=args.test_dir)
73    if return_code:
74        print('Cloning test262 repository failed - invalid git revision.')
75        return return_code
76
77    if args.es2015:
78        shutil.copyfile(os.path.join('tests', 'test262-es6-excludelist.xml'),
79                        os.path.join(args.test_dir, 'excludelist.xml'))
80
81        return_code = subprocess.call(['git', 'apply', os.path.join('..', '..', 'test262-es6.patch')],
82                                      cwd=args.test_dir)
83        if return_code:
84            print('Applying test262-es6.patch failed')
85            return return_code
86
87    else:
88        path_to_remove = os.path.join(args.test_dir, 'test', 'suite', 'bestPractice')
89        if os.path.isdir(path_to_remove):
90            shutil.rmtree(path_to_remove)
91
92        path_to_remove = os.path.join(args.test_dir, 'test', 'suite', 'intl402')
93        if os.path.isdir(path_to_remove):
94            shutil.rmtree(path_to_remove)
95
96    return 0
97
98def main(args):
99    return_code = prepare_test262_test_suite(args)
100    if return_code:
101        return return_code
102
103    if sys.platform == 'win32':
104        original_timezone = util.get_timezone()
105        util.set_sighdl_to_reset_timezone(original_timezone)
106        util.set_timezone('Pacific Standard Time')
107
108    proc = subprocess.Popen(get_platform_cmd_prefix() +
109                            [os.path.join(args.test_dir, 'tools/packaging/test262.py'),
110                             '--command', (args.runtime + ' ' + args.engine).strip(),
111                             '--tests', args.test_dir,
112                             '--summary'],
113                            universal_newlines=True,
114                            stdout=subprocess.PIPE)
115
116    return_code = 1
117    with open(os.path.join(os.path.dirname(args.engine), 'test262.report'), 'w') as output_file:
118        counter = 0
119        summary_found = False
120        while True:
121            counter += 1
122            output = proc.stdout.readline()
123            if not output:
124                break
125            output_file.write(output)
126            if not summary_found and (counter % 100) == 0:
127                print("\rExecuted approx %d tests..." % counter, end='')
128
129            if output.startswith('=== Summary ==='):
130                summary_found = True
131                print('')
132
133            if summary_found:
134                print(output, end='')
135                if 'All tests succeeded' in output:
136                    return_code = 0
137
138    proc.wait()
139
140    if sys.platform == 'win32':
141        util.set_timezone(original_timezone)
142
143    return return_code
144
145
146if __name__ == "__main__":
147    sys.exit(main(get_arguments()))
148