Add app to run all tests with nice output

This commit is contained in:
Alex David 2025-07-18 10:46:48 -07:00
commit 3abb0273cd

View file

@ -8,6 +8,7 @@
jail = import ../lib.nix { inherit pkgs; };
ansi = clrCode: "[${toString clrCode}m";
green = ansi 32;
red = ansi 31;
reset = ansi 0;
@ -34,7 +35,44 @@
if type == "list" then lib.concatStringsSep "\n" (map normalizeTestBody testBody) else
if lib.isDerivation testBody then lib.getExe testBody else
throw "Unknown test body type ${type}";
testCases = import ./test-cases.nix { inherit pkgs lib test jail; };
in {
apps.x86_64-linux.runChecks = {
type = "app";
program = lib.getExe (pkgs.writeShellApplication {
name = "run-all-jail-nix-tests";
text = ''
printf 'Running tests...\n'
TESTS=(${lib.pipe testCases [
lib.attrNames
(map lib.escapeShellArg)
(lib.concatStringsSep " ")
]})
TEST_FAIL_LOG=$(mktemp)
trap 'rm $TEST_FAIL_LOG' EXIT
(
for TEST_NAME in "''${TESTS[@]}"; do
(
if TEST_OUTPUT=$(nix build -L .#checks.x86_64-linux."$TEST_NAME" 2>&1); then
printf ' %s [${green}✔${reset}]\n' "$TEST_NAME"
else
printf ' %s [${red}✖${reset}]\n' "$TEST_NAME"
printf '${red}✖ %s${reset}\n%s\n\n\n' "$TEST_NAME" "$TEST_OUTPUT" >> "$TEST_FAIL_LOG"
fi
) &
done
) | sort
wait
FAILURES=$(cat "$TEST_FAIL_LOG")
if [ "$FAILURES" != "" ]; then
printf '\n${red}Failing tests:${reset}\n\n%s' "$FAILURES"
exit 1
fi
'';
});
};
checks.x86_64-linux = lib.mapAttrs (name: testBody:
pkgs.runCommand (lib.strings.sanitizeDerivationName name) {} ''
export HOME=$(pwd)
@ -42,6 +80,6 @@
${normalizeTestBody testBody}
touch $out
''
) (import ./test-cases.nix { inherit pkgs lib test jail; });
) testCases;
};
}