#!/bin/bash
set -eux
# By default, run tests with pytest-forked plugin,
# disable in terminal for debugging, you may add --forked
flag_forked="--forked"
if [[ -z "${CONTINUOUS_INTEGRATION-}" ]] && [[ -t 1 ]] ; then
	flag_forked=""
fi
test_flags=(
	$@
	$flag_forked
	tests/
)

cd "$( dirname "${BASH_SOURCE[0]}" )/.."
if [[ -n "${CONTINUOUS_INTEGRATION-}" ]] ; then
	case "${test_group-}" in
	pep8)
		if [[ "${TRAVIS_PYTHON_VERSION}" = "2.7" ]] ; then
			flake8 python2/
		else
			flake8 python3/ tests/
		fi
		;;
	package)
	    # TODO: sdist bdist_wheel
	    # but wheels don't roll well with our 2/3 split code base
		python setup.py sdist
		pip install dist/httplib2*
		;;
	*)
		pip install -e .
		httplib2_test_still_run_skipped=1 pytest --fulltrace -k test_303 $@ tests/ || true
		httplib2_test_still_run_skipped=1 pytest --fulltrace -k test_head_301 $@ tests/ || true
		pytest --fulltrace ${test_flags[@]}
		codecov --flags=$(echo $python |tr -d -- '-.')
		;;
	esac
else
	if [[ ! -d ./venv-27 ]] ; then
		virtualenv --python=python2.7 ./venv-27
		./venv-27/bin/pip install -e . -r requirements-test.txt
	fi
	if [[ ! -d ./venv-36 ]] ; then
		virtualenv --python=python3.6 ./venv-36
		./venv-36/bin/pip install -e . -r requirements-test.txt
	fi

	./venv-27/bin/pytest ${test_flags[@]}
	./venv-36/bin/pytest ${test_flags[@]}

	# FIXME: too many errors
	# ./venv-27/bin/flake8 python2/
	# ./venv-36/bin/flake8 python3/ tests/

    # TODO: sdist bdist_wheel
    # but wheels don't roll well with our 2/3 split code base
	./venv-36/bin/python setup.py sdist
fi
rm -rf ./_httplib2_test_cache
