testing: migrate flakes/config.sh

Change-Id: I7f19b33c30330ccd1f01b72b2e36a542f03f4e49
This commit is contained in:
eldritch horrors
2026-02-17 16:01:00 +01:00
parent 762a28a698
commit 8500339d95
3 changed files with 64 additions and 48 deletions
-47
View File
@@ -1,47 +0,0 @@
source common.sh
cp ../simple.nix ../simple.builder.sh ../config.nix $TEST_HOME
cd $TEST_HOME
rm -f post-hook-ran
cat <<EOF > echoing-post-hook.sh
#!/bin/sh
echo "ThePostHookRan as \$0" > $PWD/post-hook-ran
EOF
chmod +x echoing-post-hook.sh
cat <<EOF > flake.nix
{
nixConfig.post-build-hook = ./echoing-post-hook.sh;
nixConfig.allow-dirty = false; # See #5621
outputs = a: {
packages.$system.default = import ./simple.nix;
};
}
EOF
# Without --accept-flake-config, the post hook should not run.
nix build < /dev/null
(! [[ -f post-hook-ran ]])
clearStore
# likewise with no-accept-flake-config
nix build --no-accept-flake-config
(! [[ -f post-hook-ran ]])
clearStore
nix build --accept-flake-config
test -f post-hook-ran || fail "The post hook should have ran"
# Make sure that the path to the post hook doesnt change if we change
# something in the flake.
# Otherwise the user would have to re-validate the setting each time.
mv post-hook-ran previous-post-hook-run
echo "# Dummy comment" >> flake.nix
clearStore
nix build --accept-flake-config
diff -q post-hook-ran previous-post-hook-run || \
fail "Both post hook runs should report the same filename"
-1
View File
@@ -118,7 +118,6 @@ functional_tests_scripts = [
'pre-hook.sh',
'post-hook.sh',
'function-trace.sh',
'flakes/config.sh',
'fmt.sh',
'eval-store.sh',
'derivation-json.sh',
+64
View File
@@ -0,0 +1,64 @@
from testlib.fixtures.nix import Nix
from testlib.fixtures.file_helper import with_files, File
from testlib.environ import environ
from testlib.utils import get_global_asset_pack
from pathlib import Path
from textwrap import dedent
import pytest
system = environ.get("system")
files = get_global_asset_pack("simple-drv") | {
"flake.nix": File(f"""
{{
nixConfig.post-build-hook = ./echoing-post-hook.sh;
nixConfig.allow-dirty = false; # See #5621
outputs = a: {{
packages.{system}.default = import ./simple.nix;
}};
}}
""")
}
@pytest.fixture(autouse=True)
def common_init(nix: Nix, files: Path):
nix.settings.add_xp_feature("nix-command", "flakes")
hook = files / "echoing-post-hook.sh"
hook.write_text(
dedent(f"""\
#!/bin/sh
echo "ThePostHookRan as $0" > {files}/post-hook-ran
""")
)
hook.chmod(0o755)
@with_files(files)
class TestFlakeConfig:
def test_post_hook_ignored_without_accept_config(self, nix: Nix, files: Path):
nix.nix(["build"]).run().ok()
assert not (files / "post-hook-ran").exists()
def test_post_hook_ignored_with_no_accept_config(self, nix: Nix, files: Path):
nix.nix(["build", "--no-accept-flake-config"]).run().ok()
assert not (files / "post-hook-ran").exists()
def test_post_hook_runs_with_accept_config(self, nix: Nix, files: Path):
nix.nix(["build", "--accept-flake-config"]).run().ok()
hook_output = files / "post-hook-ran"
assert hook_output.exists()
first_output = hook_output.read_text()
# Make sure that the path to the post hook doesn't change if we change
# something in the flake.
# Otherwise the user would have to re-validate the setting each time.
flake = files / "flake.nix"
flake.write_text(flake.read_text() + "# comment\n")
nix.clear_store()
nix.nix(["build", "--accept-flake-config"]).run().ok()
assert hook_output.read_text() == first_output