-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest-cover.sh
executable file
·96 lines (73 loc) · 2.18 KB
/
test-cover.sh
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
#!/bin/bash
set -e
source "$(dirname $0)/variables.sh"
function bk_log() {
if [[ "${BUILDKITE}" == "true" ]]; then
echo "--- $1"
fi
}
TARGET=${1:-profile.cov}
EXCLUDE_FILE=${2:-.excludecoverage}
LOG=${3:-test.log}
rm $TARGET &>/dev/null || true
echo "mode: atomic" > $TARGET
echo "" > $LOG
DIRS=""
# Depending on what version of Go is being used, callers may wish to use "go
# list" or find target directories by looking for test files.
if [[ "$TESTDIR_SOURCE_TYPE" == "golist" ]]; then
DIRS=$(go list ./...)
else
for DIR in $SRC;
do
if ls $DIR/*_test.go &> /dev/null; then
DIRS="$DIRS $DIR"
fi
done
fi
# defaults to all DIRS if the split vars are unset
pick_subset "$DIRS" TESTS $SPLIT_IDX $TOTAL_SPLITS
if [ "$NPROC" = "" ]; then
NPROC=$(getconf _NPROCESSORS_ONLN)
fi
# Sometimes has a space at the end (i.e. "2 "), regardless of source. Strip all
# spaces.
NPROC=${NPROC// /}
bk_log ":golang: Running tests and coverage"
echo "test-cover begin: concurrency $NPROC"
PROFILE_REG="profile_reg.tmp"
export COVERTMP
COVERTMP="$(mktemp -d)"
rm -rf "$COVERTMP"
mkdir -p "$COVERTMP"
function cleanup {
rm -rf "$COVERTMP"
}
trap cleanup EXIT
echo 'mode: atomic' > "$TARGET"
echo "" > "$LOG"
# In parallel, write each package's coverage information to a package-specific
# file. Sanitize each package path to a file-friendly name.
set +e
<<<"$TESTS" xargs -P "$NPROC" -n1 .ci/process-cover-package.sh
TEST_EXIT=$?
set -e
FAILED_PKGS=""
# Combine all per-package cover results into one larger result.
while read -r F; do
if [[ "$(head -c 6 "$F")" == "FAILED" ]]; then
FAILED_PKGS="${FAILED_PKGS} $(awk '{print $2}' "$F")"
else
tail -n +2 "$F" >> "$PROFILE_REG"
fi
# NB(schallert): see https://github.com/koalaman/shellcheck/wiki/SC2031 for why
# find results are passed at end of loop to preserve FAILED_PKGS
done < <(find "$COVERTMP/" -type f)
filter_cover_profile $PROFILE_REG "$TARGET" "$EXCLUDE_FILE"
find . -not -path '*/vendor/*' | grep \\.tmp$ | xargs -I{} rm {}
if [[ -n "$FAILED_PKGS" ]]; then
bk_log ":bk-status-failed: encountered package failure(s)"
echo "packages with failures: [${FAILED_PKGS}]"
fi
echo "test-cover result: $TEST_EXIT"
exit $TEST_EXIT