diff --git a/pkgs/clan-cli/clan_lib/persist/fixtures/deferred.json b/pkgs/clan-cli/clan_lib/persist/fixtures/deferred.json new file mode 100644 index 000000000..0967ef424 --- /dev/null +++ b/pkgs/clan-cli/clan_lib/persist/fixtures/deferred.json @@ -0,0 +1 @@ +{} diff --git a/pkgs/clan-cli/clan_lib/persist/fixtures/deferred.nix b/pkgs/clan-cli/clan_lib/persist/fixtures/deferred.nix new file mode 100644 index 000000000..5c9c9e6ba --- /dev/null +++ b/pkgs/clan-cli/clan_lib/persist/fixtures/deferred.nix @@ -0,0 +1,28 @@ +{ clanLib, lib, ... }: +let + eval = lib.evalModules { + modules = [ + { + # Trying to write into the default + options.foo = lib.mkOption { + type = lib.types.attrsOf clanLib.types.uniqueDeferredSerializableModule; + }; + } + { + foo = { + a = { }; + b = { }; + }; + } + + # Merge the "inventory.json" + (builtins.fromJSON (builtins.readFile ./deferred.json)) + ]; + }; +in +{ + clanInternals.inventoryClass.inventory = eval.config; + clanInternals.inventoryClass.introspection = clanLib.introspection.getPrios { + options = eval.options; + }; +} diff --git a/pkgs/clan-cli/clan_lib/persist/inventory_store_test.py b/pkgs/clan-cli/clan_lib/persist/inventory_store_test.py index 50d9f789f..4828c5467 100644 --- a/pkgs/clan-cli/clan_lib/persist/inventory_store_test.py +++ b/pkgs/clan-cli/clan_lib/persist/inventory_store_test.py @@ -11,6 +11,7 @@ import pytest from clan_lib.errors import ClanError from clan_lib.persist.inventory_store import InventoryStore +from clan_lib.persist.util import apply_patch, delete_by_path class MockFlake: @@ -112,3 +113,51 @@ def test_simple_read_write() -> None: # Technically data = { } should also work data = {"protected": "protected"} store.write(data, "test", commit=False) # type: ignore + + +def test_read_deferred() -> None: + entry_file = "deferred.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), + ) + + store = InventoryStore( + flake=MockFlake(Path(tmp) / entry_file), + inventory_file_name=inventory_file, + _allowed_path_transforms=["foo.*"], + ) + + data = store.read() + assert data == {"foo": {"a": {}, "b": {}}} + + # Create a new "deferredModule" "C" + apply_patch(data, "foo.c", {}) + store.write(data, "test", commit=False) # type: ignore + + assert store.read() == {"foo": {"a": {}, "b": {}, "c": {}}} + + # Remove the "deferredModule" "C" + delete_by_path(data, "foo.c") # type: ignore + store.write(data, "test", commit=False) + assert store.read() == {"foo": {"a": {}, "b": {}}} + + # Write settings into a new "deferredModule" "C" and read them back + apply_patch(data, "foo.c", {"timeout": "1s"}) + store.write(data, "test", commit=False) # type: ignore + + assert store.read() == {"foo": {"a": {}, "b": {}, "c": {"timeout": "1s"}}} + + # Remove the "deferredModule" "C" along with its settings + delete_by_path(data, "foo.c") # type: ignore + store.write(data, "test", commit=False) + assert store.read() == {"foo": {"a": {}, "b": {}}}