• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3#  Copyright (c) 2023 Huawei Device Co., Ltd.
4#  Licensed under the Apache License, Version 2.0 (the "License");
5#  you may not use this file except in compliance with the License.
6#  You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10#  Unless required by applicable law or agreed to in writing, software
11#  distributed under the License is distributed on an "AS IS" BASIS,
12#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13#  See the License for the specific language governing permissions and
14#  limitations under the License.
15
16
17import os
18import argparse
19import subprocess
20import re
21
22asserts = os.path.join(os.getcwd(), "test262", 'assert')
23total = 0
24errno = 0
25
26
27def is_testcase_exist(parse, arg):
28    if not os.path.isabs(arg):
29        arg = './' + arg
30    if not os.path.exists(arg):
31        parse.error("The directory or file '%s' does not exist" % arg)
32    return os.path.abspath(arg)
33
34
35def is_directory(parse, arg):
36    if not os.path.isdir(arg):
37        parse.error("The directory '%s' does not exist" % arg)
38
39    return os.path.abspath(arg)
40
41
42# create a parser object
43parser = argparse.ArgumentParser(description="TypeScript Test262 Test Tool")
44parser.add_argument("release", nargs='*', metavar="release", type=lambda arg: is_testcase_exist(parser, arg),
45                    help="All test case in the release will be execute")
46parser.add_argument(
47    '--js-runtime', dest='js_runtime_path', default=None, type=lambda arg: is_directory(parser, arg),
48    help='the path of js vm runtime')
49parser.add_argument(
50    '--LD_LIBRARY_PATH', dest='ld_library_path', default=None, help='LD_LIBRARY_PATH')
51
52parser.add_argument(
53    '--es2abc', dest='es2abc', default=None, help='ES2ABC_PATH')
54
55parser.add_argument(
56    '--ark_aot', dest='ark_aot', default=None, help='ARK_AOT')
57
58args = parser.parse_args()
59
60
61def get_path_file(dir_path, allfiles=None):
62    if allfiles is None:
63        allfiles = []
64    file_or_dir = os.listdir(dir_path)
65    for fileOrDir in file_or_dir:
66        file_or_dir_path = os.path.join(dir_path, fileOrDir)
67        if os.path.isfile(file_or_dir_path):
68            allfiles.append(file_or_dir_path)
69        else:
70            get_path_file(file_or_dir_path, allfiles)
71    return allfiles
72
73
74def get_node_cmd(filepath):
75    cmd = ['ts-node', filepath]
76    return cmd
77
78
79def get_error_message(strs, filename):
80    if len(re.findall(filename + r':(\d+)', strs)) > 0:
81        line_number = re.findall(filename + r':(\d+)', strs)
82    else:
83        line_number = 0
84    err_message = strs
85    return err_message, line_number
86
87
88def get_es2abc_cmd(filepath):
89    abc_file_path = ("%s.abc" % (os.path.splitext(filepath)[0]))
90    abc_file_path_temp = abc_file_path
91    cmd = [args.es2abc + 'es2abc']
92    cmd.extend(['--module', '--merge-abc', '--type-extractor', '--extension=ts', '--output', abc_file_path, filepath])
93    return cmd, abc_file_path_temp
94
95
96# get es2abc file commands
97def get_ark_js_cmd_aot(abc_file_path_temp):
98    os.environ.setdefault("LD_LIBRARY_PATH", args.ld_library_path)
99    path = abc_file_path_temp.split('.abc')[0]
100    run_abc_cmd = [os.path.join(args.js_runtime_path, 'ark_js_vm'), '--asm-interpreter=true',
101                   '--aot-file=' + path, '--entry-point=' + abc_file_path_temp.split('/')[-1].split('.abc')[0],
102                   abc_file_path_temp]
103    return run_abc_cmd
104
105
106# get es2abc file commands
107def get_ark_js_cmd(abc_file_path_temp):
108    os.environ.setdefault("LD_LIBRARY_PATH", args.ld_library_path)
109    run_abc_cmd = [os.path.join(args.js_runtime_path, 'ark_js_vm'), '--asm-interpreter=true',
110                   '--entry-point=' + abc_file_path_temp.split('/')[-1].split('.abc')[0],
111                   abc_file_path_temp]
112    return run_abc_cmd
113
114
115def create_abc(path):
116    # compiler to abc
117    cmd, abc_file_path_temp = get_es2abc_cmd(path)
118    process = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
119    process.communicate(timeout=5000)
120
121
122def test_es2abc(path):
123    # compiler to abc
124    cmd, abc_file_path_temp = get_es2abc_cmd(path)
125    process = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
126    out, err = process.communicate(timeout=5000)
127    return_code = process.returncode
128    if return_code != 0:
129        get_error_message(
130            out.decode("utf-8", errors="ignore") + err.decode("utf-8", errors="ignore"), path)
131        return 0
132
133    paths = abc_file_path_temp.split('.abc')[0]
134    os.environ.setdefault("LD_LIBRARY_PATH", args.ld_library_path)
135    cmds = [args.ark_aot, '--aot-file=', paths, abc_file_path_temp]
136    # execute ark_aot
137    if args.ark_aot:
138        process = subprocess.Popen(cmds, stdin=subprocess.PIPE,
139                                   stdout=subprocess.PIPE, stderr=subprocess.PIPE)
140        process.communicate(timeout=5000)
141
142        # execute ark_js_vm
143        process = subprocess.Popen(get_ark_js_cmd_aot(abc_file_path_temp), stdin=subprocess.PIPE,
144                                   stdout=subprocess.PIPE,
145                                   stderr=subprocess.PIPE)
146        out, err = process.communicate(timeout=5000)
147    else:
148        # execute ark_js_vm
149        process = subprocess.Popen(get_ark_js_cmd(abc_file_path_temp), stdin=subprocess.PIPE, stdout=subprocess.PIPE,
150                                   stderr=subprocess.PIPE)
151        out, err = process.communicate(timeout=5000)
152
153    if ("SUCCESS" not in out.decode("utf-8", errors="ignore")) | (
154            "AsyncTestFailure" in out.decode("utf-8", errors="ignore")):
155        err_msg, lines = get_error_message(out.decode("utf-8", errors="ignore") + err.decode("utf-8", errors="ignore"),
156                                           filePath)
157        print("NEW ERROR :")
158        print(filePath, err_msg, sep='\t')
159        return 1
160    else:
161        return 0
162
163
164contents = ['{']
165for ass in os.listdir(asserts):
166    if os.path.isfile(os.path.join(asserts, ass)):
167        with open(os.path.join(asserts, ass), 'r', encoding="utf-8") as file:
168            contents.append('\n')
169            contents.extend(file.readlines())
170            contents.append('\n')
171print("TEST CASE", "FAIL REASON", "FAIL LINE", sep="\t")
172
173for file_path in args.release:
174    all_files = get_path_file(file_path)
175    for filePath in all_files:
176        create_abc(filePath)
177    for filePath in all_files:
178        count = 0
179        is_case = False
180        c = []
181        c.extend(contents)
182        content = []
183        with open(filePath, 'r', encoding="ISO-8859-1") as file:
184            while True:
185                count += 1
186                line = file.readline()
187                start_pattern = re.compile(r'^\/\*\-*')
188                if start_pattern.match(line):
189                    is_case = True
190                    break
191                if count > 100:
192                    break
193
194            if is_case:
195                with open(filePath, 'r', encoding="utf-8") as file1:
196                    content.extend(file1.readlines())
197                    content.append('\n')
198                    c.extend(content)
199                with open(filePath, 'w', encoding="utf-8") as files:
200                    c.append('}')
201                    c.extend(['\n', 'print("SUCCESS")'])
202                    files.writelines(c)
203
204        # abc
205        if is_case:
206            total += 1
207            errno += test_es2abc(filePath)
208            # cover files
209            with open(filePath, 'w', encoding="utf-8") as files:
210                content[-1] = content[-1].replace('\n', '')
211                files.writelines(content)
212
213# delete abc,ai,an files
214for file_paths in get_path_file("test262/test"):
215    if file_paths.endswith(".abc") | file_paths.endswith(".ai") | file_paths.endswith(".an"):
216        if os.path.exists(file_paths):
217            os.remove(file_paths)
218
219print("TOTAL CASE COUNT:%d" % total)
220print("SUCCESS CASE COUNT:%d" % (total - errno))
221print("FAILED CASE COUNT:%d" % errno)
222