From fe12a092f6507da22d90a01254d5b6588dc858ed Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Thu, 22 Aug 2024 13:46:05 +0100 Subject: [PATCH] tests: general cleanup - Refactor much of `tests/fetch-tests.nix` - Move `mkTest` up to let-block in `tests/default.nix` --- tests/default.nix | 34 ++++++++---------- tests/fetch-tests.nix | 84 +++++++++++++++++++++---------------------- 2 files changed, 55 insertions(+), 63 deletions(-) diff --git a/tests/default.nix b/tests/default.nix index 5de8922c..1f6692ee 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -9,6 +9,16 @@ let test-derivation = import ../lib/tests.nix { inherit pkgs lib; }; inherit (test-derivation) mkTestDerivationFromNixvimModule; + mkTest = + { name, module }: + { + inherit name; + path = mkTestDerivationFromNixvimModule { + inherit name module; + pkgs = pkgsUnfree; + }; + }; + # List of files containing configurations testFiles = fetchTests { inherit lib pkgs helpers; @@ -17,7 +27,7 @@ let exampleFiles = { name = "examples"; - cases = + modules = let config = import ../example.nix { inherit pkgs; }; in @@ -37,24 +47,10 @@ let in # We attempt to build & execute all configurations lib.pipe (testFiles ++ [ exampleFiles ]) [ - (builtins.map ( - file: - let - mkTest = - { name, module }: - { - inherit name; - path = mkTestDerivationFromNixvimModule { - inherit name module; - pkgs = pkgsUnfree; - }; - }; - in - { - inherit (file) name; - path = pkgs.linkFarm file.name (builtins.map mkTest file.cases); - } - )) + (builtins.map (file: { + inherit (file) name; + path = pkgs.linkFarm file.name (builtins.map mkTest file.modules); + })) (helpers.groupListBySize 10) (lib.imap1 ( i: group: rec { diff --git a/tests/fetch-tests.nix b/tests/fetch-tests.nix index b9b9654e..3e3e03fb 100644 --- a/tests/fetch-tests.nix +++ b/tests/fetch-tests.nix @@ -5,54 +5,50 @@ helpers, }: 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: + # Import a test file into the form { name = ""; file = ""; modules = []; } + handleTestFile = + file: namespace: let - file = "${root}/${relativePath}/${name}"; + fnOrAttrs = import file; + cases = + if builtins.isFunction fnOrAttrs then + # Call the function + fnOrAttrs { inherit pkgs lib helpers; } + else + fnOrAttrs; in - if type == "regular" then - lib.optional (lib.hasSuffix ".nix" name) [ - { - namespace = namespace ++ [ (lib.strings.removeSuffix ".nix" name) ]; - cases = import file; - } - ] - else - parseDirectories file (namespace ++ [ name ]); + { + inherit file; + name = lib.strings.concatStringsSep "-" namespace; + modules = lib.mapAttrsToList (name: module: { inherit name module; }) cases; + }; - # Recurse into all directories, extracting files as we find them. This returns a deeply nested - # list, where each non list element is a set of test cases. - parseDirectories = + # Recurse into all directories, extracting files as we find them. + # This returns a list of { name; file; modules; } attrsets. + fetchTests = 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; + # 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.removeSuffix ".nix" name) ]) + ) + else + fetchTests file (namespace ++ [ name ]); in - childrenRecursed; - - # Remove the nesting - testsList = lib.lists.flatten (parseDirectories root [ ]); - - testsListEvaluated = builtins.map ( - { cases, ... }@args: - if builtins.isFunction cases then args // { cases = cases { inherit pkgs lib helpers; }; } else args - ) 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 }: - { - name = lib.strings.concatStringsSep "-" namespace; - cases = lib.mapAttrsToList (name: module: { inherit module name; }) cases; - }; + lib.pipe path [ + builtins.readDir + (lib.filterAttrs (n: v: v != "symlink")) + (lib.mapAttrsToList handleEntry) + builtins.concatLists + ]; in -# A list of the form [ { name = "..."; modules = [ /* test cases */ ]; } ] -builtins.map handleTestFile testsListEvaluated +# A list of the form [ { name = "..."; modules = [ /* test case modules */ ]; } ] +fetchTests root [ ]