1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-09 12:06:01 +01:00

Merge pull request #13370 from NixOS/unit-tests-home

Give unit tests access to a $HOME directory
This commit is contained in:
Jörg Thalheim 2025-07-17 15:56:26 +02:00 committed by GitHub
commit a4d03ba527
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 20 additions and 38 deletions

View file

@ -60,6 +60,7 @@ test(
env : { env : {
'_NIX_TEST_UNIT_DATA': meson.current_source_dir() / 'data', '_NIX_TEST_UNIT_DATA': meson.current_source_dir() / 'data',
'NIX_CONFIG': 'extra-experimental-features = flakes', 'NIX_CONFIG': 'extra-experimental-features = flakes',
'HOME': meson.current_build_dir() / 'test-home',
}, },
protocol : 'gtest', protocol : 'gtest',
) )

View file

@ -3,6 +3,7 @@
buildPackages, buildPackages,
stdenv, stdenv,
mkMesonExecutable, mkMesonExecutable,
writableTmpDirAsHomeHook,
nix-flake, nix-flake,
nix-flake-c, nix-flake-c,
@ -55,19 +56,14 @@ mkMesonExecutable (finalAttrs: {
runCommand "${finalAttrs.pname}-run" runCommand "${finalAttrs.pname}-run"
{ {
meta.broken = !stdenv.hostPlatform.emulatorAvailable buildPackages; meta.broken = !stdenv.hostPlatform.emulatorAvailable buildPackages;
buildInputs = [ writableTmpDirAsHomeHook ];
} }
( (''
lib.optionalString stdenv.hostPlatform.isWindows '' export _NIX_TEST_UNIT_DATA=${resolvePath ./data}
export HOME="$PWD/home-dir" export NIX_CONFIG="extra-experimental-features = flakes"
mkdir -p "$HOME" ${stdenv.hostPlatform.emulator buildPackages} ${lib.getExe finalAttrs.finalPackage}
'' touch $out
+ '' '');
export _NIX_TEST_UNIT_DATA=${resolvePath ./data}
export NIX_CONFIG="extra-experimental-features = flakes"
${stdenv.hostPlatform.emulator buildPackages} ${lib.getExe finalAttrs.finalPackage}
touch $out
''
);
}; };
}; };

View file

@ -100,6 +100,8 @@ test(
this_exe, this_exe,
env : { env : {
'_NIX_TEST_UNIT_DATA': meson.current_source_dir() / 'data', '_NIX_TEST_UNIT_DATA': meson.current_source_dir() / 'data',
'HOME': meson.current_build_dir() / 'test-home',
'NIX_REMOTE': meson.current_build_dir() / 'test-home' / 'store',
}, },
protocol : 'gtest', protocol : 'gtest',
) )

View file

@ -28,10 +28,6 @@ TEST_F(nix_api_store_test, nix_store_get_uri)
TEST_F(nix_api_util_context, nix_store_get_storedir_default) TEST_F(nix_api_util_context, nix_store_get_storedir_default)
{ {
if (nix::getEnv("HOME").value_or("") == "/homeless-shelter") {
// skipping test in sandbox because nix_store_open tries to create /nix/var/nix/profiles
GTEST_SKIP();
}
nix_libstore_init(ctx); nix_libstore_init(ctx);
Store * store = nix_store_open(ctx, nullptr, nullptr); Store * store = nix_store_open(ctx, nullptr, nullptr);
assert_ctx_ok(); assert_ctx_ok();
@ -141,10 +137,6 @@ TEST_F(nix_api_store_test, nix_store_real_path)
TEST_F(nix_api_util_context, nix_store_real_path_relocated) TEST_F(nix_api_util_context, nix_store_real_path_relocated)
{ {
if (nix::getEnv("HOME").value_or("") == "/homeless-shelter") {
// Can't open default store from within sandbox
GTEST_SKIP();
}
auto tmp = nix::createTempDir(); auto tmp = nix::createTempDir();
std::string storeRoot = tmp + "/store"; std::string storeRoot = tmp + "/store";
std::string stateDir = tmp + "/state"; std::string stateDir = tmp + "/state";
@ -184,13 +176,7 @@ TEST_F(nix_api_util_context, nix_store_real_path_relocated)
TEST_F(nix_api_util_context, nix_store_real_path_binary_cache) TEST_F(nix_api_util_context, nix_store_real_path_binary_cache)
{ {
if (nix::getEnv("HOME").value_or("") == "/homeless-shelter") { Store * store = nix_store_open(ctx, nix::fmt("file://%s/binary-cache", nix::createTempDir()).c_str(), nullptr);
// TODO: override NIX_CACHE_HOME?
// skipping test in sandbox because narinfo cache can't be written
GTEST_SKIP();
}
Store * store = nix_store_open(ctx, "https://cache.nixos.org", nullptr);
assert_ctx_ok(); assert_ctx_ok();
ASSERT_NE(store, nullptr); ASSERT_NE(store, nullptr);

View file

@ -3,6 +3,7 @@
buildPackages, buildPackages,
stdenv, stdenv,
mkMesonExecutable, mkMesonExecutable,
writableTmpDirAsHomeHook,
nix-store, nix-store,
nix-store-c, nix-store-c,
@ -72,18 +73,14 @@ mkMesonExecutable (finalAttrs: {
runCommand "${finalAttrs.pname}-run" runCommand "${finalAttrs.pname}-run"
{ {
meta.broken = !stdenv.hostPlatform.emulatorAvailable buildPackages; meta.broken = !stdenv.hostPlatform.emulatorAvailable buildPackages;
buildInputs = [ writableTmpDirAsHomeHook ];
} }
( (''
lib.optionalString stdenv.hostPlatform.isWindows '' export _NIX_TEST_UNIT_DATA=${data + "/src/libstore-tests/data"}
export HOME="$PWD/home-dir" export NIX_REMOTE=$HOME/store
mkdir -p "$HOME" ${stdenv.hostPlatform.emulator buildPackages} ${lib.getExe finalAttrs.finalPackage}
'' touch $out
+ '' '');
export _NIX_TEST_UNIT_DATA=${data + "/src/libstore-tests/data"}
${stdenv.hostPlatform.emulator buildPackages} ${lib.getExe finalAttrs.finalPackage}
touch $out
''
);
}; };
}; };