1#!/usr/bin/env python3 2# coding=utf-8 3# 4# Copyright (c) 2024 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 logging.config 19import os 20import json 21import subprocess 22import logging 23import sys 24 25 26def ensure_exists(path): 27 if not os.path.exists(path): 28 raise RuntimeError(f'The file {path} cannot be found') 29 30 31def es2panda_command(es2panda_path, stdlib_path, config_path): 32 return [ 33 *str(es2panda_path).split(), 34 '--stdlib', stdlib_path, 35 '--arktsconfig', config_path, 36 ] 37 38 39def compare_output(lhs, rhs): 40 for k in rhs: 41 attr = getattr(lhs, k) 42 if attr != rhs[k]: 43 message = "\n".join([f'In {k} field', 44 f'Expected: {rhs[k]}', 45 f'Got: {attr}']) 46 raise RuntimeError(message) 47 48 49parser = argparse.ArgumentParser() 50parser.add_argument('--es2panda', required=True, 51 help='Path to es2panda executable, could be prefixed') 52parser.add_argument('--config', required=True, help='Path to project config') 53parser.add_argument('--stdlib', required=True, help='Path to es2panda stdlib') 54 55args = parser.parse_args() 56 57project_dir = os.path.dirname(args.config) 58expected_path = os.path.join(project_dir, 'expected.json') 59 60[ensure_exists(f) for f in [ 61 str(args.es2panda).split()[-1], args.config, expected_path]] 62 63cmd = es2panda_command(args.es2panda, args.stdlib, args.config) 64 65actual = subprocess.run(cmd, 66 stdout=subprocess.PIPE, 67 stderr=subprocess.PIPE, 68 encoding='utf-8') 69 70with open(expected_path, "r", encoding="utf-8") as expected_file: 71 expected = json.load(expected_file) 72 compare_output(actual, expected) 73