-
Notifications
You must be signed in to change notification settings - Fork 76
/
go_test_with_coverage_report
executable file
·153 lines (114 loc) · 3.92 KB
/
go_test_with_coverage_report
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env bash
set -eo pipefail
testsDefinitions="testsdefinitions.txt"
TESTFLAGS=${TESTFLAGS:-"-cover"}
PARALLEL_TESTS_LIMIT=${PARALLEL_TESTS_LIMIT:-10}
SUITE_TOTAL=${SUITE_TOTAL:-1}
SUITE_INDEX=${SUITE_INDEX:-0}
output="regular"
coverMode="count"
if [[ ${TESTFLAGS} = *"-race"* ]]; then
output="race"
coverMode="atomic"
fi
printMessage() {
echo -e "\033[1m${@}\033[0m"
}
joinBy() {
local IFS="${1}"
shift
echo "${*}"
}
prepareTestCommands() {
[[ ! -f ${testsDefinitions} ]] || rm ${testsDefinitions}
for pkg in ${OUR_PACKAGES}; do
testIndex=0
runTests=()
tests=$(go test -list "Test.*" ${pkg} | grep "^Test" || echo "")
if [[ -z "${tests}" ]]; then
continue
fi
counter=0
for test in ${tests}; do
counter=$((counter+1))
runTests+=("${test}")
if [[ ${counter} -ge ${PARALLEL_TESTS_LIMIT} ]]; then
if [[ ${#runTests[@]} -gt 0 ]]; then
echo ${pkg} ${testIndex} $(joinBy "|" "${runTests[@]}") | tee -a ${testsDefinitions}
fi
counter=0
runTests=()
testIndex=$((testIndex+1))
fi
done
if [[ ${#runTests[@]} -gt 0 ]]; then
echo ${pkg} ${testIndex} $(joinBy "|" "${runTests[@]}") | tee -a ${testsDefinitions}
fi
done
}
executeTestCommand() {
local pkg=${1}
local index=${2}
local runTests=${3}
local options=""
local pkgSlug=$(echo ${pkg} | tr "/" "-")
if [[ ${TESTFLAGS} = *"-cover"* ]]; then
mkdir -p ".cover"
mkdir -p ".testoutput"
printMessage "\n\n--- Starting part ${index} of go tests of '${pkg}' package with coverprofile in '${coverMode}' mode:\n"
profileFile=".cover/${pkgSlug}.${index}.${coverMode}.cover.txt"
options="-covermode=${coverMode} -coverprofile=${profileFile}"
else
echo "Starting go test"
fi
testOutputFile=".testoutput/${pkgSlug}.${index}.${output}.output.txt"
exitCode=0
go test ${options} ${TESTFLAGS} -v ${pkg} -run "${runTests}" 2>&1 | tee ${testOutputFile} || exitCode=1
return $exitCode
}
executeTestPart() {
rm -rf ".cover/"
rm -rf ".testoutput/"
numberOfDefinitions=$(cat ${testsDefinitions} | wc -l)
executionSize=$((numberOfDefinitions/SUITE_TOTAL+1))
executionOffset=$((SUITE_INDEX*executionSize+1))
printMessage "Number of definitions: ${numberOfDefinitions}"
printMessage "Suite size: ${SUITE_TOTAL}"
printMessage "Suite index: ${SUITE_INDEX}"
printMessage "Execution size: ${executionSize}"
printMessage "Execution offset: ${executionOffset}"
exitCode=0
tail -n +${executionOffset} ${testsDefinitions} | head -n ${executionSize} | while read pkg index tests; do
executeTestCommand ${pkg} ${index} ${tests} || exitCode=1
done
exit $exitCode
}
computeCoverageReport() {
local reportDirectory="out/coverage"
local sourceFile="${reportDirectory}/coverprofile.${output}.source.txt"
local htmlReportFile="${reportDirectory}/coverprofile.${output}.html"
local textReportFile="${reportDirectory}/coverprofile.${output}.txt"
mkdir -p ${reportDirectory}
echo "mode: ${coverMode}" > ${sourceFile}
grep -h -v -e "^mode:" -e "executors/docker/bindata.go" .cover/*.${coverMode}.cover.txt >> ${sourceFile}
printMessage "Generating HTML coverage report"
go tool cover -o ${htmlReportFile} -html=${sourceFile}
printMessage "Generating TXT coverage report"
go tool cover -o ${textReportFile} -func=${sourceFile}
printMessage "General coverage percentage:"
total=$(grep "total" "${textReportFile}" || echo "")
if [[ -n "${total}" ]]; then
echo "${output} ${total}"
fi
}
case "$1" in
prepare)
prepareTestCommands
;;
execute)
executeTestPart
;;
coverage)
computeCoverageReport
;;
esac