diff --git a/tests/functional/flakes/show.sh b/tests/functional/flakes/show.sh index 53e849a40..d08f74066 100644 --- a/tests/functional/flakes/show.sh +++ b/tests/functional/flakes/show.sh @@ -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 < 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 < 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< FileDeclaration: + return get_global_asset_pack("simple-drv") | { + "flake.nix": CopyTemplate("assets/simple-flake.nix", {"system": environ.get("system")}) + } diff --git a/tests/functional2/flakes/test_show.py b/tests/functional2/flakes/test_show.py new file mode 100644 index 000000000..4ada5867c --- /dev/null +++ b/tests/functional2/flakes/test_show.py @@ -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"