#!/usr/bin/env bash
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

#
# Copyright (c) 2015, Joyent, Inc.
#

#
# Run *integration* tests.
#
# This creates .tap files in OUTPUT_DIR that can be processed by a TAP reader.
# Testing config and log files are also placed in this dir.
#
# Run `./runtests -h` for usage info.
#

if [ "$TRACE" != "" ]; then
    export PS4='${BASH_SOURCE}:${LINENO}: ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
    set -o xtrace
fi
set -o errexit
set -o pipefail



#---- support functions

function fatal
{
    echo "$(basename $0): fatal error: $*"
    exit 1
}

function usage
{
    echo "Usage:"
    echo "  runtests [OPTIONS...]"
    echo ""
    echo "Options:"
    echo "  -f FILTER   Filter pattern (substring match) for test files to run."
    echo "  -s          Stop on first error."
}



#---- mainline

start_time=$(date +%s)

API=docker
TOP=$(cd $(dirname $0)/../; pwd)
NODE_INSTALL=$TOP/build/node
OUTPUT_DIR=/var/tmp/${API}test
TAPE=$TOP/node_modules/.bin/tape
FAILING_LIST=$OUTPUT_DIR/failing-tests.txt

# Options.
opt_test_pattern=
opt_stop_on_failure=
while getopts "hf:s" opt
do
    case "$opt" in
        h)
            usage
            exit 0
            ;;
        f)
            opt_test_pattern=$OPTARG
            ;;
        s)
            opt_stop_on_failure="true"
            ;;
        *)
            usage
            exit 1
            ;;
    esac
done


# Setup a clean output dir.
echo "# Setup a clean output dir ($OUTPUT_DIR)."
rm -rf $OUTPUT_DIR
mkdir -p /$OUTPUT_DIR
touch $FAILING_LIST

cd $TOP


# Run the integration tests

echo ""
test_files=$(ls -1 test/integration/*.test.js)
if [[ -n "$opt_test_pattern" ]]; then
    test_files=$(echo "$test_files" | grep "$opt_test_pattern" || true)
    echo "# Running filtered set of test files: $test_files"
fi

set +o errexit

for file in $test_files; do
    test_file=$(basename $file)
    echo "# $test_file"
    PATH=$NODE_INSTALL/bin:$PATH $TAPE $file \
        | tee $OUTPUT_DIR/$test_file.tap
    if [[ "$?" != "0" ]]; then
        echo $file >> $OUTPUT_DIR/failing-tests.txt
        [[ -n "$opt_stop_on_failure" ]] && break
    fi
done

set -o errexit

echo ""
echo "# test output in $OUTPUT_DIR:"
cd $OUTPUT_DIR
ls *.tap


# Colored summary of results (borrowed from smartos-live.git/src/vm/run-tests).
echo ""
echo "# test results:"

end_time=$(date +%s)
elapsed=$((${end_time} - ${start_time}))

tests=$(grep "^# tests [0-9]" $OUTPUT_DIR/*.tap | cut -d ' ' -f3 | xargs | tr ' ' '+' | bc)
passed=$(grep "^# pass  [0-9]" $OUTPUT_DIR/*.tap | tr -s ' ' | cut -d ' ' -f3 | xargs | tr ' ' '+' | bc)
[[ -z ${tests} ]] && tests=0
[[ -z ${passed} ]] && passed=0
fail=$((${tests} - ${passed}))
failing_tests=$(cat ${FAILING_LIST} | wc -l)

echo "# Completed in ${elapsed} seconds."
echo -e "# \033[32mPASS: ${passed} / ${tests}\033[39m"
if [[ ${fail} -gt 0 ]]; then
    echo -e "# \033[31mFAIL: ${fail} / ${tests}\033[39m"
fi

if [[ ${failing_tests} -gt 0 ]]; then
    echo ""
    echo -e "# \033[31mFAILING TESTS:\033[39m"
    cat $FAILING_LIST | sed -e 's,^,#   ,'
fi
echo ""

exit $failing_tests