#!/bin/bash
# This is a convenient high-level script to run all the files with the
# extension .test found in the tests/ subdirectory, using tewiba that can 
# be provided in the tests/ dir or above, or in the PATH.
# RUN-ALL-TESTS can be placed inside the tests/ dir or above.
# Useful to include in your software distributions with tewiba for providing
# a simple way to run all the tests in the distrib.
# (c) 2020 Colas Nahaboo http://colas.nahaboo.net/Software/Tewiba - MIT License

#TEWIBA_IGNORE# This is not a tewiba test.

# Change dir to where this script is, if was called via a complete path
scriptpath=$(realpath $(which "$0"))
cd "${scriptpath%/*}" || exit 1

# Sanity check: stop immediately if we have a syntax error in bash scripts
ok=true
for i in $(find -type f -executable); do 
    if grep -q '^#! */bin/bash' < <(head -1 "$i") && [ -x "$i" ]; then
	bash -n "$i" || ok=false
    fi
done
$ok || { echo "***ABORTING: syntax errors in scripts" >&2; exit 1; }
unset TEWIBA			# clean slate for tewiba

# otherwise, run all the *.test files in the tests/ dir in alphabetical order
# to run a single test, go into tests and run it directly (verbose mode)
# or as argument to tewiba (terse mode), e.g: cd tests; tewiba foo.test
# we look for the tewiba executable in ./, tests/, ../, $PATH

if [ -d tests ]; then cd tests
elif [[ $PWD =~ tests$ ]]; then :
else echo "***ERROR: must be run in a tests dir or above one"; exit 1
fi

if [ -x tewiba ]; then tewiba=./tewiba
elif [ -x ../tewiba ]; then tewiba=../tewiba
elif command -v tewiba >/dev/null; then tewiba=tewiba
else { echo "***ABORTING: tewiba executable not found" >&2; exit 1; }
fi

$tewiba "$@" .
