Skip to content

Commit

Permalink
Harden tests bash a bit with set -u
Browse files Browse the repository at this point in the history
(cherry picked from commit cd9496c)
  • Loading branch information
Ericson2314 committed Dec 9, 2021
1 parent 9bdcacc commit c4c448c
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
8 changes: 6 additions & 2 deletions mk/run_test.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh
#!/usr/bin/env bash

set -u
set -eu -o pipefail

red=""
green=""
Expand All @@ -15,8 +15,12 @@ if [ -t 1 ]; then
normal=""
fi
(cd $(dirname $1) && env ${TESTS_ENVIRONMENT} init.sh 2>/dev/null > /dev/null)

set +e
log="$(cd $(dirname $1) && env ${TESTS_ENVIRONMENT} $(basename $1) 2>&1)"
status=$?
set -e

if [ $status -eq 0 ]; then
echo "$post_run_msg [${green}PASS$normal]"
elif [ $status -eq 99 ]; then
Expand Down
2 changes: 1 addition & 1 deletion tests/build-dry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ nix build -f dependencies.nix --dry-run 2>&1 | grep "will be built"

# TODO: XXX: FIXME: #1793
# Disable this part of the test until the problem is resolved:
if [ -n "$ISSUE_1795_IS_FIXED" ]; then
if [ -n "${ISSUE_1795_IS_FIXED-}" ]; then
clearStore
clearCache

Expand Down
7 changes: 5 additions & 2 deletions tests/common.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,14 @@ startDaemon() {
# ‘nix-daemon’ should have an option to fork into the background.
rm -f $NIX_DAEMON_SOCKET_PATH
PATH=$DAEMON_PATH nix-daemon &
pidDaemon=$!
for ((i = 0; i < 30; i++)); do
if [[ -S $NIX_DAEMON_SOCKET_PATH ]]; then break; fi
sleep 1
done
pidDaemon=$!
trap "killDaemon" EXIT
# Save for if daemon is killed
NIX_REMOTE_OLD=$NIX_REMOTE
export NIX_REMOTE=daemon
}

Expand All @@ -106,14 +108,15 @@ killDaemon() {
done
kill -9 $pidDaemon || true
wait $pidDaemon || true
# Restore old nix remote
NIX_REMOTE=$NIX_REMOTE_OLD
trap "" EXIT
}

restartDaemon() {
[[ -z "${pidDaemon:-}" ]] && return 0

killDaemon
unset NIX_REMOTE
startDaemon
}

Expand Down
2 changes: 1 addition & 1 deletion tests/hash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ source common.sh

try () {
printf "%s" "$2" > $TEST_ROOT/vector
hash=$(nix hash-file --base16 $EXTRA --type "$1" $TEST_ROOT/vector)
hash=$(nix hash-file --base16 ${EXTRA-} --type "$1" $TEST_ROOT/vector)
if test "$hash" != "$3"; then
echo "hash $1, expected $3, got $hash"
exit 1
Expand Down
4 changes: 2 additions & 2 deletions tests/user-envs.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source common.sh

if [ -z "$storeCleared" ]; then
if [ -z "${storeCleared-}" ]; then
clearStore
fi

Expand Down Expand Up @@ -69,7 +69,7 @@ nix-env -q '*' | grep -q foo-2.0pre1
test "$($profiles/test/bin/foo)" = "foo-2.0pre1"

# Upgrade "foo": should install foo-2.0.
NIX_PATH=nixpkgs=./user-envs.nix:$NIX_PATH nix-env -f '<nixpkgs>' -u foo
NIX_PATH=nixpkgs=./user-envs.nix:${NIX_PATH-} nix-env -f '<nixpkgs>' -u foo

# Query installed: should contain foo-2.0 now.
test "$(nix-env -q '*' | wc -l)" -eq 1
Expand Down

0 comments on commit c4c448c

Please sign in to comment.