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 (
lib.flip withSystem (
{ pkgs, config, ... }:
import ../lib {
inherit pkgs lib;
inherit (config.legacyPackages) makeNixvimWithModule;
}
)
lib.flip withSystem ({ pkgs, ... }: import ../lib { inherit pkgs lib; })
);
}

View file

@ -10,8 +10,7 @@
...
}:
{
checks =
{
checks = {
extra-args-tests = import ../tests/extra-args.nix {
inherit pkgs;
inherit makeNixvimWithModule;
@ -40,14 +39,6 @@
maintainers = import ../tests/maintainers.nix { inherit pkgs; };
generated = pkgs.callPackage ../tests/generated.nix { };
}
// (import ../tests {
inherit
pkgs
pkgsUnfree
helpers
makeNixvimWithModule
;
});
} // import ../tests { inherit pkgs pkgsUnfree helpers; };
};
}

View file

@ -1,6 +1,5 @@
# Args probably only needs pkgs and lib
{
makeNixvimWithModule,
pkgs,
lib ? pkgs.lib,
_nixvimTests ? false,
@ -8,6 +7,6 @@
}@args:
{
# 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; });
}

View file

@ -23,7 +23,7 @@ rec {
modules ? [ ],
extraSpecialArgs ? { },
# 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:
check ? true,
}:

View file

@ -1,10 +1,64 @@
{ lib, ... }:
{
pkgs,
config,
lib,
...
}:
let
cfg = config.test;
in
{
options.test = {
name = lib.mkOption {
type = lib.types.str;
default = "nixvim-check";
description = "The test derivation's name.";
};
runNvim = lib.mkOption {
type = lib.types.bool;
description = "Whether to run `nvim` in the test.";
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,
helpers,
pkgs,
@ -7,7 +6,7 @@
}:
let
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;
# List of files containing configurations

View file

@ -1,7 +1,6 @@
{
pkgs,
lib ? pkgs.lib,
makeNixvimWithModule,
...
}:
let
@ -16,46 +15,26 @@ let
...
}@args:
let
cfg = nvim.config.test;
runNvim =
lib.warnIf (args ? dontRun)
"mkTestDerivationFromNvim: the `dontRun` argument is deprecated. You should use the `test.runNvim` module option instead."
(cfg.runNvim && !dontRun);
in
pkgs.stdenv.mkDerivation {
# FIXME: this doesn't support helpers.enableExceptInTests, a context option would be better
result = nvim.extend {
config.test =
{
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
'';
}
// lib.optionalAttrs (args ? dontRun) (
lib.warn
"mkTestDerivationFromNvim: the `dontRun` argument is deprecated. You should use the `test.runNvim` module option instead."
{ runNvim = !dontRun; }
);
};
in
result.config.test.derivation;
# Create a nix derivation from a nixvim configuration.
# The build phase simply consists in running neovim with the given configuration.
mkTestDerivationFromNixvimModule =
{
name ? "nixvim-check",
name ? null,
pkgs ? pkgs,
module,
extraSpecialArgs ? { },
@ -63,22 +42,29 @@ let
dontRun ? false,
}@args:
let
nvim = makeNixvimWithModule {
inherit pkgs extraSpecialArgs;
helpers = import ../lib/helpers.nix {
inherit pkgs lib;
# TODO: deprecate helpers.enableExceptInTests,
# add a context option e.g. `config.isTest`?
_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
"mkTestDerivationFromNixvimModule: the `dontRun` argument is deprecated. You should use the `test.runNvim` module option instead."
{
imports = [ module ];
config.test.runNvim = !dontRun;
}
else
module;
{ config.test.runNvim = !dontRun; }
))
];
extraSpecialArgs = {
defaultPkgs = pkgs;
} // extraSpecialArgs;
};
in
mkTestDerivationFromNvim { inherit name nvim; };
result.config.test.derivation;
in
{
inherit mkTestDerivationFromNvim mkTestDerivationFromNixvimModule;