commit 277fd1225cff11f36ef40177f5a515ff7072ab3c Author: tristan Date: Tue Jan 2 09:02:41 2024 +0000 i made a testing lib diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..1ba1fae --- /dev/null +++ b/flake.nix @@ -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; +} diff --git a/testing/default.nix b/testing/default.nix new file mode 100644 index 0000000..ce8bc15 --- /dev/null +++ b/testing/default.nix @@ -0,0 +1,7 @@ +{ pkgs ? import {} }: +{ + run = import ./run.nix pkgs; + it = import ./it.nix; + import = import ./import.nix; + watch = import ./watch.nix pkgs; +} diff --git a/testing/import.nix b/testing/import.nix new file mode 100644 index 0000000..f3d3cc7 --- /dev/null +++ b/testing/import.nix @@ -0,0 +1,7 @@ +path: +builtins.trace ("testing " + builtins.baseNameOf path) + +{ + inherit path; + results = (import path); +} diff --git a/testing/it.nix b/testing/it.nix new file mode 100644 index 0000000..a3cb9d0 --- /dev/null +++ b/testing/it.nix @@ -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; }) diff --git a/testing/run.nix b/testing/run.nix new file mode 100644 index 0000000..7c214bb --- /dev/null +++ b/testing/run.nix @@ -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 +'') diff --git a/testing/watch.nix b/testing/watch.nix new file mode 100644 index 0000000..dfec78d --- /dev/null +++ b/testing/watch.nix @@ -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 +'' +