add test help text

This commit is contained in:
tristan 2024-12-08 21:27:18 +00:00
parent bfa12db1db
commit cf125cc808
6 changed files with 64 additions and 49 deletions

View file

@ -18,9 +18,22 @@
./test/test.test.nix ./test/test.test.nix
./test/describe.test.nix ./test/describe.test.nix
./test/watch.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 [ packages.x86_64-linux.example = tix.run [
@ -28,13 +41,15 @@
]; ];
packages.x86_64-linux.watchpipe = tix.watch { 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"; view = "tee -a /tmp/tix-output";
}; };
packages.x86_64-linux.watch = tix.watch { packages.x86_64-linux.watch = tix.watch {
cmd = "nix run --show-trace .#test $2"; cmd = "nix run --show-trace .#test -- $2";
view = "cat"; view = ''
${pkgs.lib.getExe pkgs.bat} \
--paging=never --language=''${2:-json} --file-name="test results"'';
}; };
packages.x86_64-linux.results = pkgs.writeShellScriptBin "results" '' packages.x86_64-linux.results = pkgs.writeShellScriptBin "results" ''

View file

@ -7,16 +7,7 @@
in [ in [
(describe "the test function" [ (describe "the test function" [
(it "get's test results" { (it "get's test results" {
expected = { expected."an example".succeeds.success = true;
name = builtins.baseNameOf ./example.test.nix;
value = {
"an example" = {
succeeds = {
success = true;
};
};
};
};
actual = test ./example.test.nix; actual = test ./example.test.nix;
}) })
(it "fails to build non test" { (it "fails to build non test" {
@ -25,7 +16,7 @@ in [
}) })
(it "tests suite that is not a file" { (it "tests suite that is not a file" {
actual = test ({...}: []); actual = test ({...}: []);
expected = {name = "anonymous"; value = {};}; expected = {};
}) })
]) ])
] ]

View file

@ -1,4 +1,4 @@
{ {
pass = ""; pass = "";
fail = ""; fail = "";
} }

View file

@ -8,9 +8,7 @@ in rec {
(.msg): select(.success == false) | {expected, actual, success} (.msg): select(.success == false) | {expected, actual, success}
}] | add }] | add
}] | add }] | add
}] | add }] | add'';
'';
overview = '' overview = ''
. .'';
'';
} }

View file

@ -1,22 +1,38 @@
pkgs: suite: let pkgs: suite: let
test = import ./test.nix; 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); resFile = builtins.toFile "results" (builtins.toJSON res);
filters = import ./filters.nix; filters = import ./filters.nix;
getNix = "import ${./eval.nix} ${resFile}"; getNix = "import ${./eval.nix} ${resFile}";
extractNix = "nix eval --impure --quiet --expr 'let r = ${getNix}; in builtins.trace r r'"; opts = {
nixRepl = "nix repl --expr '{ r = ${getNix}; }'"; 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" '' in (pkgs.writeShellScriptBin "test" ''
t=''${1:-overview} t=''${1:-json}
case "$t" in case "$t" in
"file") echo '${resFile}' ;; ${pkgs.lib.pipe opts [
"raw") cat '${resFile}' ;; (builtins.mapAttrs (name: cmd: ''"${name}") ${cmd}''))
"json") ${pkgs.jq}/bin/jq '.' '${resFile}' ;; (builtins.attrValues)
"explore") ${pkgs.fx}/bin/fx '${resFile}' ;; (builtins.concatStringsSep " ;;\n ")
"nix") ${extractNix} | ${pkgs.alejandra}/bin/alejandra -q ;; ]};;
"nixrepl") ${nixRepl} ;; *) echo options: ${builtins.concatStringsSep ", " (builtins.attrNames opts)} ;;
"nixraw") ${extractNix} ;;
"overview") ${pkgs.jq}/bin/jq '${filters.overview}' '${resFile}' ;;
*) echo format "$t" not recognised! ;;
esac esac
'') '')

View file

@ -1,17 +1,12 @@
path: let path: let
it = import ./it.nix; it = import ./it.nix;
describe = import ./describe.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; else path;
name = if builtins.typeOf path == "path" then builtins.baseNameOf path in (
else "anonymous";
in
builtins.trace "tix: ${name}"
(
if !builtins.isFunction suite if !builtins.isFunction suite
then throw "A test suite should be in the form { it }: [ <tests> ]." then throw "A test suite should be in the form { it, describe }: [ <tests> ]."
else { else builtins.listToAttrs (suite {inherit it describe;})
inherit name;
value = builtins.listToAttrs (suite {inherit it describe;});
}
) )