1
0
Fork 0
mirror of https://github.com/nix-community/nixvim.git synced 2025-11-21 17:59:41 +01:00

lib: move enableExceptInTests impl to build.test option

Simplify the `enableExceptInTests` attribute, removing the
`_nixvimTests` argument.

We now do a full re-eval of the nixvim configuration before building the
test, giving us a central place to implement `enableExceptInTests` and
its eventual replacement(s).

This extends support for `enableExceptInTests` to all methods of getting
a nixvim test derivation. Previously, it only worked when using `mkTestDerivationFromNixvimModule`.

In `tests/main.nix`, we avoid the re-eval by doing the initial eval with
a "test mode" lib from the start.
This commit is contained in:
Matt Sturgeon 2025-11-19 07:07:02 +00:00
parent 7754b4eb1b
commit 8d0ca9abc4
7 changed files with 57 additions and 31 deletions

View file

@ -2,7 +2,6 @@
lib,
flake,
_isExtended ? false,
_nixvimTests ? false,
}:
lib.makeExtensible (
self:
@ -25,7 +24,7 @@ lib.makeExtensible (
modules = call ./modules.nix { inherit flake; };
options = call ./options.nix { };
plugins = call ./plugins { };
utils = call ./utils.nix { inherit _nixvimTests; } // call ./utils.internal.nix { };
utils = call ./utils.nix { } // call ./utils.internal.nix { };
# Top-level helper aliases:
# TODO: deprecate some aliases

View file

@ -15,7 +15,6 @@ let
...
}:
let
# FIXME: this doesn't support helpers.enableExceptInTests, a context option would be better
result = nvim.extend {
config.test = {
inherit name;
@ -35,20 +34,13 @@ let
extraSpecialArgs ? { },
}:
let
# NOTE: we are importing this just for evalNixvim
helpers = self.lib.nixvim.override {
# TODO: deprecate helpers.enableExceptInTests,
# add a context option e.g. `config.isTest`?
_nixvimTests = true;
};
systemMod =
if pkgs == null then
{ nixpkgs.hostPlatform = lib.mkDefault { inherit system; }; }
else
{ nixpkgs.pkgs = lib.mkDefault pkgs; };
result = helpers.modules.evalNixvim {
result = self.lib.evalNixvim {
modules = [
module
(lib.optionalAttrs (name != null) { test.name = name; })

View file

@ -1,7 +1,4 @@
{
lib,
_nixvimTests,
}:
{ lib }:
rec {
/**
Transforms a list to an _"unkeyed"_ attribute set.
@ -23,15 +20,14 @@ rec {
builtins.listToAttrs (lib.lists.imap0 (idx: lib.nameValuePair "__unkeyed-${toString idx}") list);
/**
Usually `true`, except when nixvim is being evaluated by
`mkTestDerivationFromNixvimModule`, where it is `false`.
Usually `true`, except within the `build.test` option, where it is `false`.
This can be used to dynamically enable plugins that can't be run in the
test environment.
*/
# TODO: replace and deprecate
# We shouldn't need to use another instance of `lib` when building a test drv
enableExceptInTests = !_nixvimTests;
enableExceptInTests = true;
/**
An empty lua table `{ }` that will be included in the final lua configuration.

View file

@ -3,6 +3,8 @@
config,
options,
lib,
helpers,
extendModules,
...
}:
let
@ -263,9 +265,36 @@ in
config =
let
# Reevaluate the nixvim configuration in "test mode"
# This allows users to use `lib.nixvim.enableExceptInTests`
testConfiguration =
let
# TODO: replace and deprecate enableExceptInTests
# We shouldn't need to use another instance of `lib` when building a test drv
# Maybe add a context option e.g. `config.isTest`?
nixvimLibOverlay = final: prev: {
utils = prev.utils // {
enableExceptInTests = false;
};
};
extendedConfiguration = extendModules {
specialArgs = {
lib = lib.extend (
final: prev: {
nixvim = prev.nixvim.extend nixvimLibOverlay;
}
);
helpers = helpers.extend nixvimLibOverlay;
};
};
in
if lib.nixvim.enableExceptInTests then extendedConfiguration else { inherit config options; };
input = {
inherit (config) warnings;
assertions = builtins.concatMap (x: lib.optional (!x.assertion) x.message) config.assertions;
inherit (testConfiguration.config) warnings;
assertions = builtins.concatMap (
x: lib.optional (!x.assertion) x.message
) testConfiguration.config.assertions;
};
expectationMessages =
@ -323,7 +352,7 @@ in
nativeBuildInputs =
cfg.extraInputs
++ lib.optionals cfg.buildNixvim [
config.build.nvimPackage
testConfiguration.config.build.nvimPackage
];
inherit (failedExpectations) warnings assertions;
@ -335,7 +364,8 @@ in
#
# Yes, three levels of `entries` is cursed.
passthru = {
inherit config options;
inherit (testConfiguration) config options;
configuration = testConfiguration;
};
}
(

View file

@ -15,9 +15,8 @@ let
{
assertions = [
{
# FIXME: should be false
assertion = lib.nixvim.enableExceptInTests;
message = "Expected lib.nixvim.enableExceptInTests to be true";
assertion = !lib.nixvim.enableExceptInTests;
message = "Expected lib.nixvim.enableExceptInTests to be false";
}
{
# NOTE: evaluating `helpers` here prints an eval warning

View file

@ -9,10 +9,24 @@
let
fetchTests = callTest ./fetch-tests.nix { };
# Avoid `build.test` re-evaluating its nixvim configuration by providing a
# "test mode" lib from the start
testLib = lib.extend (
final: prev: {
nixvim = prev.nixvim.extend (
final: prev: {
utils = prev.utils // {
enableExceptInTests = false;
};
}
);
}
);
moduleToTest =
file: name: module:
let
configuration = lib.nixvim.modules.evalNixvim {
configuration = testLib.nixvim.modules.evalNixvim {
modules = [
{
test.name = lib.mkDefault name;

View file

@ -9,14 +9,10 @@
# NOTE: `defaultSystem` is the only reason this function can't go in `<nixvim>.lib`
system ? defaultSystem,
extraSpecialArgs ? { },
_nixvimTests ? false,
module,
}:
let
# NOTE: we are importing this just for evalNixvim
helpers = self.lib.nixvim.override { inherit _nixvimTests; };
inherit (helpers.modules) evalNixvim;
systemMod =
if pkgs == null then
{
@ -33,7 +29,7 @@ let
mod:
let
modules = lib.toList mod;
nixvimConfig = evalNixvim {
nixvimConfig = self.lib.evalNixvim {
modules = modules ++ [
systemMod
];