Skip to content

Commit 8d0ca9a

Browse files
committed
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.
1 parent 7754b4e commit 8d0ca9a

File tree

7 files changed

+57
-31
lines changed

7 files changed

+57
-31
lines changed

lib/default.nix

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
lib,
33
flake,
44
_isExtended ? false,
5-
_nixvimTests ? false,
65
}:
76
lib.makeExtensible (
87
self:
@@ -25,7 +24,7 @@ lib.makeExtensible (
2524
modules = call ./modules.nix { inherit flake; };
2625
options = call ./options.nix { };
2726
plugins = call ./plugins { };
28-
utils = call ./utils.nix { inherit _nixvimTests; } // call ./utils.internal.nix { };
27+
utils = call ./utils.nix { } // call ./utils.internal.nix { };
2928

3029
# Top-level helper aliases:
3130
# TODO: deprecate some aliases

lib/tests.nix

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ let
1515
...
1616
}:
1717
let
18-
# FIXME: this doesn't support helpers.enableExceptInTests, a context option would be better
1918
result = nvim.extend {
2019
config.test = {
2120
inherit name;
@@ -35,20 +34,13 @@ let
3534
extraSpecialArgs ? { },
3635
}:
3736
let
38-
# NOTE: we are importing this just for evalNixvim
39-
helpers = self.lib.nixvim.override {
40-
# TODO: deprecate helpers.enableExceptInTests,
41-
# add a context option e.g. `config.isTest`?
42-
_nixvimTests = true;
43-
};
44-
4537
systemMod =
4638
if pkgs == null then
4739
{ nixpkgs.hostPlatform = lib.mkDefault { inherit system; }; }
4840
else
4941
{ nixpkgs.pkgs = lib.mkDefault pkgs; };
5042

51-
result = helpers.modules.evalNixvim {
43+
result = self.lib.evalNixvim {
5244
modules = [
5345
module
5446
(lib.optionalAttrs (name != null) { test.name = name; })

lib/utils.nix

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
{
2-
lib,
3-
_nixvimTests,
4-
}:
1+
{ lib }:
52
rec {
63
/**
74
Transforms a list to an _"unkeyed"_ attribute set.
@@ -23,15 +20,14 @@ rec {
2320
builtins.listToAttrs (lib.lists.imap0 (idx: lib.nameValuePair "__unkeyed-${toString idx}") list);
2421

2522
/**
26-
Usually `true`, except when nixvim is being evaluated by
27-
`mkTestDerivationFromNixvimModule`, where it is `false`.
23+
Usually `true`, except within the `build.test` option, where it is `false`.
2824
2925
This can be used to dynamically enable plugins that can't be run in the
3026
test environment.
3127
*/
3228
# TODO: replace and deprecate
3329
# We shouldn't need to use another instance of `lib` when building a test drv
34-
enableExceptInTests = !_nixvimTests;
30+
enableExceptInTests = true;
3531

3632
/**
3733
An empty lua table `{ }` that will be included in the final lua configuration.

modules/top-level/test.nix

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
config,
44
options,
55
lib,
6+
helpers,
7+
extendModules,
68
...
79
}:
810
let
@@ -263,9 +265,36 @@ in
263265

264266
config =
265267
let
268+
# Reevaluate the nixvim configuration in "test mode"
269+
# This allows users to use `lib.nixvim.enableExceptInTests`
270+
testConfiguration =
271+
let
272+
# TODO: replace and deprecate enableExceptInTests
273+
# We shouldn't need to use another instance of `lib` when building a test drv
274+
# Maybe add a context option e.g. `config.isTest`?
275+
nixvimLibOverlay = final: prev: {
276+
utils = prev.utils // {
277+
enableExceptInTests = false;
278+
};
279+
};
280+
extendedConfiguration = extendModules {
281+
specialArgs = {
282+
lib = lib.extend (
283+
final: prev: {
284+
nixvim = prev.nixvim.extend nixvimLibOverlay;
285+
}
286+
);
287+
helpers = helpers.extend nixvimLibOverlay;
288+
};
289+
};
290+
in
291+
if lib.nixvim.enableExceptInTests then extendedConfiguration else { inherit config options; };
292+
266293
input = {
267-
inherit (config) warnings;
268-
assertions = builtins.concatMap (x: lib.optional (!x.assertion) x.message) config.assertions;
294+
inherit (testConfiguration.config) warnings;
295+
assertions = builtins.concatMap (
296+
x: lib.optional (!x.assertion) x.message
297+
) testConfiguration.config.assertions;
269298
};
270299

271300
expectationMessages =
@@ -323,7 +352,7 @@ in
323352
nativeBuildInputs =
324353
cfg.extraInputs
325354
++ lib.optionals cfg.buildNixvim [
326-
config.build.nvimPackage
355+
testConfiguration.config.build.nvimPackage
327356
];
328357

329358
inherit (failedExpectations) warnings assertions;
@@ -335,7 +364,8 @@ in
335364
#
336365
# Yes, three levels of `entries` is cursed.
337366
passthru = {
338-
inherit config options;
367+
inherit (testConfiguration) config options;
368+
configuration = testConfiguration;
339369
};
340370
}
341371
(

tests/enable-except-in-tests.nix

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ let
1515
{
1616
assertions = [
1717
{
18-
# FIXME: should be false
19-
assertion = lib.nixvim.enableExceptInTests;
20-
message = "Expected lib.nixvim.enableExceptInTests to be true";
18+
assertion = !lib.nixvim.enableExceptInTests;
19+
message = "Expected lib.nixvim.enableExceptInTests to be false";
2120
}
2221
{
2322
# NOTE: evaluating `helpers` here prints an eval warning

tests/main.nix

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,24 @@
99
let
1010
fetchTests = callTest ./fetch-tests.nix { };
1111

12+
# Avoid `build.test` re-evaluating its nixvim configuration by providing a
13+
# "test mode" lib from the start
14+
testLib = lib.extend (
15+
final: prev: {
16+
nixvim = prev.nixvim.extend (
17+
final: prev: {
18+
utils = prev.utils // {
19+
enableExceptInTests = false;
20+
};
21+
}
22+
);
23+
}
24+
);
25+
1226
moduleToTest =
1327
file: name: module:
1428
let
15-
configuration = lib.nixvim.modules.evalNixvim {
29+
configuration = testLib.nixvim.modules.evalNixvim {
1630
modules = [
1731
{
1832
test.name = lib.mkDefault name;

wrappers/standalone.nix

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,10 @@
99
# NOTE: `defaultSystem` is the only reason this function can't go in `<nixvim>.lib`
1010
system ? defaultSystem,
1111
extraSpecialArgs ? { },
12-
_nixvimTests ? false,
1312
module,
1413
}:
1514
let
1615
# NOTE: we are importing this just for evalNixvim
17-
helpers = self.lib.nixvim.override { inherit _nixvimTests; };
18-
inherit (helpers.modules) evalNixvim;
19-
2016
systemMod =
2117
if pkgs == null then
2218
{
@@ -33,7 +29,7 @@ let
3329
mod:
3430
let
3531
modules = lib.toList mod;
36-
nixvimConfig = evalNixvim {
32+
nixvimConfig = self.lib.evalNixvim {
3733
modules = modules ++ [
3834
systemMod
3935
];

0 commit comments

Comments
 (0)