clanLib: init flakeModules for better testing

This commit is contained in:
Johannes Kirschbauer
2025-04-21 16:09:14 +02:00
parent f7b12d8bda
commit b791656694
2 changed files with 59 additions and 0 deletions

View File

@@ -6,6 +6,9 @@ let
; ;
in in
{ {
flakeModules = clanLib.callLib ./flakeModules.nix { };
#
minifyModule = ./minify.nix; minifyModule = ./minify.nix;
sopsModule = ./sops.nix; sopsModule = ./sops.nix;
# A function that returns an extension to runTest # A function that returns an extension to runTest

56
lib/test/flakeModules.nix Normal file
View File

@@ -0,0 +1,56 @@
{ lib, clanLib }:
{
/**
This function is used to create a module that can be imported into flake-parts
and used to run eval tests.
Usage: `lib.modules.importApply makeEvalChecks { module = module-under-test; testArgs = { }; }`
returns a module to be imported into flake-parts
Which in turn adds to the flake:
- legacyPackages.<system>.eval-tests-<testName>: The attribute set passed to nix-unit. (Exposed for debugging i.e. via nix repl).
- checks.<system>.eval-tests-<testName>: A derivation that can be built and fails if nix-unit fails
*/
makeEvalChecks =
{
self,
inputs,
testName,
tests,
module,
testArgs ? { },
}:
let
inputOverrides = builtins.concatStringsSep " " (
builtins.map (input: " --override-input ${input} ${inputs.${input}}") (builtins.attrNames inputs)
);
attrName = "eval-tests-${testName}";
in
{
pkgs,
system,
...
}:
{
legacyPackages.${attrName} = import tests (
{
inherit clanLib lib module;
}
// testArgs
);
checks.${attrName} = pkgs.runCommand "tests" { nativeBuildInputs = [ pkgs.nix-unit ]; } ''
export HOME="$(realpath .)"
nix-unit --eval-store "$HOME" \
--extra-experimental-features flakes \
--show-trace \
${inputOverrides} \
--flake ${self}#legacyPackages.${system}.${attrName}
touch $out
'';
};
}