#!/usr/bin/env bash

# Creates a PuppetDB uberjar and runs tests against it

set -ueo pipefail

failures=0

# Log failed command
failure() {
    if test "$TERM"; then
        echo "===== $(tput setaf 1)FAILED:$(tput sgr 0) $@" 1>&2
    else
        echo "===== FAILED: $@" 1>&2
    fi
    ((failures++)) || true
}

# Log successful command
success() {
    echo "===== OK: $@" 1>&2
}

# Run a command and log success or failure based on exit status
run() {
    echo -e "\n===== testing $@" 1>&2
    rc=0
    "$@" || rc=$?
    if test "$rc" -eq 0; then
        success "$@"
    else
        failure "$@"
    fi
}

# Create a PuppetDB uberjar
run lein uberjar

unset PDBBOX
export PDB_JAR="$(pwd)/target/puppetdb.jar"

# Run tests
run ext/test/oom-causes-shutdown
run ext/test/top-level-cli
run ext/test/upgrade-and-exit
run ext/test/database-migration-config
run ext/test/schema-mismatch-causes-pdb-shutdown

# If there are no failures, say so and exit
if test "$failures" -eq 0; then
    echo "failures: $failures" 1>&2
    exit 0
fi

# If there are any failures, print them out
# For some reason github's macos TERM=dumb doesn't work with tput:
#     tput: No value for $TERM and no -T specified
# though dumb appears to work fine in Linux.
if tput longname 2>/dev/null 1>&2; then
    echo "$(tput setaf 1)failures: $failures$(tput sgr 0)" 1>&2
else
    echo "failures: $failures" 1>&2
fi

exit 2
