From f47374fd268e2c8842dde8cc08f4ab06a6026745 Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Tue, 20 Aug 2024 00:30:16 +0100 Subject: [PATCH] modules/test: init, replacing `dontRun` arg Introduces the `test.runNvim` module option. Deprecates the historic `dontRun` argument passed to the test-derivation helpers. Soft-deprecates the `tests.dontRun` attr used in tests currently. --- flake-modules/lib.nix | 2 +- lib/default.nix | 4 +-- modules/top-level/default.nix | 1 + modules/top-level/test.nix | 10 +++++++ tests/default.nix | 23 +++++++++++---- tests/test-derivation.nix | 54 ++++++++++++++++++++++++----------- 6 files changed, 69 insertions(+), 25 deletions(-) create mode 100644 modules/top-level/test.nix diff --git a/flake-modules/lib.nix b/flake-modules/lib.nix index 4deda797..b5f646c4 100644 --- a/flake-modules/lib.nix +++ b/flake-modules/lib.nix @@ -10,7 +10,7 @@ { pkgs, config, ... }: import ../lib { inherit pkgs lib; - inherit (config.legacyPackages) makeNixvim makeNixvimWithModule; + inherit (config.legacyPackages) makeNixvimWithModule; } ) ); diff --git a/lib/default.nix b/lib/default.nix index df862730..d247ec6e 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -1,13 +1,13 @@ # Args probably only needs pkgs and lib { - makeNixvim, makeNixvimWithModule, pkgs, + lib ? pkgs.lib, _nixvimTests ? false, ... }@args: { # Add all exported modules here - check = import ../tests/test-derivation.nix { inherit makeNixvim makeNixvimWithModule pkgs; }; + check = import ../tests/test-derivation.nix { inherit makeNixvimWithModule lib pkgs; }; helpers = import ./helpers.nix (args // { inherit _nixvimTests; }); } diff --git a/modules/top-level/default.nix b/modules/top-level/default.nix index bac6bde0..581a1e6e 100644 --- a/modules/top-level/default.nix +++ b/modules/top-level/default.nix @@ -8,5 +8,6 @@ ../. ./files ./output.nix + ./test.nix ]; } diff --git a/modules/top-level/test.nix b/modules/top-level/test.nix new file mode 100644 index 00000000..a2dda021 --- /dev/null +++ b/modules/top-level/test.nix @@ -0,0 +1,10 @@ +{ lib, ... }: +{ + options.test = { + runNvim = lib.mkOption { + type = lib.types.bool; + description = "Whether to run `nvim` in the test."; + default = true; + }; + }; +} diff --git a/tests/default.nix b/tests/default.nix index 6db20386..df0c4901 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -7,7 +7,7 @@ }: let fetchTests = import ./fetch-tests.nix; - test-derivation = import ./test-derivation.nix { inherit pkgs makeNixvimWithModule; }; + test-derivation = import ./test-derivation.nix { inherit pkgs lib makeNixvimWithModule; }; inherit (test-derivation) mkTestDerivationFromNixvimModule; # List of files containing configurations @@ -43,16 +43,29 @@ lib.pipe (testFiles ++ [ exampleFiles ]) [ let # The test case can either be the actual definition, # or a child attr named `module`. - prepareModule = case: case.module or (lib.removeAttrs case [ "tests" ]); - dontRunModule = case: case.tests.dontRun or false; + prepareModule = + case: if lib.isFunction case then case else case.module or (lib.removeAttrs case [ "tests" ]); + + # Convert legacy `dontRun` definitions into `test.runNvim` option defs + dontRunModule = + case: + let + dontRun = case.tests.dontRun or false; + in + lib.optionalAttrs dontRun { test.runNvim = false; }; + mkTest = { name, case }: { inherit name; path = mkTestDerivationFromNixvimModule { inherit name; - module = prepareModule case; - dontRun = dontRunModule case; + module = { + imports = [ + (dontRunModule case) + (prepareModule case) + ]; + }; pkgs = pkgsUnfree; }; }; diff --git a/tests/test-derivation.nix b/tests/test-derivation.nix index 9c5355ee..7e6d2836 100644 --- a/tests/test-derivation.nix +++ b/tests/test-derivation.nix @@ -1,4 +1,9 @@ -{ pkgs, makeNixvimWithModule, ... }: +{ + pkgs, + lib ? pkgs.lib, + makeNixvimWithModule, + ... +}: let # Create a nix derivation from a nixvim executable. # The build phase simply consists in running the provided nvim binary. @@ -6,9 +11,17 @@ let { name, nvim, + # TODO: Deprecated 2024-08-20, remove after 24.11 dontRun ? false, ... - }: + }@args: + let + cfg = nvim.config.test; + runNvim = + lib.warnIf (args ? dontRun) + "mkTestDerivationFromNvim: the `dontRun` argument is deprecated. You should use the `test.runNvim` module option instead." + (cfg.runNvim && !dontRun); + in pkgs.stdenv.mkDerivation { inherit name; @@ -22,19 +35,15 @@ let # # 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 + buildPhase = lib.optionalString 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 - '' - else - ''''; + 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 = '' @@ -50,15 +59,26 @@ let pkgs ? pkgs, module, extraSpecialArgs ? { }, + # TODO: Deprecated 2024-08-20, remove after 24.11 dontRun ? false, - }: + }@args: let nvim = makeNixvimWithModule { - inherit pkgs module extraSpecialArgs; + inherit pkgs extraSpecialArgs; _nixvimTests = true; + module = + if args ? dontRun then + lib.warn + "mkTestDerivationFromNixvimModule: the `dontRun` argument is deprecated. You should use the `test.runNvim` module option instead." + { + imports = [ module ]; + config.test.runNvim = !dontRun; + } + else + module; }; in - mkTestDerivationFromNvim { inherit name nvim dontRun; }; + mkTestDerivationFromNvim { inherit name nvim; }; in { inherit mkTestDerivationFromNvim mkTestDerivationFromNixvimModule;