Skip to content

Commit

Permalink
lib.packagesFromDirectoryRecursive: add tests for nested scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
nbraud committed Dec 1, 2024
1 parent 259a007 commit 8a5830c
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/tests/misc.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2520,4 +2520,38 @@ runTests {
};
expected = "c";
};

# 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";
};
};
};
};
}
1 change: 1 addition & 0 deletions lib/tests/packages-from-directory/scope/a.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ }: "a"
3 changes: 3 additions & 0 deletions lib/tests/packages-from-directory/scope/b.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{ a }:
assert a == "a";
"b"
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ }
1 change: 1 addition & 0 deletions lib/tests/packages-from-directory/scope/c/package.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ }: "c"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ }
5 changes: 5 additions & 0 deletions lib/tests/packages-from-directory/scope/my-namespace/d.nix
Original file line number Diff line number Diff line change
@@ -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"
3 changes: 3 additions & 0 deletions lib/tests/packages-from-directory/scope/my-namespace/e.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{ d }:
# Check that mutual recursion is possible
"e"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ }: "f"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
a,
d,
h,
}:
# Check we can get parameters from ancestral scopes (e.g. the scope's grandparent)
"g"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ }: "h"

0 comments on commit 8a5830c

Please sign in to comment.