#!/bin/bash

CPWD=$(dirname $(readlink -f $0))

coverage erase
if [ $? -ne 0 ];then
	echo "You have to install coverage.py!!!!!, excute 'pip install coverage'!!!!"
	exit 1
fi

TEST_DIR=$CPWD
PYTHONPATH=$CPWD:$CPWD/../:$TEST_DIR:$PYTHONPATH
export PYTHONPATH=$PYTHONPATH

MODSRC_DIR=$CPWD/../mods
WEBSRC_DIR=$CPWD/../web

coverage run --source=$MODSRC_DIR,$WEBSRC_DIR--concurrency=thread --branch --append "$TEST_DIR"/runner.py
if [ $? -ne 0 ];then
	echo "Test failed!!!!"
	exit 1
fi

coverage report -m
if [ $? -ne 0 ];then
	echo "An error occured while generating report!!!!"
	exit 1
fi

if [ "$1" == xml ];then
	rm -f coverage.xml
	coverage xml -o coverage.xml
else
  rm -rf htmlcov
  coverage html
fi
