Skip to content

Commit 6e7b5aa

Browse files
Clemens Buchachergitster
authored andcommitted
test-lib: allow exit trap to be used for cleanup by tests
Exit trap should not be removed in case tests require cleanup code. This is especially important if tests are executed with the --immediate option. Signed-off-by: Clemens Buchacher <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1f729dc commit 6e7b5aa

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

t/test-lib.sh

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ fi
147147

148148
error () {
149149
say_color error "error: $*"
150-
trap - EXIT
150+
GIT_EXIT_OK=t
151151
exit 1
152152
}
153153

@@ -179,10 +179,17 @@ test_broken=0
179179
test_success=0
180180

181181
die () {
182-
echo >&5 "FATAL: Unexpected exit with code $?"
183-
exit 1
182+
code=$?
183+
if test -n "$GIT_EXIT_OK"
184+
then
185+
exit $code
186+
else
187+
echo >&5 "FATAL: Unexpected exit with code $code"
188+
exit 1
189+
fi
184190
}
185191

192+
GIT_EXIT_OK=
186193
trap 'die' EXIT
187194

188195
# The semantics of the editor variables are that of invoking
@@ -285,7 +292,7 @@ test_failure_ () {
285292
say_color error "FAIL $test_count: $1"
286293
shift
287294
echo "$@" | sed -e 's/^/ /'
288-
test "$immediate" = "" || { trap - EXIT; exit 1; }
295+
test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }
289296
}
290297

291298
test_known_broken_ok_ () {
@@ -347,7 +354,7 @@ test_expect_failure () {
347354
then
348355
test_known_broken_ok_ "$1"
349356
else
350-
test_known_broken_failure_ "$1"
357+
test_known_broken_failure_ "$1"
351358
fi
352359
fi
353360
echo >&3 ""
@@ -498,7 +505,7 @@ test_create_repo () {
498505
}
499506

500507
test_done () {
501-
trap - EXIT
508+
GIT_EXIT_OK=t
502509
test_results_dir="$TEST_DIRECTORY/test-results"
503510
mkdir -p "$test_results_dir"
504511
test_results_path="$test_results_dir/${0%.sh}-$$"
@@ -640,7 +647,7 @@ fi
640647
test="trash directory.$(basename "$0" .sh)"
641648
test ! -z "$debug" || remove_trash="$TEST_DIRECTORY/$test"
642649
rm -fr "$test" || {
643-
trap - EXIT
650+
GIT_EXIT_OK=t
644651
echo >&5 "FATAL: Cannot prepare test area"
645652
exit 1
646653
}

0 commit comments

Comments
 (0)