2024-05-27 16:40:49 +03:00
|
|
|
#!/usr/bin/env bash
|
2024-05-28 19:43:04 +03:00
|
|
|
|
2021-07-15 19:17:18 +03:00
|
|
|
source common.sh
|
2024-07-08 18:39:26 +03:00
|
|
|
source characterisation/framework.sh
|
2021-07-15 19:17:18 +03:00
|
|
|
|
2022-04-18 20:21:47 +03:00
|
|
|
testDir="$PWD"
|
|
|
|
cd "$TEST_ROOT"
|
|
|
|
|
2021-07-15 19:17:18 +03:00
|
|
|
replCmds="
|
2021-12-09 17:35:27 +02:00
|
|
|
simple = 1
|
2022-04-18 20:21:47 +03:00
|
|
|
simple = import $testDir/simple.nix
|
|
|
|
:bl simple
|
2021-11-26 17:03:07 +02:00
|
|
|
:log simple
|
|
|
|
"
|
|
|
|
|
|
|
|
replFailingCmds="
|
2022-04-18 20:21:47 +03:00
|
|
|
failing = import $testDir/simple-failing.nix
|
2021-11-26 17:03:07 +02:00
|
|
|
:b failing
|
|
|
|
:log failing
|
2021-07-15 19:17:18 +03:00
|
|
|
"
|
|
|
|
|
2021-12-28 14:54:46 +02:00
|
|
|
replUndefinedVariable="
|
2022-04-18 20:21:47 +03:00
|
|
|
import $testDir/undefined-variable.nix
|
2021-12-28 14:54:46 +02:00
|
|
|
"
|
|
|
|
|
2024-06-16 13:51:46 +03:00
|
|
|
TODO_NixOS
|
|
|
|
|
2021-07-15 19:17:18 +03:00
|
|
|
testRepl () {
|
2024-05-27 16:40:49 +03:00
|
|
|
local nixArgs
|
|
|
|
nixArgs=("$@")
|
2022-04-18 20:21:47 +03:00
|
|
|
rm -rf repl-result-out || true # cleanup from other runs backed by a foreign nix store
|
2024-05-27 16:40:49 +03:00
|
|
|
local replOutput
|
|
|
|
replOutput="$(nix repl "${nixArgs[@]}" <<< "$replCmds")"
|
2021-11-03 11:54:17 +02:00
|
|
|
echo "$replOutput"
|
2024-05-27 16:40:49 +03:00
|
|
|
local outPath
|
|
|
|
outPath=$(echo "$replOutput" |&
|
2021-07-15 19:17:18 +03:00
|
|
|
grep -o -E "$NIX_STORE_DIR/\w*-simple")
|
|
|
|
nix path-info "${nixArgs[@]}" "$outPath"
|
2022-04-18 20:21:47 +03: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 17:03:07 +02:00
|
|
|
# simple.nix prints a PATH during build
|
2021-12-09 17:26:46 +02:00
|
|
|
echo "$replOutput" | grepQuiet -s 'PATH=' || fail "nix repl :log doesn't output logs"
|
2024-05-27 16:40:49 +03:00
|
|
|
replOutput="$(nix repl "${nixArgs[@]}" <<< "$replFailingCmds" 2>&1)"
|
2021-11-26 17:03:07 +02:00
|
|
|
echo "$replOutput"
|
2021-12-09 17:26:46 +02:00
|
|
|
echo "$replOutput" | grepQuiet -s 'This should fail' \
|
2021-11-26 17:03:07 +02:00
|
|
|
|| fail "nix repl :log doesn't output logs for a failed derivation"
|
2024-05-27 16:40:49 +03:00
|
|
|
replOutput="$(nix repl --show-trace "${nixArgs[@]}" <<< "$replUndefinedVariable" 2>&1)"
|
2021-12-28 14:54:46 +02:00
|
|
|
echo "$replOutput"
|
2021-12-09 17:26:46 +02:00
|
|
|
echo "$replOutput" | grepQuiet -s "while evaluating the file" \
|
2021-12-28 14:54:46 +02:00
|
|
|
|| fail "nix repl --show-trace doesn't show the trace"
|
2022-01-31 19:03:24 +02: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')"
|
2024-03-23 03:56:19 +02:00
|
|
|
|
2024-05-27 16:40:49 +03:00
|
|
|
expectStderr 1 nix repl "${testDir}/simple.nix" \
|
2024-03-23 03:56:19 +02:00
|
|
|
| grepQuiet -s "error: path '$testDir/simple.nix' is not a flake"
|
2021-07-15 19:17:18 +03: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 08:36:56 +02:00
|
|
|
|
2023-08-17 14:03:43 +03: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 () {
|
2024-05-27 16:40:49 +03:00
|
|
|
local grepMode commands expectedResponse response
|
|
|
|
grepMode="$1"; shift
|
|
|
|
commands="$1"; shift
|
2024-07-15 23:11:26 +03:00
|
|
|
# Expected response can contain newlines.
|
|
|
|
# grep can't handle multiline patterns, so replace newlines with TEST_NEWLINE
|
|
|
|
# in both expectedResponse and response.
|
|
|
|
# awk ORS always adds a trailing record separator, so we strip it with sed.
|
|
|
|
expectedResponse="$(printf '%s' "$1" | awk 1 ORS=TEST_NEWLINE | sed 's/TEST_NEWLINE$//')"; shift
|
|
|
|
# We don't need to strip trailing record separator here, since extra data is ok.
|
|
|
|
response="$(nix repl "$@" <<< "$commands" 2>&1 | stripColors | awk 1 ORS=TEST_NEWLINE)"
|
|
|
|
printf '%s' "$response" | grepQuiet "$grepMode" -s "$expectedResponse" \
|
|
|
|
|| fail "$(echo "repl command set:
|
2022-02-04 08:36:56 +02:00
|
|
|
|
2022-05-20 09:12:02 +03:00
|
|
|
$commands
|
2022-02-04 08:36:56 +02:00
|
|
|
|
|
|
|
does not respond with:
|
|
|
|
|
2024-07-15 23:11:26 +03:00
|
|
|
---
|
2022-05-20 09:12:02 +03:00
|
|
|
$expectedResponse
|
2024-07-15 23:11:26 +03:00
|
|
|
---
|
2022-02-04 08:36:56 +02:00
|
|
|
|
|
|
|
but with:
|
|
|
|
|
2024-07-15 23:11:26 +03:00
|
|
|
---
|
2023-08-17 14:03:43 +03:00
|
|
|
$response
|
2024-07-15 23:11:26 +03:00
|
|
|
---
|
|
|
|
|
|
|
|
" | sed 's/TEST_NEWLINE/\n/g')"
|
2023-08-17 14:03:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
testReplResponse () {
|
|
|
|
testReplResponseGeneral --basic-regexp "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
testReplResponseNoRegex () {
|
|
|
|
testReplResponseGeneral --fixed-strings "$@"
|
2022-02-04 08:36:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# :a uses the newest version of a symbol
|
2024-05-27 16:40:49 +03:00
|
|
|
#
|
|
|
|
# shellcheck disable=SC2016
|
2022-02-04 08:36:56 +02:00
|
|
|
testReplResponse '
|
|
|
|
:a { a = "1"; }
|
|
|
|
:a { a = "2"; }
|
|
|
|
"result: ${a}"
|
|
|
|
' "result: 2"
|
2022-05-20 09:12:02 +03:00
|
|
|
|
2023-04-09 23:42:20 +03:00
|
|
|
# check dollar escaping https://github.com/NixOS/nix/issues/4909
|
|
|
|
# note the escaped \,
|
|
|
|
# \\
|
|
|
|
# because the second argument is a regex
|
2024-05-27 16:40:49 +03:00
|
|
|
#
|
|
|
|
# shellcheck disable=SC2016
|
2023-08-17 14:03:43 +03:00
|
|
|
testReplResponseNoRegex '
|
2023-04-09 23:42:20 +03:00
|
|
|
"$" + "{hi}"
|
2023-08-17 14:03:43 +03:00
|
|
|
' '"\${hi}"'
|
2023-04-09 23:42:20 +03:00
|
|
|
|
2022-05-20 09:12:02 +03:00
|
|
|
testReplResponse '
|
|
|
|
drvPath
|
2022-05-20 15:03:41 +03:00
|
|
|
' '".*-simple.drv"' \
|
2024-05-27 16:40:49 +03:00
|
|
|
--file "$testDir/simple.nix"
|
2022-05-20 09:12:02 +03:00
|
|
|
|
|
|
|
testReplResponse '
|
|
|
|
drvPath
|
2022-05-20 15:03:41 +03:00
|
|
|
' '".*-simple.drv"' \
|
2024-05-27 16:40:49 +03:00
|
|
|
--file "$testDir/simple.nix" --experimental-features 'ca-derivations'
|
2022-05-20 09:12:02 +03:00
|
|
|
|
|
|
|
mkdir -p flake && cat <<EOF > flake/flake.nix
|
|
|
|
{
|
|
|
|
outputs = { self }: {
|
|
|
|
foo = 1;
|
|
|
|
bar.baz = 2;
|
|
|
|
|
|
|
|
changingThing = "beforeChange";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
testReplResponse '
|
|
|
|
foo + baz
|
|
|
|
' "3" \
|
2023-09-26 01:31:00 +03:00
|
|
|
./flake ./flake\#bar --experimental-features 'flakes'
|
2022-05-20 09:12:02 +03: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"
|
2023-09-26 01:31:00 +03:00
|
|
|
) | nix repl ./flake --experimental-features 'flakes')
|
2021-12-09 17:26:46 +02:00
|
|
|
echo "$replResult" | grepQuiet -s beforeChange
|
|
|
|
echo "$replResult" | grepQuiet -s afterChange
|
2023-08-17 14:03:43 +03: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; }; }; }; }
|
2024-02-04 10:40:30 +02:00
|
|
|
' \
|
|
|
|
'{
|
|
|
|
a = { ... };
|
|
|
|
l = [ ... ];
|
|
|
|
n = 1234;
|
|
|
|
s = "string";
|
|
|
|
x = { ... };
|
|
|
|
}
|
|
|
|
'
|
2023-08-17 14:03:43 +03:00
|
|
|
|
|
|
|
# Same for lists, but order is preserved
|
|
|
|
testReplResponseNoRegex '
|
|
|
|
[ 42 1 "thingy" ({ a = 1; }) ([ 1 2 3 ]) ]
|
2024-02-04 10:40:30 +02:00
|
|
|
' \
|
|
|
|
'[
|
|
|
|
42
|
|
|
|
1
|
|
|
|
"thingy"
|
|
|
|
{ ... }
|
|
|
|
[ ... ]
|
|
|
|
]
|
|
|
|
'
|
2023-08-17 14:03:43 +03:00
|
|
|
|
|
|
|
# Same for let expressions
|
|
|
|
testReplResponseNoRegex '
|
|
|
|
let x = { y = { a = 1; }; inherit x; }; in x
|
2024-02-04 10:40:30 +02:00
|
|
|
' \
|
|
|
|
'{
|
2024-07-15 23:11:26 +03:00
|
|
|
x = «repeated»;
|
2024-02-04 10:40:30 +02:00
|
|
|
y = { ... };
|
|
|
|
}
|
|
|
|
'
|
2023-08-17 14:03:43 +03: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; }; }; }; }
|
2024-02-04 10:40:30 +02:00
|
|
|
' \
|
|
|
|
'{
|
|
|
|
a = { b = 2; };
|
|
|
|
n = 1234;
|
|
|
|
s = "string";
|
|
|
|
x = {
|
|
|
|
y = {
|
|
|
|
z = {
|
|
|
|
y = «repeated»;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
'
|
2023-08-17 14:03:43 +03:00
|
|
|
|
|
|
|
# Same for lists
|
|
|
|
testReplResponseNoRegex '
|
|
|
|
:p [ 42 1 "thingy" (rec { a = 1; b = { inherit a; inherit b; }; }) ([ 1 2 3 ]) ]
|
2024-02-04 10:40:30 +02:00
|
|
|
' \
|
|
|
|
'[
|
|
|
|
42
|
|
|
|
1
|
|
|
|
"thingy"
|
|
|
|
{
|
|
|
|
a = 1;
|
|
|
|
b = {
|
|
|
|
a = 1;
|
|
|
|
b = «repeated»;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
[
|
|
|
|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
]
|
|
|
|
]
|
|
|
|
'
|
2023-08-17 14:03:43 +03:00
|
|
|
|
|
|
|
# Same for let expressions
|
|
|
|
testReplResponseNoRegex '
|
|
|
|
:p let x = { y = { a = 1; }; inherit x; }; in x
|
2024-02-04 10:40:30 +02:00
|
|
|
' \
|
|
|
|
'{
|
|
|
|
x = «repeated»;
|
2024-07-15 23:11:26 +03:00
|
|
|
y = { a = 1; };
|
2024-02-04 10:40:30 +02:00
|
|
|
}
|
|
|
|
'
|
2024-07-08 18:39:26 +03:00
|
|
|
|
|
|
|
# TODO: move init to characterisation/framework.sh
|
|
|
|
badDiff=0
|
|
|
|
badExitCode=0
|
|
|
|
|
|
|
|
nixVersion="$(nix eval --impure --raw --expr 'builtins.nixVersion' --extra-experimental-features nix-command)"
|
|
|
|
|
|
|
|
runRepl () {
|
2024-07-10 01:48:23 +03:00
|
|
|
|
|
|
|
# That is right, we are also filtering out the testdir _without underscores_.
|
|
|
|
# This is crazy, but without it, GHA will fail to run the tests, showing paths
|
2024-07-15 23:11:26 +03:00
|
|
|
# _with_ underscores in the set -x log, but _without_ underscores in the
|
2024-07-10 01:48:23 +03:00
|
|
|
# supposed nix repl output. I have looked in a number of places, but I cannot
|
|
|
|
# find a mechanism that could cause this to happen.
|
|
|
|
local testDirNoUnderscores
|
|
|
|
testDirNoUnderscores="${testDir//_/}"
|
|
|
|
|
2024-07-08 18:39:26 +03:00
|
|
|
# TODO: pass arguments to nix repl; see lang.sh
|
|
|
|
nix repl 2>&1 \
|
|
|
|
| stripColors \
|
|
|
|
| sed \
|
|
|
|
-e "s@$testDir@/path/to/tests/functional@g" \
|
2024-07-10 01:48:23 +03:00
|
|
|
-e "s@$testDirNoUnderscores@/path/to/tests/functional@g" \
|
2024-07-08 18:39:26 +03:00
|
|
|
-e "s@$nixVersion@<nix version>@g" \
|
|
|
|
-e "s@Added [0-9]* variables@Added <number omitted> variables@g" \
|
|
|
|
| grep -vF $'warning: you don\'t have Internet access; disabling some network-dependent features' \
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
for test in $(cd "$testDir/repl"; echo *.in); do
|
|
|
|
test="$(basename "$test" .in)"
|
|
|
|
in="$testDir/repl/$test.in"
|
|
|
|
actual="$testDir/repl/$test.actual"
|
|
|
|
expected="$testDir/repl/$test.expected"
|
|
|
|
(cd "$testDir/repl"; set +x; runRepl 2>&1) < "$in" > "$actual"
|
|
|
|
diffAndAcceptInner "$test" "$actual" "$expected"
|
|
|
|
done
|
|
|
|
|
|
|
|
characterisationTestExit
|