#!/usr/bin/env bash

if [ -n "$INSIDE_EMACS" ]
then
    EMACS_BIN="emacs"
else
    EMACS_BIN="${EMACS:-emacs}"
fi

usage () {
    cat <<EOF
$0 [OPTIONS] [DIRS]

Buttercup will search recursively in each of DIRS for test files (any
elisp file starting with "test-" or ending with "-test.el" or
"-tests.el"). It will load all of those files and then run the tests
defined in those files. If no directories are specified, buttercup
will search in the current directory.

Options can include the options described below, as well as the
standard Emacs options "--directory", "--funcall", "--load", "--eval",
and "--execute". These Emacs options should be used to ensure that any
Elisp files required for the tests can be found in Emacs' load path.
For package testing, "-L ." is commonly used. See "emacs --help" for
more information on Emacs options.

Buttercup options:

--pattern, -p PATTERN   Only run tests with names matching PATTERN.
                          This option can be used multiple times, in
                          which case tests will be run if they match
                          any of the given patterns. PATTERN should be
                          an Emacs regex that will be matched against
                          the full test description (the concatenation
                          of the test and all parent suites
                          descriptions).

--no-skip               Do not print the descriptions for tests that
                          are filtered out with "--pattern" or disabled
                          with "xit". Tests skipped wiath "assume" will
                          still be priuted,

--only-error            Only print failed tests and their containing suites.
                          Implies "--no-skip".

--no-color, -c          Do not colorize test output.

--traceback STYLE       When printing backtraces for errors that occur
                          during tests, print them in the chosen
                          STYLE. Available styles are
                         "full", which shows the full function call for
                          each stack frame on a single line,
                         "crop", which truncates each stack frame to 80
                          characters (the default),
                         "pretty", which uses Emacs' pretty-printing
                          facilities to print each stack frame, and also
                          annotates each frame with a lambda or M to
                          indicate whether it is a normal function call
                          or a macro/special form and
                         "omit", which omits the backtraces alltogether.

--stale-file-error      Fail the test run if stale .elc files are loaded.
EOF
}

EMACS_ARGS=()
BUTTERCUP_ARGS=()

while [[ "$#" -gt 0 ]]
do
    case "$1" in
        "-h"|"--help")
            usage
            exit
            ;;
        "-L"|"--directory"|"-f"|"--funcall"|"-l"|"--load"|"--eval"|"--execute")
            EMACS_ARGS+=("$1")
            EMACS_ARGS+=("$2")
            shift
            shift
            ;;
        "-p"|"--pattern")
            BUTTERCUP_ARGS+=("$1")
            BUTTERCUP_ARGS+=("$2")
            shift
            shift
            ;;
        "-c"|"--no-color"|"--no-skip"|"--only-error"|"--stale-file-error")
            BUTTERCUP_ARGS+=("$1")
            shift
            ;;
        "--traceback")
            BUTTERCUP_ARGS+=("$1")
            BUTTERCUP_ARGS+=("$2")
            shift
            shift
            ;;
        *)
            BUTTERCUP_ARGS+=("$1")
            shift
            ;;
    esac
done

# `--' is needed so that Buttercup options don't get parsed by Emacs itself.
exec "$EMACS_BIN" -batch "${EMACS_ARGS[@]}" -l buttercup -f buttercup-run-discover -- "${BUTTERCUP_ARGS[@]}"

# Local Variables:
# indent-tabs-mode: nil
# End:
