Skip to content

Commit

Permalink
lib.types.attrsWith: add some more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hsjobeki committed Oct 17, 2024
1 parent 95c2276 commit 66f9d13
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lib/tests/modules.sh
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,11 @@ 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
checkConfigOutput '^"mergedName.<id>.nested"$' config.result ./name-merge-attrsWith-1.nix
checkConfigError 'The option .mergedName. in .*\.nix. is already declared in .*\.nix' config.mergedName ./name-merge-attrsWith-2.nix

cat <<EOF
====== module tests ======
$pass Pass
Expand Down
45 changes: 45 additions & 0 deletions lib/tests/modules/lazy-attrsWith.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Check that AttrsWith { lazy = true; } is lazy
{ lib, ... }:
let
inherit (lib) types mkOption;
in
{
imports = [
# Module A
(
{ ... }:
{
options.mergedLazy = mkOption {
# Same as lazyAttrsOf
type = types.attrsWith {
lazy = true;
elemType = types.int;
};
};
}
)
# Module B
(
{ ... }:
{
options.mergedLazy = lib.mkOption {
# Same as lazyAttrsOf
type = types.attrsWith {
lazy = true;
elemType = types.int;
};
};
}
)
# Result
(
{ config, ... }:
{
# Can only evaluate if lazy
config.mergedLazy.bar = config.mergedLazy.baz + 1;
config.mergedLazy.baz = 10;
options.result = mkOption { default = config.mergedLazy.bar; };
}
)
];
}
53 changes: 53 additions & 0 deletions lib/tests/modules/name-merge-attrsWith-1.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Check that AttrsWith { lazy = true; } is lazy
{ lib, ... }:
let
inherit (lib) types mkOption;
in
{
imports = [
# Module A
(
{ ... }:
{
options.mergedName = mkOption {
default = { };
type = types.attrsWith {
# Declare <name> = "id"
name = "id";
elemType = types.submodule {
options.nested = mkOption {
type = types.int;
default = 1;
};
};
};
};
}
)
# Module B
(
{ ... }:
{
options.mergedName = mkOption {
# default: "<name>"
type = types.attrsOf (types.submodule { });
# default = {};
};
}
)

# Output
(
{
options,
...
}:
{
options.result = mkOption {
default = lib.concatStringsSep "." (options.mergedName.type.getSubOptions options.mergedName.loc)
.nested.loc;
};
}
)
];
}
39 changes: 39 additions & 0 deletions lib/tests/modules/name-merge-attrsWith-2.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Non mergable attrsWith
{ lib, ... }:
let
inherit (lib) types mkOption;
in
{
imports = [
# Module A
(
{ ... }:
{
options.mergedName = mkOption {
default = { };
type = types.attrsWith {
name = "id";
elemType = types.submodule {
options.nested = mkOption {
type = types.int;
default = 1;
};
};
};
};
}
)
# Module B
(
{ ... }:
{
options.mergedName = mkOption {
type = types.attrsWith {
name = "other";
elemType = types.submodule { };
};
};
}
)
];
}
4 changes: 2 additions & 2 deletions lib/types.nix
Original file line number Diff line number Diff line change
Expand Up @@ -617,9 +617,9 @@ rec {
getSubModules = elemType.getSubModules;
substSubModules = m: attrsWith { elemType = elemType.substSubModules m; inherit name lazy; };
functor = defaultFunctor "attrsWith" // {
wrapped = elemType;
type = t: attrsWith { elemType = t; inherit name lazy; };
type = payload: attrsWith payload;
payload = {
# Important!: Add new function attributes here in case of future changes
inherit elemType name lazy;
};
binOp = lhs: rhs:
Expand Down

0 comments on commit 66f9d13

Please sign in to comment.