From 4deed5ab35b0326c93888bc2a8c230253170b11c Mon Sep 17 00:00:00 2001 From: eldritch horrors Date: Tue, 17 Feb 2026 23:09:18 +0100 Subject: [PATCH] testing: migrate flakes/build-paths.sh Change-Id: Ia7f15abd8f5444c69fef4da0d080a2933ac264d8 --- tests/functional/flakes/build-paths.sh | 96 ------------- tests/functional/meson.build | 1 - tests/functional2/flakes/test_build_paths.py | 136 +++++++++++++++++++ 3 files changed, 136 insertions(+), 97 deletions(-) delete mode 100644 tests/functional/flakes/build-paths.sh create mode 100644 tests/functional2/flakes/test_build_paths.py diff --git a/tests/functional/flakes/build-paths.sh b/tests/functional/flakes/build-paths.sh deleted file mode 100644 index ff012e1b3..000000000 --- a/tests/functional/flakes/build-paths.sh +++ /dev/null @@ -1,96 +0,0 @@ -source ./common.sh - -flake1Dir=$TEST_ROOT/flake1 -flake2Dir=$TEST_ROOT/flake2 - -mkdir -p $flake1Dir $flake2Dir - -writeSimpleFlake $flake2Dir -tar cfz $TEST_ROOT/flake.tar.gz -C $TEST_ROOT flake2 -hash=$(nix hash path $flake2Dir) - -dep=$(nix store add-path ./common.sh) - -cat > $flake1Dir/flake.nix < $flake1Dir/foo - -nix build --json --out-link $TEST_ROOT/result $flake1Dir#a1 -[[ -e $TEST_ROOT/result/simple.nix ]] - -nix build --json --out-link $TEST_ROOT/result $flake1Dir#a2 -[[ $(cat $TEST_ROOT/result) = bar ]] - -nix build --json --out-link $TEST_ROOT/result $flake1Dir#a3 - -nix build --json --out-link $TEST_ROOT/result $flake1Dir#a4 - -nix build --json --out-link $TEST_ROOT/result $flake1Dir#a6 -[[ -e $TEST_ROOT/result/simple.nix ]] - -nix build --impure --json --out-link $TEST_ROOT/result $flake1Dir#a8 -diff common.sh $TEST_ROOT/result - -expectStderr 1 nix build --impure --json --out-link $TEST_ROOT/result $flake1Dir#a9 \ - | grepQuiet "has 0 entries in its context. It should only have exactly one entry" - -nix build --json --out-link $TEST_ROOT/result $flake1Dir#a10 -[[ $(readlink -e $TEST_ROOT/result) = *simple.drv ]] - -expectStderr 1 nix build --json --out-link $TEST_ROOT/result $flake1Dir#a11 \ - | grepQuiet "has a context which refers to a complete source and binary closure" - -nix build --json --out-link $TEST_ROOT/result $flake1Dir#a12 -[[ -e $TEST_ROOT/result/hello ]] - -expectStderr 1 nix build --impure --json --out-link $TEST_ROOT/result $flake1Dir#a13 \ - | grepQuiet "has 2 entries in its context. It should only have exactly one entry" diff --git a/tests/functional/meson.build b/tests/functional/meson.build index 0077de1e6..7ce6a9a8c 100644 --- a/tests/functional/meson.build +++ b/tests/functional/meson.build @@ -37,7 +37,6 @@ functional_tests_scripts = [ 'flakes/develop-r8854.sh', 'flakes/mercurial.sh', 'flakes/follow-paths.sh', - 'flakes/build-paths.sh', 'flakes/flake-in-submodule.sh', 'flakes/flake-registry.sh', 'gc.sh', diff --git a/tests/functional2/flakes/test_build_paths.py b/tests/functional2/flakes/test_build_paths.py new file mode 100644 index 000000000..493bc63c0 --- /dev/null +++ b/tests/functional2/flakes/test_build_paths.py @@ -0,0 +1,136 @@ +from testlib.fixtures.nix import Nix +from testlib.fixtures.file_helper import with_files, File +from testlib.fixtures.command import CommandResult +from pathlib import Path +from .common import simple_flake +import tarfile +import pytest + +flake_nix = """ +{ + inputs.flake2.url = "file://$TEST_ROOT/flake.tar.gz"; + + outputs = { self, flake2 }: { + + a1 = builtins.fetchTarball { + #type = "tarball"; + url = "file://$TEST_ROOT/flake.tar.gz"; + sha256 = "$hash"; + }; + + a2 = ./foo; + + a3 = ./.; + + a4 = self.outPath; + + # FIXME + a5 = self; + + a6 = flake2.outPath; + + # FIXME + a7 = "${flake2}/config.nix"; + + # This is only allowed in impure mode. + a8 = builtins.storePath $dep; + + a9 = "$dep"; + + drvCall = with import ./config.nix; mkDerivation { + name = "simple"; + builder = ./simple.builder.sh; + PATH = ""; + goodPath = path; + }; + + a10 = builtins.unsafeDiscardOutputDependency self.drvCall.drvPath; + + a11 = self.drvCall.drvPath; + + a12 = self.drvCall.outPath; + + a13 = "${self.drvCall.drvPath}${self.drvCall.outPath}"; + }; +} +""" + +files = { + "flake1": simple_flake() | {"foo": File("bar")}, + "flake2": simple_flake(), + "common.sh": File("nothing important"), +} + + +@with_files(files) +class TestBuild: + @pytest.fixture(autouse=True) + def common_init(self, nix: Nix, files: Path): + self.nix = nix + self.out = nix.env.dirs.home / "result" + + nix.settings.add_xp_feature("nix-command", "flakes") + + with tarfile.open(files / "flake.tar.gz", "w:gz") as tar: + + def drop_prefix(ti: tarfile.TarInfo) -> tarfile.TarInfo: + ti.name = ti.name.replace(str(files)[1:] + "/", "") + return ti + + tar.add(files / "flake2", filter=drop_prefix) + + flake_hash = nix.hash_path(files / "flake2") + dep = nix.nix(["store", "add-path", files / "common.sh"]).run().ok().stdout_s.strip() + + flake = files / "flake1/flake.nix" + flake.write_text( + flake_nix.replace("$TEST_ROOT", str(files)) + .replace("$hash", flake_hash) + .replace("$dep", dep) + ) + + def build(self, *args) -> CommandResult: + return self.nix.nix(["build", "--json", "--out-link", self.out, *args]).run() + + @pytest.mark.parametrize("attr", ["a1", "a6"]) + def test_build_simple_nix(self, files: Path, attr: str): + self.build(f"{files}/flake1#{attr}").ok() + assert (self.out / "simple.nix").exists() + + def test_build_a2(self, files: Path): + self.build(f"{files}/flake1#a2").ok() + assert self.out.read_text() == "bar" + + @pytest.mark.parametrize("attr", ["a3", "a4"]) + def test_build_plain(self, files: Path, attr: str): + self.build(f"{files}/flake1#{attr}").ok() + + def test_build_a8(self, files: Path): + self.build("--impure", f"{files}/flake1#a8").ok() + assert self.out.read_text() == "nothing important" + + def test_build_a9(self, files: Path): + result = self.build(f"{files}/flake1#a9").expect(1) + assert ( + "has 0 entries in its context. It should only have exactly one entry" in result.stderr_s + ) + + def test_build_a10(self, files: Path): + self.build(f"{files}/flake1#a10").ok() + assert str(self.out.readlink()).endswith("simple.drv") + + def test_build_a11(self, files: Path): + result = self.build(f"{files}/flake1#a11").expect(1) + assert ( + "has a context which refers to a complete source and binary closure" in result.stderr_s + ) + + def test_build_a12(self, files: Path): + self.build(f"{files}/flake1#a12").ok() + assert (self.out / "hello").exists() + + def test_build_a13(self, files: Path): + result = self.build("--impure", f"{files}/flake1#a13").expect(1) + assert ( + "has 2 entries in its context. It should only have exactly one entry" in result.stderr_s + )