lib/types: Test attrsWith type merging

Co-Authored-By: @hsjobeki
This commit is contained in:
Silvan Mosberger 2024-11-25 16:06:23 +01:00
parent c4a9529071
commit 415d1932ea
3 changed files with 35 additions and 20 deletions

View file

@ -386,6 +386,10 @@ checkConfigOutput '^true$' config.conditionalWorks ./declare-attrsOf.nix ./attrs
checkConfigOutput '^false$' config.conditionalWorks ./declare-lazyAttrsOf.nix ./attrsOf-conditional-check.nix
checkConfigOutput '^"empty"$' config.value.foo ./declare-lazyAttrsOf.nix ./attrsOf-conditional-check.nix
# Check attrsWith type merging
checkConfigError 'The option `mergedLazyNonLazy'\'' in `.*'\'' is already declared in `.*'\''\.' options.mergedLazyNonLazy ./lazy-attrsWith.nix
checkConfigOutput '^11$' config.lazyResult ./lazy-attrsWith.nix
checkConfigError 'infinite recursion encountered' config.nonLazyResult ./lazy-attrsWith.nix
# Even with multiple assignments, a type error should be thrown if any of them aren't valid
checkConfigError 'A definition for option .* is not of type .*' \
@ -575,8 +579,6 @@ checkConfigOutput '^38|27$' options.submoduleLine38.declarationPositions.1.line
# nested options work
checkConfigOutput '^34$' options.nested.nestedLine34.declarationPositions.0.line ./declaration-positions.nix
# AttrsWith tests
checkConfigOutput '^11$' config.result ./lazy-attrsWith.nix
cat <<EOF
====== module tests ======

View file

@ -2,6 +2,21 @@
{ lib, ... }:
let
inherit (lib) types mkOption;
lazyAttrsOf = mkOption {
# Same as lazyAttrsOf
type = types.attrsWith {
lazy = true;
elemType = types.int;
};
};
attrsOf = mkOption {
# Same as lazyAttrsOf
type = types.attrsWith {
elemType = types.int;
};
};
in
{
imports = [
@ -9,26 +24,18 @@ in
(
{ ... }:
{
options.mergedLazy = mkOption {
# Same as lazyAttrsOf
type = types.attrsWith {
lazy = true;
elemType = types.int;
};
};
options.mergedLazyLazy = lazyAttrsOf;
options.mergedLazyNonLazy = lazyAttrsOf;
options.mergedNonLazyNonLazy = attrsOf;
}
)
# Module B
(
{ ... }:
{
options.mergedLazy = lib.mkOption {
# Same as lazyAttrsOf
type = types.attrsWith {
lazy = true;
elemType = types.int;
};
};
options.mergedLazyLazy = lazyAttrsOf;
options.mergedLazyNonLazy = attrsOf;
options.mergedNonLazyNonLazy = attrsOf;
}
)
# Result
@ -36,9 +43,14 @@ in
{ config, ... }:
{
# Can only evaluate if lazy
config.mergedLazy.bar = config.mergedLazy.baz + 1;
config.mergedLazy.baz = 10;
options.result = mkOption { default = config.mergedLazy.bar; };
config.mergedLazyLazy.bar = config.mergedLazyLazy.baz + 1;
config.mergedLazyLazy.baz = 10;
options.lazyResult = mkOption { default = config.mergedLazyLazy.bar; };
# Can not only evaluate if not lazy
config.mergedNonLazyNonLazy.bar = config.mergedNonLazyNonLazy.baz + 1;
config.mergedNonLazyNonLazy.baz = 10;
options.nonLazyResult = mkOption { default = config.mergedNonLazyNonLazy.bar; };
}
)
];

View file

@ -630,7 +630,8 @@ rec {
getSubModules = elemType.getSubModules;
substSubModules = m: attrsWith { elemType = elemType.substSubModules m; inherit lazy; };
functor = defaultFunctor "attrsWith" // {
wrapped = elemType;
# TODO: This breaks stuff
# wrapped = elemType;
payload = {
# Important!: Add new function attributes here in case of future changes
inherit elemType lazy;