threads / discuss / 19627

various fixes to the test library

Subject: various fixes to the test library

## tl;dr

5 messages between Jun 1, 2009 and Jun 1, 2009.

replies: 4people: 1as markdown or json

Clemens Buchacher· Jun 1, 2009, 12:14 UTC · lore

If a http test failed, and the test was run with --immediate, the cleanup code would not be executed and stale web server instance were left behind. This is fixed by 2/3.

Clemens

[PATCH 1/3] test-lib: fail if invalid options are passed [PATCH 2/3] test-lib: allow exit trap to be used for cleanup by tests [PATCH 3/3] test-lib: fix http exit codes

 t/lib-httpd.sh |    6 ++++--
 t/test-lib.sh  |   23 +++++++++++++++--------
 2 files changed, 19 insertions(+), 10 deletions(-)
Clemens Buchacher· Jun 1, 2009, 12:14 UTC · re: Clemens Buchacher · lore

[PATCH 1/3] test-lib: fail if invalid options are passed

Previously, unknown options would be ignored, including any subsequent valid options.

Signed-off-by: Clemens Buchacher <drizzd@aon.at>
---
 t/test-lib.sh |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/t/test-lib.sh b/t/test-lib.sh
index dad1437..6e83ceb 100644
--- a/t/test-lib.sh
+++ b/t/test-lib.sh
@@ -115,7 +115,7 @@ do
 	--tee)
 		shift ;; # was handled already
 	*)
-		break ;;
+		echo "error: unknown test option '$1'" >&2; exit 1 ;;
 	esac
 done
 
-- 
1.6.3.1.147.g637c3
Clemens Buchacher· Jun 1, 2009, 12:14 UTC · re: Clemens Buchacher · lore

[PATCH 2/3] 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 <drizzd@aon.at>
---
 t/test-lib.sh |   21 ++++++++++++++-------
 1 files changed, 14 insertions(+), 7 deletions(-)
diff --git a/t/test-lib.sh b/t/test-lib.sh
index 6e83ceb..5fdc5d9 100644
--- a/t/test-lib.sh
+++ b/t/test-lib.sh
@@ -147,7 +147,7 @@ fi
 
 error () {
 	say_color error "error: $*"
-	trap - EXIT
+	GIT_EXIT_OK=t
 	exit 1
 }
 
@@ -179,10 +179,17 @@ test_broken=0
 test_success=0
 
 die () {
-	echo >&5 "FATAL: Unexpected exit with code $?"
-	exit 1
+	code=$?
+	if test -n "$GIT_EXIT_OK"
+	then
+		exit $code
+	else
+		echo >&5 "FATAL: Unexpected exit with code $code"
+		exit 1
+	fi
 }
 
+GIT_EXIT_OK=
 trap 'die' EXIT
 
 # The semantics of the editor variables are that of invoking
@@ -285,7 +292,7 @@ test_failure_ () {
 	say_color error "FAIL $test_count: $1"
 	shift
 	echo "$@" | sed -e 's/^/	/'
-	test "$immediate" = "" || { trap - EXIT; exit 1; }
+	test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }
 }
 
 test_known_broken_ok_ () {
@@ -347,7 +354,7 @@ test_expect_failure () {
 		then
 			test_known_broken_ok_ "$1"
 		else
-		    test_known_broken_failure_ "$1"
+			test_known_broken_failure_ "$1"
 		fi
 	fi
 	echo >&3 ""
@@ -498,7 +505,7 @@ test_create_repo () {
 }
 
 test_done () {
-	trap - EXIT
+	GIT_EXIT_OK=t
 	test_results_dir="$TEST_DIRECTORY/test-results"
 	mkdir -p "$test_results_dir"
 	test_results_path="$test_results_dir/${0%.sh}-$$"
@@ -640,7 +647,7 @@ fi
 test="trash directory.$(basename "$0" .sh)"
 test ! -z "$debug" || remove_trash="$TEST_DIRECTORY/$test"
 rm -fr "$test" || {
-	trap - EXIT
+	GIT_EXIT_OK=t
 	echo >&5 "FATAL: Cannot prepare test area"
 	exit 1
 }
-- 
1.6.3.1.147.g637c3
Clemens Buchacher· Jun 1, 2009, 12:14 UTC · re: Clemens Buchacher · lore

[PATCH 3/3] test-lib: fix http exit codes

Previously, die() would report the exit code of stop_httpd. Instead, save the exit code and pass it to die() explicitly.

Signed-off-by: Clemens Buchacher <drizzd@aon.at>
---
 t/lib-httpd.sh |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/t/lib-httpd.sh b/t/lib-httpd.sh
index cde659d..6765b08 100644
--- a/t/lib-httpd.sh
+++ b/t/lib-httpd.sh
@@ -93,14 +93,16 @@ prepare_httpd() {
 start_httpd() {
 	prepare_httpd >&3 2>&4
 
-	trap 'stop_httpd; die' EXIT
+	trap 'code=$?; stop_httpd; (exit $code); die' EXIT
 
 	"$LIB_HTTPD_PATH" -d "$HTTPD_ROOT_PATH" \
 		-f "$TEST_PATH/apache.conf" $HTTPD_PARA \
 		-c "Listen 127.0.0.1:$LIB_HTTPD_PORT" -k start \
 		>&3 2>&4
-	if ! test $? = 0; then
+	if test $? -ne 0
+	then
 		say "skipping test, web server setup failed"
+		trap 'die' EXIT
 		test_done
 	fi
 }
-- 
1.6.3.1.147.g637c3
Clemens Buchacher· Jun 1, 2009, 12:28 UTC · re: Clemens Buchacher · lore

Re: [PATCH 3/3 v2] test-lib: fix http exit codes

Previously, die() would report the exit code of stop_httpd. Instead, save and reset the exit code before dying.

Signed-off-by: Clemens Buchacher <drizzd@aon.at>
---
On Mon, Jun 01, 2009 at 02:14:42PM +0200, Clemens Buchacher wrote:
> Previously, die() would report the exit code of stop_httpd. Instead,
> save the exit code and pass it to die() explicitly.

Oops, I forgot to fix the commit message. (I tried to pass the exit code to die() explicitly, but then I realized that this is much easier.)

Clemens
 t/lib-httpd.sh |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/t/lib-httpd.sh b/t/lib-httpd.sh
index cde659d..6765b08 100644
--- a/t/lib-httpd.sh
+++ b/t/lib-httpd.sh
@@ -93,14 +93,16 @@ prepare_httpd() {
 start_httpd() {
 	prepare_httpd >&3 2>&4
 
-	trap 'stop_httpd; die' EXIT
+	trap 'code=$?; stop_httpd; (exit $code); die' EXIT
 
 	"$LIB_HTTPD_PATH" -d "$HTTPD_ROOT_PATH" \
 		-f "$TEST_PATH/apache.conf" $HTTPD_PARA \
 		-c "Listen 127.0.0.1:$LIB_HTTPD_PORT" -k start \
 		>&3 2>&4
-	if ! test $? = 0; then
+	if test $? -ne 0
+	then
 		say "skipping test, web server setup failed"
+		trap 'die' EXIT
 		test_done
 	fi
 }
-- 
1.6.3.1.147.g637c3

← back to recent threads