nixtest/lib/default.nix
technofab e029fae0b8 feat: add support for pretty/nix format
improve handling string test values
2025-05-10 21:55:08 +02:00

45 lines
1 KiB
Nix

{
pkgs,
lib ? pkgs.lib,
self ? "",
...
}: {
mkTest = {
type ? "unit",
name,
description ? "",
format ? "json",
expected ? null,
actual ? null,
actualDrv ? null,
pos ? null,
}: let
fileRelative = lib.removePrefix ((toString self) + "/") pos.file;
actual' =
if format == "json"
then actual
else lib.generators.toPretty {} actual;
in {
inherit type name description expected;
actual = actual';
# discard string context, otherwise it's being built instantly which we don't want
actualDrv = builtins.unsafeDiscardStringContext (actualDrv.drvPath or "");
pos =
if pos == null
then ""
else "${fileRelative}:${toString pos.line}:${toString pos.column}";
};
mkSuite = name: tests: {
inherit name tests;
};
exportSuites = suites: let
suitesList =
if builtins.isList suites
then suites
else [suites];
testsMapped = builtins.toJSON suitesList;
in
pkgs.runCommand "tests.json" {} ''
echo '${testsMapped}' > $out
'';
}