#!/bin/bash

# run pytest for all run_modules
python3.6 -m pytest --html=report.html --self-contained-html test/ | tee test_results.out

IS_FAILED=$?

if [ -z ${TEST_TYPE+x} ]; then
    TEST_TYPE=""; else
      TEST_TYPE="_"${TEST_TYPE};
fi

# move html test report
TEST_RESULTS_DIR="test_results${TEST_TYPE}/"
mkdir ${TEST_RESULTS_DIR}
BRANCH_NAME=$( echo -e "${CI_COMMIT_REF_NAME////_}")
mkdir "${TEST_RESULTS_DIR}/${BRANCH_NAME}"
mkdir "${TEST_RESULTS_DIR}/recent"
cp report.html "${TEST_RESULTS_DIR}/${BRANCH_NAME}/."
cp report.html "${TEST_RESULTS_DIR}/recent/."
if [[ "${CI_COMMIT_REF_NAME}" = "master" ]]; then
    cp -r report.html ${TEST_RESULTS_DIR}/.
fi

# exit 0 if no tests implemented
RUN_NO_TESTS="$(grep -c 'no tests ran' test_results.out)"
if [[ ${RUN_NO_TESTS} -gt 0 ]]; then
    echo "no test available"
    echo "incomplete" > status.txt
    echo "no tests avail" > incomplete.txt
    exit 0
fi

# extract if tests passed or not
TEST_FAILED="$(grep -oP '(\d+\s{1}failed)' test_results.out)"
TEST_FAILED="$(echo "${TEST_FAILED}" | (grep -oP '\d*'))"
TEST_PASSED="$(grep -oP '\d+\s{1}passed' test_results.out)"
TEST_PASSED="$(echo "${TEST_PASSED}" | (grep -oP '\d*'))"
if [[ -z "$TEST_FAILED" ]]; then
    TEST_FAILED=0
fi
(( TEST_PASSED=TEST_PASSED-TEST_FAILED ))
# calculate metrics
(( SUM=TEST_FAILED+TEST_PASSED ))
(( TEST_PASSED_RATIO=TEST_PASSED*100/SUM ))

# report
if [[ ${IS_FAILED} -eq 0 ]]; then
    if [[ ${TEST_PASSED_RATIO} -lt 100 ]]; then
        echo "only ${TEST_PASSED_RATIO}% passed"
        echo "incomplete" > status.txt
        echo "${TEST_PASSED_RATIO}%25 passed" > incomplete.txt
        if [[ ${TEST_PASSED_RATIO} -lt ${FAILURE_THRESHOLD} ]]; then
            echo -e "\033[1;31monly ${TEST_PASSED_RATIO}% passed!!\033[0m"
            exit 1
        fi
    else
        echo "passed"
        echo "success" > status.txt
    fi
    exit 0
else
    echo "not passed"
    exit 1
fi
