Merge pull request 'clan-cli: Don't eval 'modules' while getting templates. 'modules' can be not JSON serializable' (#3280) from Qubasa/clan-core:fix_templates into main
Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/3280
This commit is contained in:
@@ -27,15 +27,6 @@ class InputVariant:
|
||||
|
||||
TemplateName = NewType("TemplateName", str)
|
||||
TemplateType = Literal["clan", "disko", "machine"]
|
||||
ModuleName = NewType("ModuleName", str)
|
||||
|
||||
|
||||
class ClanModule(TypedDict):
|
||||
description: str
|
||||
|
||||
|
||||
class InternalClanModule(ClanModule):
|
||||
path: str
|
||||
|
||||
|
||||
class Template(TypedDict):
|
||||
@@ -61,7 +52,6 @@ class TemplateTypeDict(TypedDict):
|
||||
|
||||
class ClanAttrset(TypedDict):
|
||||
templates: TemplateTypeDict
|
||||
modules: dict[ModuleName, ClanModule]
|
||||
|
||||
|
||||
class ClanExports(TypedDict):
|
||||
@@ -85,9 +75,6 @@ def apply_fallback_structure(attrset: dict[str, Any]) -> ClanAttrset:
|
||||
"machine": templates.get("machine", {}),
|
||||
}
|
||||
|
||||
# Ensure modules field exists
|
||||
result["modules"] = attrset.get("modules", {})
|
||||
|
||||
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": {}}}
|
||||
|
||||
try:
|
||||
raw_clan_exports["self"] = clan_dir.select("clan")
|
||||
except ClanCmdError:
|
||||
raw_clan_exports["self"] = clan_dir.select("clan.{templates}")
|
||||
except ClanCmdError as ex:
|
||||
log.debug(ex)
|
||||
log.info("Current flake does not export the 'clan' attribute")
|
||||
|
||||
# FIXME: flake.select destroys lazy evaluation
|
||||
# this is why if one input has a template with a non existant path
|
||||
# the whole evaluation will fail
|
||||
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:
|
||||
msg = "Failed to evaluate flake inputs"
|
||||
raise ClanError(msg) from e
|
||||
|
||||
@@ -135,7 +135,7 @@ def test_clan_get_nix_attrset_case_1(
|
||||
injected = {"templates": {"disko": {}, "machine": {}}}
|
||||
expected = {
|
||||
"inputs": {},
|
||||
"self": {"templates": {"disko": {}, "machine": {}, "clan": {}}, "modules": {}},
|
||||
"self": {"templates": {"disko": {}, "machine": {}, "clan": {}}},
|
||||
}
|
||||
nix_attr_tester(test_flake_with_core, injected, expected, test_number)
|
||||
|
||||
@@ -171,7 +171,6 @@ def test_clan_get_nix_attrset_case_2(
|
||||
"disko": {},
|
||||
"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)
|
||||
@@ -321,6 +250,6 @@ def test_clan_get_nix_attrset_case_6(
|
||||
injected = {}
|
||||
expected = {
|
||||
"inputs": {},
|
||||
"self": {"templates": {"disko": {}, "machine": {}, "clan": {}}, "modules": {}},
|
||||
"self": {"templates": {"disko": {}, "machine": {}, "clan": {}}},
|
||||
}
|
||||
nix_attr_tester(test_flake_with_core, injected, expected, test_number)
|
||||
|
||||
Reference in New Issue
Block a user