From bfb9eb87fe98f96969188df9df866e15800bd55b Mon Sep 17 00:00:00 2001 From: John Ericson Date: Thu, 16 Mar 2023 16:00:20 -0400 Subject: [PATCH] Cleanup test skipping - Try not to put cryptic "99" in many places Factor out `exit 99` into `skipTest` function - Alows make sure skipping a test is done with a reason `skipTest` takes a mandatory argument - Separate pure conditionals vs side-effectful test skipping. "require daemon" already had this, but "sandbox support" did not. --- tests/build-remote.sh | 4 ++-- tests/common/vars-and-functions.sh.in | 25 ++++++++++++++++--------- tests/db-migration.sh | 6 +++--- tests/fetchGit.sh | 5 +---- tests/fetchGitRefs.sh | 5 +---- tests/fetchGitSubmodules.sh | 5 +---- tests/fetchMercurial.sh | 5 +---- tests/flakes/common.sh | 7 ------- tests/flakes/mercurial.sh | 5 +---- tests/gc-runtime.sh | 2 +- tests/linux-sandbox.sh | 4 ++-- tests/plugins.sh | 3 +-- tests/recursive.sh | 2 +- tests/shell.sh | 2 +- tests/user-envs-migration.sh | 2 +- 15 files changed, 33 insertions(+), 49 deletions(-) diff --git a/tests/build-remote.sh b/tests/build-remote.sh index 6da128d1b..78e12b477 100644 --- a/tests/build-remote.sh +++ b/tests/build-remote.sh @@ -1,5 +1,5 @@ -if ! canUseSandbox; then exit 99; fi -if ! [[ $busybox =~ busybox ]]; then exit 99; fi +requireSandboxSupport +[[ $busybox =~ busybox ]] || skipTest "no busybox" unset NIX_STORE_DIR unset NIX_STATE_DIR diff --git a/tests/common/vars-and-functions.sh.in b/tests/common/vars-and-functions.sh.in index e4a773f34..a9e6c802f 100644 --- a/tests/common/vars-and-functions.sh.in +++ b/tests/common/vars-and-functions.sh.in @@ -152,21 +152,29 @@ isDaemonNewer () { [[ $(nix eval --expr "builtins.compareVersions ''$daemonVersion'' ''$requiredVersion''") -ge 0 ]] } +skipTest () { + echo "$1, skipping this test..." >&2 + exit 99 +} + requireDaemonNewerThan () { - isDaemonNewer "$1" || exit 99 + isDaemonNewer "$1" || skipTest "Daemon is too old" } canUseSandbox() { - if [[ ! ${_canUseSandbox-} ]]; then - echo "Sandboxing not supported, skipping this test..." - return 1 - fi + [[ ${_canUseSandbox-} ]] +} - return 0 +requireSandboxSupport () { + canUseSandbox || skipTest "Sandboxing not supported" +} + +requireGit() { + [[ $(type -p git) ]] || skipTest "Git not installed" } fail() { - echo "$1" + echo "$1" >&2 exit 1 } @@ -209,8 +217,7 @@ expectStderr() { needLocalStore() { if [[ "$NIX_REMOTE" == "daemon" ]]; then - echo "Can’t run through the daemon ($1), skipping this test..." - return 99 + skipTest "Can’t run through the daemon ($1)" fi } diff --git a/tests/db-migration.sh b/tests/db-migration.sh index 7d243eefb..44cd16bc0 100644 --- a/tests/db-migration.sh +++ b/tests/db-migration.sh @@ -1,13 +1,13 @@ # Test that we can successfully migrate from an older db schema +source common.sh + # Only run this if we have an older Nix available # XXX: This assumes that the `daemon` package is older than the `client` one if [[ -z "${NIX_DAEMON_PACKAGE-}" ]]; then - exit 99 + skipTest "not using the Nix daemon" fi -source common.sh - killDaemon # Fill the db using the older Nix diff --git a/tests/fetchGit.sh b/tests/fetchGit.sh index a7a8df186..e2ccb0e97 100644 --- a/tests/fetchGit.sh +++ b/tests/fetchGit.sh @@ -1,9 +1,6 @@ source common.sh -if [[ -z $(type -p git) ]]; then - echo "Git not installed; skipping Git tests" - exit 99 -fi +requireGit clearStore diff --git a/tests/fetchGitRefs.sh b/tests/fetchGitRefs.sh index a0d86ca5e..d643fea04 100644 --- a/tests/fetchGitRefs.sh +++ b/tests/fetchGitRefs.sh @@ -1,9 +1,6 @@ source common.sh -if [[ -z $(type -p git) ]]; then - echo "Git not installed; skipping Git tests" - exit 99 -fi +requireGit clearStore diff --git a/tests/fetchGitSubmodules.sh b/tests/fetchGitSubmodules.sh index 08ccaa3cd..df81232e5 100644 --- a/tests/fetchGitSubmodules.sh +++ b/tests/fetchGitSubmodules.sh @@ -2,10 +2,7 @@ source common.sh set -u -if [[ -z $(type -p git) ]]; then - echo "Git not installed; skipping Git submodule tests" - exit 99 -fi +requireGit clearStore diff --git a/tests/fetchMercurial.sh b/tests/fetchMercurial.sh index 5c64ffd26..e6f8525c6 100644 --- a/tests/fetchMercurial.sh +++ b/tests/fetchMercurial.sh @@ -1,9 +1,6 @@ source common.sh -if [[ -z $(type -p hg) ]]; then - echo "Mercurial not installed; skipping Mercurial tests" - exit 99 -fi +[[ $(type -p hq) ]] || skipTest "Mercurial not installed" clearStore diff --git a/tests/flakes/common.sh b/tests/flakes/common.sh index 58616ac4c..427abcdde 100644 --- a/tests/flakes/common.sh +++ b/tests/flakes/common.sh @@ -2,13 +2,6 @@ source ../common.sh registry=$TEST_ROOT/registry.json -requireGit() { - if [[ -z $(type -p git) ]]; then - echo "Git not installed; skipping flake tests" - exit 99 - fi -} - writeSimpleFlake() { local flakeDir="$1" cat > $flakeDir/flake.nix <