From 8a5830c48ba920bd2abf55616097395960fe8778 Mon Sep 17 00:00:00 2001 From: nicoo Date: Thu, 28 Nov 2024 21:42:35 +0000 Subject: [PATCH] lib.packagesFromDirectoryRecursive: add tests for nested scopes --- lib/tests/misc.nix | 34 +++++++++++++++++++ lib/tests/packages-from-directory/scope/a.nix | 1 + lib/tests/packages-from-directory/scope/b.nix | 3 ++ .../scope/c/my-extra-feature.patch | 0 .../scope/c/not-a-namespace/not-a-package.nix | 1 + .../scope/c/package.nix | 1 + .../scope/c/support-definitions.nix | 1 + .../scope/my-namespace/d.nix | 5 +++ .../scope/my-namespace/e.nix | 3 ++ .../scope/my-namespace/f/package.nix | 1 + .../scope/my-namespace/my-sub-namespace/g.nix | 7 ++++ .../scope/my-namespace/my-sub-namespace/h.nix | 1 + 12 files changed, 58 insertions(+) create mode 100644 lib/tests/packages-from-directory/scope/a.nix create mode 100644 lib/tests/packages-from-directory/scope/b.nix create mode 100644 lib/tests/packages-from-directory/scope/c/my-extra-feature.patch create mode 100644 lib/tests/packages-from-directory/scope/c/not-a-namespace/not-a-package.nix create mode 100644 lib/tests/packages-from-directory/scope/c/package.nix create mode 100644 lib/tests/packages-from-directory/scope/c/support-definitions.nix create mode 100644 lib/tests/packages-from-directory/scope/my-namespace/d.nix create mode 100644 lib/tests/packages-from-directory/scope/my-namespace/e.nix create mode 100644 lib/tests/packages-from-directory/scope/my-namespace/f/package.nix create mode 100644 lib/tests/packages-from-directory/scope/my-namespace/my-sub-namespace/g.nix create mode 100644 lib/tests/packages-from-directory/scope/my-namespace/my-sub-namespace/h.nix diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix index 77f72fa942767..0c30b2874a86f 100644 --- a/lib/tests/misc.nix +++ b/lib/tests/misc.nix @@ -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"; + }; + }; + }; + }; } diff --git a/lib/tests/packages-from-directory/scope/a.nix b/lib/tests/packages-from-directory/scope/a.nix new file mode 100644 index 0000000000000..0ba3be1fa5a5c --- /dev/null +++ b/lib/tests/packages-from-directory/scope/a.nix @@ -0,0 +1 @@ +{ }: "a" diff --git a/lib/tests/packages-from-directory/scope/b.nix b/lib/tests/packages-from-directory/scope/b.nix new file mode 100644 index 0000000000000..4201bac5693eb --- /dev/null +++ b/lib/tests/packages-from-directory/scope/b.nix @@ -0,0 +1,3 @@ +{ a }: +assert a == "a"; +"b" diff --git a/lib/tests/packages-from-directory/scope/c/my-extra-feature.patch b/lib/tests/packages-from-directory/scope/c/my-extra-feature.patch new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/lib/tests/packages-from-directory/scope/c/not-a-namespace/not-a-package.nix b/lib/tests/packages-from-directory/scope/c/not-a-namespace/not-a-package.nix new file mode 100644 index 0000000000000..ffcd4415b08f8 --- /dev/null +++ b/lib/tests/packages-from-directory/scope/c/not-a-namespace/not-a-package.nix @@ -0,0 +1 @@ +{ } diff --git a/lib/tests/packages-from-directory/scope/c/package.nix b/lib/tests/packages-from-directory/scope/c/package.nix new file mode 100644 index 0000000000000..33caddbec8808 --- /dev/null +++ b/lib/tests/packages-from-directory/scope/c/package.nix @@ -0,0 +1 @@ +{ }: "c" diff --git a/lib/tests/packages-from-directory/scope/c/support-definitions.nix b/lib/tests/packages-from-directory/scope/c/support-definitions.nix new file mode 100644 index 0000000000000..ffcd4415b08f8 --- /dev/null +++ b/lib/tests/packages-from-directory/scope/c/support-definitions.nix @@ -0,0 +1 @@ +{ } diff --git a/lib/tests/packages-from-directory/scope/my-namespace/d.nix b/lib/tests/packages-from-directory/scope/my-namespace/d.nix new file mode 100644 index 0000000000000..9496d021a94c1 --- /dev/null +++ b/lib/tests/packages-from-directory/scope/my-namespace/d.nix @@ -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" diff --git a/lib/tests/packages-from-directory/scope/my-namespace/e.nix b/lib/tests/packages-from-directory/scope/my-namespace/e.nix new file mode 100644 index 0000000000000..276b80deab406 --- /dev/null +++ b/lib/tests/packages-from-directory/scope/my-namespace/e.nix @@ -0,0 +1,3 @@ +{ d }: +# Check that mutual recursion is possible +"e" diff --git a/lib/tests/packages-from-directory/scope/my-namespace/f/package.nix b/lib/tests/packages-from-directory/scope/my-namespace/f/package.nix new file mode 100644 index 0000000000000..d58cfe01d606b --- /dev/null +++ b/lib/tests/packages-from-directory/scope/my-namespace/f/package.nix @@ -0,0 +1 @@ +{ }: "f" diff --git a/lib/tests/packages-from-directory/scope/my-namespace/my-sub-namespace/g.nix b/lib/tests/packages-from-directory/scope/my-namespace/my-sub-namespace/g.nix new file mode 100644 index 0000000000000..77a79907a1511 --- /dev/null +++ b/lib/tests/packages-from-directory/scope/my-namespace/my-sub-namespace/g.nix @@ -0,0 +1,7 @@ +{ + a, + d, + h, +}: +# Check we can get parameters from ancestral scopes (e.g. the scope's grandparent) +"g" diff --git a/lib/tests/packages-from-directory/scope/my-namespace/my-sub-namespace/h.nix b/lib/tests/packages-from-directory/scope/my-namespace/my-sub-namespace/h.nix new file mode 100644 index 0000000000000..34a0416e95772 --- /dev/null +++ b/lib/tests/packages-from-directory/scope/my-namespace/my-sub-namespace/h.nix @@ -0,0 +1 @@ +{ }: "h"