jail.nix/tests/flake.nix

85 lines
2.7 KiB
Nix
Raw Normal View History

2025-07-17 20:25:17 -07:00
{
# This is a separate flake file from the one at the root because we don't
# want to depend on nixpkgs
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05";
outputs = { nixpkgs, ... }: let
pkgs = import nixpkgs { system = "x86_64-linux"; };
lib = pkgs.lib;
2025-08-07 15:52:43 -07:00
jail-nix.lib = import ../lib;
2025-07-17 20:25:17 -07:00
ansi = clrCode: "[${toString clrCode}m";
green = ansi 32;
2025-07-17 20:25:17 -07:00
red = ansi 31;
reset = ansi 0;
test = {
assertStdout = actual: expected: ''
ACTUAL=$(${lib.getExe actual})
if test "$ACTUAL" != ${lib.escapeShellArg expected}; then
printf '${red}Expected "%s" to equal "%s".${reset}' "$ACTUAL" ${lib.escapeShellArg expected}
exit 1
fi
'';
assertFileDoesNotExist = fileName: ''
if test -e ${lib.escapeShellArg fileName}; then
printf '${red}Expected "%s" to not exist.${reset}' ${lib.escapeShellArg fileName}
exit 1
fi
'';
};
normalizeTestBody = testBody:
let type = builtins.typeOf testBody; in
if type == "string" then testBody else
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}";
2025-08-07 15:29:16 -07:00
testCases = import ./test-cases.nix { inherit pkgs lib test jail-nix; };
2025-07-17 20:25:17 -07:00
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
'';
});
};
2025-07-17 20:25:17 -07:00
checks.x86_64-linux = lib.mapAttrs (name: testBody:
pkgs.runCommand (lib.strings.sanitizeDerivationName name) {} ''
export HOME=$(pwd)
export LANG=
${normalizeTestBody testBody}
touch $out
''
) testCases;
2025-07-17 20:25:17 -07:00
};
}