lib.packagesFromDirectoryRecursive: add tests for nested scopes

This commit is contained in:
nicoo 2024-11-28 21:42:35 +00:00 committed by Paul Meyer
parent 6b7576b0cf
commit 41f219d1d9
12 changed files with 58 additions and 0 deletions

View file

@ -2687,4 +2687,38 @@ runTests {
checkC = false;
};
};
# Check that `packagesFromDirectoryRecursive` can be used to create scopes
# for sub-directories
testPackagesFromDirectoryNestedScopes = let
inherit (lib) makeScope recurseIntoAttrs;
emptyScope = makeScope lib.callPackageWith (_: {});
in {
expr = lib.filterAttrsRecursive (name: value: !lib.elem name [ "callPackage" "newScope" "overrideScope" "packages" ]) (packagesFromDirectoryRecursive {
inherit (emptyScope) callPackage newScope;
recurseIntoDirectory = f: { newScope, ... }@args:
recurseIntoAttrs (makeScope newScope (self:
f (args // {
inherit (self) callPackage newScope;
})
));
directory = ./packages-from-directory/scope;
});
expected = lib.recurseIntoAttrs {
a = "a";
b = "b";
# Note: Other files/directories in `./test-data/c/` are ignored and can be
# used by `package.nix`.
c = "c";
my-namespace = lib.recurseIntoAttrs {
d = "d";
e = "e";
f = "f";
my-sub-namespace = lib.recurseIntoAttrs {
g = "g";
h = "h";
};
};
};
};
}

View file

@ -0,0 +1 @@
{ }: "a"

View file

@ -0,0 +1,3 @@
{ a }:
assert a == "a";
"b"

View file

@ -0,0 +1 @@
{ }: "c"

View file

@ -0,0 +1 @@
{ }

View file

@ -0,0 +1,5 @@
{ a, e }:
# Check we can get parameter from the parent scope(s) as well as the current one
assert a == "a";
assert e == "e";
"d"

View file

@ -0,0 +1,3 @@
{ d }:
# Check that mutual recursion is possible
"e"

View file

@ -0,0 +1 @@
{ }: "f"

View file

@ -0,0 +1,7 @@
{
a,
d,
h,
}:
# Check we can get parameters from ancestral scopes (e.g. the scope's grandparent)
"g"