modules/test: move test derivation to an option

Introduced the `test.derivation` read-only option.
This commit is contained in:
Matt Sturgeon 2024-08-05 12:21:24 +01:00
parent 851edc8df1
commit cbd1003d9d
No known key found for this signature in database
GPG key ID: 4F91844CED1A8299
7 changed files with 114 additions and 91 deletions

View file

@ -6,12 +6,6 @@
}: }:
{ {
flake.lib = lib.genAttrs config.systems ( flake.lib = lib.genAttrs config.systems (
lib.flip withSystem ( lib.flip withSystem ({ pkgs, ... }: import ../lib { inherit pkgs lib; })
{ pkgs, config, ... }:
import ../lib {
inherit pkgs lib;
inherit (config.legacyPackages) makeNixvimWithModule;
}
)
); );
} }

View file

@ -10,44 +10,35 @@
... ...
}: }:
{ {
checks = checks = {
{ extra-args-tests = import ../tests/extra-args.nix {
extra-args-tests = import ../tests/extra-args.nix { inherit pkgs;
inherit pkgs; inherit makeNixvimWithModule;
inherit makeNixvimWithModule; };
};
extend = import ../tests/extend.nix { inherit pkgs makeNixvimWithModule; }; extend = import ../tests/extend.nix { inherit pkgs makeNixvimWithModule; };
extra-files = import ../tests/extra-files.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 { enable-except-in-tests = import ../tests/enable-except-in-tests.nix {
inherit pkgs makeNixvimWithModule; inherit pkgs makeNixvimWithModule;
inherit (self.lib.${system}.check) mkTestDerivationFromNixvimModule; inherit (self.lib.${system}.check) mkTestDerivationFromNixvimModule;
}; };
no-flake = import ../tests/no-flake.nix { no-flake = import ../tests/no-flake.nix {
inherit system; inherit system;
inherit (self.lib.${system}.check) mkTestDerivationFromNvim; inherit (self.lib.${system}.check) mkTestDerivationFromNvim;
nixvim = "${self}"; nixvim = "${self}";
}; };
lib-tests = import ../tests/lib-tests.nix { lib-tests = import ../tests/lib-tests.nix {
inherit pkgs helpers; inherit pkgs helpers;
inherit (pkgs) lib; inherit (pkgs) lib;
}; };
maintainers = import ../tests/maintainers.nix { inherit pkgs; }; maintainers = import ../tests/maintainers.nix { inherit pkgs; };
generated = pkgs.callPackage ../tests/generated.nix { }; generated = pkgs.callPackage ../tests/generated.nix { };
} } // import ../tests { inherit pkgs pkgsUnfree helpers; };
// (import ../tests {
inherit
pkgs
pkgsUnfree
helpers
makeNixvimWithModule
;
});
}; };
} }

View file

@ -1,6 +1,5 @@
# Args probably only needs pkgs and lib # Args probably only needs pkgs and lib
{ {
makeNixvimWithModule,
pkgs, pkgs,
lib ? pkgs.lib, lib ? pkgs.lib,
_nixvimTests ? false, _nixvimTests ? false,
@ -8,6 +7,6 @@
}@args: }@args:
{ {
# Add all exported modules here # Add all exported modules here
check = import ../tests/test-derivation.nix { inherit makeNixvimWithModule lib pkgs; }; check = import ../tests/test-derivation.nix { inherit lib pkgs; };
helpers = import ./helpers.nix (args // { inherit _nixvimTests; }); helpers = import ./helpers.nix (args // { inherit _nixvimTests; });
} }

View file

@ -23,7 +23,7 @@ rec {
modules ? [ ], modules ? [ ],
extraSpecialArgs ? { }, extraSpecialArgs ? { },
# Set to false to disable warnings and assertions # Set to false to disable warnings and assertions
# Intended for use with tests, where we may not want to check assertions # Intended to aid accessing config.test.derivation
# WARNING: This argument may be removed without notice: # WARNING: This argument may be removed without notice:
check ? true, check ? true,
}: }:

View file

@ -1,10 +1,64 @@
{ lib, ... }: {
pkgs,
config,
lib,
...
}:
let
cfg = config.test;
in
{ {
options.test = { options.test = {
name = lib.mkOption {
type = lib.types.str;
default = "nixvim-check";
description = "The test derivation's name.";
};
runNvim = lib.mkOption { runNvim = lib.mkOption {
type = lib.types.bool; type = lib.types.bool;
description = "Whether to run `nvim` in the test."; description = "Whether to run `nvim` in the test.";
default = true; default = true;
}; };
# Output
derivation = lib.mkOption {
type = lib.types.package;
description = ''
A derivation that tests the config by running neovim.
'';
readOnly = true;
};
};
config = {
test.derivation = pkgs.stdenv.mkDerivation {
inherit (cfg) name;
dontUnpack = true;
nativeBuildInputs = [
config.finalPackage
pkgs.docker-client
];
# We need to set HOME because neovim will try to create some files
#
# Because neovim does not return an exitcode when quitting we need to check if there are
# errors on stderr
buildPhase = lib.optionalString cfg.runNvim ''
mkdir -p .cache/nvim
output=$(HOME=$(realpath .) nvim -mn --headless "+q" 2>&1 >/dev/null)
if [[ -n $output ]]; then
echo "ERROR: $output"
exit 1
fi
'';
# If we don't do this nix is not happy
installPhase = ''
touch $out
'';
};
}; };
} }

View file

@ -1,5 +1,4 @@
{ {
makeNixvimWithModule,
lib ? pkgs.lib, lib ? pkgs.lib,
helpers, helpers,
pkgs, pkgs,
@ -7,7 +6,7 @@
}: }:
let let
fetchTests = import ./fetch-tests.nix; fetchTests = import ./fetch-tests.nix;
test-derivation = import ./test-derivation.nix { inherit pkgs lib makeNixvimWithModule; }; test-derivation = import ./test-derivation.nix { inherit pkgs lib; };
inherit (test-derivation) mkTestDerivationFromNixvimModule; inherit (test-derivation) mkTestDerivationFromNixvimModule;
# List of files containing configurations # List of files containing configurations

View file

@ -1,7 +1,6 @@
{ {
pkgs, pkgs,
lib ? pkgs.lib, lib ? pkgs.lib,
makeNixvimWithModule,
... ...
}: }:
let let
@ -16,46 +15,26 @@ let
... ...
}@args: }@args:
let let
cfg = nvim.config.test; # FIXME: this doesn't support helpers.enableExceptInTests, a context option would be better
runNvim = result = nvim.extend {
lib.warnIf (args ? dontRun) config.test =
"mkTestDerivationFromNvim: the `dontRun` argument is deprecated. You should use the `test.runNvim` module option instead." {
(cfg.runNvim && !dontRun); inherit name;
}
// lib.optionalAttrs (args ? dontRun) (
lib.warn
"mkTestDerivationFromNvim: the `dontRun` argument is deprecated. You should use the `test.runNvim` module option instead."
{ runNvim = !dontRun; }
);
};
in in
pkgs.stdenv.mkDerivation { result.config.test.derivation;
inherit name;
nativeBuildInputs = [
nvim
pkgs.docker-client
];
dontUnpack = true;
# We need to set HOME because neovim will try to create some files
#
# Because neovim does not return an exitcode when quitting we need to check if there are
# errors on stderr
buildPhase = lib.optionalString runNvim ''
mkdir -p .cache/nvim
output=$(HOME=$(realpath .) nvim -mn --headless "+q" 2>&1 >/dev/null)
if [[ -n $output ]]; then
echo "ERROR: $output"
exit 1
fi
'';
# If we don't do this nix is not happy
installPhase = ''
mkdir $out
'';
};
# Create a nix derivation from a nixvim configuration. # Create a nix derivation from a nixvim configuration.
# The build phase simply consists in running neovim with the given configuration. # The build phase simply consists in running neovim with the given configuration.
mkTestDerivationFromNixvimModule = mkTestDerivationFromNixvimModule =
{ {
name ? "nixvim-check", name ? null,
pkgs ? pkgs, pkgs ? pkgs,
module, module,
extraSpecialArgs ? { }, extraSpecialArgs ? { },
@ -63,22 +42,29 @@ let
dontRun ? false, dontRun ? false,
}@args: }@args:
let let
nvim = makeNixvimWithModule { helpers = import ../lib/helpers.nix {
inherit pkgs extraSpecialArgs; inherit pkgs lib;
# TODO: deprecate helpers.enableExceptInTests,
# add a context option e.g. `config.isTest`?
_nixvimTests = true; _nixvimTests = true;
module = };
if args ? dontRun then
result = helpers.modules.evalNixvim {
modules = [
module
(lib.optionalAttrs (name != null) { test.name = name; })
(lib.optionalAttrs (args ? dontRun) (
lib.warn lib.warn
"mkTestDerivationFromNixvimModule: the `dontRun` argument is deprecated. You should use the `test.runNvim` module option instead." "mkTestDerivationFromNixvimModule: the `dontRun` argument is deprecated. You should use the `test.runNvim` module option instead."
{ { config.test.runNvim = !dontRun; }
imports = [ module ]; ))
config.test.runNvim = !dontRun; ];
} extraSpecialArgs = {
else defaultPkgs = pkgs;
module; } // extraSpecialArgs;
}; };
in in
mkTestDerivationFromNvim { inherit name nvim; }; result.config.test.derivation;
in in
{ {
inherit mkTestDerivationFromNvim mkTestDerivationFromNixvimModule; inherit mkTestDerivationFromNvim mkTestDerivationFromNixvimModule;