From 990ef039f79cd75a4a2b2ffeba5f45a21cb5ccc6 Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Fri, 18 Oct 2024 09:44:59 +0100 Subject: [PATCH 1/3] tests: move test derivations to `tests/default.nix` Move the previous `default.nix` to `main.nix` so that `default.nix` can be used for defining the set of all test derivations. `main.nix` is imported by `default.nix`, but is only responsible for the tests built from `tests/test-sources/`. --- flake-modules/tests.nix | 62 ++++++----------------- tests/default.nix | 106 ++++++++++++++++++---------------------- tests/main.nix | 66 +++++++++++++++++++++++++ 3 files changed, 129 insertions(+), 105 deletions(-) create mode 100644 tests/main.nix diff --git a/flake-modules/tests.nix b/flake-modules/tests.nix index 912e9869fd..5c0a39ca88 100644 --- a/flake-modules/tests.nix +++ b/flake-modules/tests.nix @@ -1,57 +1,27 @@ -{ self, helpers, ... }: +{ + self, + lib, + helpers, + ... +}: { perSystem = { pkgs, pkgsUnfree, system, - makeNixvimWithModule, - self', ... }: - let - inherit (self'.legacyPackages) nixvimConfiguration; - in { - checks = { - extra-args-tests = import ../tests/extra-args.nix { - inherit pkgs; - inherit makeNixvimWithModule; - }; - - extend = import ../tests/extend.nix { inherit pkgs makeNixvimWithModule; }; - - extra-files = import ../tests/extra-files.nix { inherit pkgs makeNixvimWithModule; }; - - enable-except-in-tests = import ../tests/enable-except-in-tests.nix { - inherit pkgs makeNixvimWithModule; - inherit (self.lib.${system}.check) mkTestDerivationFromNixvimModule; - }; - - failing-tests = pkgs.callPackage ../tests/failing-tests.nix { - inherit (self.lib.${system}.check) mkTestDerivationFromNixvimModule; - }; - - no-flake = import ../tests/no-flake.nix { - inherit system; - inherit (self.lib.${system}.check) mkTestDerivationFromNvim; - nixvim = "${self}"; - }; - - lib-tests = import ../tests/lib-tests.nix { - inherit pkgs helpers; - inherit (pkgs) lib; - }; - - maintainers = import ../tests/maintainers.nix { inherit pkgs; }; - - plugins-by-name = pkgs.callPackage ../tests/plugins-by-name.nix { inherit nixvimConfiguration; }; - - generated = pkgs.callPackage ../tests/generated.nix { }; - - package-options = pkgs.callPackage ../tests/package-options.nix { inherit nixvimConfiguration; }; - - lsp-all-servers = pkgs.callPackage ../tests/lsp-servers.nix { inherit nixvimConfiguration; }; - } // import ../tests { inherit pkgs pkgsUnfree helpers; }; + checks = import ../tests { + inherit + helpers + lib + pkgs + pkgsUnfree + self + system + ; + }; }; } diff --git a/tests/default.nix b/tests/default.nix index 4d7a18c96f..97d8b4f248 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -1,65 +1,53 @@ { - lib ? pkgs.lib, - helpers, pkgs, pkgsUnfree, + helpers, + lib, + system, + self, # The flake instance }: let - fetchTests = import ./fetch-tests.nix { inherit lib pkgs helpers; }; - test-derivation = import ../lib/tests.nix { inherit pkgs lib; }; - inherit (test-derivation) mkTestDerivationFromNixvimModule; - - moduleToTest = - file: name: module: - mkTestDerivationFromNixvimModule { - inherit name; - module = { - _file = file; - imports = [ module ]; - }; - pkgs = pkgsUnfree; - }; - - # List of files containing configurations - testFiles = fetchTests ./test-sources; - - exampleFiles = { - name = "examples"; - file = ../example.nix; - cases = - let - config = import ../example.nix { inherit pkgs; }; - in - { - main = builtins.removeAttrs config.programs.nixvim [ - # This is not available to standalone modules, only HM & NixOS Modules - "enable" - # This is purely an example, it does not reflect a real usage - "extraConfigLua" - "extraConfigVim" - ]; - }; - }; + inherit (self.legacyPackages.${system}) + makeNixvimWithModule + nixvimConfiguration + ; in -# We attempt to build & execute all configurations -lib.pipe (testFiles ++ [ exampleFiles ]) [ - (builtins.map ( - { - name, - file, - cases, - }: - { - inherit name; - path = pkgs.linkFarm name (builtins.mapAttrs (moduleToTest file) cases); - } - )) - (helpers.groupListBySize 10) - (lib.imap1 ( - i: group: rec { - name = "test-${toString i}"; - value = pkgs.linkFarm name group; - } - )) - builtins.listToAttrs -] +{ + extra-args-tests = import ./extra-args.nix { + inherit pkgs; + inherit makeNixvimWithModule; + }; + extend = import ./extend.nix { inherit pkgs makeNixvimWithModule; }; + extra-files = import ./extra-files.nix { inherit pkgs makeNixvimWithModule; }; + enable-except-in-tests = import ./enable-except-in-tests.nix { + inherit pkgs makeNixvimWithModule; + inherit (self.lib.${system}.check) mkTestDerivationFromNixvimModule; + }; + failing-tests = pkgs.callPackage ./failing-tests.nix { + inherit (self.lib.${system}.check) mkTestDerivationFromNixvimModule; + }; + no-flake = import ./no-flake.nix { + inherit system; + inherit (self.lib.${system}.check) mkTestDerivationFromNvim; + nixvim = "${self}"; + }; + lib-tests = import ./lib-tests.nix { + inherit pkgs helpers; + inherit (pkgs) lib; + }; + maintainers = import ./maintainers.nix { inherit pkgs; }; + plugins-by-name = pkgs.callPackage ./plugins-by-name.nix { inherit nixvimConfiguration; }; + generated = pkgs.callPackage ./generated.nix { }; + package-options = pkgs.callPackage ./package-options.nix { inherit nixvimConfiguration; }; + lsp-all-servers = pkgs.callPackage ./lsp-servers.nix { inherit nixvimConfiguration; }; +} +# Tests generated from ./test-sources +# Grouped as a number of link-farms in the form { test-1, test-2, ... test-N } +// import ./main.nix { + inherit + lib + pkgs + pkgsUnfree + helpers + ; +} diff --git a/tests/main.nix b/tests/main.nix new file mode 100644 index 0000000000..bf435e3171 --- /dev/null +++ b/tests/main.nix @@ -0,0 +1,66 @@ +# Collects the various test modules in tests/test-sources/ and groups them into a number of test derivations +{ + lib ? pkgs.lib, + helpers, + pkgs, + pkgsUnfree, +}: +let + fetchTests = import ./fetch-tests.nix { inherit lib pkgs helpers; }; + test-derivation = import ../lib/tests.nix { inherit pkgs lib; }; + inherit (test-derivation) mkTestDerivationFromNixvimModule; + + moduleToTest = + file: name: module: + mkTestDerivationFromNixvimModule { + inherit name; + module = { + _file = file; + imports = [ module ]; + }; + pkgs = pkgsUnfree; + }; + + # List of files containing configurations + testFiles = fetchTests ./test-sources; + + exampleFiles = { + name = "examples"; + file = ../example.nix; + cases = + let + config = import ../example.nix { inherit pkgs; }; + in + { + main = builtins.removeAttrs config.programs.nixvim [ + # This is not available to standalone modules, only HM & NixOS Modules + "enable" + # This is purely an example, it does not reflect a real usage + "extraConfigLua" + "extraConfigVim" + ]; + }; + }; +in +# We attempt to build & execute all configurations +lib.pipe (testFiles ++ [ exampleFiles ]) [ + (builtins.map ( + { + name, + file, + cases, + }: + { + inherit name; + path = pkgs.linkFarm name (builtins.mapAttrs (moduleToTest file) cases); + } + )) + (helpers.groupListBySize 10) + (lib.imap1 ( + i: group: rec { + name = "test-${toString i}"; + value = pkgs.linkFarm name group; + } + )) + builtins.listToAttrs +] From 4a508ceee235edf0a094e21ea1687c45d3cc4d6b Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Thu, 17 Oct 2024 19:59:37 +0100 Subject: [PATCH 2/3] tests: use `callTest` pattern Allows using the `callPackage(s)` pattern on tests. Rather than using `pkgs.callPackage`, we implement our own variant using `lib.callPackageWith`. Our variant (`callTest`) includes additional nixvim-specific stuff commonly used by our tests. --- tests/default.nix | 74 ++++++++++++++++++++++------------------------ tests/main.nix | 8 +++-- tests/no-flake.nix | 3 +- 3 files changed, 42 insertions(+), 43 deletions(-) diff --git a/tests/default.nix b/tests/default.nix index 97d8b4f248..93484f1821 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -7,47 +7,43 @@ self, # The flake instance }: let - inherit (self.legacyPackages.${system}) - makeNixvimWithModule - nixvimConfiguration - ; + autoArgs = pkgs // { + inherit + helpers + lib + pkgsUnfree + self + system + ; + inherit (self.legacyPackages.${system}) + makeNixvimWithModule + nixvimConfiguration + ; + inherit (self.lib.${system}.check) + mkTestDerivationFromNvim + mkTestDerivationFromNixvimModule + ; + # Recursive: + inherit callTest callTests; + }; + + callTest = lib.callPackageWith autoArgs; + callTests = lib.callPackagesWith autoArgs; in { - extra-args-tests = import ./extra-args.nix { - inherit pkgs; - inherit makeNixvimWithModule; - }; - extend = import ./extend.nix { inherit pkgs makeNixvimWithModule; }; - extra-files = import ./extra-files.nix { inherit pkgs makeNixvimWithModule; }; - enable-except-in-tests = import ./enable-except-in-tests.nix { - inherit pkgs makeNixvimWithModule; - inherit (self.lib.${system}.check) mkTestDerivationFromNixvimModule; - }; - failing-tests = pkgs.callPackage ./failing-tests.nix { - inherit (self.lib.${system}.check) mkTestDerivationFromNixvimModule; - }; - no-flake = import ./no-flake.nix { - inherit system; - inherit (self.lib.${system}.check) mkTestDerivationFromNvim; - nixvim = "${self}"; - }; - lib-tests = import ./lib-tests.nix { - inherit pkgs helpers; - inherit (pkgs) lib; - }; - maintainers = import ./maintainers.nix { inherit pkgs; }; - plugins-by-name = pkgs.callPackage ./plugins-by-name.nix { inherit nixvimConfiguration; }; - generated = pkgs.callPackage ./generated.nix { }; - package-options = pkgs.callPackage ./package-options.nix { inherit nixvimConfiguration; }; - lsp-all-servers = pkgs.callPackage ./lsp-servers.nix { inherit nixvimConfiguration; }; + extra-args-tests = callTest ./extra-args.nix { }; + extend = callTest ./extend.nix { }; + extra-files = callTest ./extra-files.nix { }; + enable-except-in-tests = callTest ./enable-except-in-tests.nix { }; + failing-tests = callTest ./failing-tests.nix { }; + no-flake = callTest ./no-flake.nix { }; + lib-tests = callTest ./lib-tests.nix { }; + maintainers = callTest ./maintainers.nix { }; + plugins-by-name = callTest ./plugins-by-name.nix { }; + generated = callTest ./generated.nix { }; + package-options = callTest ./package-options.nix { }; + lsp-all-servers = callTest ./lsp-servers.nix { }; } # Tests generated from ./test-sources # Grouped as a number of link-farms in the form { test-1, test-2, ... test-N } -// import ./main.nix { - inherit - lib - pkgs - pkgsUnfree - helpers - ; -} +// callTests ./main.nix { } diff --git a/tests/main.nix b/tests/main.nix index bf435e3171..d43eac61ed 100644 --- a/tests/main.nix +++ b/tests/main.nix @@ -1,13 +1,15 @@ # Collects the various test modules in tests/test-sources/ and groups them into a number of test derivations { - lib ? pkgs.lib, + callPackage, + callTest, helpers, + lib ? pkgs.lib, pkgs, pkgsUnfree, }: let - fetchTests = import ./fetch-tests.nix { inherit lib pkgs helpers; }; - test-derivation = import ../lib/tests.nix { inherit pkgs lib; }; + fetchTests = callTest ./fetch-tests.nix { }; + test-derivation = callPackage ../lib/tests.nix { }; inherit (test-derivation) mkTestDerivationFromNixvimModule; moduleToTest = diff --git a/tests/no-flake.nix b/tests/no-flake.nix index ceacf1dda7..9b4e415d0f 100644 --- a/tests/no-flake.nix +++ b/tests/no-flake.nix @@ -1,6 +1,7 @@ { + nixvim ? "${self}", + self ? throw "either supply `self` or `nixvim`", system, - nixvim, mkTestDerivationFromNvim, }: let From 036e11665f819ebf1dddf493ba212c1ec441eaad Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Wed, 28 Aug 2024 09:48:10 +0100 Subject: [PATCH 3/3] tests: cleanup tests to better use `callTest` pattern e.g. Prefer taking `pkgs.*` attrs directly as function arguments. --- tests/enable-except-in-tests.nix | 6 ++++-- tests/extend.nix | 7 +++++-- tests/extra-args.nix | 7 +++++-- tests/extra-files.nix | 7 +++++-- tests/lib-tests.nix | 15 ++++++++------- tests/main.nix | 5 +++-- tests/maintainers.nix | 6 +++--- 7 files changed, 33 insertions(+), 20 deletions(-) diff --git a/tests/enable-except-in-tests.nix b/tests/enable-except-in-tests.nix index 65070198c6..2ef815e3ed 100644 --- a/tests/enable-except-in-tests.nix +++ b/tests/enable-except-in-tests.nix @@ -1,5 +1,7 @@ { pkgs, + linkFarm, + runCommandNoCCLocal, mkTestDerivationFromNixvimModule, makeNixvimWithModule, }: @@ -19,7 +21,7 @@ let let nvim = makeNixvimWithModule { inherit pkgs module; }; in - pkgs.runCommand "enable-except-in-tests-not-in-test" + runCommandNoCCLocal "enable-except-in-tests-not-in-test" { printConfig = "${nvim}/bin/nixvim-print-init"; } '' if ! "$printConfig" | grep 'require("image").setup'; then @@ -31,7 +33,7 @@ let touch $out ''; in -pkgs.linkFarm "enable-except-in-tests" [ +linkFarm "enable-except-in-tests" [ { name = "in-test"; path = inTest; diff --git a/tests/extend.nix b/tests/extend.nix index 00bd828152..507428be32 100644 --- a/tests/extend.nix +++ b/tests/extend.nix @@ -1,4 +1,7 @@ -{ makeNixvimWithModule, pkgs }: +{ + makeNixvimWithModule, + runCommandNoCCLocal, +}: let firstStage = makeNixvimWithModule { module = { @@ -10,7 +13,7 @@ let generated = secondStage.extend { extraConfigLua = "-- third stage"; }; in -pkgs.runCommand "extend-test" { printConfig = "${generated}/bin/nixvim-print-init"; } '' +runCommandNoCCLocal "extend-test" { printConfig = "${generated}/bin/nixvim-print-init"; } '' config=$($printConfig) for stage in "first" "second" "third"; do if ! "$printConfig" | grep -q -- "-- $stage stage"; then diff --git a/tests/extra-args.nix b/tests/extra-args.nix index e43f6dff12..3492b10c40 100644 --- a/tests/extra-args.nix +++ b/tests/extra-args.nix @@ -1,4 +1,7 @@ -{ makeNixvimWithModule, pkgs }: +{ + makeNixvimWithModule, + runCommandNoCCLocal, +}: let defaultModule = { regularArg, ... }: @@ -28,7 +31,7 @@ let }; }; in -pkgs.runCommand "special-arg-test" { printConfig = "${generated}/bin/nixvim-print-init"; } '' +runCommandNoCCLocal "special-arg-test" { printConfig = "${generated}/bin/nixvim-print-init"; } '' config=$($printConfig) if ! "$printConfig" | grep -- '-- regularArg=regularValue'; then echo "Missing regularArg in config" diff --git a/tests/extra-files.nix b/tests/extra-files.nix index c9894501f7..d959de0838 100644 --- a/tests/extra-files.nix +++ b/tests/extra-files.nix @@ -1,4 +1,7 @@ -{ makeNixvimWithModule, pkgs }: +{ + makeNixvimWithModule, + runCommandNoCCLocal, +}: let extraFiles = { "one".text = "one"; @@ -12,7 +15,7 @@ let }; }; in -pkgs.runCommand "extra-files-test" +runCommandNoCCLocal "extra-files-test" { root = build.config.build.extraFiles; files = builtins.attrNames extraFiles; diff --git a/tests/lib-tests.nix b/tests/lib-tests.nix index 870669c8cd..b09cec1838 100644 --- a/tests/lib-tests.nix +++ b/tests/lib-tests.nix @@ -1,9 +1,10 @@ # For shorter test iterations run the following in the root of the repo: # `echo ':b checks.${builtins.currentSystem}.lib-tests' | nix repl .` { - lib, - pkgs, helpers, + lib, + runCommandNoCCLocal, + writeText, }: let luaNames = { @@ -45,9 +46,9 @@ let ]; }; - drv = pkgs.writeText "example-derivation" "hello, world!"; + drv = writeText "example-derivation" "hello, world!"; - results = pkgs.lib.runTests { + results = lib.runTests { testToLuaObject = { expr = helpers.toLuaObject { foo = "bar"; @@ -412,11 +413,11 @@ let }; in if results == [ ] then - pkgs.runCommand "lib-tests-success" { } "touch $out" + runCommandNoCCLocal "lib-tests-success" { } "touch $out" else - pkgs.runCommand "lib-tests-failure" + runCommandNoCCLocal "lib-tests-failure" { - results = pkgs.lib.concatStringsSep "\n" ( + results = lib.concatStringsSep "\n" ( builtins.map (result: '' ${result.name}: expected: ${lib.generators.toPretty { } result.expected} diff --git a/tests/main.nix b/tests/main.nix index d43eac61ed..e2c2de1ba4 100644 --- a/tests/main.nix +++ b/tests/main.nix @@ -4,6 +4,7 @@ callTest, helpers, lib ? pkgs.lib, + linkFarm, pkgs, pkgsUnfree, }: @@ -54,14 +55,14 @@ lib.pipe (testFiles ++ [ exampleFiles ]) [ }: { inherit name; - path = pkgs.linkFarm name (builtins.mapAttrs (moduleToTest file) cases); + path = linkFarm name (builtins.mapAttrs (moduleToTest file) cases); } )) (helpers.groupListBySize 10) (lib.imap1 ( i: group: rec { name = "test-${toString i}"; - value = pkgs.linkFarm name group; + value = linkFarm name group; } )) builtins.listToAttrs diff --git a/tests/maintainers.nix b/tests/maintainers.nix index a9033aacff..f99e73e34f 100644 --- a/tests/maintainers.nix +++ b/tests/maintainers.nix @@ -1,6 +1,6 @@ { - pkgs ? import { }, - lib ? pkgs.lib, + lib, + runCommandNoCCLocal, }: let inherit (lib) attrNames filter length; @@ -9,7 +9,7 @@ let duplicates = filter (name: nixpkgsList ? ${name}) (attrNames nixvimList); count = length duplicates; in -pkgs.runCommand "maintainers-test" { inherit count duplicates; } '' +runCommandNoCCLocal "maintainers-test" { inherit count duplicates; } '' if [ $count -gt 0 ]; then echo "$count nixvim maintainers are also nixpkgs maintainers:" for name in $duplicates; do