nix-filter/tests/run-tests.sh
zimbatm 3d50f761b2
tests: evaluate the tests in restrict-eval mode
This simulates a stricter environment like flakes
2022-08-22 22:02:13 +02:00

95 lines
3.0 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# Our little test runner.
set -euo pipefail
cd "$(dirname "$0")"
nix_args=(
# Simulate flakes. Paths that are in the include are allowed.
--include "prj_root=.."
--option restrict-eval true
--option allow-import-from-derivation false
)
if [[ "$#" -eq 1 ]]; then
nix_args+=(-A "$1")
fi
# Need to build first or the store paths don't exist
# for default.nix to traverse
nix-build "${nix_args[@]}" >/dev/null
echo "---------------------------------------------------------------------"
results="$(nix-instantiate --eval --strict --json "${nix_args[@]}")"
# Normalize input before handing it over to jq
if [[ "$#" -eq 1 && "${1::1}" != "@" ]]; then
results="{ \"$1\": $results }"
fi
# Parse and format the results with jq
#
# This expects the JSON format to look like:
# { "test-case-name": [ { "path": "./path/to/file", "status": "missing" } ] }
#
# There is a special case ("@onlyFailures") which is a key with
# the expected format nested inside of it. In order to not choke
# on this "report" style of key, we filter out keys starting with
# "@".
result_string=$(
echo "$results" |\
jq -r '
"\\e[0;91m" as $red |
"\\e[0;92m" as $green |
"\\e[0m" as $reset |
to_entries |
map(select(.key|startswith("@") == false)) |
(
map(
(.value|length) as $errors |
"TEST: " + .key + " " +
(
if $errors > 0
then $red + "(" + ($errors|tostring) + " errors"
else $green + "(SUCCESS" end
) +
")" + $reset + "\n" +
( .value |
map(
" " +
(
if .status == "missing"
then "MISSING "
else "EXTRA " end
) +
.path + "\n"
) | add
)
)|.[]
),
(
[ (map(select(.value|length > 0))|length)
, (map(select(.value|length == 0))|length)
] |
"Tests completed. " +
(
if .[1] > 0
then $green + (.[1]|tostring) + " succeeded" + $reset + ". "
else "" end
) +
(
if .[0] > 0
then $red + (.[0]|tostring) + " failed" + $reset + "."
else "" end
)
)'
)
echo -e "$result_string"
# If there are errors in the output, return a non-zero exit code
if grep -Po "^TEST:.*?\(\d+ errors\)" <<< "$result_string" &>/dev/null; then
exit 1
fi