feat(lib): withDynamicArgs, add basic module system tests

This commit is contained in:
Jake Hamilton 2024-06-02 01:01:38 -07:00
parent 0409563e32
commit b7456195bd
Signed by: jakehamilton
GPG key ID: 9762169A1B35EA68
3 changed files with 62 additions and 1 deletions

View file

@ -42,5 +42,17 @@ lib: {
if f ? __functor if f ? __functor
then f.__args__ or lib.fp.args (f.__functor f) then f.__args__ or lib.fp.args (f.__functor f)
else builtins.functionArgs f; else builtins.functionArgs f;
# TODO: Document this.
withDynamicArgs = f: args: let
fArgs = lib.fp.args f;
common = builtins.intersectAttrs fArgs args;
in
if builtins.isAttrs args
then
if fArgs == {}
then f args
else f common
else f args;
}; };
} }

View file

@ -172,7 +172,7 @@ lib: {
(lib.fp.args module); (lib.fp.args module);
in in
if builtins.isFunction module if builtins.isFunction module
then module (args // dynamicArgs) then lib.fp.withDynamicArgs module (args // dynamicArgs)
else module; else module;
# TODO: Document this. # TODO: Document this.

View file

@ -3,6 +3,25 @@ let
in { in {
examples = { examples = {
"empty" = let "empty" = let
evaluated = lib.modules.run {
modules = [
{
options.aux = {
message = lib.options.create {
type = lib.types.string;
};
};
config = {
aux.message = "Hello, World!";
};
}
];
};
in
evaluated ? config;
"hello world" = let
expected = "Hello, World!"; expected = "Hello, World!";
evaluated = lib.modules.run { evaluated = lib.modules.run {
@ -21,6 +40,36 @@ in {
]; ];
}; };
actual = evaluated.config.aux.message;
in
actual == expected;
"recursive" = let
expected = "Hello, World!";
evaluated = lib.modules.run {
modules = [
({config}: {
options.aux = {
message = lib.options.create {
type = lib.types.string;
};
proxy = lib.options.create {
type = lib.types.string;
};
};
config = {
aux = {
proxy = "Hello, World!";
message = config.aux.proxy;
};
};
})
];
};
actual = evaluated.config.aux.message; actual = evaluated.config.aux.message;
in in
actual == expected; actual == expected;