testing: migrate non-pty bits of flakes/show.sh

Change-Id: I4884fd34fa485f9f91281dc3efd69dedd3aad3d9
This commit is contained in:
eldritch horrors
2026-02-17 16:00:59 +01:00
parent ffefebd9f4
commit 762a28a698
4 changed files with 114 additions and 86 deletions
-86
View File
@@ -7,92 +7,6 @@ writeSimpleFlake "$flakeDir"
cd "$flakeDir"
# FIXME(jade): the following is rather absurd. we have jq!
# By default: Only show the packages content for the current system and no
# legacyPackages at all
nix flake show --json > show-output.json
nix eval --impure --expr '
let show_output = builtins.fromJSON (builtins.readFile ./show-output.json);
in
assert show_output.packages.someOtherSystem.default == {};
assert show_output.packages.${builtins.currentSystem}.default.name == "simple";
assert show_output.legacyPackages.${builtins.currentSystem} == {};
true
'
# Follow --eval-system for determining the system for flakes
nix flake show --eval-system someOtherSystem --json > show-output.json
drvTitle=$(jq -r '.packages.someOtherSystem.default.name' show-output.json)
[[ $drvTitle == 'simple' ]]
# With `--all-systems`, show the packages for all systems
nix flake show --json --all-systems > show-output.json
nix eval --impure --expr '
let show_output = builtins.fromJSON (builtins.readFile ./show-output.json);
in
assert show_output.packages.someOtherSystem.default.name == "simple";
assert show_output.legacyPackages.${builtins.currentSystem} == {};
true
'
# With `--legacy`, show the legacy packages
nix flake show --json --legacy > show-output.json
nix eval --impure --expr '
let show_output = builtins.fromJSON (builtins.readFile ./show-output.json);
in
assert show_output.legacyPackages.${builtins.currentSystem}.hello.name == "simple";
true
'
# Test that attributes are only reported when they have actual content
cat >flake.nix <<EOF
{
description = "Bla bla";
outputs = inputs: rec {
apps.$system = { };
checks.$system = { };
devShells.$system = { };
legacyPackages.$system = { };
packages.$system = { };
packages.someOtherSystem = { };
formatter = { };
nixosConfigurations = { };
nixosModules = { };
};
}
EOF
nix flake show --json --all-systems > show-output.json
nix eval --impure --expr '
let show_output = builtins.fromJSON (builtins.readFile ./show-output.json);
in
assert show_output == { };
true
'
# Test that attributes with errors are handled correctly.
# nixpkgs.legacyPackages is a particularly prominent instance of this.
cat >flake.nix <<EOF
{
outputs = inputs: {
legacyPackages.$system = {
AAAAAASomeThingsFailToEvaluate = throw "nooo";
simple = import ./simple.nix;
};
};
}
EOF
nix flake show --json --legacy --all-systems > show-output.json
nix eval --impure --expr '
let show_output = builtins.fromJSON (builtins.readFile ./show-output.json);
in
assert show_output.legacyPackages.${builtins.currentSystem}.AAAAAASomeThingsFailToEvaluate == { };
assert show_output.legacyPackages.${builtins.currentSystem}.simple.name == "simple";
true
'
cat >flake.nix<<EOF
{
outputs = inputs: {
@@ -0,0 +1,17 @@
{
description = "Bla bla";
outputs = inputs: rec {
packages.@system@ = rec {
foo = import ./simple.nix;
default = foo;
};
packages.someOtherSystem = rec {
foo = import ./simple.nix;
default = foo;
};
# To test "nix flake init".
legacyPackages.@system@.hello = import ./simple.nix;
};
}
+8
View File
@@ -0,0 +1,8 @@
from testlib.utils import FileDeclaration, get_global_asset_pack, CopyTemplate
from testlib.environ import environ
def simple_flake() -> FileDeclaration:
return get_global_asset_pack("simple-drv") | {
"flake.nix": CopyTemplate("assets/simple-flake.nix", {"system": environ.get("system")})
}
+89
View File
@@ -0,0 +1,89 @@
from testlib.fixtures.nix import Nix
from testlib.fixtures.file_helper import with_files, File
from testlib.environ import environ
from pathlib import Path
from .common import simple_flake
import pytest
system = environ.get("system")
@pytest.fixture(autouse=True)
def common_init(nix: Nix):
nix.settings.add_xp_feature("nix-command", "flakes")
@with_files(simple_flake())
class TestFlakeShow:
def test_default_behavior(self, nix: Nix, files: Path):
"""
By default: Only show the packages content for the current system and no legacyPackages at all
"""
result = nix.nix(["flake", "show", "--json", files]).run().ok().json()
assert result["packages"]["someOtherSystem"]["default"] == {}
assert result["packages"][system]["default"]["name"] == "simple"
assert result["legacyPackages"][system] == {}
def test_eval_system_is_honored(self, nix: Nix, files: Path):
result = (
nix.nix(["flake", "show", "--eval-system", "someOtherSystem", "--json", files])
.run()
.ok()
.json()
)
assert result["packages"]["someOtherSystem"]["default"]["name"] == "simple"
def test_all_systems_shows_all(self, nix: Nix, files: Path):
result = nix.nix(["flake", "show", "--json", "--all-systems", files]).run().ok().json()
assert result["packages"]["someOtherSystem"]["default"]["name"] == "simple"
assert result["legacyPackages"][system] == {}
def test_legacy_flag_shows_legacy_packages(self, nix: Nix, files: Path):
result = nix.nix(["flake", "show", "--json", "--legacy", files]).run().ok().json()
assert result["legacyPackages"][system]["hello"]["name"] == "simple"
@with_files(
{
"flake.nix": File(f"""{{
description = "Bla bla";
outputs = inputs: rec {{
apps.{system} = {{ }};
checks.{system} = {{ }};
devShells.{system} = {{ }};
legacyPackages.{system} = {{ }};
packages.{system} = {{ }};
packages.someOtherSystem = {{ }};
formatter = {{ }};
nixosConfigurations = {{ }};
nixosModules = {{ }};
}};
}}""")
}
)
def test_show_without_contents(nix: Nix, files: Path):
assert nix.nix(["flake", "show", "--json", "--all-systems", files]).run().ok().json() == {}
@with_files(
simple_flake()
| {
"flake.nix": File(f"""{{
outputs = inputs: {{
legacyPackages.{system} = {{
# nixpkgs.legacyPackages is a particularly prominent source of eval errors
AAAAAASomeThingsFailToEvaluate = throw "nooo";
simple = import ./simple.nix;
}};
}};
}}""")
}
)
def test_show_handles_eval_errors(nix: Nix, files: Path):
result = (
nix.nix(["flake", "show", "--json", "--legacy", "--all-systems", files]).run().ok().json()
)
assert result["legacyPackages"][system]["AAAAAASomeThingsFailToEvaluate"] == {}
assert result["legacyPackages"][system]["simple"]["name"] == "simple"