git » gofer » commit 5c9742c

test: Move common bash functions to a helper library

author Alberto Bertogli
2020-06-13 13:19:00 UTC
committer Alberto Bertogli
2020-06-13 13:19:00 UTC
parent b334116fe30705a8b752219307d206b5de1c97e1

test: Move common bash functions to a helper library

Many of the functions in test/test.sh will be useful for other tests
in future patches, so move them to a common helper library.

Makefile +1 -1
test/test.sh +4 -92
test/util/cover-report.sh +3 -6
test/util/lib.sh +97 -0

diff --git a/Makefile b/Makefile
index e4818ec..cda8649 100644
--- a/Makefile
+++ b/Makefile
@@ -32,6 +32,6 @@ cover:
 		-coverprofile=".cover/pkg-tests.out"\
 		-coverpkg=./... ./...
 	COVER_DIR=$$PWD/.cover/ setsid -w ./test/test.sh
-	COVER_DIR=$$PWD/.cover/ ./test/util/cover-report.sh
+	COVER_DIR=$$PWD/.cover/ setsid -w ./test/util/cover-report.sh
 
 .PHONY: gofer vet test cover
diff --git a/test/test.sh b/test/test.sh
index bf79725..7b35c42 100755
--- a/test/test.sh
+++ b/test/test.sh
@@ -2,102 +2,14 @@
 
 set -e
 
-if [ "$V" == "1" ]; then
-	set -v
-fi
-
-UTILDIR="$( realpath `dirname "${0}"` )/util"
-
-# Set traps to kill our subprocesses when we exit (for any reason).
-trap ":" TERM      # Avoid the EXIT handler from killing bash.
-trap "exit 2" INT  # Ctrl-C, make sure we fail in that case.
-trap "kill 0" EXIT # Kill children on exit.
-
-# The tests are run from the test root.
-cd "$(realpath `dirname ${0}`)/"
-
-# Build the binaries.
-if [ "$COVER_DIR" != "" ]; then
-	(
-		cd ..
-		go test -covermode=count -coverpkg=./... -c -tags coveragebin
-		mv gofer.test gofer
-	)
-else
-	( cd ..; make )
-fi
-( cd util; go build exp.go )
-
-function set_cover() {
-	# Set the coverage arguments each time, as we don't want the different
-	# runs to override the generated profile.
-	if [ "$COVER_DIR" != "" ]; then
-		COVER_ARGS="-test.run=^TestRunMain$ \
-			-test.coverprofile=$COVER_DIR/it-`date +%s.%N`.out"
-	fi
-}
-
-function gofer() {
-	set_cover
-	../gofer $COVER_ARGS -v=3  "$@"  >> .out.log 2>&1
-}
-
-# Run gofer in the background (sets $PID to its process id).
-function gofer_bg() {
-	# Duplicate gofer() because if we put the function in the background,
-	# the pid will be of bash, not the subprocess.
-	set_cover
-	../gofer $COVER_ARGS -v=3  "$@"  >> .out.log 2>&1 &
-	PID=$!
-}
-
-# Wait until there's something listening on the given port.
-function wait_until_ready() {
-	PORT=$1
-
-	while ! bash -c "true < /dev/tcp/localhost/$PORT" 2>/dev/null ; do
-		sleep 0.01
-	done
-}
-
-function generate_certs() {
-	mkdir -p .certs/localhost
-	(
-		cd .certs/localhost
-		go run ${UTILDIR}/generate_cert.go \
-			-ca -duration=1h --host=localhost
-	)
-}
-
-function exp() {
-	if [ "$V" == "1" ]; then
-		VF="-v"
-	fi
-	echo "  $@"
-	${UTILDIR}/exp "$@" \
-		$VF \
-		-cacert=".certs/localhost/fullchain.pem"
-}
-
-function snoop() {
-	if [ "$SNOOP" == "1" ]; then
-		read -p"Press enter to continue"
-	fi
-}
-
-function waitgrep() {
-	for i in 0.01 0.02 0.05 0.1 0.2; do
-		if grep "$@"; then
-				return 0
-		fi
-		sleep $i
-	done
-	return 1	
-}
+. $(dirname ${0})/util/lib.sh
+init
 
 
 echo "## Setup"
 
+build
+
 # Remove old request log files, since we will be checking their contents.
 rm -f .01-fe.requests.log .01-be.requests.log
 
diff --git a/test/util/cover-report.sh b/test/util/cover-report.sh
index 32995a8..dc16535 100755
--- a/test/util/cover-report.sh
+++ b/test/util/cover-report.sh
@@ -2,14 +2,11 @@
 
 set -e
 
-if [ "$V" == "1" ]; then
-	set -v
-fi
-
-UTILDIR="$( realpath `dirname "${0}"` )/"
+. $(dirname ${0})/lib.sh
+init
 
 # Run from the repo root.
-cd "$(realpath `dirname ${0}`)/../../"
+cd ../
 
 echo "## Coverage"
 
diff --git a/test/util/lib.sh b/test/util/lib.sh
new file mode 100644
index 0000000..608ecb2
--- /dev/null
+++ b/test/util/lib.sh
@@ -0,0 +1,97 @@
+
+function init() {
+	if [ "$V" == "1" ]; then
+		set -v
+	fi
+
+	export UTILDIR=$( realpath `dirname "${BASH_SOURCE[0]}"` )
+	export TESTDIR=$( realpath "$UTILDIR"/../ )
+	cd ${TESTDIR}
+
+	# Set traps to kill our subprocesses when we exit (for any reason).
+	trap ":" TERM      # Avoid the EXIT handler from killing bash.
+	trap "exit 2" INT  # Ctrl-C, make sure we fail in that case.
+	trap "kill 0" EXIT # Kill children on exit.
+}
+
+function build() {
+	if [ "$COVER_DIR" != "" ]; then
+		(
+			cd ..
+			go test -covermode=count -coverpkg=./... \
+				-c -tags coveragebin
+			mv gofer.test gofer
+		)
+	else
+		( cd ..; make )
+	fi
+	( cd util; go build exp.go )
+}
+
+function set_cover() {
+	# Set the coverage arguments each time, as we don't want the different
+	# runs to override the generated profile.
+	if [ "$COVER_DIR" != "" ]; then
+		COVER_ARGS="-test.run=^TestRunMain$ \
+			-test.coverprofile=$COVER_DIR/it-`date +%s.%N`.out"
+	fi
+}
+
+function gofer() {
+	set_cover
+	../gofer $COVER_ARGS -v=3  "$@"  >> .out.log 2>&1
+}
+
+# Run gofer in the background (sets $PID to its process id).
+function gofer_bg() {
+	# Duplicate gofer() because if we put the function in the background,
+	# the pid will be of bash, not the subprocess.
+	set_cover
+	../gofer $COVER_ARGS -v=3  "$@"  >> .out.log 2>&1 &
+	PID=$!
+}
+
+# Wait until there's something listening on the given port.
+function wait_until_ready() {
+	PORT=$1
+
+	while ! bash -c "true < /dev/tcp/localhost/$PORT" 2>/dev/null ; do
+		sleep 0.01
+	done
+}
+
+function generate_certs() {
+	mkdir -p .certs/localhost
+	(
+		cd .certs/localhost
+		go run ${UTILDIR}/generate_cert.go \
+			-ca -duration=1h --host=localhost
+	)
+}
+
+function exp() {
+	if [ "$V" == "1" ]; then
+		VF="-v"
+	fi
+	echo "  $@"
+	${UTILDIR}/exp "$@" \
+		$VF \
+		-cacert=".certs/localhost/fullchain.pem"
+}
+
+function snoop() {
+	if [ "$SNOOP" == "1" ]; then
+		read -p"Press enter to continue"
+	fi
+}
+
+function waitgrep() {
+	for i in 0.01 0.02 0.05 0.1 0.2; do
+		if grep "$@"; then
+				return 0
+		fi
+		sleep $i
+	done
+	return 1	
+}
+