clan-cli: Fix failing tests. Add a big FIXME for clan.select

This commit is contained in:
Qubasa
2025-04-11 23:36:02 +02:00
parent bfba638b1e
commit af776f1efa
2 changed files with 22 additions and 89 deletions

View File

@@ -27,15 +27,6 @@ class InputVariant:
TemplateName = NewType("TemplateName", str) TemplateName = NewType("TemplateName", str)
TemplateType = Literal["clan", "disko", "machine"] TemplateType = Literal["clan", "disko", "machine"]
ModuleName = NewType("ModuleName", str)
class ClanModule(TypedDict):
description: str
class InternalClanModule(ClanModule):
path: str
class Template(TypedDict): class Template(TypedDict):
@@ -61,7 +52,6 @@ class TemplateTypeDict(TypedDict):
class ClanAttrset(TypedDict): class ClanAttrset(TypedDict):
templates: TemplateTypeDict templates: TemplateTypeDict
modules: dict[ModuleName, ClanModule]
class ClanExports(TypedDict): class ClanExports(TypedDict):
@@ -85,9 +75,6 @@ def apply_fallback_structure(attrset: dict[str, Any]) -> ClanAttrset:
"machine": templates.get("machine", {}), "machine": templates.get("machine", {}),
} }
# Ensure modules field exists
result["modules"] = attrset.get("modules", {})
return cast(ClanAttrset, result) return cast(ClanAttrset, result)
@@ -109,15 +96,32 @@ def get_clan_nix_attrset(clan_dir: Flake | None = None) -> ClanExports:
raw_clan_exports: dict[str, Any] = {"self": {"clan": {}}, "inputs": {"clan": {}}} raw_clan_exports: dict[str, Any] = {"self": {"clan": {}}, "inputs": {"clan": {}}}
try: try:
raw_clan_exports["self"] = clan_dir.select("clan") raw_clan_exports["self"] = clan_dir.select("clan.{templates}")
except ClanCmdError: except ClanCmdError as ex:
log.debug(ex)
log.info("Current flake does not export the 'clan' attribute") log.info("Current flake does not export the 'clan' attribute")
# FIXME: flake.select destroys lazy evaluation # FIXME: flake.select destroys lazy evaluation
# this is why if one input has a template with a non existant path # this is why if one input has a template with a non existant path
# the whole evaluation will fail # the whole evaluation will fail
try: try:
raw_clan_exports["inputs"] = clan_dir.select("inputs.*.{clan}") # FIXME: We expect here that if the input exports the clan attribute it also has clan.templates
# this is not always the case if we just want to export clan.modules for example
# However, there is no way to fix this, as clan.select does not support two optional selectors
# and we cannot eval the clan attribute as clan.modules can be non JSON serializable because
# of import statements.
# This needs to be fixed in clan.select
# For now always define clan.templates or no clan attribute at all
temp = clan_dir.select("inputs.*.{clan}.templates")
# FIXME: We need this because clan.select removes the templates attribute
# but not the clan and other attributes leading up to templates
for input_name, attrset in temp.items():
if "clan" in attrset:
raw_clan_exports["inputs"][input_name] = {
"clan": {"templates": {**attrset["clan"]}}
}
except ClanCmdError as e: except ClanCmdError as e:
msg = "Failed to evaluate flake inputs" msg = "Failed to evaluate flake inputs"
raise ClanError(msg) from e raise ClanError(msg) from e

View File

@@ -135,7 +135,7 @@ def test_clan_get_nix_attrset_case_1(
injected = {"templates": {"disko": {}, "machine": {}}} injected = {"templates": {"disko": {}, "machine": {}}}
expected = { expected = {
"inputs": {}, "inputs": {},
"self": {"templates": {"disko": {}, "machine": {}, "clan": {}}, "modules": {}}, "self": {"templates": {"disko": {}, "machine": {}, "clan": {}}},
} }
nix_attr_tester(test_flake_with_core, injected, expected, test_number) nix_attr_tester(test_flake_with_core, injected, expected, test_number)
@@ -171,7 +171,6 @@ def test_clan_get_nix_attrset_case_2(
"disko": {}, "disko": {},
"machine": {}, "machine": {},
}, },
"modules": {},
}, },
} }
@@ -235,76 +234,6 @@ def test_clan_get_nix_attrset_case_3(
} }
}, },
}, },
"modules": {},
},
}
nix_attr_tester(test_flake_with_core, injected, expected, test_number)
# Test Case 4: Input with modules only
@pytest.mark.with_core
def test_clan_get_nix_attrset_case_4(
monkeypatch: pytest.MonkeyPatch,
temporary_home: Path,
test_flake_with_core: FlakeForTest,
) -> None:
test_number = 4
injected = {
"modules": {
"module1": {"description": "First module", "path": "/module1/path"},
"module2": {"description": "Second module", "path": "/module2/path"},
}
}
expected = {
"inputs": {},
"self": {
"modules": {
"module1": {"description": "First module", "path": "/module1/path"},
"module2": {"description": "Second module", "path": "/module2/path"},
},
"templates": {"disko": {}, "machine": {}, "clan": {}},
},
}
nix_attr_tester(test_flake_with_core, injected, expected, test_number)
# Test Case 5: Input with both templates and modules
@pytest.mark.with_core
def test_clan_get_nix_attrset_case_5(
monkeypatch: pytest.MonkeyPatch,
temporary_home: Path,
test_flake_with_core: FlakeForTest,
) -> None:
test_number = 5
injected = {
"templates": {
"clan": {
"clan_template": {
"description": "A clan template.",
"path": "/clan/path",
}
}
},
"modules": {
"module1": {"description": "First module", "path": "/module1/path"}
},
}
expected = {
"inputs": {},
"self": {
"modules": {
"module1": {"description": "First module", "path": "/module1/path"}
},
"templates": {
"clan": {
"clan_template": {
"description": "A clan template.",
"path": "/clan/path",
}
},
"disko": {},
"machine": {},
},
}, },
} }
nix_attr_tester(test_flake_with_core, injected, expected, test_number) nix_attr_tester(test_flake_with_core, injected, expected, test_number)
@@ -321,6 +250,6 @@ def test_clan_get_nix_attrset_case_6(
injected = {} injected = {}
expected = { expected = {
"inputs": {}, "inputs": {},
"self": {"templates": {"disko": {}, "machine": {}, "clan": {}}, "modules": {}}, "self": {"templates": {"disko": {}, "machine": {}, "clan": {}}},
} }
nix_attr_tester(test_flake_with_core, injected, expected, test_number) nix_attr_tester(test_flake_with_core, injected, expected, test_number)