testing: migrate flakes/flakes.sh (part 4)

metadata query functionality

Change-Id: I8f3866316994426664468edfbe154505ed2428af
This commit is contained in:
eldritch horrors
2026-02-25 12:47:08 +00:00
parent 126ef6321d
commit 4ef9fb3aee
2 changed files with 47 additions and 22 deletions
-22
View File
@@ -82,33 +82,14 @@ nix registry list | grepInverse '^user' # nothing in user registry
expectStderr 1 nix eval flake1#ERROR | grepQuiet "error:.*does not provide attribute.*or 'ERROR'$"
expectStderr 1 nix eval flake1#.ERROR | grepQuiet "error:.*does not provide attribute 'ERROR'$"
# Test 'nix flake metadata'.
nix flake metadata flake1
nix flake metadata flake1 | grepQuiet 'Locked URL:.*flake1.*'
# Test 'nix flake metadata' on a local flake.
(cd $flake1Dir && nix flake metadata) | grepQuiet 'URL:.*flake1.*'
(cd $flake1Dir && nix flake metadata .) | grepQuiet 'URL:.*flake1.*'
nix flake metadata $flake1Dir | grepQuiet 'URL:.*flake1.*'
# Test 'nix flake metadata --json'.
json=$(nix flake metadata flake1 --json | jq .)
[[ $(echo "$json" | jq -r .description) = 'Bla bla' ]]
[[ -d $(echo "$json" | jq -r .path) ]]
[[ $(echo "$json" | jq -r .lastModified) = $(git -C $flake1Dir log -n1 --format=%ct) ]]
hash1=$(echo "$json" | jq -r .revision)
echo foo > $flake1Dir/foo
git -C $flake1Dir add $flake1Dir/foo
[[ $(nix flake metadata flake1 --json --refresh | jq -r .dirtyRevision) == "$hash1-dirty" ]]
echo -n '# foo' >> $flake1Dir/flake.nix
flake1OriginalCommit=$(git -C $flake1Dir rev-parse HEAD)
git -C $flake1Dir commit -a -m 'Foo'
flake1NewCommit=$(git -C $flake1Dir rev-parse HEAD)
hash2=$(nix flake metadata flake1 --json --refresh | jq -r .revision)
[[ $(nix flake metadata flake1 --json --refresh | jq -r .dirtyRevision) == "null" ]]
[[ $hash1 != $hash2 ]]
# Test 'nix build' on a flake.
nix build -o $TEST_ROOT/result flake1#foo
@@ -522,6 +503,3 @@ nix flake lock "$flake3Dir" --override-input flake2/flake1 flake1/master/$hash1
nix flake update flake1 flake2/flake1 --flake "$flake3Dir"
[[ $(jq -r .nodes.flake1.locked.rev "$flake3Dir/flake.lock") =~ $hash2 ]]
[[ $(jq -r .nodes.flake1_2.locked.rev "$flake3Dir/flake.lock") =~ $hash2 ]]
# Test 'nix flake metadata --json'.
nix flake metadata $flake3Dir --json | jq .
+47
View File
@@ -1,5 +1,6 @@
from pathlib import Path
import pytest
import re
from testlib.fixtures.nix import Nix
from testlib.fixtures.env import ManagedEnv
@@ -68,6 +69,14 @@ def flake1(git: Git, env: ManagedEnv, request: pytest.FixtureRequest) -> Path:
return _make_flake_repo("flake1", flake1_files, git, env, request)
def _modify_flake1(flake1: Path, git: Git | None):
(flake1 / "foo").write_text("foo")
(flake1 / "flake.nix").write_text((flake1 / "flake.nix").read_text() + "# foo")
if git is not None:
git(flake1, "add", "foo")
git(flake1, "commit", "-a", "-m", "Foo")
@pytest.fixture
def flake2(git: Git, env: ManagedEnv, request: pytest.FixtureRequest) -> Path:
return _make_flake_repo("flake2", flake2_files, git, env, request)
@@ -99,6 +108,44 @@ def registry(nix: Nix, flake1: Path, flake2: Path, flake3: Path) -> Path:
return registry
@pytest.mark.usefixtures("registry")
class TestMetadata:
def test_registry(self, nix: Nix):
metadata = nix.nix(["flake", "metadata", "flake1"]).run().ok().stdout_s
assert re.search(r"Locked URL:.*flake1.*", metadata)
@pytest.mark.parametrize("source", [[], ["."]])
def test_cwd(self, nix: Nix, flake1: Path, source: list[str]):
metadata = nix.nix(["flake", "metadata", *source], cwd=flake1).run().ok().stdout_s
assert re.search(r"Locked URL:.*flake1.*", metadata)
def test_absolute(self, nix: Nix, flake1: Path):
metadata = nix.nix(["flake", "metadata", flake1]).run().ok().stdout_s
assert re.search(r"Locked URL:.*flake1.*", metadata)
@pytest.mark.parametrize("source", ["flake1", None])
def test_json(self, nix: Nix, flake1: Path, source: str | None, git: Git):
flake1_original_commit = git(flake1, "rev-parse", "HEAD").stdout_s.strip()
metadata = nix.nix(["flake", "metadata", source or flake1, "--json"]).run().json()
assert metadata["description"] == "Bla bla"
assert Path(metadata["path"]).is_dir()
assert metadata["lastModified"] == 23
assert metadata["revision"] == flake1_original_commit
def test_json_registry_dirty(self, nix: Nix, flake1: Path, git: Git):
flake1_original_commit = git(flake1, "rev-parse", "HEAD").stdout_s.strip()
(flake1 / "foo").write_text("foo")
git(flake1, "add", "foo")
metadata = nix.nix(["flake", "metadata", "flake1", "--json"]).run().json()
assert metadata["dirtyRevision"] == f"{flake1_original_commit}-dirty"
_modify_flake1(flake1, git)
flake1_modified_commit = git(flake1, "rev-parse", "HEAD").stdout_s.strip()
metadata = nix.nix(["flake", "metadata", "flake1", "--json", "--refresh"]).run().json()
assert metadata["revision"] == flake1_modified_commit
assert "dirtyRevision" not in metadata
class TestAddPath:
@with_files({"badFlake": {"flake.nix": File("INVALID")}})
def test_does_not_eval(self, nix: Nix, files: Path):