#!/bin/bash

function usage()
{
    cat <<EOF
${SCRIPT_NAME}: Generate coverage using grcov.

Usage:
  coverage [opts] [suites...] : Run the provided test suites.

Options:
  -h: Print this message.

Suites:
  "unit": equivalent to cargo test --all-features
  "integration": a simple integration test with a chutney network.
  "all": enables all suites.

Notes:
  You need to have grcov and llvm-tools-preview installed.
  For integration tests, you'll need chutney and tor.
EOF
}

set -euo pipefail

TOPDIR=$(dirname "$0")/..
cd "$TOPDIR"

UNIT=no
INTEGRATION=no

while getopts "ch" opt ; do
    case "$opt" in
	h) usage
	   exit 0
	   ;;
	*) echo "Unknown option. (Run '$0 -h' for help.)"
	   exit 1
	   ;;
    esac
done

# Remove the parsed flags.
shift $((OPTIND-1))

for suite in "$@"; do
    case "$suite" in
	unit) UNIT=yes
	      ;;
	integration) INTEGRATION=yes
		     ;;
	all) UNIT=yes
	     INTEGRATION=yes
	     ;;
	*) echo "Unrecognized test suite '$suite'. (Run '$0 -h' for help.)"
	   exit 1
	   ;;
    esac
done

if [ "$UNIT" = no ] && [ "$INTEGRATION" = no ]; then
    echo "No test suites listed; nothing will be done. (Run '$0 -h' for help.)"
    exit 1
fi

# Clear the old coverage report and profiling data.
rm -r coverage coverage_meta_{unit,integration} || true
mkdir coverage
echo '<a href="all">all</a><br/>' > coverage/index.html

if [ "$UNIT" = yes ] ; then
    # Run the unit tests, with coverage.
    ./maint/with_coverage -o coverage/unit cargo test --all-features
    mv coverage_meta coverage_meta_unit
    echo '<a href="unit">unit</a><br/>' >> coverage/index.html
fi

if [ "$INTEGRATION" = yes ] ; then
    # Run the integration tests, with coverage.
    #
    # (This is just a basic test that uses curl over Arti over a
    # chutney network. It's taken from the gitlab-ci tests.)

    # TODO: we might want, at some point, to have a the following stuff
    # go into a basic extensible integration-testing script that gets
    # run both from here and from the .gitlab-ci.yml file.
    trap ./tests/chutney/teardown 0
    ./maint/with_coverage -o coverage/integration -s ./tests/chutney/setup proxy
    curl http://example.com -vs --socks5-hostname 127.0.0.1:9150 -o /dev/null
    trap - 0
    ./tests/chutney/teardown
    # Report is generated after teardown because chutney/setup returns before any
    # test was done, so the report would be generated based on incomplete data.
    ./maint/with_coverage -o coverage/integration -c true
    mv coverage_meta coverage_meta_integration
    echo '<a href="integration">integration</a><br/>' >> coverage/index.html
fi

# Generate merged coverage report.
mkdir coverage_meta
cat coverage_meta_*/commands > coverage_meta/commands
mv coverage_meta_* coverage_meta

./maint/with_coverage -o coverage/all -c true

