Refactor bash test build system a bit

The basic idea here is to separate a few intertwined notions:

1. Not all "run bash tests" are "install tests"

2. Not all "run bash tests" use `tests/functional/init.sh`, or any
   pre-test initialization at all.

This will used in the next commit when we have a test that check unit
test golden master data.

Also, move our custom `PS4` from the test to the test runner, as it is
part of how we want to display the tests, not the test themselves.

Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>
This commit is contained in:
John Ericson 2023-11-08 00:30:55 -05:00
parent 3d9d5dc189
commit 9afa697ab6
7 changed files with 48 additions and 26 deletions

View file

@ -133,17 +133,17 @@ ran test tests/functional/${testName}.sh... [PASS]
or without `make`: or without `make`:
```shell-session ```shell-session
$ ./mk/run-test.sh tests/functional/${testName}.sh $ ./mk/run-test.sh tests/functional/${testName}.sh tests/functional/init.sh
ran test tests/functional/${testName}.sh... [PASS] ran test tests/functional/${testName}.sh... [PASS]
``` ```
To see the complete output, one can also run: To see the complete output, one can also run:
```shell-session ```shell-session
$ ./mk/debug-test.sh tests/functional/${testName}.sh $ ./mk/debug-test.sh tests/functional/${testName}.sh tests/functional/init.sh
+ foo +(${testName}.sh:1) foo
output from foo output from foo
+ bar +(${testName}.sh:2) bar
output from bar output from bar
... ...
``` ```
@ -175,7 +175,7 @@ edit it like so:
Then, running the test with `./mk/debug-test.sh` will drop you into GDB once the script reaches that point: Then, running the test with `./mk/debug-test.sh` will drop you into GDB once the script reaches that point:
```shell-session ```shell-session
$ ./mk/debug-test.sh tests/functional/${testName}.sh $ ./mk/debug-test.sh tests/functional/${testName}.sh tests/functional/init.sh
... ...
+ gdb blash blub + gdb blash blub
GNU gdb (GDB) 12.1 GNU gdb (GDB) 12.1

View file

@ -1,15 +1,27 @@
test_dir=tests/functional # Remove overall test dir (at most one of the two should match) and
# remove file extension.
test_name=$(echo -n "$test" | sed \
-e "s|^unit-test-data/||" \
-e "s|^tests/functional/||" \
-e "s|\.sh$||" \
)
test=$(echo -n "$test" | sed -e "s|^$test_dir/||") TESTS_ENVIRONMENT=(
"TEST_NAME=$test_name"
TESTS_ENVIRONMENT=("TEST_NAME=${test%.*}" 'NIX_REMOTE=') 'NIX_REMOTE='
'PS4=+(${BASH_SOURCE[0]-$0}:$LINENO) '
)
: ${BASH:=/usr/bin/env bash} : ${BASH:=/usr/bin/env bash}
run () {
cd "$(dirname $1)" && env "${TESTS_ENVIRONMENT[@]}" $BASH -x -e -u -o pipefail $(basename $1)
}
init_test () { init_test () {
cd "$test_dir" && env "${TESTS_ENVIRONMENT[@]}" $BASH -e init.sh 2>/dev/null > /dev/null run "$init" 2>/dev/null > /dev/null
} }
run_test_proper () { run_test_proper () {
cd "$test_dir/$(dirname $test)" && env "${TESTS_ENVIRONMENT[@]}" $BASH -e $(basename $test) run "$test"
} }

View file

@ -3,9 +3,12 @@
set -eu -o pipefail set -eu -o pipefail
test=$1 test=$1
init=${2-}
dir="$(dirname "${BASH_SOURCE[0]}")" dir="$(dirname "${BASH_SOURCE[0]}")"
source "$dir/common-test.sh" source "$dir/common-test.sh"
(init_test) if [ -n "$init" ]; then
(init_test)
fi
run_test_proper run_test_proper

View file

@ -122,14 +122,15 @@ $(foreach script, $(bin-scripts), $(eval $(call install-program-in,$(script),$(b
$(foreach script, $(bin-scripts), $(eval programs-list += $(script))) $(foreach script, $(bin-scripts), $(eval programs-list += $(script)))
$(foreach script, $(noinst-scripts), $(eval programs-list += $(script))) $(foreach script, $(noinst-scripts), $(eval programs-list += $(script)))
$(foreach template, $(template-files), $(eval $(call instantiate-template,$(template)))) $(foreach template, $(template-files), $(eval $(call instantiate-template,$(template))))
install_test_init=tests/functional/init.sh
$(foreach test, $(install-tests), \ $(foreach test, $(install-tests), \
$(eval $(call run-install-test,$(test))) \ $(eval $(call run-test,$(test),$(install_test_init))) \
$(eval installcheck: $(test).test)) $(eval installcheck: $(test).test))
$(foreach test-group, $(install-tests-groups), \ $(foreach test-group, $(install-tests-groups), \
$(eval $(call run-install-test-group,$(test-group))) \ $(eval $(call run-test-group,$(test-group),$(install_test_init))) \
$(eval installcheck: $(test-group).test-group) \ $(eval installcheck: $(test-group).test-group) \
$(foreach test, $($(test-group)-tests), \ $(foreach test, $($(test-group)-tests), \
$(eval $(call run-install-test,$(test))) \ $(eval $(call run-test,$(test),$(install_test_init))) \
$(eval $(test-group).test-group: $(test).test))) $(eval $(test-group).test-group: $(test).test)))
$(foreach file, $(man-pages), $(eval $(call install-data-in, $(file), $(mandir)/man$(patsubst .%,%,$(suffix $(file)))))) $(foreach file, $(man-pages), $(eval $(call install-data-in, $(file), $(mandir)/man$(patsubst .%,%,$(suffix $(file))))))

View file

@ -8,6 +8,7 @@ yellow=""
normal="" normal=""
test=$1 test=$1
init=${2-}
dir="$(dirname "${BASH_SOURCE[0]}")" dir="$(dirname "${BASH_SOURCE[0]}")"
source "$dir/common-test.sh" source "$dir/common-test.sh"
@ -21,7 +22,9 @@ if [ -t 1 ]; then
fi fi
run_test () { run_test () {
(init_test 2>/dev/null > /dev/null) if [ -n "$init" ]; then
(init_test 2>/dev/null > /dev/null)
fi
log="$(run_test_proper 2>&1)" && status=0 || status=$? log="$(run_test_proper 2>&1)" && status=0 || status=$?
} }

View file

@ -2,19 +2,22 @@
test-deps = test-deps =
define run-install-test define run-bash
.PHONY: $1.test .PHONY: $1
$1.test: $1 $(test-deps) $1: $2
@env BASH=$(bash) $(bash) mk/run-test.sh $1 < /dev/null @env BASH=$(bash) $(bash) $3 < /dev/null
.PHONY: $1.test-debug
$1.test-debug: $1 $(test-deps)
@env BASH=$(bash) $(bash) mk/debug-test.sh $1 < /dev/null
endef endef
define run-install-test-group define run-test
$(eval $(call run-bash,$1.test,$1 $(test-deps),mk/run-test.sh $1 $2))
$(eval $(call run-bash,$1.test-debug,$1 $(test-deps),mk/debug-test.sh $1 $2))
endef
define run-test-group
.PHONY: $1.test-group .PHONY: $1.test-group

View file

@ -4,7 +4,7 @@ if [[ -z "${COMMON_VARS_AND_FUNCTIONS_SH_SOURCED-}" ]]; then
COMMON_VARS_AND_FUNCTIONS_SH_SOURCED=1 COMMON_VARS_AND_FUNCTIONS_SH_SOURCED=1
export PS4='+(${BASH_SOURCE[0]-$0}:$LINENO) ' set +x
export TEST_ROOT=$(realpath ${TMPDIR:-/tmp}/nix-test)/${TEST_NAME:-default/tests\/functional//} export TEST_ROOT=$(realpath ${TMPDIR:-/tmp}/nix-test)/${TEST_NAME:-default/tests\/functional//}
export NIX_STORE_DIR export NIX_STORE_DIR