Merge pull request 'Fix(tests/templates): add narHash to allow local tests execution in devShell' (#3922) from fix-tests into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/3922
This commit is contained in:
hsjobeki
2025-06-10 13:50:34 +00:00

View File

@@ -50,22 +50,37 @@ def substitute(
) -> None:
sops_key = str(flake.joinpath("sops.key"))
buf = ""
clan_core_replacement = None
if clan_core_flake:
clan_core_replacement = f"path://{clan_core_flake}"
if not nix_test_store():
_flake = Flake(str(clan_core_flake))
_flake.prefetch()
assert _flake.hash, "Clan core flake hash is empty"
assert _flake.store_path, "Clan core flake store path is empty"
clan_core_replacement = f"path://{_flake.store_path}?narHash={_flake.hash}"
with file.open() as f:
for line in f:
line = line.replace("__NIXPKGS__", str(nixpkgs_source()))
if clan_core_flake:
line = line.replace("__CLAN_CORE__", f"path:{clan_core_flake}")
if clan_core_replacement:
line = line.replace("__NIXPKGS__", str(nixpkgs_source()))
line = line.replace("__CLAN_CORE__", clan_core_replacement)
line = line.replace(
"git+https://git.clan.lol/clan/clan-core", f"path:{clan_core_flake}"
"git+https://git.clan.lol/clan/clan-core", clan_core_replacement
)
line = line.replace(
"https://git.clan.lol/clan/clan-core/archive/main.tar.gz",
f"path:{clan_core_flake}",
clan_core_replacement,
)
line = line.replace("__INVENTORY_EXPR__", str(inventory_expr))
line = line.replace("__CLAN_SOPS_KEY_PATH__", sops_key)
line = line.replace("__CLAN_SOPS_KEY_DIR__", str(flake / "facts"))
buf += line
print(f"file: {file}")
print(f"clan_core: {clan_core_flake}")
print(f"flake: {flake}")
@@ -122,10 +137,26 @@ class ClanFlake:
self._flake_template = flake_template
self.inventory = nested_dict()
self.machines = nested_dict()
clan_core_flake = Flake(str(CLAN_CORE))
clan_core_replacement = f"path://{clan_core_flake}"
# If the test doesnt define a nix_test_store, we assume it is running from a local pytest
# and we need to prefetch the clan core flake, this would fail in a nix build
# But is required for local testing
if not nix_test_store():
clan_core_flake.prefetch()
assert clan_core_flake.hash, "Clan core flake hash is empty"
assert clan_core_flake.store_path, "Clan core flake store path is empty"
clan_core_replacement = (
f"path://{clan_core_flake.store_path}?narHash={clan_core_flake.hash}"
)
self.substitutions: dict[str, str] = {
"git+https://git.clan.lol/clan/clan-core": "path://" + str(CLAN_CORE),
"https://git.clan.lol/clan/clan-core/archive/main.tar.gz": "path://"
+ str(CLAN_CORE),
"git+https://git.clan.lol/clan/clan-core": clan_core_replacement,
"https://git.clan.lol/clan/clan-core/archive/main.tar.gz": clan_core_replacement,
}
self.clan_modules: list[str] = []
self.temporary_home = temporary_home