#!/bin/bash
set -e
real_path=$(readlink -f "$0")
script_dir=$(dirname "$real_path")
output_dir="${script_dir}/ut_coverage"
root_path=$(readlink -f "${script_dir}/..")
msprof_analyze_path="${root_path}/msprof_analyze"
srccode="${msprof_analyze_path}/advisor,${msprof_analyze_path}/cli,${msprof_analyze_path}/cluster_analyse,${msprof_analyze_path}/compare_tools,${msprof_analyze_path}/prof_common,${msprof_analyze_path}/prof_exports"
test_code="${script_dir}/ut"
export PYTHONPATH="${root_path}:${test_code}:${PYTHONPATH}"
umask 022
mkdir -p "$output_dir"
cd "$output_dir"
rm -f .coverage
coverage run --branch --source="${srccode}" -m pytest -s "${test_code}" --junit-xml=./final.xml
coverage xml -o coverage.xml
coverage report >python_coverage_report.log
if [[ -n "$1" && "$1" == "diff" ]]; then
target_branch=${2:-master}
diff-cover coverage.xml --compare-branch="origin/${target_branch}" --html-report inc_coverage_result.html --fail-under=80
fi
echo "Report: $output_dir"
find "${script_dir}/.." -name "__pycache__" -exec rm -r {} +