From 277fd1225cff11f36ef40177f5a515ff7072ab3c Mon Sep 17 00:00:00 2001 From: tristan Date: Tue, 2 Jan 2024 09:02:41 +0000 Subject: [PATCH] i made a testing lib --- flake.nix | 11 +++++++++++ testing/default.nix | 7 +++++++ testing/import.nix | 7 +++++++ testing/it.nix | 41 +++++++++++++++++++++++++++++++++++++++++ testing/run.nix | 16 ++++++++++++++++ testing/watch.nix | 10 ++++++++++ 6 files changed, 92 insertions(+) create mode 100644 flake.nix create mode 100644 testing/default.nix create mode 100644 testing/import.nix create mode 100644 testing/it.nix create mode 100644 testing/run.nix create mode 100644 testing/watch.nix 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 +'' +