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: move derivation to module-option & minor cleanup #1989

Merged
merged 3 commits into from
Aug 21, 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
8 changes: 1 addition & 7 deletions flake-modules/lib.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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; })
);
}
55 changes: 23 additions & 32 deletions flake-modules/tests.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,35 @@
...
}:
{
checks =
{
extra-args-tests = import ../tests/extra-args.nix {
inherit pkgs;
inherit makeNixvimWithModule;
};
checks = {
extra-args-tests = import ../tests/extra-args.nix {
inherit pkgs;
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 {
inherit pkgs makeNixvimWithModule;
inherit (self.lib.${system}.check) mkTestDerivationFromNixvimModule;
};
enable-except-in-tests = import ../tests/enable-except-in-tests.nix {
inherit pkgs makeNixvimWithModule;
inherit (self.lib.${system}.check) mkTestDerivationFromNixvimModule;
};

no-flake = import ../tests/no-flake.nix {
inherit system;
inherit (self.lib.${system}.check) mkTestDerivationFromNvim;
nixvim = "${self}";
};
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;
};
lib-tests = import ../tests/lib-tests.nix {
inherit pkgs helpers;
inherit (pkgs) lib;
};

maintainers = import ../tests/maintainers.nix { inherit pkgs; };
maintainers = import ../tests/maintainers.nix { inherit pkgs; };

generated = pkgs.callPackage ../tests/generated.nix { };
}
// (import ../tests {
inherit
pkgs
pkgsUnfree
helpers
makeNixvimWithModule
;
});
generated = pkgs.callPackage ../tests/generated.nix { };
} // import ../tests { inherit pkgs pkgsUnfree helpers; };
};
}
3 changes: 1 addition & 2 deletions lib/default.nix
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# Args probably only needs pkgs and lib
{
makeNixvimWithModule,
pkgs,
lib ? pkgs.lib,
_nixvimTests ? false,
...
}@args:
{
# Add all exported modules here
check = import ../tests/test-derivation.nix { inherit makeNixvimWithModule lib pkgs; };
check = import ./tests.nix { inherit lib pkgs; };
helpers = import ./helpers.nix (args // { inherit _nixvimTests; });
}
2 changes: 1 addition & 1 deletion lib/modules.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}:
Expand Down
71 changes: 71 additions & 0 deletions lib/tests.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
pkgs,
lib ? pkgs.lib,
...
}:
let
# Create a nix derivation from a nixvim executable.
# The build phase simply consists in running the provided nvim binary.
mkTestDerivationFromNvim =
{
name,
nvim,
# TODO: Deprecated 2024-08-20, remove after 24.11
dontRun ? false,
...
}@args:
let
# FIXME: this doesn't support helpers.enableExceptInTests, a context option would be better
result = nvim.extend {
config.test =
{
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
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 ? null,
pkgs ? pkgs,
module,
extraSpecialArgs ? { },
# TODO: Deprecated 2024-08-20, remove after 24.11
dontRun ? false,
}@args:
let
helpers = import ../lib/helpers.nix {
inherit pkgs lib;
# TODO: deprecate helpers.enableExceptInTests,
# add a context option e.g. `config.isTest`?
_nixvimTests = true;
};

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."
{ config.test.runNvim = !dontRun; }
))
];
extraSpecialArgs = {
defaultPkgs = pkgs;
} // extraSpecialArgs;
};
in
result.config.test.derivation;
in
{
inherit mkTestDerivationFromNvim mkTestDerivationFromNixvimModule;
}
53 changes: 52 additions & 1 deletion modules/top-level/test.nix
Original file line number Diff line number Diff line change
@@ -1,10 +1,61 @@
{ 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 ];

# 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
khaneliman marked this conversation as resolved.
Show resolved Hide resolved
'';
};
};
}
3 changes: 1 addition & 2 deletions tests/default.nix
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
{
makeNixvimWithModule,
lib ? pkgs.lib,
helpers,
pkgs,
pkgsUnfree,
}:
let
fetchTests = import ./fetch-tests.nix;
test-derivation = import ./test-derivation.nix { inherit pkgs lib makeNixvimWithModule; };
test-derivation = import ../lib/tests.nix { inherit pkgs lib; };
inherit (test-derivation) mkTestDerivationFromNixvimModule;

# List of files containing configurations
Expand Down
85 changes: 0 additions & 85 deletions tests/test-derivation.nix

This file was deleted.