forked from lix-project/lix
c11836126b
Use `set -u` and `set -o pipefail` to catch accidental mistakes and failures more strongly. - `set -u` catches the use of undefined variables - `set -o pipefail` catches failures (like `set -e`) earlier in the pipeline. This makes the tests a bit more robust. It is nice to read code not worrying about these spurious success paths (via uncaught) errors undermining the tests. Indeed, I caught some bugs doing this. There are a few tests where we run a command that should fail, and then search its output to make sure the failure message is one that we expect. Before, since the `grep` was the last command in the pipeline the exit code of those failing programs was silently ignored. Now with `set -o pipefail` it won't be, and we have to do something so the expected failure doesn't accidentally fail the test. To do that we use `expect` and a new `expectStderr` to check for the exact failing exit code. See the comments on each for why. `grep -q` is replaced with `grepQuiet`, see the comments on that function for why. `grep -v` when we just want the exit code is replaced with `grepInverse, see the comments on that function for why. `grep -q -v` together is, surprise surprise, replaced with `grepQuietInverse`, which is both combined. Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>
126 lines
3.4 KiB
Bash
126 lines
3.4 KiB
Bash
source common.sh
|
|
|
|
testDir="$PWD"
|
|
cd "$TEST_ROOT"
|
|
|
|
replCmds="
|
|
simple = 1
|
|
simple = import $testDir/simple.nix
|
|
:bl simple
|
|
:log simple
|
|
"
|
|
|
|
replFailingCmds="
|
|
failing = import $testDir/simple-failing.nix
|
|
:b failing
|
|
:log failing
|
|
"
|
|
|
|
replUndefinedVariable="
|
|
import $testDir/undefined-variable.nix
|
|
"
|
|
|
|
testRepl () {
|
|
local nixArgs=("$@")
|
|
rm -rf repl-result-out || true # cleanup from other runs backed by a foreign nix store
|
|
local replOutput="$(nix repl "${nixArgs[@]}" <<< "$replCmds")"
|
|
echo "$replOutput"
|
|
local outPath=$(echo "$replOutput" |&
|
|
grep -o -E "$NIX_STORE_DIR/\w*-simple")
|
|
nix path-info "${nixArgs[@]}" "$outPath"
|
|
[ "$(realpath ./repl-result-out)" == "$outPath" ] || fail "nix repl :bl doesn't make a symlink"
|
|
# run it again without checking the output to ensure the previously created symlink gets overwritten
|
|
nix repl "${nixArgs[@]}" <<< "$replCmds" || fail "nix repl does not work twice with the same inputs"
|
|
|
|
# simple.nix prints a PATH during build
|
|
echo "$replOutput" | grepQuiet -s 'PATH=' || fail "nix repl :log doesn't output logs"
|
|
local replOutput="$(nix repl "${nixArgs[@]}" <<< "$replFailingCmds" 2>&1)"
|
|
echo "$replOutput"
|
|
echo "$replOutput" | grepQuiet -s 'This should fail' \
|
|
|| fail "nix repl :log doesn't output logs for a failed derivation"
|
|
local replOutput="$(nix repl --show-trace "${nixArgs[@]}" <<< "$replUndefinedVariable" 2>&1)"
|
|
echo "$replOutput"
|
|
echo "$replOutput" | grepQuiet -s "while evaluating the file" \
|
|
|| fail "nix repl --show-trace doesn't show the trace"
|
|
|
|
nix repl "${nixArgs[@]}" --option pure-eval true 2>&1 <<< "builtins.currentSystem" \
|
|
| grep "attribute 'currentSystem' missing"
|
|
nix repl "${nixArgs[@]}" 2>&1 <<< "builtins.currentSystem" \
|
|
| grep "$(nix-instantiate --eval -E 'builtins.currentSystem')"
|
|
}
|
|
|
|
# Simple test, try building a drv
|
|
testRepl
|
|
# Same thing (kind-of), but with a remote store.
|
|
testRepl --store "$TEST_ROOT/store?real=$NIX_STORE_DIR"
|
|
|
|
testReplResponse () {
|
|
local commands="$1"; shift
|
|
local expectedResponse="$1"; shift
|
|
local response="$(nix repl "$@" <<< "$commands")"
|
|
echo "$response" | grepQuiet -s "$expectedResponse" \
|
|
|| fail "repl command set:
|
|
|
|
$commands
|
|
|
|
does not respond with:
|
|
|
|
$expectedResponse
|
|
|
|
but with:
|
|
|
|
$response"
|
|
}
|
|
|
|
# :a uses the newest version of a symbol
|
|
testReplResponse '
|
|
:a { a = "1"; }
|
|
:a { a = "2"; }
|
|
"result: ${a}"
|
|
' "result: 2"
|
|
|
|
testReplResponse '
|
|
drvPath
|
|
' '".*-simple.drv"' \
|
|
$testDir/simple.nix
|
|
|
|
testReplResponse '
|
|
drvPath
|
|
' '".*-simple.drv"' \
|
|
--file $testDir/simple.nix --experimental-features 'ca-derivations'
|
|
|
|
testReplResponse '
|
|
drvPath
|
|
' '".*-simple.drv"' \
|
|
--file $testDir/simple.nix --extra-experimental-features 'repl-flake ca-derivations'
|
|
|
|
mkdir -p flake && cat <<EOF > flake/flake.nix
|
|
{
|
|
outputs = { self }: {
|
|
foo = 1;
|
|
bar.baz = 2;
|
|
|
|
changingThing = "beforeChange";
|
|
};
|
|
}
|
|
EOF
|
|
testReplResponse '
|
|
foo + baz
|
|
' "3" \
|
|
./flake ./flake\#bar --experimental-features 'flakes repl-flake'
|
|
|
|
# Test the `:reload` mechansim with flakes:
|
|
# - Eval `./flake#changingThing`
|
|
# - Modify the flake
|
|
# - Re-eval it
|
|
# - Check that the result has changed
|
|
replResult=$( (
|
|
echo "changingThing"
|
|
sleep 1 # Leave the repl the time to eval 'foo'
|
|
sed -i 's/beforeChange/afterChange/' flake/flake.nix
|
|
echo ":reload"
|
|
echo "changingThing"
|
|
) | nix repl ./flake --experimental-features 'flakes repl-flake')
|
|
echo "$replResult" | grepQuiet -s beforeChange
|
|
echo "$replResult" | grepQuiet -s afterChange
|