1#!/usr/bin/env bash
2# Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15set -e
16
17SCRIPT_DIR="$(realpath "$(dirname "${BASH_SOURCE[0]}")")"
18
19usage() {
20    echo "Usage: $0 path/to/panda/build/bin/es2panda path/to/tsproject [PANDA_RUN_PREFIX]"
21}
22
23ensure_exists() {
24    if [ ! -f "$1" ]; then
25        echo "Error: No such file: '$1'"
26        usage
27        exit 1
28    fi
29}
30
31ES2PANDA="$1"
32TSCONFIG_DIR="$2"
33PANDA_RUN_PREFIX="$3"
34TSCONFIG="$TSCONFIG_DIR"/tsconfig.json
35EXPECTED="$TSCONFIG_DIR"/expected.txt
36BUILD="$TSCONFIG_DIR"/build
37PANDA_ROOT="$4"
38
39ensure_exists "$TSCONFIG"
40ensure_exists "$ES2PANDA"
41ensure_exists "$EXPECTED"
42
43rm -r -f "$BUILD"
44
45ACTUAL=$(mktemp /tmp/actual.XXXXXX)
46STDLIB="$PANDA_ROOT/plugins/ets/stdlib"
47CMD="$PANDA_RUN_PREFIX $ES2PANDA --stdlib=$STDLIB --arktsconfig=$TSCONFIG"
48$CMD 2> /dev/null
49pushd "$TSCONFIG_DIR" &> /dev/null
50find . -type f -name '*abc' | sort --version-sort > "$ACTUAL"
51popd &> /dev/null
52
53set +e
54diff "$EXPECTED" "$ACTUAL"
55RES=$?
56set -e
57if [ "$RES" -ne 0 ]; then
58    echo "Expected:"
59    cat "$EXPECTED"
60    echo "Actual:"
61    cat "$ACTUAL"
62    echo "How to reproduce:"
63    echo "(cd $(pwd) && $CMD)"
64    echo "(cd $(realpath $TSCONFIG_DIR) && find . -type f -name '*abc' | sort > $(pwd)/actual.txt)"
65    echo "diff $(realpath $EXPECTED) $(pwd)/actual.txt"
66fi
67rm "$ACTUAL"
68rm -r "$BUILD"
69exit $RES
70