From f16cfe68b6fb322bf3819e8d7ecba26bd90189ad Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Wed, 21 May 2025 18:54:07 +0200 Subject: [PATCH] Tests(deferred custom module): add more tests, dissallow nested imports --- lib/types/default.nix | 12 +++++++--- lib/types/tests.nix | 51 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/lib/types/default.nix b/lib/types/default.nix index 868a83541..0073c0d2b 100644 --- a/lib/types/default.nix +++ b/lib/types/default.nix @@ -2,18 +2,24 @@ { uniqueDeferredSerializableModule = lib.fix ( self: + + let + checkDef = loc: def: if def.value ? imports then throw "uniqueDeferredSerializableModule doesn't allow nested imports" else def; + in # Essentially the "raw" type, but with a custom name and check lib.mkOptionType { name = "deferredModule"; - description = "deferred module that has custom check and merge behavior"; + description = "deferred custom module. Must be JSON serializable."; descriptionClass = "noun"; # Unfortunately, tryEval doesn't catch JSON errors - check = value: lib.seq (builtins.toJSON value) true; + check = value: lib.seq (builtins.toJSON value) (lib.isAttrs value); merge = lib.options.mergeUniqueOption { message = "------"; merge = loc: defs: { imports = map ( - def: lib.setDefaultModuleLocation "${def.file}, via option ${lib.showOption loc}" def.value + def: + lib.seq (checkDef loc def) + lib.setDefaultModuleLocation "${def.file}, via option ${lib.showOption loc}" def.value ) defs; }; }; diff --git a/lib/types/tests.nix b/lib/types/tests.nix index 268292356..850ffaf9d 100644 --- a/lib/types/tests.nix +++ b/lib/types/tests.nix @@ -14,7 +14,7 @@ let }; in { - test_1 = + test_simple = let eval = evalSettingsModule { foo = { }; @@ -38,4 +38,53 @@ in ]; }; }; + + test_no_nested_imports = + let + eval = evalSettingsModule { + foo = { + imports = []; + }; + }; + in + { + inherit eval; + expr = eval.config.foo; + expectedError = { + type = "ThrownError"; + message = "*nested imports"; + }; + }; + + test_no_function_modules = + let + eval = evalSettingsModule { + foo = {...}: { + + }; + }; + in + { + inherit eval; + expr = eval.config.foo; + expectedError = { + type = "TypeError"; + message = "cannot convert a function to JSON"; + }; + }; + + test_non_attrs_module = + let + eval = evalSettingsModule { + foo = "foo.nix"; + }; + in + { + inherit eval; + expr = eval.config.foo; + expectedError = { + type = "ThrownError"; + message = ".*foo.* is not of type"; + }; + }; }