Tests(inventoryStore): add fixture, make extensible

This commit is contained in:
Johannes Kirschbauer
2025-05-28 15:06:17 +02:00
parent 4b5880d1cb
commit 07a574cbb0

View File

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