Merge pull request #6194 from edolstra/nix-profile

Add basic tests for 'nix profile'
This commit is contained in:
Eelco Dolstra 2022-03-02 16:07:00 +01:00 committed by GitHub
commit b2da2a22c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 90 additions and 22 deletions

View file

@ -374,14 +374,14 @@ struct CmdProfileRemove : virtual EvalCommand, MixDefaultProfile, MixProfileElem
if (removedCount == 0) { if (removedCount == 0) {
for (auto matcher: matchers) { for (auto matcher: matchers) {
if (const size_t * index = std::get_if<size_t>(&matcher)){ if (const size_t * index = std::get_if<size_t>(&matcher)){
warn("'%d' is not a valid index in profile", *index); warn("'%d' is not a valid index", *index);
} else if (const Path * path = std::get_if<Path>(&matcher)){ } else if (const Path * path = std::get_if<Path>(&matcher)){
warn("'%s' does not match any paths in profile", *path); warn("'%s' does not match any paths", *path);
} else if (const RegexPattern * regex = std::get_if<RegexPattern>(&matcher)){ } else if (const RegexPattern * regex = std::get_if<RegexPattern>(&matcher)){
warn("'%s' does not match any packages in profile", regex->pattern); warn("'%s' does not match any packages", regex->pattern);
} }
} }
warn ("Try `nix profile list` to see the current profile."); warn ("Use 'nix profile list' to see the current profile.");
} }
updateProfile(newManifest.build(store)); updateProfile(newManifest.build(store));
} }
@ -455,11 +455,11 @@ struct CmdProfileUpgrade : virtual SourceExprCommand, MixDefaultProfile, MixProf
if (upgradedCount == 0) { if (upgradedCount == 0) {
for (auto & matcher : matchers) { for (auto & matcher : matchers) {
if (const size_t * index = std::get_if<size_t>(&matcher)){ if (const size_t * index = std::get_if<size_t>(&matcher)){
warn("'%d' is not a valid index in profile", *index); warn("'%d' is not a valid index", *index);
} else if (const Path * path = std::get_if<Path>(&matcher)){ } else if (const Path * path = std::get_if<Path>(&matcher)){
warn("'%s' does not match any paths in profile", *path); warn("'%s' does not match any paths", *path);
} else if (const RegexPattern * regex = std::get_if<RegexPattern>(&matcher)){ } else if (const RegexPattern * regex = std::get_if<RegexPattern>(&matcher)){
warn("'%s' does not match any packages in profile", regex->pattern); warn("'%s' does not match any packages", regex->pattern);
} }
} }
warn ("Use 'nix profile list' to see the current profile."); warn ("Use 'nix profile list' to see the current profile.");

9
tests/bash-profile.sh Normal file
View file

@ -0,0 +1,9 @@
source common.sh
sed -e "s|@localstatedir@|$TEST_ROOT/profile-var|g" -e "s|@coreutils@|$coreutils|g" < ../scripts/nix-profile.sh.in > $TEST_ROOT/nix-profile.sh
user=$(whoami)
rm -rf $TEST_HOME $TEST_ROOT/profile-var
mkdir -p $TEST_HOME
USER=$user $SHELL -e -c ". $TEST_ROOT/nix-profile.sh; set"
USER=$user $SHELL -e -c ". $TEST_ROOT/nix-profile.sh" # test idempotency

View file

@ -89,10 +89,11 @@ nix_tests = \
build.sh \ build.sh \
ca/nix-run.sh \ ca/nix-run.sh \
db-migration.sh \ db-migration.sh \
nix-profile.sh \ bash-profile.sh \
pass-as-file.sh \ pass-as-file.sh \
describe-stores.sh \ describe-stores.sh \
store-ping.sh store-ping.sh \
nix-profile.sh
ifeq ($(HAVE_LIBCPUID), 1) ifeq ($(HAVE_LIBCPUID), 1)
nix_tests += compute-levels.sh nix_tests += compute-levels.sh

View file

@ -1,9 +1,68 @@
source common.sh source common.sh
sed -e "s|@localstatedir@|$TEST_ROOT/profile-var|g" -e "s|@coreutils@|$coreutils|g" < ../scripts/nix-profile.sh.in > $TEST_ROOT/nix-profile.sh clearStore
clearProfiles
user=$(whoami) # Make a flake.
rm -rf $TEST_HOME $TEST_ROOT/profile-var flake1Dir=$TEST_ROOT/flake1
mkdir -p $TEST_HOME mkdir -p $flake1Dir
USER=$user $SHELL -e -c ". $TEST_ROOT/nix-profile.sh; set"
USER=$user $SHELL -e -c ". $TEST_ROOT/nix-profile.sh" # test idempotency cat > $flake1Dir/flake.nix <<EOF
{
description = "Bla bla";
outputs = { self }: with import ./config.nix; rec {
packages.$system.default = mkDerivation {
name = "simple-\${builtins.readFile ./version}";
builder = builtins.toFile "builder.sh"
''
mkdir -p \$out/bin
cat > \$out/bin/hello <<EOF
#! ${shell}
echo Hello \${builtins.readFile ./who}
EOF
chmod +x \$out/bin/hello
'';
};
};
}
EOF
printf World > $flake1Dir/who
printf 1.0 > $flake1Dir/version
cp ./config.nix $flake1Dir/
# Test upgrading from nix-env.
nix-env -f ./user-envs.nix -i foo-1.0
nix profile list | grep '0 - - .*-foo-1.0'
nix profile install $flake1Dir -L
[[ $($TEST_HOME/.nix-profile/bin/hello) = "Hello World" ]]
nix profile history
nix profile history | grep "packages.$system.default: ∅ -> 1.0"
nix profile diff-closures | grep 'env-manifest.nix: ε → ∅'
# Test upgrading a package.
printf NixOS > $flake1Dir/who
printf 2.0 > $flake1Dir/version
nix profile upgrade 1
[[ $($TEST_HOME/.nix-profile/bin/hello) = "Hello NixOS" ]]
nix profile history | grep "packages.$system.default: 1.0 -> 2.0"
# Test 'history', 'diff-closures'.
nix profile diff-closures
# Test rollback.
nix profile rollback
[[ $($TEST_HOME/.nix-profile/bin/hello) = "Hello World" ]]
# Test uninstall.
[ -e $TEST_HOME/.nix-profile/bin/foo ]
nix profile remove 0
(! [ -e $TEST_HOME/.nix-profile/bin/foo ])
nix profile history | grep 'foo: 1.0 -> ∅'
nix profile diff-closures | grep 'Version 3 -> 4'
# Test wipe-history.
nix profile wipe-history
[[ $(nix profile history | grep Version | wc -l) -eq 1 ]]

View file

@ -9,7 +9,6 @@ clearProfiles
# Query installed: should be empty. # Query installed: should be empty.
test "$(nix-env -p $profiles/test -q '*' | wc -l)" -eq 0 test "$(nix-env -p $profiles/test -q '*' | wc -l)" -eq 0
mkdir -p $TEST_HOME
nix-env --switch-profile $profiles/test nix-env --switch-profile $profiles/test
# Query available: should contain several. # Query available: should contain several.