testing: migrate flakes/check.sh

Change-Id: I4cb9c675ccfc1763e54ce80f8521300b608bb0eb
This commit is contained in:
eldritch horrors
2026-02-17 14:20:50 +01:00
parent c6931c4176
commit ffefebd9f4
3 changed files with 164 additions and 119 deletions
-118
View File
@@ -1,118 +0,0 @@
source common.sh
flakeDir=$TEST_ROOT/flake3
mkdir -p $flakeDir
cat > $flakeDir/flake.nix <<EOF
{
outputs = { self }: {
overlay = final: prev: {
};
};
}
EOF
nix flake check $flakeDir
cat > $flakeDir/flake.nix <<EOF
{
outputs = { self }: {
overlay = finalll: prev: {
};
};
}
EOF
(nix flake check $flakeDir)
cat > $flakeDir/flake.nix <<EOF
{
outputs = { self }: {
overlay = one: two: three: {
};
};
}
EOF
(! nix flake check $flakeDir)
cat > $flakeDir/flake.nix <<EOF
{
outputs = { self }: {
overlay = one: {
};
};
}
EOF
(! nix flake check $flakeDir)
cat > $flakeDir/flake.nix <<EOF
{
outputs = { self, ... }: {
overlays.x86_64-linux.foo = final: prev: {
};
};
}
EOF
checkRes=$(nix flake check $flakeDir 2>&1 && fail "nix flake check --all-systems should have failed" || true)
echo "$checkRes" | grepQuiet "error: overlay is not a function, but a set instead"
cat > $flakeDir/flake.nix <<EOF
{
outputs = { self }: {
nixosModules.foo = {
a.b.c = 123;
foo = true;
};
};
}
EOF
nix flake check $flakeDir
cat > $flakeDir/flake.nix <<EOF
{
outputs = { self }: {
nixosModules.foo = assert false; {
a.b.c = 123;
foo = true;
};
};
}
EOF
(! nix flake check $flakeDir)
cat > $flakeDir/flake.nix <<EOF
{
outputs = { self }: {
nixosModule = { config, pkgs, ... }: {
a.b.c = 123;
};
};
}
EOF
nix flake check $flakeDir
cat > $flakeDir/flake.nix <<EOF
{
outputs = { self }: {
packages.system-1.default = "foo";
packages.system-2.default = "bar";
};
}
EOF
nix flake check $flakeDir
# --eval-system should be considered for which the local system is for flake
# purposes, and thus it should fail checking that attr
(! nix flake check --eval-system system-1 $flakeDir)
# likewise with --system
(! nix flake check --system system-1 $flakeDir)
checkRes=$(nix flake check --all-systems --keep-going $flakeDir 2>&1 && fail "nix flake check --all-systems should have failed" || true)
echo "$checkRes" | grepQuiet "packages.system-1.default"
echo "$checkRes" | grepQuiet "packages.system-2.default"
-1
View File
@@ -40,7 +40,6 @@ functional_tests_scripts = [
'flakes/inputs.sh',
'flakes/follow-paths.sh',
'flakes/bundle.sh',
'flakes/check.sh',
'flakes/unlocked-override.sh',
'flakes/absolute-paths.sh',
'flakes/build-paths.sh',
+164
View File
@@ -0,0 +1,164 @@
from testlib.fixtures.nix import Nix
from testlib.fixtures.file_helper import FileDeclaration, with_files
from testlib.utils import File
from pathlib import Path
import pytest
@pytest.fixture(autouse=True)
def common_init(nix: Nix):
nix.settings.add_xp_feature("nix-command", "flakes")
def make_flake(text: str) -> FileDeclaration:
return {"flake.nix": File(text)}
@pytest.mark.parametrize(
"files",
[
make_flake("""{
outputs = { self }: {
overlay = final: prev: {};
};
}"""),
make_flake("""{
outputs = { self }: {
overlay = finall: prev: {};
};
}"""),
],
indirect=True,
)
def test_check_overlay_args_good(nix: Nix, files: Path):
nix.nix(["flake", "check", str(files)]).run().ok()
@with_files(
make_flake("""{
outputs = { self }: {
overlay = one: two: three: {};
};
}""")
)
def test_check_overlay_too_many_args(nix: Nix, files: Path):
assert (
"error: overlay is not a function with two arguments, but takes more than two"
in nix.nix(["flake", "check", str(files)]).run().expect(1).stderr_s
)
@with_files(
make_flake("""{
outputs = { self }: {
overlay = one: {};
};
}""")
)
def test_check_overlay_not_enough_args(nix: Nix, files: Path):
assert (
"error: overlay is not a function with two arguments, but only takes one"
in nix.nix(["flake", "check", str(files)]).run().expect(1).stderr_s
)
@with_files(
make_flake("""{
outputs = { self, ... }: {
overlays.x86_64-linux.foo = final: prev: {};
};
}""")
)
def test_check_overlay_not_a_function(nix: Nix, files: Path):
assert (
"error: overlay is not a function, but a set instead"
in nix.nix(["flake", "check", str(files)]).run().expect(1).stderr_s
)
@pytest.mark.parametrize(
"files",
[
make_flake("""{
outputs = { self }: {
nixosModules.foo = {
a.b.c = 123;
foo = true;
};
};
}"""),
make_flake("""{
outputs = { self }: {
nixosModule = { config, pkgs, ... }: {
a.b.c = 123;
};
};
}"""),
],
indirect=True,
)
def test_check_nixos_modules_good(nix: Nix, files: Path):
nix.nix(["flake", "check", str(files)]).run().ok()
@with_files(
make_flake("""{
outputs = { self }: {
nixosModules.foo = assert false; {
a.b.c = 123;
foo = true;
};
};
}""")
)
def test_check_nixos_modules_eval_fail(nix: Nix, files: Path):
assert "assertion failed" in nix.nix(["flake", "check", str(files)]).run().expect(1).stderr_s
@with_files(
make_flake("""{
outputs = { self }: {
packages.system-1.default = "foo";
packages.system-2.default = "bar";
};
}""")
)
class TestMultipleSystems:
def test_check(self, nix: Nix, files: Path):
nix.nix(["flake", "check", str(files)]).run().ok()
def test_check_with_eval_system(self, nix: Nix, files: Path):
"""
--eval-system should be considered for which the local system is for flake
purposes, and thus it should fail checking that attr
"""
assert (
"'packages.system-1.default' is not a derivation"
in nix.nix(["flake", "check", "--eval-system", "system-1", str(files)])
.run()
.expect(1)
.stderr_s
)
def test_check_with_system(self, nix: Nix, files: Path):
"""
--system should be considered for which the local system is for flake
purposes, and thus it should fail checking that attr
"""
assert (
"'packages.system-1.default' is not a derivation"
in nix.nix(["flake", "check", "--system", "system-1", str(files)])
.run()
.expect(1)
.stderr_s
)
def test_check_all_systems(self, nix: Nix, files: Path):
result = (
nix.nix(["flake", "check", "--all-systems", "--keep-going", str(files)])
.run()
.expect(1)
.stderr_s
)
assert "'packages.system-1.default' is not a derivation" in result
assert "'packages.system-2.default' is not a derivation" in result