From 3abb0273cd791406a06a5f4fa7a369b5eab963a8 Mon Sep 17 00:00:00 2001 From: Alex David Date: Fri, 18 Jul 2025 10:46:48 -0700 Subject: [PATCH] Add app to run all tests with nice output --- tests/flake.nix | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/tests/flake.nix b/tests/flake.nix index ded15cc..343eb11 100644 --- a/tests/flake.nix +++ b/tests/flake.nix @@ -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; }; }