From 6354a6c8a092ec5b6e062c8e19695bacc35be000 Mon Sep 17 00:00:00 2001 From: xucheng46 Date: Tue, 6 Sep 2022 20:56:27 +0800 Subject: [PATCH] Enable compiler tests in es2abc Issue:https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I5QCS5 Test: es2abc parser tests, es2abc compiler tests Signed-off-by: xucheng46 Change-Id: I9580df5fb15baa576345157e82e5b3f781ae3cf6 --- es2panda/BUILD.gn | 1 + es2panda/test/BUILD.gn | 36 ++++++++ .../compiler/ts-print-test-1-expected.txt | 1 + .../ts/cases/compiler/ts-print-test-1.ts | 17 ++++ es2panda/test/runner.py | 86 +++++++++++++++++++ 5 files changed, 141 insertions(+) create mode 100644 es2panda/test/compiler/ts/cases/compiler/ts-print-test-1-expected.txt create mode 100644 es2panda/test/compiler/ts/cases/compiler/ts-print-test-1.ts diff --git a/es2panda/BUILD.gn b/es2panda/BUILD.gn index ed8b51073e..17dff9d9cd 100644 --- a/es2panda/BUILD.gn +++ b/es2panda/BUILD.gn @@ -531,4 +531,5 @@ group("es2panda_build_win") { group("es2abc_tests") { deps = [ "./test:es2abc_parser_tests" ] + deps += [ "./test:es2abc_compiler_tests" ] } diff --git a/es2panda/test/BUILD.gn b/es2panda/test/BUILD.gn index 1ce46568c7..027287cc37 100644 --- a/es2panda/test/BUILD.gn +++ b/es2panda/test/BUILD.gn @@ -12,6 +12,8 @@ # limitations under the License. import("//arkcompiler/ets_frontend/es2panda/es2abc_config.gni") +import("//arkcompiler/ets_runtime/js_runtime_config.gni") +import("//build/ohos.gni") action("es2abc_parser_tests") { script = "${es2abc_root}/test/runner.py" @@ -26,3 +28,37 @@ action("es2abc_parser_tests") { outputs = [ "${es2abc_build_path}/keep_es2abc_parser_tests_run" ] } + +action("es2abc_compiler_tests") { + script = "${es2abc_root}/test/runner.py" + + deps = es2abc_build_deps + deps += [ "${js_root}:ark_js_host_linux_tools_packages" ] + + host_jsvm_target = "${js_root}/ecmascript/js_vm:ark_js_vm(${host_toolchain})" + root_out_dir = get_label_info(host_jsvm_target, "root_out_dir") + ark_js_runtime_dir = rebase_path("${root_out_dir}/ark/ark_js_runtime") + + if (is_standard_system) { + icu_path = "thirdparty/icu" + } else { + icu_path = "global/i18n" + } + icu_dir = rebase_path("${root_out_dir}/${icu_path}") + + llvm_lib_dir = rebase_path("//prebuilts/clang/ohos/linux-x86_64/llvm/lib/") + + ld_library_path = "${ark_js_runtime_dir}:${icu_dir}:${llvm_lib_dir}" + + args = [ + "--no-progress", + "--js-runtime", + ark_js_runtime_dir, + "--LD_LIBRARY_PATH", + ld_library_path, + "--compiler", + rebase_path("${es2abc_build_path}"), + ] + + outputs = [ "${es2abc_build_path}/keep_es2abc_compiler_tests_run" ] +} diff --git a/es2panda/test/compiler/ts/cases/compiler/ts-print-test-1-expected.txt b/es2panda/test/compiler/ts/cases/compiler/ts-print-test-1-expected.txt new file mode 100644 index 0000000000..629e8342b2 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/compiler/ts-print-test-1-expected.txt @@ -0,0 +1 @@ +test ts-print-test-1 diff --git a/es2panda/test/compiler/ts/cases/compiler/ts-print-test-1.ts b/es2panda/test/compiler/ts/cases/compiler/ts-print-test-1.ts new file mode 100644 index 0000000000..79a5e23f3b --- /dev/null +++ b/es2panda/test/compiler/ts/cases/compiler/ts-print-test-1.ts @@ -0,0 +1,17 @@ +/* + * 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. + */ + + +print("test ts-print-test-1"); diff --git a/es2panda/test/runner.py b/es2panda/test/runner.py index b4793f8485..2ccecdf984 100755 --- a/es2panda/test/runner.py +++ b/es2panda/test/runner.py @@ -26,6 +26,7 @@ import subprocess import sys import test262util + def is_directory(parser, arg): if not path.isdir(arg): parser.error("The directory '%s' does not exist" % arg) @@ -62,6 +63,9 @@ def get_args(): parser.add_argument( '--regression', '-r', action='store_true', dest='regression', default=False, help='run regression tests') + parser.add_argument( + '--compiler', '-c', action='store_true', dest='compiler', + default=False, help='run compiler tests') parser.add_argument( '--tsc', action='store_true', dest='tsc', default=False, help='run tsc tests') @@ -118,6 +122,11 @@ def get_args(): parser.add_argument( '--verbose', '-v', action='store_true', dest='verbose', default=False, help='Enable verbose output') + 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') return parser.parse_args() @@ -381,7 +390,16 @@ class Runner: self.failed = 0 self.passed = 0 self.es2panda = path.join(args.build_dir, 'es2abc') + self.test_abc = path.join(args.build_dir, 'runner_test.abc') self.cmd_prefix = [] + self.ark_js_vm = "" + self.ld_library_path = "" + + if args.js_runtime_path: + self.ark_js_vm = path.join(args.js_runtime_path, 'ark_js_vm') + + if args.ld_library_path: + self.ld_library_path = args.ld_library_path if args.arm64_qemu: self.cmd_prefix = ["qemu-aarch64", "-L", "/usr/aarch64-linux-gnu/"] @@ -434,7 +452,9 @@ class Runner: if self.args.error: print("steps:", test.reproduce) + print("error:") print(test.error) + print("\n") print("") @@ -713,6 +733,65 @@ class TSCRunner(Runner): return src +class CompilerRunner(Runner): + def __init__(self, args): + Runner.__init__(self, args, "Compiler") + + def add_directory(self, directory, extension, flags): + glob_expression = path.join( + self.test_root, directory, "**/*.%s" % (extension)) + files = glob(glob_expression, recursive=True) + files = fnmatch.filter(files, self.test_root + '**' + self.args.filter) + + self.tests += list(map(lambda f: CompilerTest(f, flags), files)) + + def test_path(self, src): + return src + + +class CompilerTest(Test): + def __init__(self, test_path, flags): + Test.__init__(self, test_path, flags) + + def run(self, runner): + es2abc_cmd = runner.cmd_prefix + [runner.es2panda] + es2abc_cmd.extend(self.flags) + es2abc_cmd.extend(["--output=" + runner.test_abc]) + es2abc_cmd.append(self.path) + self.log_cmd(es2abc_cmd) + + process = subprocess.Popen(es2abc_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + out, err = process.communicate() + if err: + self.passed = False + self.error = err.decode("utf-8", errors="ignore") + return self + + ld_library_path = runner.ld_library_path + os.environ.setdefault("LD_LIBRARY_PATH", ld_library_path) + run_abc_cmd = [runner.ark_js_vm] + run_abc_cmd.extend([runner.test_abc]) + self.log_cmd(run_abc_cmd) + + process = subprocess.Popen(run_abc_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + out, err = process.communicate() + self.output = out.decode("utf-8", errors="ignore") + err.decode("utf-8", errors="ignore") + expected_path = "%s-expected.txt" % (path.splitext(self.path)[0]) + try: + with open(expected_path, 'r') as fp: + expected = fp.read() + self.passed = expected == self.output and process.returncode in [0, 1] + except Exception: + self.passed = False + + if not self.passed: + self.error = err.decode("utf-8", errors="ignore") + + os.remove(runner.test_abc) + + return self + + def main(): args = get_args() @@ -734,6 +813,13 @@ def main(): if args.tsc: runners.append(TSCRunner(args)) + if args.compiler: + runner = CompilerRunner(args) + runner.add_directory("compiler/js", "js", []) + runner.add_directory("compiler/ts", "ts", ["--extension=ts"]) + + runners.append(runner) + failed_tests = 0 for runner in runners: -- Gitee