1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-08 19:46:02 +01:00

shellcheck fix: tests/functional/user-envs-test-case.sh

This commit is contained in:
Farid Zakaria 2025-09-30 20:22:11 -07:00
parent 359e73a6db
commit 049c4c7546
2 changed files with 39 additions and 34 deletions

View file

@ -1,14 +1,17 @@
# shellcheck shell=bash
clearProfiles
# Query installed: should be empty.
test "$(nix-env -p $profiles/test -q '*' | wc -l)" -eq 0
# shellcheck disable=SC2154
test "$(nix-env -p "$profiles"/test -q '*' | wc -l)" -eq 0
nix-env --switch-profile $profiles/test
nix-env --switch-profile "$profiles"/test
# Query available: should contain several.
test "$(nix-env -f ./user-envs.nix -qa '*' | wc -l)" -eq 6
outPath10=$(nix-env -f ./user-envs.nix -qa --out-path --no-name '*' | grep foo-1.0)
drvPath10=$(nix-env -f ./user-envs.nix -qa --drv-path --no-name '*' | grep foo-1.0)
# shellcheck disable=SC2166
[ -n "$outPath10" -a -n "$drvPath10" ]
TODO_NixOS
@ -20,18 +23,19 @@ nix-env -f ./user-envs.nix -qa --json | jq -e '.[] | select(.name == "bar-0.1")
] | all'
nix-env -f ./user-envs.nix -qa --json --out-path | jq -e '.[] | select(.name == "bar-0.1") | [
.outputName == "out",
(.outputs.out | test("'$NIX_STORE_DIR'.*-0\\.1"))
(.outputs.out | test("'"$NIX_STORE_DIR"'.*-0\\.1"))
] | all'
nix-env -f ./user-envs.nix -qa --json --drv-path | jq -e '.[] | select(.name == "bar-0.1") | (.drvPath | test("'$NIX_STORE_DIR'.*-0\\.1\\.drv"))'
nix-env -f ./user-envs.nix -qa --json --drv-path | jq -e '.[] | select(.name == "bar-0.1") | (.drvPath | test("'"$NIX_STORE_DIR"'.*-0\\.1\\.drv"))'
# Query descriptions.
nix-env -f ./user-envs.nix -qa '*' --description | grepQuiet silly
rm -rf $HOME/.nix-defexpr
ln -s $(pwd)/user-envs.nix $HOME/.nix-defexpr
rm -rf "$HOME"/.nix-defexpr
ln -s "$(pwd)"/user-envs.nix "$HOME"/.nix-defexpr
nix-env -qa '*' --description | grepQuiet silly
# Query the system.
nix-env -qa '*' --system | grepQuiet $system
# shellcheck disable=SC2154
nix-env -qa '*' --system | grepQuiet "$system"
# Install "foo-1.0".
nix-env -i foo-1.0
@ -40,7 +44,7 @@ nix-env -i foo-1.0
# executable).
test "$(nix-env -q '*' | wc -l)" -eq 1
nix-env -q '*' | grepQuiet foo-1.0
test "$($profiles/test/bin/foo)" = "foo-1.0"
test "$("$profiles"/test/bin/foo)" = "foo-1.0"
# Test nix-env -qc to compare installed against available packages, and vice versa.
nix-env -qc '*' | grepQuiet '< 2.0'
@ -55,6 +59,7 @@ nix-env -qas | grepQuiet -- '--- bar-0.1'
# Disable foo.
nix-env --set-flag active false foo
# shellcheck disable=SC2235
(! [ -e "$profiles/test/bin/foo" ])
# Enable foo.
@ -72,7 +77,7 @@ nix-env -i foo-2.0pre1
# Query installed: should contain foo-2.0pre1 now.
test "$(nix-env -q '*' | wc -l)" -eq 1
nix-env -q '*' | grepQuiet foo-2.0pre1
test "$($profiles/test/bin/foo)" = "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
@ -80,7 +85,7 @@ 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
nix-env -q '*' | grepQuiet foo-2.0
test "$($profiles/test/bin/foo)" = "foo-2.0"
test "$("$profiles"/test/bin/foo)" = "foo-2.0"
# Store the path of foo-2.0.
outPath20=$(nix-env -q --out-path --no-name '*' | grep foo-2.0)
@ -95,9 +100,9 @@ if nix-env -q '*' | grepQuiet foo; then false; fi
nix-env -q '*' | grepQuiet bar
# Rollback: should bring "foo" back.
oldGen="$(nix-store -q --resolve $profiles/test)"
oldGen="$(nix-store -q --resolve "$profiles"/test)"
nix-env --rollback
[ "$(nix-store -q --resolve $profiles/test)" != "$oldGen" ]
[ "$(nix-store -q --resolve "$profiles"/test)" != "$oldGen" ]
nix-env -q '*' | grepQuiet foo-2.0
nix-env -q '*' | grepQuiet bar
@ -122,23 +127,23 @@ test "$(nix-env --list-generations | wc -l)" -eq 8
# Switch to a specified generation.
nix-env --switch-generation 7
[ "$(nix-store -q --resolve $profiles/test)" = "$oldGen" ]
[ "$(nix-store -q --resolve "$profiles"/test)" = "$oldGen" ]
# Install foo-1.0, now using its store path.
nix-env -i "$outPath10"
nix-env -q '*' | grepQuiet foo-1.0
nix-store -qR $profiles/test | grep "$outPath10"
nix-store -q --referrers-closure $profiles/test | grep "$(nix-store -q --resolve $profiles/test)"
[ "$(nix-store -q --deriver "$outPath10")" = $drvPath10 ]
nix-store -qR "$profiles"/test | grep "$outPath10"
nix-store -q --referrers-closure "$profiles"/test | grep "$(nix-store -q --resolve "$profiles"/test)"
[ "$(nix-store -q --deriver "$outPath10")" = "$drvPath10" ]
# Uninstall foo-1.0, using a symlink to its store path.
ln -sfn $outPath10/bin/foo $TEST_ROOT/symlink
nix-env -e $TEST_ROOT/symlink
ln -sfn "$outPath10"/bin/foo "$TEST_ROOT"/symlink
nix-env -e "$TEST_ROOT"/symlink
if nix-env -q '*' | grepQuiet foo; then false; fi
nix-store -qR $profiles/test | grepInverse "$outPath10"
nix-store -qR "$profiles"/test | grepInverse "$outPath10"
# Install foo-1.0, now using a symlink to its store path.
nix-env -i $TEST_ROOT/symlink
nix-env -i "$TEST_ROOT"/symlink
nix-env -q '*' | grepQuiet foo
# Delete all old generations.
@ -148,15 +153,16 @@ nix-env --delete-generations old
# foo-1.0.
nix-collect-garbage
test -e "$outPath10"
# shellcheck disable=SC2235
(! [ -e "$outPath20" ])
# Uninstall everything
nix-env -e '*'
test "$(nix-env -q '*' | wc -l)" -eq 0
test "$(nix-env -q '*' -c)" -eq 0
# Installing "foo" should only install the newest foo.
nix-env -i foo
test "$(nix-env -q '*' | grep foo- | wc -l)" -eq 1
test "$(nix-env -q '*' | grep foo- -c)" -eq 1
nix-env -q '*' | grepQuiet foo-2.0
# On the other hand, this should install both (and should fail due to
@ -177,25 +183,25 @@ nix-env -q '*' | grepQuiet bar-0.1.1
# declared priorities.
nix-env -e '*'
nix-env -i foo-0.1 foo-1.0
[ "$($profiles/test/bin/foo)" = "foo-1.0" ]
[ "$("$profiles"/test/bin/foo)" = "foo-1.0" ]
nix-env --set-flag priority 1 foo-0.1
[ "$($profiles/test/bin/foo)" = "foo-0.1" ]
[ "$("$profiles"/test/bin/foo)" = "foo-0.1" ]
# Priorities can be overridden with the --priority flag
nix-env -e '*'
nix-env -i foo-1.0
[ "$($profiles/test/bin/foo)" = "foo-1.0" ]
[ "$("$profiles"/test/bin/foo)" = "foo-1.0" ]
nix-env -i --priority 1 foo-0.1
[ "$($profiles/test/bin/foo)" = "foo-0.1" ]
[ "$("$profiles"/test/bin/foo)" = "foo-0.1" ]
# Test nix-env --set.
nix-env --set $outPath10
[ "$(nix-store -q --resolve $profiles/test)" = $outPath10 ]
nix-env --set $drvPath10
[ "$(nix-store -q --resolve $profiles/test)" = $outPath10 ]
nix-env --set "$outPath10"
[ "$(nix-store -q --resolve "$profiles"/test)" = "$outPath10" ]
nix-env --set "$drvPath10"
[ "$(nix-store -q --resolve "$profiles"/test)" = "$outPath10" ]
# Test the case where $HOME contains a symlink.
mkdir -p $TEST_ROOT/real-home/alice/.nix-defexpr/channels
ln -sfn $TEST_ROOT/real-home $TEST_ROOT/home
ln -sfn $(pwd)/user-envs.nix $TEST_ROOT/home/alice/.nix-defexpr/channels/foo
mkdir -p "$TEST_ROOT"/real-home/alice/.nix-defexpr/channels
ln -sfn "$TEST_ROOT"/real-home "$TEST_ROOT"/home
ln -sfn "$(pwd)"/user-envs.nix "$TEST_ROOT"/home/alice/.nix-defexpr/channels/foo
HOME=$TEST_ROOT/home/alice nix-env -i foo-0.1