pkgs/clan: Add machine validator with suggestion logic

Add machine validator with suggestion logic to:
- `clan machines update`
- `clan machines delete`
- `clan machines update-hardware-config`
This commit is contained in:
a-kenji
2025-06-26 16:57:51 +02:00
parent c079d6b65f
commit 3e70e30b6b
6 changed files with 213 additions and 0 deletions

View File

@@ -90,6 +90,57 @@ def test_machines_update_with_tags(
assert args.tags == ["vm"]
@pytest.mark.impure
def test_machines_update_nonexistent_machine(
test_flake_with_core: fixtures_flakes.FlakeForTest,
) -> None:
"""Test that update command gives helpful error messages for non-existent machines."""
from clan_lib.errors import ClanError
with pytest.raises(ClanError) as exc_info:
cli.run(
[
"machines",
"update",
"--flake",
str(test_flake_with_core.path),
"nonexistent-machine",
]
)
error_message = str(exc_info.value)
assert "nonexistent-machine" in error_message
assert "not found." in error_message
# Should suggest similar machines (vm1, vm2 exist in test flake)
assert "Did you mean:" in error_message or "Available machines:" in error_message
@pytest.mark.impure
def test_machines_update_typo_in_machine_name(
test_flake_with_core: fixtures_flakes.FlakeForTest,
) -> None:
"""Test that update command suggests similar machine names for typos."""
from clan_lib.errors import ClanError
with pytest.raises(ClanError) as exc_info:
cli.run(
[
"machines",
"update",
"--flake",
str(test_flake_with_core.path),
"v1", # typo of "vm1"
]
)
error_message = str(exc_info.value)
assert "v1" in error_message
assert "not found." in error_message
assert "Did you mean:" in error_message
# Should suggest vm1 as it's the closest match
assert "vm1" in error_message
@pytest.mark.with_core
def test_machine_delete(
monkeypatch: pytest.MonkeyPatch,