Skip to content

Commit

Permalink
tests/plugins: refactor (nix-community#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
GaetanLepage authored Mar 22, 2023
1 parent 89f54be commit db5061b
Show file tree
Hide file tree
Showing 38 changed files with 485 additions and 744 deletions.
3 changes: 2 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@
extractRustAnalyzerPkg = pkgs.callPackage extractRustAnalyzer {};
in {
checks =
(import ./tests/checks.nix {
(import ./tests {
inherit pkgs;
inherit (pkgs) lib;
makeNixvim = self.legacyPackages."${system}".makeNixvim;
})
// {
Expand Down
2 changes: 1 addition & 1 deletion modules/autocmd.nix
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ with lib; let
{
event = [ "BufEnter" "BufWinEnter" ];
pattern = [ "*.c" "*.h" ];
callback = { __raw = "function() print("This buffer enters") end"; };
callback = { __raw = "function() print('This buffer enters') end"; };
}
'';

Expand Down
25 changes: 0 additions & 25 deletions tests/checks.nix

This file was deleted.

16 changes: 16 additions & 0 deletions tests/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
makeNixvim,
lib,
pkgs,
}: let
fetchTests = import ./fetch-tests.nix;
mkTestDerivation = import ./test-derivation.nix {inherit pkgs makeNixvim;};

# List of files containing configurations
testFiles = fetchTests {
inherit lib pkgs;
root = ./test-sources;
};
in
# We attempt to build & execute all configurations
builtins.mapAttrs mkTestDerivation testFiles
67 changes: 67 additions & 0 deletions tests/fetch-tests.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
root,
lib,
pkgs,
}: let
# 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
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]);

# Recurse into all directories, extracting files as we find them. This returs a deeply nested
# list, where each non list element is a set of test cases.
parseDirectories = path: namespace: let
relativePath = lib.removePrefix "${root}" "${path}";

children = builtins.readDir path;
childrenFiltered =
lib.attrsets.filterAttrs (n: v: v != "symlink") children;

childrenRecursed =
lib.attrsets.mapAttrsToList (handleEntry relativePath namespace) childrenFiltered;
in
childrenRecursed;

# Remove the nesting
testsList = lib.lists.flatten (parseDirectories root []);

testsListEvaluated = builtins.map ({
cases,
namespace,
} @ args:
if builtins.isAttrs cases
then args
else {
# cases = cases {inherit pkgs;};
cases = cases {inherit pkgs;};
inherit namespace;
})
testsList;

# Take a list of test cases (i.e the content of a file) and prepare a test case that can be
# handled by mkTestDerivation
handleTestFile = {
namespace,
cases,
}:
lib.attrsets.mapAttrs' (case: config: {
name = lib.strings.concatStringsSep "-" (namespace ++ [case]);
value = config;
})
cases;

# Helper function that calls `//` for each attrset of a list
concatMany = list: lib.lists.foldr lib.mergeAttrs {} list;
# An attrset of 'test-name' -> 'test-config'
in
concatMany (builtins.map handleTestFile testsListEvaluated)
Loading

0 comments on commit db5061b

Please sign in to comment.