Skip to content
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
3 changes: 1 addition & 2 deletions lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
lib,
flake,
_isExtended ? false,
_nixvimTests ? false,
}:
lib.makeExtensible (
self:
Expand All @@ -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
Expand Down
10 changes: 1 addition & 9 deletions lib/tests.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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; })
Expand Down
10 changes: 3 additions & 7 deletions lib/utils.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{
lib,
_nixvimTests,
}:
{ lib }:
rec {
/**
Transforms a list to an _"unkeyed"_ attribute set.
Expand All @@ -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.
Expand Down
42 changes: 36 additions & 6 deletions modules/top-level/test.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
config,
options,
lib,
helpers,
extendModules,
...
}:
let
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -323,19 +352,20 @@ in
nativeBuildInputs =
cfg.extraInputs
++ lib.optionals cfg.buildNixvim [
config.build.nvimPackage
testConfiguration.config.build.nvimPackage
];

inherit (failedExpectations) warnings assertions;

# Allow inspecting the test's module a little from the repl
# e.g.
# :lf .
# :p checks.x86_64-linux.test-1.passthru.entries.modules-autocmd.passthru.entries.example.passthru.config.extraConfigLua
# :p checks.x86_64-linux.tests.entries.modules-autocmd.entries.example.config.extraConfigLua
#
# Yes, three levels of passthru is cursed.
# Yes, three levels of `entries` is cursed.
passthru = {
inherit config options;
inherit (testConfiguration) config options;
configuration = testConfiguration;
};
}
(
Expand Down
5 changes: 2 additions & 3 deletions tests/enable-except-in-tests.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion tests/main.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 1 addition & 5 deletions wrappers/standalone.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -33,7 +29,7 @@ let
mod:
let
modules = lib.toList mod;
nixvimConfig = evalNixvim {
nixvimConfig = self.lib.evalNixvim {
modules = modules ++ [
systemMod
];
Expand Down