diff --git a/testJs/README.md b/testJs/README.md new file mode 100644 index 0000000000000000000000000000000000000000..c2c09e55de23f4cea5b3406b579ec18d807e46cd --- /dev/null +++ b/testJs/README.md @@ -0,0 +1,15 @@ +### File Introduction + +### Environment preparation +1. Compilation: ./build.sh --product-name hispark_taurus_standard --build-target ark_js_host_linux_tools_packages --build-target ets_frontend_build +2. Npm install: cd out/hispark_taurus/clang_x64/ark/ark/build && npm install && cd - + + +### Execute the test framework +1. Execute Options
+1.1 Perform full testing
+cd arkcompiler/ets_frontend && python3 ./testJs/run_testJs.py
+1.2 Execute the directory test
+cd arkcompiler/ets_frontend && python3 ./testJs/run_testJs.py --dir file directory, eg (./testJs/test/moduletest)
+1.3 Execute a single file test
+cd arkcompiler/ets_frontend && python3 ./testJs/run_testJs.py --file file path, eg (./testJs/test/moduletest/arrBuffer/arr.js)
\ No newline at end of file diff --git a/testJs/README_zh.md b/testJs/README_zh.md new file mode 100644 index 0000000000000000000000000000000000000000..3df7b55f6a245fff53167770da8e232665e7f82f --- /dev/null +++ b/testJs/README_zh.md @@ -0,0 +1,17 @@ +# testJs + +### 文件简介 + +### 环境准备 +1. 编译:./build.sh --product-name hispark_taurus_standard --build-target ark_js_host_linux_tools_packages --build-target ark_ts2abc_build命令进行编译。
+2. Npm install: cd out/hispark_taurus/clang_x64/ark/ark/build && npm install && cd -
+ + +### 执行测试框架 +1. 执行选项
+1.1 执行全量测试
+cd arkcompiler/ets_frontend && python3 ./testJs/run_testJs.py
+1.2 执行目录测试
+cd arkcompiler/ets_frontend && python3 ./testJs/run_testJs.py --dir 文件目录,例如(./testJs/test/moduletest)
+1.3 执行单个文件测试
+cd arkcompiler/ets_frontend && python3 ./testJs/run_testJs.py --file 文件路径,例如(./testJs/test/moduletest/arr/arr.js)
diff --git a/testJs/config.py b/testJs/config.py new file mode 100644 index 0000000000000000000000000000000000000000..a6f69d092fd1a829e142b88e3ad98e571ff338b0 --- /dev/null +++ b/testJs/config.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +""" +Copyright (c) 2022 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. + +Description: Use ark to execute test 262 test suite +""" + +import os +import json + +LD_LIBRARY_PATH_LIST = [ + 'out/hispark_taurus/clang_x64/ark/ark', + 'out/hispark_taurus/clang_x64/ark/ark_js_runtime', + 'out/hispark_taurus/clang_x64/thirdparty/icu', + 'prebuilts/clang/ohos/linux-x86_64/llvm/lib' +] + +OUT_DIR = os.path.join("out") +OUT_TEST_DIR = os.path.join("out", "testJs") +OUT_RESULT_FILE = os.path.join("out", "testJs", "result.txt") +TEST_DIR = os.path.join("testJs") +TS_CASES_DIR = os.path.join(".", "testJs", "test") +CUR_FILE_DIR = os.path.dirname(__file__) +CODE_ROOT = os.path.abspath(os.path.join(CUR_FILE_DIR, "../../..")) +ARK_TS2ABC_PATH = 'arkcompiler/ets_frontend/' +ARK_JS_VM = './out/hispark_taurus/clang_x64/ark/ark_js_runtime/ark_js_vm' +ARK_DIR = f"{CODE_ROOT}/out/hispark_taurus/clang_x64/ark/ark" +WORK_PATH = f'{CODE_ROOT}/{ARK_TS2ABC_PATH}' +EXPECT_PATH = '_expect.txt' +DEFAULT_ARK_FRONTEND_TOOL = os.path.join(ARK_DIR, "build", "src", "index.js") +ARK_FRONTEND_TOOL_ES2ABC = os.path.join(ARK_DIR, "es2abc") + +TEST_PATH = os.sep.join([".", "testJs", "test"]) +OUT_PATH = os.sep.join([".", "out", "testJs"]) +JS_EXT = ".js" +TXT_EXT = ".txt" +ABC_EXT = ".abc" diff --git a/testJs/run_testJs.py b/testJs/run_testJs.py new file mode 100644 index 0000000000000000000000000000000000000000..fc8e81f134cd0f3db13d231b7e38f9cf6eecde42 --- /dev/null +++ b/testJs/run_testJs.py @@ -0,0 +1,126 @@ +import os +import subprocess +import argparse +import sys +from utils import * +from config import * + + +def parse_args(): + parser = argparse.ArgumentParser() + parser.add_argument('--dir', metavar='DIR', help="Directory to test") + parser.add_argument('--file', metavar='FILE', help="File to test") + parser.add_argument( + '--ark_frontend_tool', + default=DEFAULT_ARK_FRONTEND_TOOL, + help="ark frontend conversion tool") + arguments = parser.parse_args() + return arguments + + +def run_js_test(filepath,frontend_tool): + abc_filepath = os.path.join(WORK_PATH, filepath.replace(JS_EXT, ABC_EXT).lstrip('./')) + output_path = filepath.replace(JS_EXT, TXT_EXT) + if frontend_tool == DEFAULT_ARK_FRONTEND_TOOL: + command_os(['node', '--expose-gc', frontend_tool, filepath]) + elif frontend_tool == ARK_FRONTEND_TOOL_ES2ABC: + command_os([frontend_tool, filepath, '--output', abc_filepath]) + os.chdir('../../') + run_abc_cmd = [ARK_JS_VM, abc_filepath] + run_abc_result = subprocess.Popen(run_abc_cmd, stdout=subprocess.PIPE) + js_result_list = [] + for line in run_abc_result.stdout.readlines(): + js_result = str(line).lstrip("b'").rstrip("\\n'")+'\n' + js_result_list.append(js_result) + remove_file(abc_filepath) + os.chdir(ARK_TS2ABC_PATH) + with open(output_path,'w') as output: + output.writelines(js_result_list) + + + +def compare(filepath): + outputfilepath = filepath.replace(JS_EXT, TXT_EXT) + outcont = read_file(outputfilepath) + filepath_list = filepath.split(os.sep) + filepath_list[-1] = filepath_list[-1].replace(JS_EXT, EXPECT_PATH) + expectfilepath = (os.sep).join(filepath_list) + expectcont = read_file(expectfilepath) + if outcont == expectcont: + result = f'PASS {filepath}\n' + else: + result = f'FAIL {filepath}\n' + remove_file(outputfilepath) + print(result) + return result + + +def run_test_machine(args): + frontend_tool = DEFAULT_ARK_FRONTEND_TOOL + if args.ark_frontend_tool: + frontend_tool = process_tool_path(args.ark_frontend_tool) + result_path = [] + if args.file: + run_js_test(args.file, frontend_tool) + result = compare(args.file) + result_path.append(result) + elif args.dir: + for root, dirs, files in os.walk(args.dir): + for file in files: + filepath = f'{root}/{file}' + if filepath.endswith(JS_EXT): + run_js_test(filepath, frontend_tool) + result = compare(filepath) + result_path.append(result) + elif args.file is None and args.dir is None: + for root, dirs, files in os.walk(TS_CASES_DIR): + for file in files: + filepath = f'{root}/{file}' + if filepath.endswith(JS_EXT): + run_js_test(filepath, frontend_tool) + result = compare(filepath) + result_path.append(result) + with open(OUT_RESULT_FILE, 'w') as read_out_result: + read_out_result.writelines(result_path) + + +def init_path(): + remove_dir(OUT_TEST_DIR) + mk_dir(OUT_TEST_DIR) + + +def summary(): + if not os.path.exists(OUT_RESULT_FILE): + return + count = -1 + fail_count = 0 + with open(OUT_RESULT_FILE, 'r') as read_outfile: + for count, line in enumerate(read_outfile): + if line.startswith("FAIL"): + fail_count += 1 + pass + count += 1 + + print("\n Regression summary") + print("===============================") + print(" Total %5d " % (count)) + print("-------------------------------") + print(" Passed tests: %5d " % (count - fail_count)) + print(" Failed tests: %5d " % (fail_count)) + print("===============================") + + +def main(args): + try: + init_path() + excuting_npm_install(args) + export_path() + run_test_machine(args) + summary() + except BaseException: + print("Run Python Script Fail") + + +if __name__ == "__main__": + sys.exit(main(parse_args())) + diff --git a/testJs/test/moduletest/allocatearraybuffer/case1.js b/testJs/test/moduletest/allocatearraybuffer/case1.js new file mode 100644 index 0000000000000000000000000000000000000000..d16629afe8561d5ada7115947aeb7399665dc236 --- /dev/null +++ b/testJs/test/moduletest/allocatearraybuffer/case1.js @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2022 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. + */ + +var newTarget = function() {}.bind(null); +var arrayBuffer = Reflect.construct(ArrayBuffer, [16], newTarget); +print(arrayBuffer.length); \ No newline at end of file diff --git a/testJs/test/moduletest/allocatearraybuffer/case1_expect.txt b/testJs/test/moduletest/allocatearraybuffer/case1_expect.txt new file mode 100644 index 0000000000000000000000000000000000000000..417b7b5370df81a7316ee9f983437444afdae432 --- /dev/null +++ b/testJs/test/moduletest/allocatearraybuffer/case1_expect.txt @@ -0,0 +1 @@ +undefined diff --git a/testJs/utils.py b/testJs/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..f09e6f01086f0571abd7de881fb904ec68f2a018 --- /dev/null +++ b/testJs/utils.py @@ -0,0 +1,94 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +""" +Copyright (c) 2022 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. + +Description: Use ark to execute test 262 test suite +""" + +import os +import shutil +from config import * +import subprocess + + +def command_os(order): + subprocess.run(order) + + +def mk_dir(path): + if not os.path.exists(path): + os.makedirs(path) + + +def remove_dir(path): + if os.path.exists(path): + shutil.rmtree(path) + + +def remove_file(path): + if os.path.exists(path): + os.remove(path) + + +def read_file(path): + util_read_content = [] + with open(path, "r") as utils_read: + util_read_content = utils_read.readlines() + + return util_read_content + + +def excuting_npm_install(args): + ark_frontend_tool = os.path.join(DEFAULT_ARK_FRONTEND_TOOL) + if args.ark_frontend_tool: + ark_frontend_tool = os.path.join(args.ark_frontend_tool) + + ts2abc_build_dir = os.path.join(os.path.dirname(os.path.realpath(ark_frontend_tool)), "..") + if os.path.exists(os.path.join(ts2abc_build_dir, "package.json")): + npm_install(ts2abc_build_dir) + elif os.path.exists(os.path.join(ts2abc_build_dir, "..", "package.json")): + npm_install(os.path.join(ts2abc_build_dir, "..")) + + +def npm_install(cwd): + try: + os.chdir(cwd) + command_os(["npm", "install"]) + os.chdir(WORK_PATH) + except BaseException as e: + print(e) + + +def process_tool_path(tool_path): + if tool_path.startswith('../'): + tool_path_list = tool_path.split(os.sep) + dir_level = tool_path.count('..') + for i in range(dir_level): + del tool_path_list[0] + tool_path = os.sep.join(tool_path_list) + frontend_tool_path = f'{CODE_ROOT}/{tool_path}' + return frontend_tool_path + return tool_path + + +def export_path(): + ld_library_path = ':'.join(LD_LIBRARY_PATH_LIST) + try: + os.chdir(CODE_ROOT) + os.environ['LD_LIBRARY_PATH'] = ld_library_path + os.chdir(WORK_PATH) + except BaseException as e: + print(e)