diff --git a/flake.nix b/flake.nix index fae18e3..3593cd4 100644 --- a/flake.nix +++ b/flake.nix @@ -18,9 +18,22 @@ ./test/test.test.nix ./test/describe.test.nix ./test/watch.test.nix - ({describe, it, ...}: [(describe "suite" [ - (it "runs this test" {actual = true; expected = true;}) - ])]) + ({ + describe, + it, + ... + }: [ + (describe "suite" [ + (it "runs this test" { + actual = true; + expected = true; + }) + ]) + (it "runs another test" { + actual = true; + expected = true; + }) + ]) ]; packages.x86_64-linux.example = tix.run [ @@ -28,13 +41,15 @@ ]; packages.x86_64-linux.watchpipe = tix.watch { - cmd = "nix run --show-trace .#test file"; + cmd = "nix run --show-trace .#test -- file"; view = "tee -a /tmp/tix-output"; }; packages.x86_64-linux.watch = tix.watch { - cmd = "nix run --show-trace .#test $2"; - view = "cat"; + cmd = "nix run --show-trace .#test -- $2"; + view = '' + ${pkgs.lib.getExe pkgs.bat} \ + --paging=never --language=''${2:-json} --file-name="test results"''; }; packages.x86_64-linux.results = pkgs.writeShellScriptBin "results" '' diff --git a/test/test.test.nix b/test/test.test.nix index 5509fd0..9a5c9c5 100644 --- a/test/test.test.nix +++ b/test/test.test.nix @@ -7,16 +7,7 @@ in [ (describe "the test function" [ (it "get's test results" { - expected = { - name = builtins.baseNameOf ./example.test.nix; - value = { - "an example" = { - succeeds = { - success = true; - }; - }; - }; - }; + expected."an example".succeeds.success = true; actual = test ./example.test.nix; }) (it "fails to build non test" { @@ -25,7 +16,7 @@ in [ }) (it "tests suite that is not a file" { actual = test ({...}: []); - expected = {name = "anonymous"; value = {};}; + expected = {}; }) ]) ] diff --git a/tix/emotes.nix b/tix/emotes.nix index c8eec46..2007f25 100644 --- a/tix/emotes.nix +++ b/tix/emotes.nix @@ -1,4 +1,4 @@ { - pass = "✔️"; - fail = "❌"; + pass = "✅"; + fail = "❎"; } diff --git a/tix/filters.nix b/tix/filters.nix index 5dd1f7c..2f17916 100644 --- a/tix/filters.nix +++ b/tix/filters.nix @@ -8,9 +8,7 @@ in rec { (.msg): select(.success == false) | {expected, actual, success} }] | add }] | add - }] | add - ''; + }] | add''; overview = '' - . - ''; + .''; } diff --git a/tix/run.nix b/tix/run.nix index 9068f19..18f2e34 100644 --- a/tix/run.nix +++ b/tix/run.nix @@ -1,22 +1,38 @@ pkgs: suite: let test = import ./test.nix; - res = builtins.listToAttrs (map test suite); + res = builtins.listToAttrs (pkgs.lib.imap0 ( + i: t: let + name = + if builtins.typeOf t == "path" + then builtins.baseNameOf t + else "test suite ${toString i} (anonymous)"; + in { + inherit name; + value = test t; + } + ) + suite); resFile = builtins.toFile "results" (builtins.toJSON res); filters = import ./filters.nix; getNix = "import ${./eval.nix} ${resFile}"; - extractNix = "nix eval --impure --quiet --expr 'let r = ${getNix}; in builtins.trace r r'"; - nixRepl = "nix repl --expr '{ r = ${getNix}; }'"; + opts = { + nix-raw = "nix eval --impure --quiet --expr '${getNix}'"; + nix-repl = "nix repl --expr '{ r = ${getNix}; }'"; + file = "echo '${resFile}'"; + json-raw = "cat '${resFile}'"; + json = "${pkgs.jq}/bin/jq '.' '${resFile}'"; + explore = "${pkgs.fx}/bin/fx '${resFile}'"; + nix = "${opts.nix-raw} | ${pkgs.alejandra}/bin/alejandra -q"; + overview = "${pkgs.jq}/bin/jq '${filters.overview}' '${resFile}'"; + }; in (pkgs.writeShellScriptBin "test" '' - t=''${1:-overview} + t=''${1:-json} case "$t" in - "file") echo '${resFile}' ;; - "raw") cat '${resFile}' ;; - "json") ${pkgs.jq}/bin/jq '.' '${resFile}' ;; - "explore") ${pkgs.fx}/bin/fx '${resFile}' ;; - "nix") ${extractNix} | ${pkgs.alejandra}/bin/alejandra -q ;; - "nixrepl") ${nixRepl} ;; - "nixraw") ${extractNix} ;; - "overview") ${pkgs.jq}/bin/jq '${filters.overview}' '${resFile}' ;; - *) echo format "$t" not recognised! ;; + ${pkgs.lib.pipe opts [ + (builtins.mapAttrs (name: cmd: ''"${name}") ${cmd}'')) + (builtins.attrValues) + (builtins.concatStringsSep " ;;\n ") + ]};; + *) echo options: ${builtins.concatStringsSep ", " (builtins.attrNames opts)} ;; esac '') diff --git a/tix/test.nix b/tix/test.nix index cc81c76..cc81a09 100644 --- a/tix/test.nix +++ b/tix/test.nix @@ -1,17 +1,12 @@ path: let it = import ./it.nix; describe = import ./describe.nix; - suite = if builtins.typeOf path == "path" then import path + suite = + if builtins.typeOf path == "path" + then import path else path; - name = if builtins.typeOf path == "path" then builtins.baseNameOf path - else "anonymous"; -in - builtins.trace "tix: ${name}" - ( - if !builtins.isFunction suite - then throw "A test suite should be in the form { it }: [ ]." - else { - inherit name; - value = builtins.listToAttrs (suite {inherit it describe;}); - } - ) +in ( + if !builtins.isFunction suite + then throw "A test suite should be in the form { it, describe }: [ ]." + else builtins.listToAttrs (suite {inherit it describe;}) +)