Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: refactor to use callTest(s) pattern + move calls to tests/default.nix #2433

Merged
merged 3 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 16 additions & 46 deletions flake-modules/tests.nix
Original file line number Diff line number Diff line change
@@ -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
;
};
};
}
100 changes: 42 additions & 58 deletions tests/default.nix
Original file line number Diff line number Diff line change
@@ -1,65 +1,49 @@
{
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"
];
};
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
# 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 = 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 }
// callTests ./main.nix { }
6 changes: 4 additions & 2 deletions tests/enable-except-in-tests.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
pkgs,
linkFarm,
runCommandNoCCLocal,
mkTestDerivationFromNixvimModule,
makeNixvimWithModule,
}:
Expand All @@ -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
Expand All @@ -31,7 +33,7 @@ let
touch $out
'';
in
pkgs.linkFarm "enable-except-in-tests" [
linkFarm "enable-except-in-tests" [
{
name = "in-test";
path = inTest;
Expand Down
7 changes: 5 additions & 2 deletions tests/extend.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{ makeNixvimWithModule, pkgs }:
{
makeNixvimWithModule,
runCommandNoCCLocal,
}:
let
firstStage = makeNixvimWithModule {
module = {
Expand All @@ -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
Expand Down
7 changes: 5 additions & 2 deletions tests/extra-args.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{ makeNixvimWithModule, pkgs }:
{
makeNixvimWithModule,
runCommandNoCCLocal,
}:
let
defaultModule =
{ regularArg, ... }:
Expand Down Expand Up @@ -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"
Expand Down
7 changes: 5 additions & 2 deletions tests/extra-files.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{ makeNixvimWithModule, pkgs }:
{
makeNixvimWithModule,
runCommandNoCCLocal,
}:
let
extraFiles = {
"one".text = "one";
Expand All @@ -12,7 +15,7 @@ let
};
};
in
pkgs.runCommand "extra-files-test"
runCommandNoCCLocal "extra-files-test"
{
root = build.config.build.extraFiles;
files = builtins.attrNames extraFiles;
Expand Down
15 changes: 8 additions & 7 deletions tests/lib-tests.nix
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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}
Expand Down
69 changes: 69 additions & 0 deletions tests/main.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Collects the various test modules in tests/test-sources/ and groups them into a number of test derivations
Copy link
Member Author

@MattSturgeon MattSturgeon Oct 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a better name than main.nix?

  • main.nix could be confusing, as default.nix is the "main" entrypoint to a directory
  • modules.nix could make sense (the file is responsible for making tests from the modules in test-sources), but we already have a modules.nix that tests the wrapper modules...
  • generated.nix could make sense too, but we already have a generated.nix test that tests the ci-generated files are valid

Maybe modules.nix is the way to go, if we renamed the current modules.nix to wrappers.nix? But this is making the diff much larger than it really should be for a minor refactoring PR...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree modules.nix would be better. We can do it in another PR though.

{
callPackage,
callTest,
helpers,
lib ? pkgs.lib,
linkFarm,
pkgs,
pkgsUnfree,
}:
let
fetchTests = callTest ./fetch-tests.nix { };
test-derivation = callPackage ../lib/tests.nix { };
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 = linkFarm name (builtins.mapAttrs (moduleToTest file) cases);
}
))
(helpers.groupListBySize 10)
(lib.imap1 (
i: group: rec {
name = "test-${toString i}";
value = linkFarm name group;
}
))
builtins.listToAttrs
]
6 changes: 3 additions & 3 deletions tests/maintainers.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
pkgs ? import <nixpkgs> { },
lib ? pkgs.lib,
lib,
runCommandNoCCLocal,
}:
let
inherit (lib) attrNames filter length;
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion tests/no-flake.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
nixvim ? "${self}",
self ? throw "either supply `self` or `nixvim`",
system,
nixvim,
mkTestDerivationFromNvim,
}:
let
Expand Down