1
1
mirror of https://github.com/nmattia/snack.git synced 2024-08-15 14:50:30 +03:00
snack/script/test
2019-02-22 11:38:40 +01:00

83 lines
1.3 KiB
Plaintext
Executable File

#!/usr/bin/env nix-shell
#!nix-shell -i bash
#!nix-shell -I nixpkgs=./nix
#!nix-shell -p git
#!nix-shell -p jq
#!nix-shell -p nix
#!nix-shell -p shfmt
#!nix-shell -p packages.snack-exe
#!nix-shell -p glibcLocales
#!nix-shell --pure
# vim: ft=sh sw=2 et
set -euo pipefail
export LC_ALL="en_US.utf-8"
export LANG="en_US.utf-8"
## Functions
banner() {
echo
echo "--- $*"
}
capture_io() {
OUT_FILE="$1"
cat <<END_HEREDOC
import GHC.IO.Handle
import System.IO
old_stdout <- hDuplicate stdout
:{
withFile "$OUT_FILE" WriteMode $ \\h -> do
hDuplicateTo h stdout
$2
hDuplicateTo old_stdout stdout
:}
END_HEREDOC
}
export -f capture_io
fail() {
echo "ERROR: $*"
exit 1
}
## Main
# Run the test name provided on the CLI _or_ all tests if none was provided
SNACK_TESTS=""
while [[ $# -gt 0 ]]; do
arg="$1"
SNACK_TESTS="$SNACK_TESTS $arg"
shift
done
if [ -z "$SNACK_TESTS" ]; then
SNACK_TESTS=$(ls tests | xargs -n 1 basename)
fi
cd "$(dirname "$0")/.."
echo "Running the following tests:"
for t in $SNACK_TESTS; do
echo " - $t"
done
for t in $SNACK_TESTS; do
banner "Test $t"
pushd "tests/$t"
./test
popd
done
banner "Test this file's formatting"
list=$(shfmt -i 2 -l script/test)
if [[ -n "$list" ]]; then
fail "Please apply script/snack-fmt to format script/test"
fi
echo OK