From 8500339d95c054fb1ec0bbb23d5a4c4d3477eeeb Mon Sep 17 00:00:00 2001 From: eldritch horrors Date: Fri, 13 Feb 2026 16:42:39 +0100 Subject: [PATCH] testing: migrate flakes/config.sh Change-Id: I7f19b33c30330ccd1f01b72b2e36a542f03f4e49 --- tests/functional/flakes/config.sh | 47 ------------------ tests/functional/meson.build | 1 - tests/functional2/flakes/test_config.py | 64 +++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 48 deletions(-) delete mode 100644 tests/functional/flakes/config.sh create mode 100644 tests/functional2/flakes/test_config.py diff --git a/tests/functional/flakes/config.sh b/tests/functional/flakes/config.sh deleted file mode 100644 index f7eae06d8..000000000 --- a/tests/functional/flakes/config.sh +++ /dev/null @@ -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 < echoing-post-hook.sh -#!/bin/sh - -echo "ThePostHookRan as \$0" > $PWD/post-hook-ran -EOF -chmod +x echoing-post-hook.sh - -cat < 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 doesn’t 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" diff --git a/tests/functional/meson.build b/tests/functional/meson.build index aa405a9e2..aefa18ac7 100644 --- a/tests/functional/meson.build +++ b/tests/functional/meson.build @@ -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', diff --git a/tests/functional2/flakes/test_config.py b/tests/functional2/flakes/test_config.py new file mode 100644 index 000000000..386057684 --- /dev/null +++ b/tests/functional2/flakes/test_config.py @@ -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