1425bb815Sopenharmony_ci#!/usr/bin/env python
2425bb815Sopenharmony_ci
3425bb815Sopenharmony_ci# Copyright JS Foundation and other contributors, http://js.foundation
4425bb815Sopenharmony_ci#
5425bb815Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
6425bb815Sopenharmony_ci# you may not use this file except in compliance with the License.
7425bb815Sopenharmony_ci# You may obtain a copy of the License at
8425bb815Sopenharmony_ci#
9425bb815Sopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
10425bb815Sopenharmony_ci#
11425bb815Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
12425bb815Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS
13425bb815Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14425bb815Sopenharmony_ci# See the License for the specific language governing permissions and
15425bb815Sopenharmony_ci# limitations under the License.
16425bb815Sopenharmony_ci
17425bb815Sopenharmony_cifrom __future__ import print_function
18425bb815Sopenharmony_ciimport argparse
19425bb815Sopenharmony_ciimport os
20425bb815Sopenharmony_ciimport shutil
21425bb815Sopenharmony_ciimport subprocess
22425bb815Sopenharmony_ciimport sys
23425bb815Sopenharmony_ci
24425bb815Sopenharmony_ciimport util
25425bb815Sopenharmony_ci
26425bb815Sopenharmony_cidef get_platform_cmd_prefix():
27425bb815Sopenharmony_ci    if sys.platform == 'win32':
28425bb815Sopenharmony_ci        return ['cmd', '/S', '/C']
29425bb815Sopenharmony_ci    return ['python2']  # The official test262.py isn't python3 compatible, but has python shebang.
30425bb815Sopenharmony_ci
31425bb815Sopenharmony_ci
32425bb815Sopenharmony_cidef get_arguments():
33425bb815Sopenharmony_ci    execution_runtime = os.environ.get('RUNTIME', '')
34425bb815Sopenharmony_ci    parser = argparse.ArgumentParser()
35425bb815Sopenharmony_ci    parser.add_argument('--runtime', metavar='FILE', default=execution_runtime,
36425bb815Sopenharmony_ci                        help='Execution runtime (e.g. qemu)')
37425bb815Sopenharmony_ci    parser.add_argument('--engine', metavar='FILE', required=True,
38425bb815Sopenharmony_ci                        help='JerryScript binary to run tests with')
39425bb815Sopenharmony_ci    parser.add_argument('--test-dir', metavar='DIR', required=True,
40425bb815Sopenharmony_ci                        help='Directory contains test262 test suite')
41425bb815Sopenharmony_ci    group = parser.add_mutually_exclusive_group(required=True)
42425bb815Sopenharmony_ci    group.add_argument('--es51', action='store_true',
43425bb815Sopenharmony_ci                       help='Run test262 ES5.1 version')
44425bb815Sopenharmony_ci    group.add_argument('--es2015', action='store_true',
45425bb815Sopenharmony_ci                       help='Run test262 ES2015 version')
46425bb815Sopenharmony_ci
47425bb815Sopenharmony_ci    args = parser.parse_args()
48425bb815Sopenharmony_ci
49425bb815Sopenharmony_ci    if args.es2015:
50425bb815Sopenharmony_ci        args.test_dir = os.path.join(args.test_dir, 'es2015')
51425bb815Sopenharmony_ci    else:
52425bb815Sopenharmony_ci        args.test_dir = os.path.join(args.test_dir, 'es51')
53425bb815Sopenharmony_ci
54425bb815Sopenharmony_ci    return args
55425bb815Sopenharmony_ci
56425bb815Sopenharmony_ci
57425bb815Sopenharmony_cidef prepare_test262_test_suite(args):
58425bb815Sopenharmony_ci    if os.path.isdir(os.path.join(args.test_dir, '.git')):
59425bb815Sopenharmony_ci        return 0
60425bb815Sopenharmony_ci
61425bb815Sopenharmony_ci    return_code = subprocess.call(['git', 'clone', '--no-checkout',
62425bb815Sopenharmony_ci                                   'https://gitee.com/Han00000000/test262.git', args.test_dir])
63425bb815Sopenharmony_ci    if return_code:
64425bb815Sopenharmony_ci        print('Cloning test262 repository failed.')
65425bb815Sopenharmony_ci        return return_code
66425bb815Sopenharmony_ci
67425bb815Sopenharmony_ci    if args.es2015:
68425bb815Sopenharmony_ci        git_hash = 'fd44cd73dfbce0b515a2474b7cd505d6176a9eb5'
69425bb815Sopenharmony_ci    else:
70425bb815Sopenharmony_ci        git_hash = 'es5-tests'
71425bb815Sopenharmony_ci
72425bb815Sopenharmony_ci    return_code = subprocess.call(['git', 'checkout', git_hash], cwd=args.test_dir)
73425bb815Sopenharmony_ci    if return_code:
74425bb815Sopenharmony_ci        print('Cloning test262 repository failed - invalid git revision.')
75425bb815Sopenharmony_ci        return return_code
76425bb815Sopenharmony_ci
77425bb815Sopenharmony_ci    if args.es2015:
78425bb815Sopenharmony_ci        shutil.copyfile(os.path.join('tests', 'test262-es6-excludelist.xml'),
79425bb815Sopenharmony_ci                        os.path.join(args.test_dir, 'excludelist.xml'))
80425bb815Sopenharmony_ci
81425bb815Sopenharmony_ci        return_code = subprocess.call(['git', 'apply', os.path.join('..', '..', 'test262-es6.patch')],
82425bb815Sopenharmony_ci                                      cwd=args.test_dir)
83425bb815Sopenharmony_ci        if return_code:
84425bb815Sopenharmony_ci            print('Applying test262-es6.patch failed')
85425bb815Sopenharmony_ci            return return_code
86425bb815Sopenharmony_ci
87425bb815Sopenharmony_ci    else:
88425bb815Sopenharmony_ci        path_to_remove = os.path.join(args.test_dir, 'test', 'suite', 'bestPractice')
89425bb815Sopenharmony_ci        if os.path.isdir(path_to_remove):
90425bb815Sopenharmony_ci            shutil.rmtree(path_to_remove)
91425bb815Sopenharmony_ci
92425bb815Sopenharmony_ci        path_to_remove = os.path.join(args.test_dir, 'test', 'suite', 'intl402')
93425bb815Sopenharmony_ci        if os.path.isdir(path_to_remove):
94425bb815Sopenharmony_ci            shutil.rmtree(path_to_remove)
95425bb815Sopenharmony_ci
96425bb815Sopenharmony_ci    return 0
97425bb815Sopenharmony_ci
98425bb815Sopenharmony_cidef main(args):
99425bb815Sopenharmony_ci    return_code = prepare_test262_test_suite(args)
100425bb815Sopenharmony_ci    if return_code:
101425bb815Sopenharmony_ci        return return_code
102425bb815Sopenharmony_ci
103425bb815Sopenharmony_ci    if sys.platform == 'win32':
104425bb815Sopenharmony_ci        original_timezone = util.get_timezone()
105425bb815Sopenharmony_ci        util.set_sighdl_to_reset_timezone(original_timezone)
106425bb815Sopenharmony_ci        util.set_timezone('Pacific Standard Time')
107425bb815Sopenharmony_ci
108425bb815Sopenharmony_ci    proc = subprocess.Popen(get_platform_cmd_prefix() +
109425bb815Sopenharmony_ci                            [os.path.join(args.test_dir, 'tools/packaging/test262.py'),
110425bb815Sopenharmony_ci                             '--command', (args.runtime + ' ' + args.engine).strip(),
111425bb815Sopenharmony_ci                             '--tests', args.test_dir,
112425bb815Sopenharmony_ci                             '--summary'],
113425bb815Sopenharmony_ci                            universal_newlines=True,
114425bb815Sopenharmony_ci                            stdout=subprocess.PIPE)
115425bb815Sopenharmony_ci
116425bb815Sopenharmony_ci    return_code = 1
117425bb815Sopenharmony_ci    with open(os.path.join(os.path.dirname(args.engine), 'test262.report'), 'w') as output_file:
118425bb815Sopenharmony_ci        counter = 0
119425bb815Sopenharmony_ci        summary_found = False
120425bb815Sopenharmony_ci        while True:
121425bb815Sopenharmony_ci            counter += 1
122425bb815Sopenharmony_ci            output = proc.stdout.readline()
123425bb815Sopenharmony_ci            if not output:
124425bb815Sopenharmony_ci                break
125425bb815Sopenharmony_ci            output_file.write(output)
126425bb815Sopenharmony_ci            if not summary_found and (counter % 100) == 0:
127425bb815Sopenharmony_ci                print("\rExecuted approx %d tests..." % counter, end='')
128425bb815Sopenharmony_ci
129425bb815Sopenharmony_ci            if output.startswith('=== Summary ==='):
130425bb815Sopenharmony_ci                summary_found = True
131425bb815Sopenharmony_ci                print('')
132425bb815Sopenharmony_ci
133425bb815Sopenharmony_ci            if summary_found:
134425bb815Sopenharmony_ci                print(output, end='')
135425bb815Sopenharmony_ci                if 'All tests succeeded' in output:
136425bb815Sopenharmony_ci                    return_code = 0
137425bb815Sopenharmony_ci
138425bb815Sopenharmony_ci    proc.wait()
139425bb815Sopenharmony_ci
140425bb815Sopenharmony_ci    if sys.platform == 'win32':
141425bb815Sopenharmony_ci        util.set_timezone(original_timezone)
142425bb815Sopenharmony_ci
143425bb815Sopenharmony_ci    return return_code
144425bb815Sopenharmony_ci
145425bb815Sopenharmony_ci
146425bb815Sopenharmony_ciif __name__ == "__main__":
147425bb815Sopenharmony_ci    sys.exit(main(get_arguments()))
148