Skip to content

Commit 89202b5

Browse files
authored
Merge pull request #129 from rancher-sandbox/defer-not-trap
trap handlers don't stack; there is only one per signal
2 parents 5774acd + 9b6b962 commit 89202b5

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

hack/test-example.sh

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ set -eu -o pipefail
33

44
scriptdir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
55

6+
cleanup_cmd=""
7+
trap 'eval ${cleanup_cmd}' EXIT
8+
function defer {
9+
[ -n "${cleanup_cmd}" ] && cleanup_cmd="${cleanup_cmd}; "
10+
cleanup_cmd="${cleanup_cmd}$1"
11+
}
12+
613
function INFO() {
714
echo "TEST| [INFO] $*"
815
}
@@ -66,7 +73,7 @@ fi
6673
if [[ -n ${CHECKS["port-forwards"]} ]]; then
6774
tmpconfig="$HOME/lima-config-tmp"
6875
mkdir -p "${tmpconfig}"
69-
trap 'rm -rf $tmpconfig' EXIT
76+
defer "rm -rf \"$tmpconfig\""
7077
tmpfile="${tmpconfig}/${NAME}.yaml"
7178
cp "$FILE" "${tmpfile}"
7279
FILE="${tmpfile}"
@@ -86,7 +93,7 @@ function diagnose() {
8693
}
8794

8895
INFO "Starting \"$NAME\" from \"$FILE\""
89-
trap 'limactl delete -f $NAME' EXIT
96+
defer "limactl delete -f \"$NAME\""
9097
set -x
9198
if ! limactl start --tty=false "$FILE"; then
9299
ERROR "Failed to start \"$NAME\""
@@ -103,7 +110,7 @@ INFO "Testing limactl copy command"
103110
tmpfile="$HOME/lima-hostname"
104111
rm -f "$tmpfile"
105112
limactl cp "$NAME":/etc/hostname "$tmpfile"
106-
trap 'rm -f $tmpfile' EXIT
113+
defer "rm -f \"$tmpfile\""
107114
expected="$(limactl shell "$NAME" cat /etc/hostname)"
108115
got="$(cat "$tmpfile")"
109116
INFO "/etc/hostname: expected=${expected}, got=${got}"
@@ -131,7 +138,7 @@ if [[ -n ${CHECKS["mount-home"]} ]]; then
131138
INFO "Testing home access (\"$hometmp\")"
132139
rm -rf "$hometmp"
133140
mkdir -p "$hometmp"
134-
trap 'rm -rf $hometmp' EXIT
141+
defer "rm -rf \"$hometmp\""
135142
echo "random-content-${RANDOM}" >"$hometmp/random"
136143
expected="$(cat "$hometmp/random")"
137144
got="$(limactl shell "$NAME" cat "$hometmp/random")"

0 commit comments

Comments
 (0)