diff --git a/tests/functional/flakes/flakes.sh b/tests/functional/flakes/flakes.sh index 2faf07209..e2e3a4e26 100644 --- a/tests/functional/flakes/flakes.sh +++ b/tests/functional/flakes/flakes.sh @@ -85,20 +85,6 @@ hash2=$(nix flake metadata flake1 --json --refresh | jq -r .revision) nix build -o $flake1Dir/result git+file://$flake1Dir nix path-info $flake1Dir/result -# 'getFlake' on an unlocked flakeref should fail in pure mode, but -# succeed in impure mode. -(! nix build -o $TEST_ROOT/result --expr "(builtins.getFlake \"$flake1Dir\").packages.$system.default") -nix build -o $TEST_ROOT/result --expr "(builtins.getFlake \"$flake1Dir\").packages.$system.default" --impure - -# 'getFlake' on a locked flakeref should succeed even in pure mode. -nix build -o $TEST_ROOT/result --expr "(builtins.getFlake \"git+file://$flake1Dir?rev=$hash2\").packages.$system.default" - -# Building a flake with an unlocked dependency should fail in pure mode. -(! nix eval --expr "builtins.getFlake \"$flake2Dir\"") - -# But should succeed in impure mode. -nix eval --expr "builtins.getFlake \"$flake2Dir\"" --impure - # set up lockfiles for later tests nix build -o $TEST_ROOT/result $flake2Dir#bar --commit-lock-file nix build -o $TEST_ROOT/result $flake3Dir#xyzzy diff --git a/tests/functional2/flakes/test_cli.py b/tests/functional2/flakes/test_cli.py index a9bb95c28..a07e12bea 100644 --- a/tests/functional2/flakes/test_cli.py +++ b/tests/functional2/flakes/test_cli.py @@ -173,6 +173,34 @@ def test_eval_system_takes_effect(nix: Nix, flake1: Path): nix.nix(["build", "--eval-system", "kitty-kitty", flake1]).run().ok() +class TestGetFlake: + def test_unlocked_pure_fails(self, nix: Nix, flake1: Path): + expr = f'(builtins.getFlake "{flake1}").packages.{system}.default' + result = nix.nix(["build", "--expr", expr]).run().expect(1) + assert "error: cannot call 'getFlake' on unlocked flake reference" in result.stderr_s + + def test_unlocked_impure_works(self, nix: Nix, flake1: Path): + expr = f'(builtins.getFlake "{flake1}").packages.{system}.default' + nix.nix(["build", "--impure", "--expr", expr]).run().ok() + assert (Path(nix.env.dirs.home / "result/hello")).exists() + + def test_locked_url_pure_works(self, nix: Nix, flake1: Path, git: Git): + flake1_original_commit = git(flake1, "rev-parse", "HEAD").stdout_s.strip() + expr = f'(builtins.getFlake "git+file://{flake1}?rev={flake1_original_commit}").packages.{system}.default' + nix.nix(["build", "--expr", expr]).run().ok() + assert (Path(nix.env.dirs.home / "result/hello")).exists() + + def test_unlocked_dep_pure_fails(self, nix: Nix, flake2: Path): + # NOTE: this is the same test as test_unlocked_dep_pure_fails because flake2 is not locked, + # and flake2 *cannot* be locked because that would also lock flake1 in the flake2 lock file + nix.nix(["eval", "--expr", f'(builtins.getFlake "{flake2}")']).run().expect(1) + + @pytest.mark.usefixtures("registry") + def test_unlocked_dep_impure_works(self, nix: Nix, flake2: Path): + # NOTE: see test above, same thing happens here + nix.nix(["eval", "--impure", "--expr", f'(builtins.getFlake "{flake2}")']).run().ok() + + @pytest.mark.usefixtures("registry") class TestRegistry: def test_registry_list(self, nix: Nix):