13af6ab5fSopenharmony_ci#!/usr/bin/env bash
23af6ab5fSopenharmony_ci# Copyright (c) 2023-2024 Huawei Device Co., Ltd.
33af6ab5fSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
43af6ab5fSopenharmony_ci# you may not use this file except in compliance with the License.
53af6ab5fSopenharmony_ci# You may obtain a copy of the License at
63af6ab5fSopenharmony_ci#
73af6ab5fSopenharmony_ci# http://www.apache.org/licenses/LICENSE-2.0
83af6ab5fSopenharmony_ci#
93af6ab5fSopenharmony_ci# Unless required by applicable law or agreed to in writing, software
103af6ab5fSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
113af6ab5fSopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123af6ab5fSopenharmony_ci# See the License for the specific language governing permissions and
133af6ab5fSopenharmony_ci# limitations under the License.
143af6ab5fSopenharmony_ci
153af6ab5fSopenharmony_ciset -e
163af6ab5fSopenharmony_ci
173af6ab5fSopenharmony_ciSCRIPT_DIR="$(realpath "$(dirname "${BASH_SOURCE[0]}")")"
183af6ab5fSopenharmony_ci
193af6ab5fSopenharmony_ciusage() {
203af6ab5fSopenharmony_ci    echo "Usage: $0 path/to/panda/build/bin/es2panda path/to/tsproject [PANDA_RUN_PREFIX]"
213af6ab5fSopenharmony_ci}
223af6ab5fSopenharmony_ci
233af6ab5fSopenharmony_ciensure_exists() {
243af6ab5fSopenharmony_ci    if [ ! -f "$1" ]; then
253af6ab5fSopenharmony_ci        echo "Error: No such file: '$1'"
263af6ab5fSopenharmony_ci        usage
273af6ab5fSopenharmony_ci        exit 1
283af6ab5fSopenharmony_ci    fi
293af6ab5fSopenharmony_ci}
303af6ab5fSopenharmony_ci
313af6ab5fSopenharmony_ciES2PANDA="$1"
323af6ab5fSopenharmony_ciTSCONFIG_DIR="$2"
333af6ab5fSopenharmony_ciPANDA_RUN_PREFIX="$3"
343af6ab5fSopenharmony_ciTSCONFIG="$TSCONFIG_DIR"/tsconfig.json
353af6ab5fSopenharmony_ciEXPECTED="$TSCONFIG_DIR"/expected.txt
363af6ab5fSopenharmony_ciBUILD="$TSCONFIG_DIR"/build
373af6ab5fSopenharmony_ciPANDA_ROOT="$4"
383af6ab5fSopenharmony_ci
393af6ab5fSopenharmony_ciensure_exists "$TSCONFIG"
403af6ab5fSopenharmony_ciensure_exists "$ES2PANDA"
413af6ab5fSopenharmony_ciensure_exists "$EXPECTED"
423af6ab5fSopenharmony_ci
433af6ab5fSopenharmony_cirm -r -f "$BUILD"
443af6ab5fSopenharmony_ci
453af6ab5fSopenharmony_ciACTUAL=$(mktemp /tmp/actual.XXXXXX)
463af6ab5fSopenharmony_ciSTDLIB="$PANDA_ROOT/plugins/ets/stdlib"
473af6ab5fSopenharmony_ciCMD="$PANDA_RUN_PREFIX $ES2PANDA --stdlib=$STDLIB --arktsconfig=$TSCONFIG"
483af6ab5fSopenharmony_ci$CMD 2> /dev/null
493af6ab5fSopenharmony_cipushd "$TSCONFIG_DIR" &> /dev/null
503af6ab5fSopenharmony_cifind . -type f -name '*abc' | sort --version-sort > "$ACTUAL"
513af6ab5fSopenharmony_cipopd &> /dev/null
523af6ab5fSopenharmony_ci
533af6ab5fSopenharmony_ciset +e
543af6ab5fSopenharmony_cidiff "$EXPECTED" "$ACTUAL"
553af6ab5fSopenharmony_ciRES=$?
563af6ab5fSopenharmony_ciset -e
573af6ab5fSopenharmony_ciif [ "$RES" -ne 0 ]; then
583af6ab5fSopenharmony_ci    echo "Expected:"
593af6ab5fSopenharmony_ci    cat "$EXPECTED"
603af6ab5fSopenharmony_ci    echo "Actual:"
613af6ab5fSopenharmony_ci    cat "$ACTUAL"
623af6ab5fSopenharmony_ci    echo "How to reproduce:"
633af6ab5fSopenharmony_ci    echo "(cd $(pwd) && $CMD)"
643af6ab5fSopenharmony_ci    echo "(cd $(realpath $TSCONFIG_DIR) && find . -type f -name '*abc' | sort > $(pwd)/actual.txt)"
653af6ab5fSopenharmony_ci    echo "diff $(realpath $EXPECTED) $(pwd)/actual.txt"
663af6ab5fSopenharmony_cifi
673af6ab5fSopenharmony_cirm "$ACTUAL"
683af6ab5fSopenharmony_cirm -r "$BUILD"
693af6ab5fSopenharmony_ciexit $RES
70