Test(InventoryPersistence): add persist integration tests
This commit is contained in:
1
pkgs/clan-cli/clan_lib/persist/fixtures/1.json
Normal file
1
pkgs/clan-cli/clan_lib/persist/fixtures/1.json
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{}
|
||||||
29
pkgs/clan-cli/clan_lib/persist/fixtures/1.nix
Normal file
29
pkgs/clan-cli/clan_lib/persist/fixtures/1.nix
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
{ clanLib, lib, ... }:
|
||||||
|
let
|
||||||
|
eval = lib.evalModules {
|
||||||
|
modules = [
|
||||||
|
{
|
||||||
|
# Trying to write into the default
|
||||||
|
options.foo = lib.mkOption {
|
||||||
|
type = lib.types.str;
|
||||||
|
default = "bar";
|
||||||
|
};
|
||||||
|
options.protected = lib.mkOption {
|
||||||
|
type = lib.types.str;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
{
|
||||||
|
# Cannot write into the default set prio
|
||||||
|
protected = "protected";
|
||||||
|
}
|
||||||
|
# Merge the "inventory.json"
|
||||||
|
(builtins.fromJSON (builtins.readFile ./1.json))
|
||||||
|
];
|
||||||
|
};
|
||||||
|
in
|
||||||
|
{
|
||||||
|
clanInternals.inventoryClass.inventory = eval.config;
|
||||||
|
clanInternals.inventoryClass.introspection = clanLib.introspection.getPrios {
|
||||||
|
options = eval.options;
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,110 @@
|
|||||||
|
# ruff: noqa: SLF001
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
import subprocess
|
||||||
|
from pathlib import Path
|
||||||
|
from tempfile import TemporaryDirectory
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from clan_lib.errors import ClanError
|
||||||
|
from clan_lib.persist.inventory_store import InventoryStore
|
||||||
|
|
||||||
|
|
||||||
|
class MockFlake:
|
||||||
|
def __init__(self, default: Path) -> None:
|
||||||
|
f = default
|
||||||
|
assert f.exists(), f"File {f} does not exist"
|
||||||
|
self._file = f
|
||||||
|
|
||||||
|
def select(
|
||||||
|
self,
|
||||||
|
selector: str,
|
||||||
|
nix_options: list[str] | None = None,
|
||||||
|
) -> Any:
|
||||||
|
nixpkgs = os.environ.get("NIXPKGS")
|
||||||
|
select = os.environ.get("NIX_SELECT")
|
||||||
|
clan_core_path = os.environ.get("CLAN_CORE_PATH")
|
||||||
|
|
||||||
|
assert nixpkgs, "NIXPKGS environment variable is not set"
|
||||||
|
assert select, "NIX_SELECT environment variable is not set"
|
||||||
|
assert clan_core_path, "CLAN_CORE_PATH environment variable is not set"
|
||||||
|
|
||||||
|
output = subprocess.run(
|
||||||
|
[
|
||||||
|
"nix",
|
||||||
|
"eval",
|
||||||
|
"--impure",
|
||||||
|
"--json",
|
||||||
|
"--expr",
|
||||||
|
f"""
|
||||||
|
let
|
||||||
|
pkgs = import {nixpkgs} {{}};
|
||||||
|
inherit (pkgs) lib;
|
||||||
|
clanLib = import {Path(clan_core_path)}/lib {{ inherit lib; self = null; nixpkgs = {nixpkgs}; }};
|
||||||
|
select = (import {select}/select.nix).select;
|
||||||
|
result = import {self._file} {{ inherit pkgs lib clanLib; }};
|
||||||
|
in
|
||||||
|
select "{selector}" result
|
||||||
|
""",
|
||||||
|
],
|
||||||
|
capture_output=True,
|
||||||
|
)
|
||||||
|
res_str = output.stdout.decode()
|
||||||
|
|
||||||
|
if output.returncode != 0:
|
||||||
|
msg = f"Failed to evaluate {selector} in {self._file}: {output.stderr.decode()}"
|
||||||
|
raise ClanError(msg)
|
||||||
|
return json.loads(res_str)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def path(self) -> Path:
|
||||||
|
return self._file.parent
|
||||||
|
|
||||||
|
|
||||||
|
folder_path = Path(__file__).parent.resolve()
|
||||||
|
|
||||||
|
|
||||||
|
def test_for_johannes() -> None:
|
||||||
|
nix_file = folder_path / "fixtures/1.nix"
|
||||||
|
json_file = folder_path / "fixtures/1.json"
|
||||||
|
with TemporaryDirectory() as tmp:
|
||||||
|
shutil.copyfile(
|
||||||
|
str(nix_file),
|
||||||
|
str(Path(tmp) / "1.nix"),
|
||||||
|
)
|
||||||
|
shutil.copyfile(
|
||||||
|
str(json_file),
|
||||||
|
str(Path(tmp) / "1.json"),
|
||||||
|
)
|
||||||
|
|
||||||
|
store = InventoryStore(
|
||||||
|
flake=MockFlake(Path(tmp) / "1.nix"),
|
||||||
|
inventory_file_name="1.json",
|
||||||
|
)
|
||||||
|
assert store.read() == {"foo": "bar", "protected": "protected"}
|
||||||
|
|
||||||
|
data = {"foo": "foo"}
|
||||||
|
store.write(data, "test", commit=False) # type: ignore
|
||||||
|
# Default method to access the inventory
|
||||||
|
assert store.read() == {"foo": "foo", "protected": "protected"}
|
||||||
|
|
||||||
|
# Test the data is actually persisted
|
||||||
|
assert store._get_persisted() == data
|
||||||
|
|
||||||
|
# clan_lib.errors.ClanError: Key 'protected' is not writeable.
|
||||||
|
invalid_data = {"protected": "foo"}
|
||||||
|
with pytest.raises(ClanError) as e:
|
||||||
|
store.write(invalid_data, "test", commit=False) # type: ignore
|
||||||
|
assert str(e.value) == "Key 'protected' is not writeable."
|
||||||
|
|
||||||
|
# Test the data is not touched
|
||||||
|
assert store.read() == {"foo": "foo", "protected": "protected"}
|
||||||
|
assert store._get_persisted() == data
|
||||||
|
|
||||||
|
# Remove the foo key from the persisted data
|
||||||
|
# Technically data = { } should also work
|
||||||
|
data = {"protected": "protected"}
|
||||||
|
store.write(data, "test", commit=False) # type: ignore
|
||||||
|
|||||||
@@ -274,6 +274,10 @@ pythonRuntime.pkgs.buildPythonApplication {
|
|||||||
xargs cp --recursive --target "$CLAN_TEST_STORE/nix/store" < "$closureInfo/store-paths"
|
xargs cp --recursive --target "$CLAN_TEST_STORE/nix/store" < "$closureInfo/store-paths"
|
||||||
nix-store --load-db --store "$CLAN_TEST_STORE" < "$closureInfo/registration"
|
nix-store --load-db --store "$CLAN_TEST_STORE" < "$closureInfo/registration"
|
||||||
|
|
||||||
|
# used for tests without flakes
|
||||||
|
export NIXPKGS=${nixpkgs}
|
||||||
|
export NIX_SELECT=${nix-select}
|
||||||
|
|
||||||
# limit build cores to 4
|
# limit build cores to 4
|
||||||
jobs="$((NIX_BUILD_CORES>4 ? 4 : NIX_BUILD_CORES))"
|
jobs="$((NIX_BUILD_CORES>4 ? 4 : NIX_BUILD_CORES))"
|
||||||
|
|
||||||
|
|||||||
@@ -35,7 +35,7 @@
|
|||||||
in
|
in
|
||||||
{
|
{
|
||||||
devShells.clan-cli = pkgs.callPackage ./shell.nix {
|
devShells.clan-cli = pkgs.callPackage ./shell.nix {
|
||||||
inherit self';
|
inherit self' self;
|
||||||
inherit (self'.packages) clan-cli;
|
inherit (self'.packages) clan-cli;
|
||||||
};
|
};
|
||||||
packages = {
|
packages = {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
clan-cli,
|
clan-cli,
|
||||||
mkShell,
|
mkShell,
|
||||||
ruff,
|
ruff,
|
||||||
|
self,
|
||||||
self',
|
self',
|
||||||
}:
|
}:
|
||||||
|
|
||||||
@@ -35,6 +36,10 @@ mkShell {
|
|||||||
|
|
||||||
export CLAN_CORE_PATH="$GIT_ROOT"
|
export CLAN_CORE_PATH="$GIT_ROOT"
|
||||||
|
|
||||||
|
# used for tests without flakes
|
||||||
|
export NIXPKGS=${self.inputs.nixpkgs.outPath}
|
||||||
|
export NIX_SELECT=${self.inputs.nix-select.outPath}
|
||||||
|
|
||||||
# Add current package to PYTHONPATH
|
# Add current package to PYTHONPATH
|
||||||
export PYTHONPATH="$PKG_ROOT''${PYTHONPATH:+:$PYTHONPATH:}"
|
export PYTHONPATH="$PKG_ROOT''${PYTHONPATH:+:$PYTHONPATH:}"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user