-
Notifications
You must be signed in to change notification settings - Fork 13
/
test.common.sh
executable file
·82 lines (64 loc) · 2.68 KB
/
test.common.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
#!/bin/bash
NIGHTLY=0
mkdir -p _out
OS=$(uname -s)
if [ $OS == "Darwin" ]; then
tac () {
tail -r -- "$@";
}
fi
check_exit_code_and_report () {
if [ $EXIT_CODE != 0 ]; then
grep -B 150 -A 15 -- "FAIL:" ./$OUT_DUR/test.out > ./$OUT_DUR/fail.out
cat ./$OUT_DUR/fail.out
grep -B 150 -A 150 -- "test timed out" ./$OUT_DUR/test.out > ./$OUT_DUR/timed.out
cat ./$OUT_DUR/timed.out
if [ ! -s ./$OUT_DUR/fail.out ] && [ ! -s ./$OUT_DUR/timed.out ]; then
cat ./$OUT_DUR/test.out
fi
fi
# copy full log for further investigation
mkdir -p ./_logs
cp ./_out/*.out ./_logs
if [ $EXIT_CODE != 0 ]; then
exit $EXIT_CODE
fi
}
go_test_junit_report () {
OUT_DIR="_out/$1"
REPORTS_DIR="_reports/$1"
shift
mkdir -p $OUT_DIR
mkdir -p $REPORTS_DIR
go test -v $@ &> ${OUT_DIR}/test.out || true # so that we always go to the junit report step
go-junit-report -set-exit-code < ${OUT_DIR}/test.out > ${OUT_DIR}/results.xml
EXIT_CODE=$?
cp ${OUT_DIR}/results.xml ${REPORTS_DIR}/results.xml # so that we have it in _out for uploading as artifact, and separately in _reports since CircleCI doesn't like test summary dir to contain huge files
if [ $EXIT_CODE != 0 ]; then
echo "xxxxxxxxxxxxxxxxxxxxx"
echo "Tests failed!"
echo ""
# junit-xml-stats is a globally installed npm package specifically for the purpose
# of grouping together logs of failing tests ONLY.
if [ $NIGHTLY == 1 ]; then
junit-xml-stats ${OUT_DIR}/results.xml
else
# Let's look for the Go package that failed:
GOLANG_PKG_ERR_LINE=$(grep -n "^FAIL" ${OUT_DIR}/test.out | grep -Eo '^[^:]+' | tail -n 1)
# Reduce the test.out file only to section that we focus on and reverse it using tac
sed -n "1,${GOLANG_PKG_ERR_LINE}p" ${OUT_DIR}/test.out | tac > ${OUT_DIR}/truncated.tac.test.out
# Look for the last test that ran before this package failed
# (It's called FIRST in the variable here since we look at the log upside down)
FIRST_RUN_LINE=$(grep -n "^=== RUN" -m 1 ${OUT_DIR}/truncated.tac.test.out | grep -Eo '^[^:]+' | tail -n 1)
echo "The following test failed: "
sed -n "1,${FIRST_RUN_LINE}p" ${OUT_DIR}/truncated.tac.test.out | tac
fi
exit $EXIT_CODE
else
echo "**************************"
echo "Tests passed!"
echo "JUnit-style XML for this run generated at: ${OUT_DIR}/results.xml"
echo "Go test output saved for reference at: ${OUT_DIR}/test.out"
echo "**************************"
fi
}