From 10f64e6c9680dc84a5855ba2959e7ec42ef11d8b Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Sat, 29 Jun 2024 20:16:16 +0100 Subject: [PATCH] tests/test-derivation: allow tests to be modules Use `mkTestDerivationFromNixvimModule` instead of `mkTestDerivation`, allowing "proper" modules to be used instead of plain attr configs. This is useful for more complex tests that wish to use `config` or `options` arguments, e.g: ```nix {config, options, ...}: { /* some cool test */ } ``` To allow `tests.dontRun` to be defined on such a test, the module is allowed to be nested as `module`, e.g: ```nix { tests.dontRun = true; module = {config, options, ...}: { /* a disabled test */ }; } ``` Also ended up doing some general cleanup, removing an unused function, etc. --- flake-modules/tests.nix | 18 +++++++----------- flake-modules/wrappers.nix | 10 +--------- tests/default.nix | 19 +++++++++++++------ tests/test-derivation.nix | 28 +++++++--------------------- 4 files changed, 28 insertions(+), 47 deletions(-) diff --git a/flake-modules/tests.nix b/flake-modules/tests.nix index 2d6a9f5d..cd6d0adc 100644 --- a/flake-modules/tests.nix +++ b/flake-modules/tests.nix @@ -3,25 +3,21 @@ perSystem = { pkgs, - config, + pkgsUnfree, system, helpers, - makeNixvimWithModuleUnfree, makeNixvimWithModule, ... }: { checks = { tests = import ../tests { - inherit pkgs helpers makeNixvimWithModule; - inherit (pkgs) lib; - makeNixvim = - configuration: - makeNixvimWithModuleUnfree { - module = { - config = configuration; - }; - }; + inherit + pkgs + pkgsUnfree + helpers + makeNixvimWithModule + ; }; extra-args-tests = import ../tests/extra-args.nix { diff --git a/flake-modules/wrappers.nix b/flake-modules/wrappers.nix index 1d9c1f0d..da5ee081 100644 --- a/flake-modules/wrappers.nix +++ b/flake-modules/wrappers.nix @@ -14,18 +14,10 @@ let in { perSystem = - { - system, - pkgs, - pkgsUnfree, - config, - ... - }: + { system, pkgs, ... }: { _module.args = { makeNixvimWithModule = import ../wrappers/standalone.nix pkgs wrapperArgs; - - makeNixvimWithModuleUnfree = import ../wrappers/standalone.nix pkgsUnfree wrapperArgs; }; checks = diff --git a/tests/default.nix b/tests/default.nix index 1767f193..2bbdd371 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -1,14 +1,14 @@ { - makeNixvim, makeNixvimWithModule, - lib, + lib ? pkgs.lib, helpers, pkgs, + pkgsUnfree, }: let fetchTests = import ./fetch-tests.nix; - test-derivation = import ./test-derivation.nix { inherit pkgs makeNixvim makeNixvimWithModule; }; - inherit (test-derivation) mkTestDerivation; + test-derivation = import ./test-derivation.nix { inherit pkgs makeNixvimWithModule; }; + inherit (test-derivation) mkTestDerivationFromNixvimModule; # List of files containing configurations testFiles = fetchTests { @@ -31,9 +31,16 @@ let }; # We attempt to build & execute all configurations - derivationList = pkgs.lib.mapAttrsToList (name: path: { + derivationList = pkgs.lib.mapAttrsToList (name: def: { inherit name; - path = mkTestDerivation name path; + path = mkTestDerivationFromNixvimModule { + inherit name; + # The module can either be the actual definition, + # or a child attr named `module`. + module = def.module or lib.removeAttrs def [ "tests" ]; + dontRun = def.tests.dontRun or false; + pkgs = pkgsUnfree; + }; }) (testFiles // exampleFiles); in pkgs.linkFarm "nixvim-tests" derivationList diff --git a/tests/test-derivation.nix b/tests/test-derivation.nix index 23fd6b50..9c5355ee 100644 --- a/tests/test-derivation.nix +++ b/tests/test-derivation.nix @@ -1,8 +1,4 @@ -{ - pkgs, - makeNixvim, - makeNixvimWithModule, -}: +{ pkgs, makeNixvimWithModule, ... }: let # Create a nix derivation from a nixvim executable. # The build phase simply consists in running the provided nvim binary. @@ -46,12 +42,15 @@ let ''; }; + # Create a nix derivation from a nixvim configuration. + # The build phase simply consists in running neovim with the given configuration. mkTestDerivationFromNixvimModule = { name ? "nixvim-check", - pkgs, + pkgs ? pkgs, module, extraSpecialArgs ? { }, + dontRun ? false, }: let nvim = makeNixvimWithModule { @@ -59,21 +58,8 @@ let _nixvimTests = true; }; in - mkTestDerivationFromNvim { inherit name nvim; }; - - # Create a nix derivation from a nixvim configuration. - # The build phase simply consists in running neovim with the given configuration. - mkTestDerivation = - name: config: - let - testAttributes = if builtins.hasAttr "tests" config then config.tests else { dontRun = false; }; - nvim = makeNixvim (pkgs.lib.attrsets.filterAttrs (n: _: n != "tests") config); - in - mkTestDerivationFromNvim { - inherit name nvim; - inherit (testAttributes) dontRun; - }; + mkTestDerivationFromNvim { inherit name nvim dontRun; }; in { - inherit mkTestDerivation mkTestDerivationFromNvim mkTestDerivationFromNixvimModule; + inherit mkTestDerivationFromNvim mkTestDerivationFromNixvimModule; }