nix-community.nixvim/tests/fetch-tests.nix
Stanislav Asunkin ddddd780e0
tests/fetch-tests: avoid unnecessary copy-to-store
Previously path was concatenated using string interpolation. This commit
switches from string interpolation to path interpolation.

The problem is nix will copy the "${directory}" to a new store
object, meaning anything outside its root won't be available.

From the nix manual:

> A path in an interpolated expression is first copied into the Nix
> store, and the resulting string is the store path of the newly created
> store object.

https://nix.dev/manual/nix/2.28/language/string-interpolation#interpolated-expression
2025-05-12 15:24:52 +01:00

53 lines
1.4 KiB
Nix

{
lib,
pkgs,
helpers,
}:
let
# Import a test file into the form { name = ""; file = ""; cases = {}; }
handleTestFile =
file: namespace:
let
fnOrAttrs = import file;
in
{
inherit file;
name = lib.strings.concatStringsSep "-" namespace;
cases =
if builtins.isFunction fnOrAttrs then
# Call the function
fnOrAttrs { inherit pkgs lib helpers; }
else
fnOrAttrs;
};
# Recurse into all directories, extracting files as we find them.
# This returns a list of { name; file; cases; } attrsets.
fetchTests =
path: namespace:
let
# Handle an entry from readDir
# - If it is a regular nix file, import its content
# - If it is a directory, continue recursively
handleEntry =
name: type:
let
file = /${path}/${name};
in
if type == "regular" then
lib.optional (lib.hasSuffix ".nix" name) (
handleTestFile file (
namespace ++ lib.optional (name != "default.nix") (lib.removeSuffix ".nix" name)
)
)
else
fetchTests file (namespace ++ [ name ]);
in
lib.pipe path [
builtins.readDir
(lib.filterAttrs (n: v: v != "symlink"))
(lib.mapAttrsToList handleEntry)
builtins.concatLists
];
in
root: fetchTests root [ ]