diff --git a/default.nix b/default.nix index 5625e99..1c1b319 100644 --- a/default.nix +++ b/default.nix @@ -171,6 +171,6 @@ in rec { pkgs.yq-go ]} export SHELL=${pkgs.bash}/bin/bash - exec ${./tests}/entrypoint.sh "$@" + exec ${pkgs.bash}/bin/bash ${./tests}/entrypoint.sh "$@" ''; } diff --git a/tests/cases/01-create-network.sh b/tests/cases/01-create-network.sh index 02b9bc2..28a0f4d 100644 --- a/tests/cases/01-create-network.sh +++ b/tests/cases/01-create-network.sh @@ -1,3 +1,6 @@ +# shellcheck source=../utils/with-tmp-for-case.sh +source "$UTILS"/with-tmp-for-case.sh + mkdir a mkdir b mkdir c diff --git a/tests/entrypoint.sh b/tests/entrypoint.sh old mode 100755 new mode 100644 index e4ea2e2..2591e8b --- a/tests/entrypoint.sh +++ b/tests/entrypoint.sh @@ -4,6 +4,8 @@ set -e cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null root=$(pwd) +export UTILS="$root"/utils + REGEXS=() while [[ $# -gt 0 ]]; do @@ -39,6 +41,11 @@ if [ -z "$KEEP_TMP" ]; then trap 'rm -rf $TMPDIR' EXIT; fi export TMPDIR echo "tmp dir is $TMPDIR" +# Blackhole these directories so that tests don't accidentally use the host's +# real ones. +export XDG_RUNTIME_DIR=/dev/null +export XDG_DATA_HOME=/dev/null + test_files=$( find ./cases -type f -name '*.sh' \ | sed "s|^\./cases/||" \ @@ -57,12 +64,7 @@ for file in $test_files; do [ -z "$VERBOSE" ] && output="$TMPDIR/$file.log" || output=/dev/stdout ( - export TMPDIR="$TMPDIR/$file.tmp" - export XDG_RUNTIME_DIR="$TMPDIR/.run" - export XDG_DATA_HOME="$TMPDIR/.data" - - mkdir -p "$TMPDIR" "$XDG_RUNTIME_DIR" "$XDG_DATA_HOME" - cd "$TMPDIR" + export TEST_CASE_FILE="$file" if ! $SHELL -e -x "$root/cases/$file" >"$output" 2>&1; then echo "$file FAILED" diff --git a/tests/utils/with-tmp-for-case.sh b/tests/utils/with-tmp-for-case.sh new file mode 100644 index 0000000..72abcd8 --- /dev/null +++ b/tests/utils/with-tmp-for-case.sh @@ -0,0 +1,9 @@ +set -e + +TMPDIR="$TMPDIR/$TEST_CASE_FILE.tmp" +XDG_RUNTIME_DIR="$TMPDIR/.run" +XDG_DATA_HOME="$TMPDIR/.data" + +mkdir -p "$TMPDIR" "$XDG_RUNTIME_DIR" "$XDG_DATA_HOME" + +cd "$TMPDIR"