clan_lib select: fix maybe select storing miss as {}

This commit is contained in:
lassulus
2025-10-24 12:46:48 +02:00
parent b7013dc795
commit 7294d8bcbe
2 changed files with 26 additions and 1 deletions

View File

@@ -594,6 +594,10 @@ class FlakeCacheEntry:
if not isinstance(selector.value, str):
msg = f"Expected str for STR selector value in select, got {type(selector.value)}"
raise ClanError(msg)
# If the entry was previously marked as non-existent by a MAYBE selector,
# we should raise KeyError for STR selectors
if selector.value in self.value and not self.value[selector.value].exists:
raise KeyError(selector.value)
return self.value[selector.value].select(selectors[1:])
# if we are a MAYBE selector, we check if the key exists in the dict
@@ -1130,7 +1134,15 @@ class Flake:
else:
log.debug(f"$ clan select {shlex.quote(selector)}")
return self._cache.select(selector)
try:
return self._cache.select(selector)
except KeyError as e:
# Convert KeyError to ClanSelectError for consistency
raise ClanSelectError(
flake_identifier=self.identifier,
selectors=[selector],
description=f"Attribute '{e.args[0]}' not found in flake",
) from e
def machine_selector(self, machine_name: str, selector: str) -> str:
"""Create a selector for a specific machine.

View File

@@ -7,6 +7,7 @@ import pytest
from clan_cli.tests.fixtures_flakes import ClanFlake, create_test_machine_config
from clan_lib.flake.flake import (
ClanSelectError,
Flake,
FlakeCache,
FlakeCacheEntry,
@@ -539,3 +540,15 @@ def test_reinserting_store_path_value() -> None:
assert isinstance(cache.value, dict)
assert "outPath" in cache.value
assert cache.value["outPath"].value == pure_path
@pytest.mark.with_core
def test_get_nonexistant_after_maybe(flake: ClanFlake) -> None:
"""Test that if a nonexistant value is cached,
that the cache miss is not falsely stored as the value.
"""
my_flake = Flake(str(flake.path))
my_flake.select("?nonExist")
with pytest.raises(ClanSelectError):
my_flake.select("nonExist")