#!/bin/bash
function filter_cov_by_tested_pkgs() {
local tested_pkgs
tested_pkgs=$(go list -f '{{if .TestGoFiles}}{{.ImportPath}}{{end}}' "${TOP_DIR}"/...)
awk -v pkgs="$tested_pkgs" '
NR==1 {print; next}
{
file=$1; sub(/:[0-9].*/, "", file)
n=split(file, p, "/"); pkg=""
for(i=1;i<n;i++) pkg=pkg p[i]"/"; sub(/\/$/,"", pkg)
if (index(pkgs, pkg)) print
}
' cov.out > cov_filtered.out
}
function execute_test() {
gotestsum --junitfile unit-tests.xml --jsonfile test.jsonl \
-- -mod=mod -count=1 -gcflags=all=-l -v -coverprofile cov.out "${TOP_DIR}"/...;
filter_cov_by_tested_pkgs
gocov convert cov_filtered.out | gocov-html > "$file_detail_output"
total_coverage=$(go tool cover -func=cov_filtered.out | grep "total:" | awk '{print $3}'| sed 's/%//')
coverage=$(echo "$total_coverage" | awk '{if ($1 >= 0) print ($1 == int($1)) ? int($1) : int($1) + 1;\
else print ($1 == int($1)) ? int($1) : int($1)}')
if [[ $coverage -ge 80 ]]; then
echo "coverage passed: $coverage%"
exit 0
else
echo "coverage failed: $coverage%, it needs to be greater than 80%."
exit 1
fi
}
export GO111MODULE="on"
export PATH=$GOPATH/bin:$PATH
CUR_DIR=$(dirname "$(readlink -f $0)")
TOP_DIR=$(realpath "${CUR_DIR}"/..)
file_detail_output='api.html'
echo "clean old version test results"
if [ -d "${TOP_DIR}"/test ]; then
rm -rf "${TOP_DIR}"/test
fi
mkdir -p "${TOP_DIR}"/test/
cd "${TOP_DIR}"/test/
if [ -f "$file_detail_output" ]; then
rm -rf $file_detail_output
fi
echo "************************************* Start LLT Test *************************************"
execute_test
echo "************************************* End LLT Test *************************************"