From 1200bc5521395363d57d636b9c14f5e71b8b3f30 Mon Sep 17 00:00:00 2001 From: pangdesong Date: Sun, 28 Apr 2024 17:38:11 +0800 Subject: [PATCH] Support arkguard for ts_extra_tests by --enable-arkguard Issue:https://gitee.com/openharmony/third_party_typescript/issues/I9KNVU Change-Id: Ia08514ebf8913253fc5c0de6761a2a269054621c Signed-off-by: pangdesong Change-Id: Idd40353ad2da5196c7ac1d7fe4d01de4271b8553 --- .gitignore | 4 +- tests/ts_extra_tests/run_ts_case.py | 243 +++++++++++++---------- tests/ts_extra_tests/tool/test_helper.py | 220 ++++++++++---------- tests/ts_extra_tests/tool/testcfg.py | 29 ++- 4 files changed, 279 insertions(+), 217 deletions(-) diff --git a/.gitignore b/.gitignore index 2c703f01f6..0adaa95b74 100644 --- a/.gitignore +++ b/.gitignore @@ -96,4 +96,6 @@ tests/cases/user/puppeteer/puppeteer tests/cases/user/axios-src/axios-src tests/cases/user/prettier/prettier .eslintcache -tests/verify_3rd_libs/libs \ No newline at end of file +tests/verify_3rd_libs/libs +.local +__pycache__ diff --git a/tests/ts_extra_tests/run_ts_case.py b/tests/ts_extra_tests/run_ts_case.py index 19bdae7e86..e6aa7053ac 100644 --- a/tests/ts_extra_tests/run_ts_case.py +++ b/tests/ts_extra_tests/run_ts_case.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -# Copyright (c) 2023 Huawei Device Co., Ltd. +# Copyright (c) 2023-2024 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -18,21 +18,10 @@ import argparse import os import shutil -from tool.test_helper import get_path_file, get_disable_list, is_disable_case +from tool.test_helper import list_all_test_files_in_dir, get_disable_list, is_disabled_case from tool.testcfg import TestCase -TEMP_PATH = os.getcwd() + '/testTmp4/' - -if os.path.exists(TEMP_PATH): - shutil.rmtree(TEMP_PATH) - -if not os.path.exists(TEMP_PATH): - os.mkdir(TEMP_PATH) - -total_case = 0 -failed_case = 0 -TestCase.temp_path = TEMP_PATH - +TEMP_DIR_NAME = ".local/" def is_testcase_exist(parsers, arg): if not os.path.isabs(arg): @@ -77,115 +66,159 @@ def parse_and_execute(path, ark_runtime=False, skip_negative=True): else: return False, False +def parse_input_args(): + # create a parser object + parser = argparse.ArgumentParser(description="TypeScript Spec&Feature Test Tool") -# create a parser object -parser = argparse.ArgumentParser(description="TypeScript Spec&Feature Test Tool") + # files or command + parser.add_argument("release", nargs='*', metavar="release", type=lambda arg: is_testcase_exist(parser, arg), + help="All test case in the release will be execute") -# files or command -parser.add_argument("release", nargs='*', metavar="release", type=lambda arg: is_testcase_exist(parser, arg), - help="All test case in the release will be execute") + parser.add_argument("-a", "--ark_runtime", action="store_true", default=False, help="test on ark_runtime") -parser.add_argument("-a", "--ark_runtime", action="store_true", default=False, help="test on ark_runtime") + parser.add_argument("-s", "--skip-abnormal-case", action="store_true", default=False, help="skip abnormal test case") -parser.add_argument("-s", "--skip-abnormal-case", action="store_true", default=False, help="skip abnormal test case") + parser.add_argument("-v", "--version", dest='limit_version', default=None, help="version limit") -parser.add_argument("-v", "--version", dest='limit_version', default=None, help="version limit") + # skip list + parser.add_argument("-d", "--disable-list", type=lambda arg: is_file(parser, arg), default=None, + help="path to the file that contains test to skip") -# skip list -parser.add_argument("-d", "--disable-list", type=lambda arg: is_file(parser, arg), default=None, - help="path to the file that contains test to skip") + parser.add_argument( + '--js-runtime', dest='js_runtime_path', default=None, type=lambda arg: is_directory(parser, arg), + help='the path of js vm runtime') + parser.add_argument( + '--LD_LIBRARY_PATH', dest='ld_library_path', default=None, help='LD_LIBRARY_PATH') -parser.add_argument( - '--js-runtime', dest='js_runtime_path', default=None, type=lambda arg: is_directory(parser, arg), - help='the path of js vm runtime') -parser.add_argument( - '--LD_LIBRARY_PATH', dest='ld_library_path', default=None, help='LD_LIBRARY_PATH') + parser.add_argument( + '--es2abc', dest='es2abc', default=None, help='ES2ABC_PATH') -parser.add_argument( - '--es2abc', dest='es2abc', default=None, help='ES2ABC_PATH') + parser.add_argument( + '-tsc', dest='tsc', default="tsc", help='tsc') -parser.add_argument( - '-tsc', dest='tsc', default="tsc", help='tsc') + parser.add_argument( + '--enable-arkguard', dest='enable_arkguard', action="store_true", default=False, help='enable arkguard for each test') -# parse the arguments from standard input -args = parser.parse_args() -if args.js_runtime_path: - TestCase.js_runtime_path = args.js_runtime_path -if args.ld_library_path: - TestCase.ld_library_path = args.ld_library_path -if args.es2abc: - TestCase.es2abc = args.es2abc + # parse the arguments from standard input + args = parser.parse_args() + return args -TestCase.tsc = args.tsc +def copy_directory(src_dir, target_dir): + if os.path.exists(target_dir): + shutil.rmtree(target_dir) + shutil.copytree(src_dir, target_dir) -disable_list = [] -if args.disable_list: - disable_list = get_disable_list(args.disable_list) -# tsc + node / es2abc -print("TEST CASE", "FAIL REASON", "FAIL LINE", sep="\t") +def copy_dir_to_temp(work_dir, dir_name, temp_dir): + src_dir = os.path.join(work_dir, dir_name) + target_dir = os.path.join(temp_dir, dir_name) + copy_directory(src_dir, target_dir) -for file_path in args.release: - root = file_path - # gen abc file - if args.ark_runtime: - for file_paths in get_path_file("suite", None, True): - if file_paths.endswith(".ts"): - test_case = TestCase(file_paths) - if not test_case.is_test_case: - test_case.create_abc(file_paths) +def prepare_for_js_runtime(args): + if args.js_runtime_path: + TestCase.js_runtime_path = args.js_runtime_path + if args.ld_library_path: + TestCase.ld_library_path = args.ld_library_path + if args.es2abc: + TestCase.es2abc = args.es2abc - for file_paths in get_path_file("test_ts_cases", None, True): - if file_paths.endswith(".ts"): - test_case = TestCase(file_paths) - if not test_case.is_test_case: - test_case.create_abc(file_paths) + TestCase.tsc = args.tsc - if is_disable_case(file_path, disable_list): - continue - if os.path.isfile(file_path): - is_test_count, failed = parse_and_execute(file_path, args.ark_runtime, args.skip_abnormal_case) - if is_test_count: - total_case += 1 - if failed: - failed_case += 1 - continue - for file_paths in get_path_file(file_path, None, True, args.limit_version): - if not file_paths.endswith(".ts"): + +def prepare_for_work_space(): + work_dir = os.getcwd() + temp_dir = os.path.join(work_dir, TEMP_DIR_NAME) + TestCase.temp_path = temp_dir + + copy_dir_to_temp(work_dir, "suite", temp_dir) + copy_dir_to_temp(work_dir, "test_ts_cases", temp_dir) + + +def parse_disabled_list(args): + disable_list = [] + if args.disable_list: + disable_list = get_disable_list(args.disable_list) + return disable_list + + +def prepare(args): + prepare_for_work_space() + prepare_for_js_runtime(args) + + if args.enable_arkguard: + TestCase.enable_arkguard = True; + + disable_list = parse_disabled_list(args) + suite_files = list_all_test_files_in_dir("%s/suite" % (TEMP_DIR_NAME), None) + case_files = list_all_test_files_in_dir("%s/test_ts_cases" % (TEMP_DIR_NAME), None, args.limit_version) + return disable_list, suite_files, case_files + + +def clean(): + # delete temp dir + if os.path.exists(TEMP_DIR_NAME): + shutil.rmtree(TEMP_DIR_NAME) + + +def run_cases(args, suite_files, case_files, disable_list): + total_case = 0 + failed_case = 0 + + for file_path in args.release: + # gen abc file + if args.ark_runtime: + for file_paths in suite_files: + if file_paths.endswith(".ts"): + test_case = TestCase(file_paths) + if not test_case.is_test_case: + test_case.create_abc(file_paths) + + for file_paths in case_files: + if file_paths.endswith(".ts"): + test_case = TestCase(file_paths) + if not test_case.is_test_case: + test_case.create_abc(file_paths) + + if is_disabled_case(file_path, disable_list): continue - if is_disable_case(file_paths, disable_list): + if os.path.isfile(file_path): + is_test_count, failed = parse_and_execute(file_path, args.ark_runtime, args.skip_abnormal_case) + if is_test_count: + total_case += 1 + if failed: + failed_case += 1 continue - is_test_count, failed = parse_and_execute(file_paths, args.ark_runtime, args.skip_abnormal_case) - if is_test_count: - total_case += 1 - if failed: - failed_case += 1 -# delete abc files -if args.ark_runtime: - for file_paths in get_path_file("suite", None, True): - if file_paths.endswith(".abc"): - if os.path.exists(file_paths): - os.remove(file_paths) + for file_paths in case_files: + if not file_paths.endswith(".ts"): + continue + if is_disabled_case(file_paths, disable_list): + continue + is_test_count, failed = parse_and_execute(file_paths, args.ark_runtime, args.skip_abnormal_case) + if is_test_count: + total_case += 1 + if failed: + failed_case += 1 + return total_case, failed_case - for file_paths in get_path_file("test_ts_cases", None, True): - if file_paths.endswith(".abc"): - if os.path.exists(file_paths): - os.remove(file_paths) - if file_paths.endswith(".ts"): - if os.path.exists(file_paths): - file = open(file_paths, 'r') - lines = file.readlines() - if lines[-1] == 'print("TESTCASE SUCCESS");': - lines.pop() - lines[-1] = lines[-1].replace('\n', '') - file = open(file_paths, 'w') - file.writelines(lines) - file.close() -print("TOTAL CASE COUNT:%d" % total_case) -print("SUCCESS CASE COUNT:%d" % (total_case-failed_case)) -print("FAILED CASE COUNT:%d" % failed_case) -# delete temp dir -if os.path.exists(TEMP_PATH): - shutil.rmtree(TEMP_PATH) +def print_result(total_case, failed_case): + print("TOTAL CASE COUNT: %d" % total_case) + print("SUCCESS CASE COUNT: %d" % (total_case - failed_case)) + print("FAILED CASE COUNT: %d" % failed_case) + + +def main(): + args = parse_input_args() + prepare(args) + disable_list, suite_files, case_files = prepare(args) + + # tsc + node / es2abc + print("TEST CASE", "FAIL REASON", "FAIL LINE", sep="\t") + total_case, failed_case = run_cases(args, suite_files, case_files, disable_list) + + print_result(total_case, failed_case) + clean() + + +main() \ No newline at end of file diff --git a/tests/ts_extra_tests/tool/test_helper.py b/tests/ts_extra_tests/tool/test_helper.py index cd4e224a25..96dbceeec9 100644 --- a/tests/ts_extra_tests/tool/test_helper.py +++ b/tests/ts_extra_tests/tool/test_helper.py @@ -1,110 +1,110 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -# Copyright (c) 2023 Huawei Device Co., Ltd. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - - -import re -import os - - -def read_declaration(path): - start_pattern = re.compile(r'^\/\*\*\-*') - end_pattern = re.compile(r'^\s*\-+\*\/') - context = "" - with open(path, 'r', encoding='utf-8', errors='ignore') as f: - declaration_begin = False - while True: - line = f.readline() - if not line: - break - if start_pattern.match(line): - declaration_begin = True - continue - if end_pattern.match(line): - declaration_begin = False - break - if declaration_begin: - context += line - return context - - -def is_root_dir(dir_path, file_or_dir, file_or_dir_results, limit_version): - rf = 'test_ts_cases' - root_folder = os.path.basename(dir_path) - if root_folder == rf: - # file_or_dir like: ['2.0', '2.1', '2.2', ... '4.9', 'spec'] - for f_item in file_or_dir: - if limit_version is None: - file_or_dir_results = file_or_dir - break - else: - limit_version = float(limit_version) - try: - f_num = float(f_item) - if f_num <= limit_version: - file_or_dir_results.append(f_item) - except Exception as e: - print(e) - continue - if limit_version is not None: - file_or_dir_results.append('spec') - return dir_path, file_or_dir, file_or_dir_results, limit_version - - -def get_path_file(dir_path, all_file_path=None, is_root=False, limit_version=None): - if all_file_path is None: - all_file_path = [] - file_or_dir = os.listdir(dir_path) - file_or_dir_results = [] - if dir_path.endswith("test_ts_cases") or dir_path.endswith("test_ts_cases/"): - is_root = True - else: - is_root = False - if is_root: - dir_path, file_or_dir, file_or_dir_results, limit_version = is_root_dir(dir_path, file_or_dir, - file_or_dir_results, limit_version) - else: - file_or_dir_results = file_or_dir - for file_dir in file_or_dir_results: - file_or_dir_path = os.path.join(dir_path, file_dir) - if '\\' in file_or_dir_path: - file_or_dir_path = file_or_dir_path.replace('\\', '/') - - if os.path.isdir(file_or_dir_path): - get_path_file(file_or_dir_path, all_file_path, False, None) - else: - all_file_path.append(file_or_dir_path) - - return all_file_path - - -def get_disable_list(file_path): - disable_list = [] - with open(file_path, 'r', encoding='utf-8', errors='ignore') as f: - while True: - line = f.readline() - if not line: - break - disable_list.append(os.path.abspath(line.strip())) - return disable_list - - -def is_disable_case(file_path, disable_list): - if disable_list is None: - return False - if file_path in disable_list: - return True - for disable_path in disable_list: - if disable_path in file_path: - return True +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# Copyright (c) 2023-2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +import re +import os + + +def read_declaration(path): + start_pattern = re.compile(r'^\/\*\*\-*') + end_pattern = re.compile(r'^\s*\-+\*\/') + context = "" + with open(path, 'r', encoding='utf-8', errors='ignore') as f: + declaration_begin = False + while True: + line = f.readline() + if not line: + break + if start_pattern.match(line): + declaration_begin = True + continue + if end_pattern.match(line): + declaration_begin = False + break + if declaration_begin: + context += line + return context + + +def list_root_directorys_new(directorys_in_root, filtered_directories, max_ts_version): + # directorys_in_root like: ['2.0', '2.1', '2.2', ... '4.9', 'spec'] + if max_ts_version is None: + filtered_directories = directorys_in_root + else: + max_ts_version = float(max_ts_version) + for f_item in directorys_in_root: + if f_item == 'spec': + filtered_directories.append(f_item) + continue + try: + f_num = float(f_item) + if f_num <= max_ts_version: + filtered_directories.append(f_item) + except Exception as e: + print(e) + + return filtered_directories + + +def list_all_test_files_in_dir(dir_path, all_file_path=None, limit_version=None): + if all_file_path is None: + all_file_path = [] + + if dir_path.endswith("test_ts_cases") or dir_path.endswith("test_ts_cases/"): + is_root = True + else: + is_root = False + + items_in_dir = os.listdir(dir_path) + filtered_directories = [] + + if is_root: + filtered_directories = list_root_directorys_new(items_in_dir, filtered_directories, limit_version) + else: + filtered_directories = items_in_dir + + for file_dir in filtered_directories: + file_or_dir_path = os.path.join(dir_path, file_dir) + if '\\' in file_or_dir_path: + file_or_dir_path = file_or_dir_path.replace('\\', '/') + + if os.path.isdir(file_or_dir_path): + list_all_test_files_in_dir(file_or_dir_path, all_file_path, None) + else: + all_file_path.append(file_or_dir_path) + + return all_file_path + + +def get_disable_list(file_path): + disable_list = [] + with open(file_path, 'r', encoding='utf-8', errors='ignore') as f: + while True: + line = f.readline() + if not line: + break + disable_list.append(line.strip()) + return disable_list + +def is_disabled_case(file_path, disable_list): + if disable_list is None: + return False + + for one in disable_list: + if file_path.endswith(one.strip()): + return True + + return False \ No newline at end of file diff --git a/tests/ts_extra_tests/tool/testcfg.py b/tests/ts_extra_tests/tool/testcfg.py index 43343db1a6..ebac5b7db6 100644 --- a/tests/ts_extra_tests/tool/testcfg.py +++ b/tests/ts_extra_tests/tool/testcfg.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -# Copyright (c) 2023 Huawei Device Co., Ltd. +# Copyright (c) 2023-2024 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -43,6 +43,7 @@ class TestCase: js_runtime_path = "" es2abc = "" tsc = "" + enable_arkguard = False def __init__(self, path): self.path = path @@ -240,7 +241,33 @@ class TestCase: self.fail = True return + + def execute_arkguard(self, input_file_path): + arkguard_root_dir = os.path.join("../../../../", "arkcompiler/ets_frontend/arkguard") + arkgurad_entry_path = os.path.join(arkguard_root_dir, "lib/cli/SecHarmony.js") + config_path = os.path.join(arkguard_root_dir, "test/compilerTestConfig.json") + arkguard_cmd = [ + 'node', + '--no-warnings', + arkgurad_entry_path, + input_file_path, + '--config-path', + config_path, + '--inplace' + ] + # print(arkguard_cmd) + process = subprocess.Popen(arkguard_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + out, err = process.communicate() + if err: + print("arkguard error: ", err) + process.wait() + + def __test_es2abc(self): + # run arkguard + if TestCase.enable_arkguard: + self.execute_arkguard(self.path) + # compiler to abc process = subprocess.Popen(self.__get_es2abc_cmd(self.path), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)