2021-07-15 16:17:18 +00:00
|
|
|
source common.sh
|
|
|
|
|
2022-04-18 17:21:47 +00:00
|
|
|
testDir="$PWD"
|
|
|
|
cd "$TEST_ROOT"
|
|
|
|
|
2021-07-15 16:17:18 +00:00
|
|
|
replCmds="
|
2021-12-09 15:35:27 +00:00
|
|
|
simple = 1
|
2022-04-18 17:21:47 +00:00
|
|
|
simple = import $testDir/simple.nix
|
|
|
|
:bl simple
|
2021-11-26 15:03:07 +00:00
|
|
|
:log simple
|
|
|
|
"
|
|
|
|
|
|
|
|
replFailingCmds="
|
2022-04-18 17:21:47 +00:00
|
|
|
failing = import $testDir/simple-failing.nix
|
2021-11-26 15:03:07 +00:00
|
|
|
:b failing
|
|
|
|
:log failing
|
2021-07-15 16:17:18 +00:00
|
|
|
"
|
|
|
|
|
2021-12-28 12:54:46 +00:00
|
|
|
replUndefinedVariable="
|
2022-04-18 17:21:47 +00:00
|
|
|
import $testDir/undefined-variable.nix
|
2021-12-28 12:54:46 +00:00
|
|
|
"
|
|
|
|
|
2021-07-15 16:17:18 +00:00
|
|
|
testRepl () {
|
|
|
|
local nixArgs=("$@")
|
2022-04-18 17:21:47 +00:00
|
|
|
rm -rf repl-result-out || true # cleanup from other runs backed by a foreign nix store
|
2021-11-03 09:54:17 +00:00
|
|
|
local replOutput="$(nix repl "${nixArgs[@]}" <<< "$replCmds")"
|
|
|
|
echo "$replOutput"
|
|
|
|
local outPath=$(echo "$replOutput" |&
|
2021-07-15 16:17:18 +00:00
|
|
|
grep -o -E "$NIX_STORE_DIR/\w*-simple")
|
|
|
|
nix path-info "${nixArgs[@]}" "$outPath"
|
2022-04-18 17:21:47 +00:00
|
|
|
[ "$(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"
|
|
|
|
|
2021-11-26 15:03:07 +00:00
|
|
|
# simple.nix prints a PATH during build
|
2021-12-09 15:26:46 +00:00
|
|
|
echo "$replOutput" | grepQuiet -s 'PATH=' || fail "nix repl :log doesn't output logs"
|
2021-12-28 12:54:46 +00:00
|
|
|
local replOutput="$(nix repl "${nixArgs[@]}" <<< "$replFailingCmds" 2>&1)"
|
2021-11-26 15:03:07 +00:00
|
|
|
echo "$replOutput"
|
2021-12-09 15:26:46 +00:00
|
|
|
echo "$replOutput" | grepQuiet -s 'This should fail' \
|
2021-11-26 15:03:07 +00:00
|
|
|
|| fail "nix repl :log doesn't output logs for a failed derivation"
|
2021-12-28 12:54:46 +00:00
|
|
|
local replOutput="$(nix repl --show-trace "${nixArgs[@]}" <<< "$replUndefinedVariable" 2>&1)"
|
|
|
|
echo "$replOutput"
|
2021-12-09 15:26:46 +00:00
|
|
|
echo "$replOutput" | grepQuiet -s "while evaluating the file" \
|
2021-12-28 12:54:46 +00:00
|
|
|
|| fail "nix repl --show-trace doesn't show the trace"
|
2022-01-31 17:03:24 +00:00
|
|
|
|
|
|
|
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')"
|
2021-07-15 16:17:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# 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"
|
2022-02-04 06:36:56 +00:00
|
|
|
|
2023-08-17 11:03:43 +00:00
|
|
|
# Remove ANSI escape sequences. They can prevent grep from finding a match.
|
|
|
|
stripColors () {
|
|
|
|
sed -E 's/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g'
|
|
|
|
}
|
|
|
|
|
|
|
|
testReplResponseGeneral () {
|
|
|
|
local grepMode="$1"; shift
|
2022-05-20 06:12:02 +00:00
|
|
|
local commands="$1"; shift
|
|
|
|
local expectedResponse="$1"; shift
|
2023-08-17 11:03:43 +00:00
|
|
|
local response="$(nix repl "$@" <<< "$commands" | stripColors)"
|
|
|
|
echo "$response" | grepQuiet "$grepMode" -s "$expectedResponse" \
|
2022-02-04 06:36:56 +00:00
|
|
|
|| fail "repl command set:
|
|
|
|
|
2022-05-20 06:12:02 +00:00
|
|
|
$commands
|
2022-02-04 06:36:56 +00:00
|
|
|
|
|
|
|
does not respond with:
|
|
|
|
|
2022-05-20 06:12:02 +00:00
|
|
|
$expectedResponse
|
2022-02-04 06:36:56 +00:00
|
|
|
|
|
|
|
but with:
|
|
|
|
|
2023-08-17 11:03:43 +00:00
|
|
|
$response
|
|
|
|
"
|
|
|
|
}
|
|
|
|
|
|
|
|
testReplResponse () {
|
|
|
|
testReplResponseGeneral --basic-regexp "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
testReplResponseNoRegex () {
|
|
|
|
testReplResponseGeneral --fixed-strings "$@"
|
2022-02-04 06:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# :a uses the newest version of a symbol
|
|
|
|
testReplResponse '
|
|
|
|
:a { a = "1"; }
|
|
|
|
:a { a = "2"; }
|
|
|
|
"result: ${a}"
|
|
|
|
' "result: 2"
|
2022-05-20 06:12:02 +00:00
|
|
|
|
2023-04-09 20:42:20 +00:00
|
|
|
# check dollar escaping https://github.com/NixOS/nix/issues/4909
|
|
|
|
# note the escaped \,
|
|
|
|
# \\
|
|
|
|
# because the second argument is a regex
|
2023-08-17 11:03:43 +00:00
|
|
|
testReplResponseNoRegex '
|
2023-04-09 20:42:20 +00:00
|
|
|
"$" + "{hi}"
|
2023-08-17 11:03:43 +00:00
|
|
|
' '"\${hi}"'
|
2023-04-09 20:42:20 +00:00
|
|
|
|
2022-05-20 06:12:02 +00:00
|
|
|
testReplResponse '
|
|
|
|
drvPath
|
2022-05-20 12:03:41 +00:00
|
|
|
' '".*-simple.drv"' \
|
2022-05-20 16:09:41 +00:00
|
|
|
$testDir/simple.nix
|
2022-05-20 06:12:02 +00:00
|
|
|
|
|
|
|
testReplResponse '
|
|
|
|
drvPath
|
2022-05-20 12:03:41 +00:00
|
|
|
' '".*-simple.drv"' \
|
2022-06-24 15:17:29 +00:00
|
|
|
--file $testDir/simple.nix --experimental-features 'ca-derivations'
|
2022-05-20 06:12:02 +00:00
|
|
|
|
|
|
|
testReplResponse '
|
|
|
|
drvPath
|
2022-05-20 12:03:41 +00:00
|
|
|
' '".*-simple.drv"' \
|
2022-06-24 15:17:29 +00:00
|
|
|
--file $testDir/simple.nix --extra-experimental-features 'repl-flake ca-derivations'
|
2022-05-20 06:12:02 +00:00
|
|
|
|
|
|
|
mkdir -p flake && cat <<EOF > flake/flake.nix
|
|
|
|
{
|
|
|
|
outputs = { self }: {
|
|
|
|
foo = 1;
|
|
|
|
bar.baz = 2;
|
|
|
|
|
|
|
|
changingThing = "beforeChange";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
testReplResponse '
|
|
|
|
foo + baz
|
|
|
|
' "3" \
|
2022-05-20 12:20:00 +00:00
|
|
|
./flake ./flake\#bar --experimental-features 'flakes repl-flake'
|
2022-05-20 06:12:02 +00:00
|
|
|
|
|
|
|
# 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"
|
2022-05-20 12:20:00 +00:00
|
|
|
) | nix repl ./flake --experimental-features 'flakes repl-flake')
|
2021-12-09 15:26:46 +00:00
|
|
|
echo "$replResult" | grepQuiet -s beforeChange
|
|
|
|
echo "$replResult" | grepQuiet -s afterChange
|
2023-08-17 11:03:43 +00:00
|
|
|
|
|
|
|
# Test recursive printing and formatting
|
|
|
|
# Normal output should print attributes in lexicographical order non-recursively
|
|
|
|
testReplResponseNoRegex '
|
|
|
|
{ a = { b = 2; }; l = [ 1 2 3 ]; s = "string"; n = 1234; x = rec { y = { z = { inherit y; }; }; }; }
|
|
|
|
' '{ a = { ... }; l = [ ... ]; n = 1234; s = "string"; x = { ... }; }'
|
|
|
|
|
|
|
|
# Same for lists, but order is preserved
|
|
|
|
testReplResponseNoRegex '
|
|
|
|
[ 42 1 "thingy" ({ a = 1; }) ([ 1 2 3 ]) ]
|
|
|
|
' '[ 42 1 "thingy" { ... } [ ... ] ]'
|
|
|
|
|
|
|
|
# Same for let expressions
|
|
|
|
testReplResponseNoRegex '
|
|
|
|
let x = { y = { a = 1; }; inherit x; }; in x
|
2024-03-08 05:26:06 +00:00
|
|
|
' '{ x = «repeated»; y = { ... }; }'
|
2023-08-17 11:03:43 +00:00
|
|
|
|
|
|
|
# The :p command should recursively print sets, but prevent infinite recursion
|
|
|
|
testReplResponseNoRegex '
|
|
|
|
:p { a = { b = 2; }; s = "string"; n = 1234; x = rec { y = { z = { inherit y; }; }; }; }
|
|
|
|
' '{ a = { b = 2; }; n = 1234; s = "string"; x = { y = { z = { y = «repeated»; }; }; }; }'
|
|
|
|
|
|
|
|
# Same for lists
|
|
|
|
testReplResponseNoRegex '
|
|
|
|
:p [ 42 1 "thingy" (rec { a = 1; b = { inherit a; inherit b; }; }) ([ 1 2 3 ]) ]
|
|
|
|
' '[ 42 1 "thingy" { a = 1; b = { a = 1; b = «repeated»; }; } [ 1 2 3 ] ]'
|
|
|
|
|
|
|
|
# Same for let expressions
|
|
|
|
testReplResponseNoRegex '
|
|
|
|
:p let x = { y = { a = 1; }; inherit x; }; in x
|
2024-03-08 05:26:06 +00:00
|
|
|
' '{ x = «repeated»; y = { a = 1; }; }'
|