nix-community.nixvim/tests/test-derivation.nix
Matt Sturgeon 10f64e6c96
tests/test-derivation: allow tests to be modules
Use `mkTestDerivationFromNixvimModule` instead of `mkTestDerivation`,
allowing "proper" modules to be used instead of plain attr configs.

This is useful for more complex tests that wish to use `config` or
`options` arguments, e.g:

```nix
{config, options, ...}: {
  /* some cool test */
}
```

To allow `tests.dontRun` to be defined on such a test, the module is
allowed to be nested as `module`, e.g:

```nix
{
  tests.dontRun = true;
  module = {config, options, ...}: {
    /* a disabled test */
  };
}
```

Also ended up doing some general cleanup, removing an unused function,
etc.
2024-06-29 22:02:44 +01:00

65 lines
1.6 KiB
Nix

{ pkgs, makeNixvimWithModule, ... }:
let
# Create a nix derivation from a nixvim executable.
# The build phase simply consists in running the provided nvim binary.
mkTestDerivationFromNvim =
{
name,
nvim,
dontRun ? false,
...
}:
pkgs.stdenv.mkDerivation {
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 =
if !dontRun then
''
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
''
else
'''';
# If we don't do this nix is not happy
installPhase = ''
mkdir $out
'';
};
# Create a nix derivation from a nixvim configuration.
# The build phase simply consists in running neovim with the given configuration.
mkTestDerivationFromNixvimModule =
{
name ? "nixvim-check",
pkgs ? pkgs,
module,
extraSpecialArgs ? { },
dontRun ? false,
}:
let
nvim = makeNixvimWithModule {
inherit pkgs module extraSpecialArgs;
_nixvimTests = true;
};
in
mkTestDerivationFromNvim { inherit name nvim dontRun; };
in
{
inherit mkTestDerivationFromNvim mkTestDerivationFromNixvimModule;
}