-
Notifications
You must be signed in to change notification settings - Fork 10
/
gotestcov
executable file
·57 lines (44 loc) · 1.9 KB
/
gotestcov
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
#!/bin/bash
# This is a wrapper script to run the Go tests and generate the coverage report.
# The coverage will then be merged with the Rust one and the HTML version will be
# exposed on localhost:6061.
set -eu
# find_go_mod walks up the directory tree looking for the go.mod file.
# If it doesn't find it, the script will be aborted.
find_go_mod() {
cwd="$(pwd)"
while [ "$cwd" != "/" ]; do
if [ -f "$cwd/go.mod" ]; then
echo "$cwd"
return
fi
cwd=$(dirname "$cwd")
done
echo "Error: go.mod not found in parent path. Aborting!"
exit 1
}
projectroot="$(find_go_mod)"
cov_dir="${projectroot}/coverage"
mkdir -p "${cov_dir}"
# start http server on 6061 if none
if ! $(nc -z localhost 6061); then
nohup python3 -m http.server --directory "${cov_dir}" 6061 1>/dev/null 2>&1 &
fi
raw_cov_dir="${cov_dir}/raw_files"
rm -fr "${raw_cov_dir}"
mkdir -p "${raw_cov_dir}"
# Run the tests adding the necessary flags to enable coverage
# Overriding the default coverage directory is currently not an exported flag of go test
# We need to override it using the test.gocoverdir flag instead.
#TODO: Update when https://go-review.googlesource.com/c/go/+/456595 is merged.
go test -cover -covermode=set -coverpkg=./... $@ -shuffle=on -args -test.gocoverdir="${raw_cov_dir}"
# Convert the raw coverage data into textfmt so we can merge the Rust one into it
go tool covdata textfmt -i="${raw_cov_dir}" -o="${cov_dir}/coverage.out"
# Append the Rust coverage data to the Go one
if [ -f "${raw_cov_dir}/rust-cov/rust2go_coverage" ]; then
cat "${raw_cov_dir}/rust-cov/rust2go_coverage" >>"${cov_dir}/coverage.out"
fi
# Filter out the testutils package and the pb.go file
grep -v -e "testutils" -e "pb.go" "${cov_dir}/coverage.out" >"${cov_dir}/coverage.out.filtered"
# Generate the HTML report
go tool cover -o "${cov_dir}/index.html" -html="${cov_dir}/coverage.out.filtered"