Skip to content

Commit

Permalink
tests: simplify initialisation and wiring
Browse files Browse the repository at this point in the history
pararameterisation is not actually needed the way things are currently
set up, and it confused me when trying to understand what the code does.

all but one test sources vars-and-functions.sh, which nominally only
defines variables, but in practice is always coupled with the actual
initialisation. while the cleaner way of making this more legible would
be to source variables and initialisation separately, this would produce
a huge diff.
  • Loading branch information
fricklerhandwerk committed May 6, 2024
1 parent 27a02bc commit c6881f1
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 24 deletions.
6 changes: 3 additions & 3 deletions doc/manual/src/contributing/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,14 @@ ran test tests/functional/${testName}.sh... [PASS]
or without `make`:

```shell-session
$ ./mk/run-test.sh tests/functional/${testName}.sh tests/functional/init.sh
$ ./mk/run-test.sh tests/functional/${testName}.sh
ran test tests/functional/${testName}.sh... [PASS]
```

To see the complete output, one can also run:

```shell-session
$ ./mk/debug-test.sh tests/functional/${testName}.sh tests/functional/init.sh
$ ./mk/debug-test.sh tests/functional/${testName}.sh
+(${testName}.sh:1) foo
output from foo
+(${testName}.sh:2) bar
Expand Down Expand Up @@ -204,7 +204,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:

```shell-session
$ ./mk/debug-test.sh tests/functional/${testName}.sh tests/functional/init.sh
$ ./mk/debug-test.sh tests/functional/${testName}.sh
...
+ gdb blash blub
GNU gdb (GDB) 12.1
Expand Down
4 changes: 0 additions & 4 deletions mk/debug-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@
set -eu -o pipefail

test=$1
init=${2-}

dir="$(dirname "${BASH_SOURCE[0]}")"
source "$dir/common-test.sh"

if [ -n "$init" ]; then
(run "$init" 2>/dev/null > /dev/null)
fi
run "$test"
7 changes: 3 additions & 4 deletions mk/lib.mk
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,14 @@ $(foreach script, $(bin-scripts), $(eval $(call install-program-in,$(script),$(b
$(foreach script, $(bin-scripts), $(eval programs-list += $(script)))
$(foreach script, $(noinst-scripts), $(eval programs-list += $(script)))
$(foreach template, $(template-files), $(eval $(call instantiate-template,$(template))))
install_test_init=tests/functional/init.sh
$(foreach test, $(install-tests), \
$(eval $(call run-test,$(test),$(install_test_init))) \
$(eval $(call run-test,$(test))) \
$(eval installcheck: $(test).test))
$(foreach test-group, $(install-tests-groups), \
$(eval $(call run-test-group,$(test-group),$(install_test_init))) \
$(eval $(call run-test-group,$(test-group))) \
$(eval installcheck: $(test-group).test-group) \
$(foreach test, $($(test-group)-tests), \
$(eval $(call run-test,$(test),$(install_test_init))) \
$(eval $(call run-test,$(test))) \
$(eval $(test-group).test-group: $(test).test)))

# Compilation database.
Expand Down
4 changes: 0 additions & 4 deletions mk/run-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ yellow=""
normal=""

test=$1
init=${2-}

dir="$(dirname "${BASH_SOURCE[0]}")"
source "$dir/common-test.sh"
Expand All @@ -22,9 +21,6 @@ if [ -t 1 ]; then
fi

run_test () {
if [ -n "$init" ]; then
(run "$init" 2>/dev/null > /dev/null)
fi
log="$(run "$test" 2>&1)" && status=0 || status=$?
}

Expand Down
4 changes: 2 additions & 2 deletions mk/tests.mk
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ endef

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))
$(eval $(call run-bash,$1.test,$1 $(test-deps),mk/run-test.sh $1))
$(eval $(call run-bash,$1.test-debug,$1 $(test-deps),mk/debug-test.sh $1))

endef

Expand Down
7 changes: 2 additions & 5 deletions tests/functional/init.sh → tests/functional/common/init.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# Don't start the daemon
source common/vars-and-functions.sh

test -n "$TEST_ROOT"
if test -d "$TEST_ROOT"; then
chmod -R u+rw "$TEST_ROOT"
Expand Down Expand Up @@ -36,7 +33,7 @@ extra-experimental-features = flakes
EOF

# Initialise the database.
# The flag itself does nothing, but running the command touches the store
nix-store --init

# Did anything happen?
# Sanity check
test -e "$NIX_STATE_DIR"/db/db.sqlite
4 changes: 4 additions & 0 deletions tests/functional/common/vars-and-functions.sh.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# NOTE: instances of @variable@ are substituted as defined in /mk/templates.mk

set -eu -o pipefail

if [[ -z "${COMMON_VARS_AND_FUNCTIONS_SH_SOURCED-}" ]]; then
Expand Down Expand Up @@ -62,6 +64,8 @@ export IMPURE_VAR2=bar

cacheDir=$TEST_ROOT/binary-cache

source ./init.sh

readLink() {
ls -l "$1" | sed 's/.*->\ //'
}
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/lang.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ for i in lang/eval-fail-*.nix; do
if [[ -e "lang/$i.flags" ]]; then
sed -e 's/#.*//' < "lang/$i.flags"
else
# note that show-trace is also set by init.sh
# note that show-trace is also set by common/init.sh
echo "--eval --strict --show-trace"
fi
)"
Expand Down
1 change: 0 additions & 1 deletion tests/functional/local.mk
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
nix_tests = \
test-infra.sh \
init.sh \
flakes/flakes.sh \
flakes/develop.sh \
flakes/run.sh \
Expand Down

0 comments on commit c6881f1

Please sign in to comment.