test(functional): add tests for new environment operation flags

This commit is contained in:
Bryan Honof 2024-09-19 18:43:22 +02:00
parent 0b790b4849
commit affd2dbc6c
No known key found for this signature in database
3 changed files with 81 additions and 13 deletions

View File

@ -547,7 +547,6 @@
''^tests/functional/flakes/absolute-paths\.sh$'' ''^tests/functional/flakes/absolute-paths\.sh$''
''^tests/functional/flakes/check\.sh$'' ''^tests/functional/flakes/check\.sh$''
''^tests/functional/flakes/config\.sh$'' ''^tests/functional/flakes/config\.sh$''
''^tests/functional/flakes/develop\.sh$''
''^tests/functional/flakes/flakes\.sh$'' ''^tests/functional/flakes/flakes\.sh$''
''^tests/functional/flakes/follow-paths\.sh$'' ''^tests/functional/flakes/follow-paths\.sh$''
''^tests/functional/flakes/prefetch\.sh$'' ''^tests/functional/flakes/prefetch\.sh$''

View File

@ -321,11 +321,7 @@ MixEnvironment::MixEnvironment() : ignoreEnvironment(false)
addFlag({ addFlag({
.longName = "set-env-var", .longName = "set-env-var",
.shortName = 's', .shortName = 's',
.description = "Add/override an environment variable *name* with *value*.\n\n" .description = "Sets an environment variable *name* with *value*.",
"> **Notes**\n"
">\n"
"> Duplicate definitions will be overwritten, last one wins.\n\n"
"> Cancles out with `--unset-env-var`.\n\n",
.category = environmentVariablesCategory, .category = environmentVariablesCategory,
.labels = {"name", "value"}, .labels = {"name", "value"},
.handler = {[&](std::string name, std::string value) { .handler = {[&](std::string name, std::string value) {

View File

@ -5,11 +5,11 @@ source ../common.sh
TODO_NixOS TODO_NixOS
clearStore clearStore
rm -rf $TEST_HOME/.cache $TEST_HOME/.config $TEST_HOME/.local rm -rf "$TEST_HOME/.cache" "$TEST_HOME/.config" "$TEST_HOME/.local"
# Create flake under test. # Create flake under test.
cp ../shell-hello.nix "${config_nix}" $TEST_HOME/ cp ../shell-hello.nix "$config_nix" "$TEST_HOME/"
cat <<EOF >$TEST_HOME/flake.nix cat <<EOF >"$TEST_HOME/flake.nix"
{ {
inputs.nixpkgs.url = "$TEST_HOME/nixpkgs"; inputs.nixpkgs.url = "$TEST_HOME/nixpkgs";
outputs = {self, nixpkgs}: { outputs = {self, nixpkgs}: {
@ -24,13 +24,13 @@ cat <<EOF >$TEST_HOME/flake.nix
EOF EOF
# Create fake nixpkgs flake. # Create fake nixpkgs flake.
mkdir -p $TEST_HOME/nixpkgs mkdir -p "$TEST_HOME/nixpkgs"
cp "${config_nix}" ../shell.nix $TEST_HOME/nixpkgs cp "${config_nix}" ../shell.nix "$TEST_HOME/nixpkgs"
# `config.nix` cannot be gotten via build dir / env var (runs afoul pure eval). Instead get from flake. # `config.nix` cannot be gotten via build dir / env var (runs afoul pure eval). Instead get from flake.
removeBuildDirRef "$TEST_HOME/nixpkgs"/*.nix removeBuildDirRef "$TEST_HOME/nixpkgs"/*.nix
cat <<EOF >$TEST_HOME/nixpkgs/flake.nix cat <<EOF >"$TEST_HOME/nixpkgs/flake.nix"
{ {
outputs = {self}: { outputs = {self}: {
legacyPackages.$system.bashInteractive = (import ./shell.nix {}).bashInteractive; legacyPackages.$system.bashInteractive = (import ./shell.nix {}).bashInteractive;
@ -38,7 +38,7 @@ cat <<EOF >$TEST_HOME/nixpkgs/flake.nix
} }
EOF EOF
cd $TEST_HOME cd "$TEST_HOME"
# Test whether `nix develop` passes through environment variables. # Test whether `nix develop` passes through environment variables.
[[ "$( [[ "$(
@ -54,6 +54,79 @@ echo "\$ENVVAR"
EOF EOF
)" ]] )" ]]
# Test wether `--keep-env-var` keeps the environment variable.
(
expect='BAR'
got="$(FOO='BAR' nix develop --ignore-env --keep-env-var FOO --no-write-lock-file .#hello <<EOF
echo "\$FOO"
EOF
)"
[[ "$got" == "$expect" ]]
)
# Test wether duplicate `--keep-env-var` keeps the environment variable.
(
expect='BAR'
got="$(FOO='BAR' nix develop --ignore-env --keep-env-var FOO --keep-env-var FOO --no-write-lock-file .#hello <<EOF
echo "\$FOO"
EOF
)"
[[ "$got" == "$expect" ]]
)
# Test wether `--set-env-var` sets the environment variable.
(
expect='BAR'
got="$(nix develop --ignore-env --set-env-var FOO 'BAR' --no-write-lock-file .#hello <<EOF
echo "\$FOO"
EOF
)"
[[ "$got" == "$expect" ]]
)
# Test that `--set-env-var` overwrites previously set variables.
(
expect='BLA'
got="$(FOO='BAR' nix develop --set-env-var FOO 'BLA' --no-write-lock-file .#hello <<EOF
echo "\$FOO"
EOF
)"
[[ "$got" == "$expect" ]]
)
# Test that multiple `--set-env-var` work.
(
expect='BARFOO'
got="$(nix develop --set-env-var FOO 'BAR' --set-env-var BAR 'FOO' --no-write-lock-file .#hello <<EOF | tr -d '\n'
echo "\$FOO"
echo "\$BAR"
EOF
)"
[[ "$got" == "$expect" ]]
)
# Check that we throw an error when `--keep-env-var` is used without `--ignore-env`.
expectStderr 1 nix develop --keep-env-var FOO .#hello |
grepQuiet "error: --keep-env-var does not make sense without --ignore-env"
# Check that we throw an error when `--unset-env-var` is used with `--ignore-env`.
expectStderr 1 nix develop --ignore-env --unset-env-var FOO .#hello |
grepQuiet "error: --unset-env-var does not make sense with --ignore-env"
# Test wether multiple occurances of `--set-env-var` throws.
expectStderr 1 nix develop --set-env-var FOO 'BAR' --set-env-var FOO 'BLA' --no-write-lock-file .#hello |
grepQuiet "error: Duplicate definition of environment variable 'FOO' with '--set-env-var' is ambiguous"
# Test wether similar `--unset-env-var` and `--set-env-var` throws.
expectStderr 1 nix develop --set-env-var FOO 'BAR' --unset-env-var FOO --no-write-lock-file .#hello |
grepQuiet "error: Cannot unset environment variable 'FOO' that is set with '--set-env-var'"
expectStderr 1 nix develop --unset-env-var FOO --set-env-var FOO 'BAR' --no-write-lock-file .#hello |
grepQuiet "error: Cannot set environment variable 'FOO' that is unset with '--unset-env-var'"
# Check that multiple `--ignore-env`'s are okay.
expectStderr 0 nix develop --ignore-env --set-env-var FOO 'BAR' --ignore-env .#hello
# Determine the bashInteractive executable. # Determine the bashInteractive executable.
nix build --no-write-lock-file './nixpkgs#bashInteractive' --out-link ./bash-interactive nix build --no-write-lock-file './nixpkgs#bashInteractive' --out-link ./bash-interactive
BASH_INTERACTIVE_EXECUTABLE="$PWD/bash-interactive/bin/bash" BASH_INTERACTIVE_EXECUTABLE="$PWD/bash-interactive/bin/bash"