#!/bin/bash

cd $( dirname "${BASH_SOURCE[0]}" )

if [ -z "$SPRITE_HOME" ]; then
  SPRITE_HOME=`realpath ../install`
fi

text="Using SPRITE_HOME=$SPRITE_HOME"
border=$(printf '%*s' $(( ${#text} + 16 )) '' | tr ' ' '*')
1>&2 printf '\n%s\n        %s\n%s\n\n' "$border" "$text" "$border"

if [ ! -d "$SPRITE_HOME" ]; then
  1>&2 echo Error: SPRITE_HOME is not a directory.
  exit 1
fi

if [ ! -r "$SPRITE_HOME/lib/libcurryrt.so" ]; then
  1>&2 echo Error: SPRITE_HOME does not appear to point to a valid installation of Sprite.  Please check the path and ensure installation completed successfully.
  exit 1
fi

# Cap the address space of every test process (and the compilers it spawns) so
# a diverging program fails with an allocation error instead of exhausting the
# machine.  Override with SPRITE_TEST_MAX_VMEM_KB; set it to "unlimited" to
# disable the cap.  The valgrind drivers run without the cap: valgrind
# reserves large address ranges at start-up.
case `basename $0` in
  run_tests)
    # The runner caps each child itself (see lib/testrunner and section 10
    # of README): a watchdog on the resident set of the file's session, and
    # the address-space limit as a backstop.  SPRITE_TEST_MAX_VMEM_KB, when
    # set, names the backstop.
    export SPRITE_TEST_MAX_VMEM_KB
    ;;
  valgrind*) : "${SPRITE_TEST_MAX_VMEM_KB:=unlimited}" ;;
  *)         : "${SPRITE_TEST_MAX_VMEM_KB:=6291456}" ;;
esac
if [ `basename $0` != run_tests ]; then
  ulimit -v "$SPRITE_TEST_MAX_VMEM_KB" || 1>&2 echo "Warning: could not set the memory cap."
fi

# python=$SPRITE_HOME/bin/python
export SPRITE_HOME
export PYTHONPATH=`pwd`/lib/:$PYTHONPATH
export CURRYPATH=`pwd`/data/curry:$CURRYPATH

# The tests compile the same Curry texts in every run, and the Curry front end
# takes seconds per module.  The ICurry cache keeps its output between runs.
# Set SPRITE_CACHE_FILE to the empty string to run without the cache.  See
# section 1 of README.
export SPRITE_CACHE_FILE="${SPRITE_CACHE_FILE-`pwd`/.cache/icurry.db}"

# The drivers other than run_tests pass the arguments to unittest discover
# as one pattern.  So the arguments must either be empty (run all tests) or
# the name of a test file (which is then taken as a pattern, meaning just
# that file will be run).
if [ $# -eq 0 ]; then
  pattern='*.py'
else
  pattern="$@"
fi

case `basename $0` in
  "run_tests")
    # Run the test files in child processes, under a memory budget: see
    # lib/testrunner and section 10 of README.  The options and the
    # patterns go to the runner; say ./run_tests -h.
    exec $SPRITE_HOME/bin/python -B -m testrunner "$@"
    ;;
  "run_benchmarks")
    # Run the benchmarks.  Arguments go to the benchmarks package under
    # tests/lib: "run [options] [PROGRAM ...]" (the default command),
    # "compare OLD NEW", "counters FILE", or "list"; see -h and section 9 of
    # README.
    $SPRITE_HOME/bin/python -m benchmarks "$@"
    ;;
  "debug_tests")
    # Run the Python unit tests under gdb.
    # gdb --args $python -B -m unittest discover -v `pwd` "$pattern"
    $SPRITE_HOME/bin/sprite-invoke gdb --args \
        $SPRITE_HOME/tools/python -B -m unittest discover -v `pwd` "$pattern"
    ;;
  "valgrind_tests")
    # Run the Python unit tests under valgrind.
    #
    # To use valgrind, rebuild a supported Python as directed by its
    # Misc/README.valgrind, then stage Sprite with that Python.
    #
    # You may also want to replace the valgrind-python.supp file in this
    # directory with the one that came with your version of Python.
    $SPRITE_HOME/bin/sprite-invoke valgrind --suppressions=sprite.supp \
        $SPRITE_HOME/tools/python -B -m unittest discover -v `pwd` "$pattern"
    ;;
  "valgrind_debug_tests")
    # Same as valgrind_tests except that vgdb is used.  Follow the onscreen
    # prompts to conntext gdb.
    $SPRITE_HOME/bin/sprite-invoke valgrind --suppressions=sprite.supp --vgdb-error=0 \
        $SPRITE_HOME/tools/python -B -m unittest discover -v `pwd` "$pattern"
    ;;
  "shell")
    # Start a new shell with the  environment used to run Python unit tests.
    export PATH=$SPRITE_HOME/bin/:$PATH
    exec $SHELL
esac

