nixtest/lib/default.nix

51 lines
1.1 KiB
Nix
Raw Normal View History

{
pkgs,
lib ? pkgs.lib,
self ? "",
...
}: {
2025-05-03 22:05:29 +02:00
mkTest = {
type ? "unit",
name,
description ? "",
format ? "json",
2025-05-03 22:05:29 +02:00
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;
expected' =
if format == "json"
then expected
else lib.generators.toPretty {} expected;
in {
inherit type name description;
actual = actual';
expected = expected';
# discard string context, otherwise it's being built instantly which we don't want
actualDrv = builtins.unsafeDiscardStringContext (actualDrv.drvPath or "");
2025-05-03 22:05:29 +02:00
pos =
if pos == null
then ""
else "${fileRelative}:${toString pos.line}";
2025-05-03 22:05:29 +02:00
};
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
'';
}