i made a testing lib

This commit is contained in:
tristan 2024-01-02 09:02:41 +00:00
commit 277fd1225c
6 changed files with 92 additions and 0 deletions

11
flake.nix Normal file
View file

@ -0,0 +1,11 @@
{
description = "A testing library for nix";
outputs = { self, nixpkgs }:
let
system = "x86_64-linux";
pkgs = import nixpkgs { inherit system; };
testing = import ./testing/. { inherit pkgs; };
in
testing;
}

7
testing/default.nix Normal file
View file

@ -0,0 +1,7 @@
{ pkgs ? import <nixpkgs> {} }:
{
run = import ./run.nix pkgs;
it = import ./it.nix;
import = import ./import.nix;
watch = import ./watch.nix pkgs;
}

7
testing/import.nix Normal file
View file

@ -0,0 +1,7 @@
path:
builtins.trace ("testing " + builtins.baseNameOf path)
{
inherit path;
results = (import path);
}

41
testing/it.nix Normal file
View file

@ -0,0 +1,41 @@
msg:
{ actual, expected ? { }, asString ? false, asJSON ? false
, removeDunders ? false, safeToPrint ? true, throws ? false }:
let
preProcess = v:
if removeDunders then
undunder v
else if asString then
toString v
else if asJSON then
builtins.toJSON v
else
v;
a = preProcess actual;
e = preProcess expected;
undunder = v:
if builtins.isAttrs v then
builtins.removeAttrs v [ "__toString" "__functor" ]
else
v;
out = (if safeToPrint then
builtins.toJSON (undunder (if throws then
(builtins.tryEval actual).value
else {
inherit actual expected;
}))
else
''{"msg": "cannot be stringified ):"}'');
success =
if throws then (builtins.tryEval actual).success == false else (a == e);
in {
inherit success msg;
} // (if success then
{ }
else
builtins.trace "FAILED ${msg}" { inherit actual expected; })

16
testing/run.nix Normal file
View file

@ -0,0 +1,16 @@
pkgs: files:
let
test = import ./import.nix;
res = map test files;
resFile = builtins.toFile "results" (builtins.toJSON res);
filter = ''
.[] | {
(.path): {
failures: [.results | map(select(.success==false))[] | {(.msg): {actual, expected}}],
success: .results | map(select(.success) | .msg) | length
}
}
'';
in (pkgs.writeShellScriptBin "test" ''
cat '${resFile}' | ${pkgs.jq}/bin/jq '${filter}' | ${pkgs.fx}/bin/fx
'')

10
testing/watch.nix Normal file
View file

@ -0,0 +1,10 @@
pkgs: cmd: pname:
pkgs.writeShellScriptBin "watch" ''
while true
do
${cmd} &
${pkgs.inotify-tools}/bin/inotifywait -e modify -r . --exclude .git
pkill ${pname}
done
''