nix-community.nixvim/tests/fetch-tests.nix

80 lines
2.4 KiB
Nix
Raw Permalink Normal View History

2023-03-22 07:42:02 +01:00
{
root,
lib,
pkgs,
helpers,
2024-05-05 19:39:35 +02:00
}:
let
2023-03-22 07:42:02 +01:00
# Handle an entry from readDir and either extract the configuration if its a regular file,
# or continue to recurse if it's a directory. While recursing maintain a list of the traversed
# directories
2024-05-05 19:39:35 +02:00
handleEntry =
relativePath: namespace: name: type:
let
file = "${root}/${relativePath}/${name}";
in
if type == "regular" then
[
{
namespace = namespace ++ [ (lib.strings.removeSuffix ".nix" name) ];
cases = import file;
}
]
else
parseDirectories file (namespace ++ [ name ]);
2023-03-22 07:42:02 +01:00
2024-03-07 19:44:13 +01:00
# Recurse into all directories, extracting files as we find them. This returns a deeply nested
2023-03-22 07:42:02 +01:00
# list, where each non list element is a set of test cases.
2024-05-05 19:39:35 +02:00
parseDirectories =
path: namespace:
let
relativePath = lib.removePrefix "${root}" "${path}";
2023-03-22 07:42:02 +01:00
2024-05-05 19:39:35 +02:00
children = builtins.readDir path;
childrenFiltered = lib.attrsets.filterAttrs (n: v: v != "symlink") children;
2023-03-22 07:42:02 +01:00
2024-05-05 19:39:35 +02:00
childrenRecursed = lib.attrsets.mapAttrsToList (handleEntry relativePath namespace) childrenFiltered;
in
2023-03-22 07:42:02 +01:00
childrenRecursed;
# Remove the nesting
2024-05-05 19:39:35 +02:00
testsList = lib.lists.flatten (parseDirectories root [ ]);
2023-03-22 07:42:02 +01:00
2024-05-05 19:39:35 +02:00
testsListEvaluated = builtins.map (
{ cases, namespace }@args:
if builtins.isAttrs cases then
args
else
{
# cases is a function
cases = cases {
inherit pkgs helpers;
efmls-options = import ../plugins/lsp/language-servers/efmls-configs.nix {
inherit pkgs lib helpers;
2024-05-05 19:39:35 +02:00
config = { };
};
nonels-sources-options = import ../plugins/none-ls/servers.nix {
inherit pkgs lib helpers;
2024-05-05 19:39:35 +02:00
config = { };
};
cmp-sources = import ../plugins/completion/cmp/sources.nix;
};
2023-03-22 07:42:02 +01:00
inherit namespace;
2024-05-05 19:39:35 +02:00
}
) testsList;
2023-03-22 07:42:02 +01:00
# Take a list of test cases (i.e the content of a file) and prepare a test case that can be
# handled by mkTestDerivation
2024-05-05 19:39:35 +02:00
handleTestFile =
{ namespace, cases }:
2023-03-22 07:42:02 +01:00
lib.attrsets.mapAttrs' (case: config: {
2024-05-05 19:39:35 +02:00
name = lib.strings.concatStringsSep "-" (namespace ++ [ case ]);
2023-03-22 07:42:02 +01:00
value = config;
2024-05-05 19:39:35 +02:00
}) cases;
2023-03-22 07:42:02 +01:00
# Helper function that calls `//` for each attrset of a list
2024-05-05 19:39:35 +02:00
concatMany = lib.lists.foldr lib.mergeAttrs { };
2023-03-22 07:42:02 +01:00
in
2024-05-05 19:39:35 +02:00
# An attrset of 'test-name' -> 'test-config'
concatMany (builtins.map handleTestFile testsListEvaluated)