Merge pull request 'Tests(inventoryStore): add fixture, make extensible' (#3782) from flake-models into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/3782
This commit is contained in:
hsjobeki
2025-05-28 13:12:19 +00:00

View File

@@ -4,7 +4,6 @@ import os
import shutil
import subprocess
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Any
import pytest
@@ -72,26 +71,36 @@ class MockFlake:
folder_path = Path(__file__).parent.resolve()
@pytest.mark.with_core
def test_simple_read_write() -> None:
entry_file = "1.nix"
@pytest.fixture
def setup_test_files(tmp_path: Path, request: pytest.FixtureRequest) -> Path:
entry_file = request.param # e.g., "1.nix"
inventory_file = entry_file.replace(".nix", ".json")
nix_file = folder_path / f"fixtures/{entry_file}"
json_file = folder_path / f"fixtures/{inventory_file}"
with TemporaryDirectory() as tmp:
shutil.copyfile(
str(nix_file),
str(Path(tmp) / entry_file),
)
shutil.copyfile(
str(json_file),
str(Path(tmp) / inventory_file),
)
nix_src = folder_path / "fixtures" / entry_file
json_src = folder_path / "fixtures" / inventory_file
nix_dst = tmp_path / entry_file
json_dst = tmp_path / inventory_file
shutil.copyfile(nix_src, nix_dst)
shutil.copyfile(json_src, json_dst)
return tmp_path
@pytest.mark.with_core
@pytest.mark.parametrize("setup_test_files", ["1.nix"], indirect=True)
def test_simple_read_write(setup_test_files: Path) -> None:
files = list(setup_test_files.iterdir())
nix_file = next(f for f in files if f.suffix == ".nix")
json_file = next(f for f in files if f.suffix == ".json")
assert nix_file.exists()
assert json_file.exists()
store = InventoryStore(
flake=MockFlake(Path(tmp) / entry_file),
inventory_file_name=inventory_file,
flake=MockFlake(nix_file),
inventory_file_name=json_file.name,
_keys=[], # disable toplevel filtering
)
store._flake.invalidate_cache()
@@ -123,25 +132,18 @@ def test_simple_read_write() -> None:
@pytest.mark.with_core
def test_read_deferred() -> None:
entry_file = "deferred.nix"
inventory_file = entry_file.replace(".nix", ".json")
@pytest.mark.parametrize("setup_test_files", ["deferred.nix"], indirect=True)
def test_read_deferred(setup_test_files: Path) -> None:
files = list(setup_test_files.iterdir())
nix_file = next(f for f in files if f.suffix == ".nix")
json_file = next(f for f in files if f.suffix == ".json")
nix_file = folder_path / f"fixtures/{entry_file}"
json_file = folder_path / f"fixtures/{inventory_file}"
with TemporaryDirectory() as tmp:
shutil.copyfile(
str(nix_file),
str(Path(tmp) / entry_file),
)
shutil.copyfile(
str(json_file),
str(Path(tmp) / inventory_file),
)
assert nix_file.exists()
assert json_file.exists()
store = InventoryStore(
flake=MockFlake(Path(tmp) / entry_file),
inventory_file_name=inventory_file,
flake=MockFlake(nix_file),
inventory_file_name=json_file.name,
_allowed_path_transforms=["foo.*"],
_keys=[], # disable toplevel filtering
)