1#!/usr/bin/env python3 2# coding=utf-8 3# 4# Copyright (c) 2025 Huawei Device Co., Ltd. 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17import argparse 18import os 19import json 20import subprocess 21 22 23def ensure_exists(path): 24 if not os.path.exists(path): 25 raise RuntimeError(f'The file {path} cannot be found') 26 27 28def es2panda_command(es2panda_path, stdlib_path, arktsconfig_path, target_path): 29 return [ 30 *str(es2panda_path).split(), 31 '--opt-level=2', 32 '--thread=0', 33 '--extension=ets', 34 '--stdlib', stdlib_path, 35 '--arktsconfig', arktsconfig_path, 36 '--ets-unnamed', 37 target_path 38 ] 39 40 41def compare_output(lhs, rhs): 42 for k in rhs: 43 attr = getattr(lhs, k) 44 if attr != rhs[k]: 45 message = "\n".join([f'In {k} field', 46 f'Expected: {rhs[k]}', 47 f'Got: {attr}']) 48 raise RuntimeError(message) 49 50 51parser = argparse.ArgumentParser() 52parser.add_argument('--es2panda', required=True, 53 help='Path to es2panda executable, could be prefixed') 54parser.add_argument('--arktsconfig', required=True, help='Path to project arktsconfig') 55parser.add_argument('--stdlib', required=True, help='Path to es2panda stdlib') 56parser.add_argument('--target', required=True, help='Path to .sts to compile it to .abc') 57 58args = parser.parse_args() 59 60project_dir = os.path.dirname(args.target) 61expected_path = os.path.join(project_dir, 'expected.json') 62 63[ensure_exists(f) for f in [ 64 str(args.es2panda).split()[-1], args.arktsconfig, expected_path]] 65 66cmd = es2panda_command(args.es2panda, args.stdlib, args.arktsconfig, args.target) 67 68 69actual = subprocess.run(cmd, 70 stdout=subprocess.PIPE, 71 stderr=subprocess.PIPE, 72 encoding='utf-8') 73 74with open(expected_path, "r", encoding="utf-8") as expected_file: 75 expected = json.load(expected_file) 76 compare_output(actual, expected) 77