Rewrite tests in haskell

This commit is contained in:
Alex David 2025-08-19 00:08:44 -07:00
commit d8b14ca5ee
9 changed files with 530 additions and 255 deletions

View file

@ -5,78 +5,36 @@
outputs = { nixpkgs, ... }: let
pkgs = import nixpkgs { system = "x86_64-linux"; };
lib = pkgs.lib;
jail-nix.lib = import ../lib;
ansi = clrCode: "[${toString clrCode}m";
green = ansi 32;
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}";
testCases = import ./test-cases.nix { inherit pkgs lib test jail-nix; };
haskellPackages = pkgs.haskellPackages.callCabal2nix "jail-nix-tests" ./. { };
testDependencies = [
(pkgs.haskellPackages.ghc.withPackages (p: haskellPackages.buildInputs))
pkgs.cabal-install
];
in {
apps.x86_64-linux.runChecks = {
type = "app";
program = lib.getExe (pkgs.writeShellApplication {
name = "run-all-jail-nix-tests";
runtimeInputs = testDependencies;
text = ''
printf 'Running tests...\n'
TESTS=(${lib.pipe testCases [
lib.attrNames
(map lib.escapeShellArg)
${lib.pipe {
jail-nix = ../.;
nixpkgs = nixpkgs;
} [
(lib.mapAttrsToList (name: path: "${name}=${path}"))
(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
FAILURES=$(cat "$TEST_FAIL_LOG")
if [ "$FAILURES" != "" ]; then
printf '\n${red}Failing tests:${reset}\n\n%s' "$FAILURES"
exit 1
fi
(lib.toShellVar "NIX_PATH")
]} cabal test
'';
});
};
checks.x86_64-linux = lib.mapAttrs (name: testBody:
pkgs.runCommand (lib.strings.sanitizeDerivationName name) {} ''
export HOME=$(pwd)
export LANG=
${normalizeTestBody testBody}
touch $out
''
) testCases;
devShells.x86_64-linux.default = pkgs.mkShell {
buildInputs = testDependencies ++ [
(pkgs.haskell-language-server.override { dynamic = true; })
pkgs.ghcid
pkgs.hpack
];
};
};
}