diff --git a/flake-modules/lib.nix b/flake-modules/lib.nix index b5f646c4..142ad436 100644 --- a/flake-modules/lib.nix +++ b/flake-modules/lib.nix @@ -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; }) ); } diff --git a/flake-modules/tests.nix b/flake-modules/tests.nix index f7c89c72..27755fb8 100644 --- a/flake-modules/tests.nix +++ b/flake-modules/tests.nix @@ -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; }; }; } diff --git a/lib/default.nix b/lib/default.nix index d247ec6e..0d7788f6 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -1,6 +1,5 @@ # Args probably only needs pkgs and lib { - makeNixvimWithModule, pkgs, lib ? pkgs.lib, _nixvimTests ? false, @@ -8,6 +7,6 @@ }@args: { # Add all exported modules here - check = import ../tests/test-derivation.nix { inherit makeNixvimWithModule lib pkgs; }; + check = import ../tests/test-derivation.nix { inherit lib pkgs; }; helpers = import ./helpers.nix (args // { inherit _nixvimTests; }); } diff --git a/lib/modules.nix b/lib/modules.nix index b6403de8..f58dee7b 100644 --- a/lib/modules.nix +++ b/lib/modules.nix @@ -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, }: diff --git a/modules/top-level/test.nix b/modules/top-level/test.nix index a2dda021..42a3f74e 100644 --- a/modules/top-level/test.nix +++ b/modules/top-level/test.nix @@ -1,10 +1,64 @@ -{ 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 + pkgs.docker-client + ]; + + # 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 + ''; + }; }; } diff --git a/tests/default.nix b/tests/default.nix index ff17d7fd..14a68cde 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -1,5 +1,4 @@ { - makeNixvimWithModule, lib ? pkgs.lib, helpers, pkgs, @@ -7,7 +6,7 @@ }: let fetchTests = import ./fetch-tests.nix; - test-derivation = import ./test-derivation.nix { inherit pkgs lib makeNixvimWithModule; }; + test-derivation = import ./test-derivation.nix { inherit pkgs lib; }; inherit (test-derivation) mkTestDerivationFromNixvimModule; # List of files containing configurations diff --git a/tests/test-derivation.nix b/tests/test-derivation.nix index 7e6d2836..8face090 100644 --- a/tests/test-derivation.nix +++ b/tests/test-derivation.nix @@ -1,7 +1,6 @@ { pkgs, lib ? pkgs.lib, - makeNixvimWithModule, ... }: let @@ -16,46 +15,26 @@ let ... }@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); + # 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 - pkgs.stdenv.mkDerivation { - inherit name; - - nativeBuildInputs = [ - nvim - pkgs.docker-client - ]; - - dontUnpack = true; - # 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 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 = '' - mkdir $out - ''; - }; + 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 ? "nixvim-check", + name ? null, pkgs ? pkgs, module, extraSpecialArgs ? { }, @@ -63,22 +42,29 @@ let dontRun ? false, }@args: let - nvim = makeNixvimWithModule { - inherit pkgs extraSpecialArgs; + helpers = import ../lib/helpers.nix { + inherit pkgs lib; + # TODO: deprecate helpers.enableExceptInTests, + # add a context option e.g. `config.isTest`? _nixvimTests = true; - module = - if args ? dontRun then + }; + + 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." - { - imports = [ module ]; - config.test.runNvim = !dontRun; - } - else - module; + { config.test.runNvim = !dontRun; } + )) + ]; + extraSpecialArgs = { + defaultPkgs = pkgs; + } // extraSpecialArgs; }; in - mkTestDerivationFromNvim { inherit name nvim; }; + result.config.test.derivation; in { inherit mkTestDerivationFromNvim mkTestDerivationFromNixvimModule;