From 65778cb9fe927c5b53935461162d6f3ea705d38d Mon Sep 17 00:00:00 2001 From: a-kenji Date: Wed, 9 Jul 2025 10:50:30 +0200 Subject: [PATCH] pkgs/clan: Fix `state list` and add regression tests Fix the `clan state list` subcommands, it now correctly propagates the flake argument. Also adds regression tests. --- pkgs/clan-cli/clan_cli/state/list.py | 32 ++++++++++++++--------- pkgs/clan-cli/clan_cli/state/list_test.py | 26 ++++++++++++++++++ 2 files changed, 46 insertions(+), 12 deletions(-) create mode 100644 pkgs/clan-cli/clan_cli/state/list_test.py diff --git a/pkgs/clan-cli/clan_cli/state/list.py b/pkgs/clan-cli/clan_cli/state/list.py index d1d29f123..387b64f2a 100644 --- a/pkgs/clan-cli/clan_cli/state/list.py +++ b/pkgs/clan-cli/clan_cli/state/list.py @@ -1,11 +1,11 @@ import argparse import json import logging -from pathlib import Path from clan_lib.cmd import RunOpts, run from clan_lib.dirs import get_clan_flake_toplevel_or_env from clan_lib.errors import ClanCmdError, ClanError +from clan_lib.flake import Flake from clan_lib.machines.machines import Machine from clan_lib.nix import nix_eval @@ -19,11 +19,8 @@ log = logging.getLogger(__name__) def list_state_folders(machine: Machine, service: None | str = None) -> None: - uri = "TODO" - if (clan_dir_result := get_clan_flake_toplevel_or_env()) is not None: - flake = clan_dir_result - else: - flake = Path() + # Use the flake from the machine object (which comes from CLI --flake argument) + flake = machine.flake.path cmd = nix_eval( [ f"{flake}#nixosConfigurations.{machine.name}.config.clan.core.state", @@ -36,11 +33,11 @@ def list_state_folders(machine: Machine, service: None | str = None) -> None: proc = run(cmd, RunOpts(prefix=machine.name)) res = proc.stdout.strip() except ClanCmdError as e: - msg = "Clan might not have meta attributes" + msg = "Failed to evaluate machine state configuration" raise ClanError( msg, - location=f"show_clan {uri}", - description="Evaluation failed on clanInternals.meta attribute", + location=f"clan state list {machine.name}", + description="Evaluation failed on clan.core.state attribute", ) from e state = json.loads(res) @@ -87,9 +84,20 @@ def list_state_folders(machine: Machine, service: None | str = None) -> None: def list_command(args: argparse.Namespace) -> None: - list_state_folders( - Machine(name=args.machine, flake=args.flake), service=args.service - ) + if args.flake: + flake = args.flake + else: + tmp = get_clan_flake_toplevel_or_env() + flake = Flake(str(tmp)) if tmp else None + + if not flake: + msg = "No clan found." + description = ( + "Run this command in a clan directory or specify the --flake option" + ) + raise ClanError(msg, description=description) + + list_state_folders(Machine(name=args.machine, flake=flake), service=args.service) def register_state_parser(parser: argparse.ArgumentParser) -> None: diff --git a/pkgs/clan-cli/clan_cli/state/list_test.py b/pkgs/clan-cli/clan_cli/state/list_test.py new file mode 100644 index 000000000..c8576fe4a --- /dev/null +++ b/pkgs/clan-cli/clan_cli/state/list_test.py @@ -0,0 +1,26 @@ +import pytest + +from clan_cli.tests.fixtures_flakes import FlakeForTest +from clan_cli.tests.helpers import cli +from clan_cli.tests.stdout import CaptureOutput + + +@pytest.mark.with_core +def test_state_list_vm1( + test_flake_with_core: FlakeForTest, capture_output: CaptureOutput +) -> None: + with capture_output as output: + cli.run(["state", "list", "vm1", "--flake", str(test_flake_with_core.path)]) + assert output.out is not None + assert "service: zerotier" in output.out + assert "folders:" in output.out + assert "/var/lib/zerotier-one" in output.out + + +@pytest.mark.with_core +def test_state_list_vm2( + test_flake_with_core: FlakeForTest, capture_output: CaptureOutput +) -> None: + with capture_output as output: + cli.run(["state", "list", "vm2", "--flake", str(test_flake_with_core.path)]) + assert output.out is not None