-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbunit_result.sh
More file actions
160 lines (130 loc) · 5.12 KB
/
Copy pathbunit_result.sh
File metadata and controls
160 lines (130 loc) · 5.12 KB
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
154
155
156
157
158
159
160
#!/bin/bash
#
# https://github.com/EngineeringSoftware/gobash/blob/main/LICENSE
#
# Test result related structs and functions.
if [ -n "${BUNIT_RESULT_MOD:-}" ]; then return 0; fi
readonly BUNIT_RESULT_MOD=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
# ----------
# Functions.
function BUnitResult() {
# Result of a test run.
local ctx; is_ctx "${1}" && ctx="${1}" && shift
[ $# -ne 0 ] && { ctx_wn $ctx; return $EC; }
shift 0 || { ctx_wn $ctx; return $EC; }
make_ $ctx \
"${FUNCNAME}" \
"tests" "$(List $ctx)" \
"timestamp" "$(time_now_iso8601)" \
"btime" 0 \
"etime" 0
}
function BUnitResult_add() {
# Add a test result.
local ctx; is_ctx "${1}" && ctx="${1}" && shift
[ $# -ne 2 ] && { ctx_wn $ctx; return $EC; }
local -r res="${1}"
local -r testt="${2}"
shift 2 || { ctx_wn $ctx; return $EC; }
$($res $ctx tests) $ctx add "${testt}"
}
function BUnitResult_to_string() {
# String version of the result.
local ctx; is_ctx "${1}" && ctx="${1}" && shift
[ $# -ne 1 ] && { ctx_wn $ctx; return $EC; }
local -r res="${1}"
shift 1 || { ctx_wn $ctx; return $EC; }
local -r tests=$($res $ctx tests)
local -r total=$($tests $ctx len)
local -r failed=$($($tests $ctx filter TestT_failed) $ctx len)
local -r skipped=$($($tests $ctx filter TestT_skipped) $ctx len)
echo "Tests run: ${total}, failed: ${failed}, skipped: ${skipped}."
echo "Total time: $(_bunit_format_duration $ctx $($res $ctx duration))"
}
function BUnitResult_duration() {
# Duration of the total test run.
local ctx; is_ctx "${1}" && ctx="${1}" && shift
[ $# -ne 1 ] && { ctx_wn $ctx; return $EC; }
local -r res="${1}"
shift 1 || { ctx_wn $ctx; return $EC; }
local duration=$(( $($res $ctx etime) - $($res $ctx btime) ))
echo "${duration}"
}
function BUnitResult_total() {
# Total number of tests.
local ctx; is_ctx "${1}" && ctx="${1}" && shift
[ $# -ne 1 ] && { ctx_wn $ctx; return $EC; }
local -r res="${1}"
shift 1 || { ctx_wn $ctx; return $EC; }
$($res $ctx tests) $ctx len
}
function BUnitResult_failed() {
# Failed number of tests.
local ctx; is_ctx "${1}" && ctx="${1}" && shift
[ $# -ne 1 ] && { ctx_wn $ctx; return $EC; }
local -r res="${1}"
shift 1 || { ctx_wn $ctx; return $EC; }
local -r tests=$($res $ctx tests)
local -r lst=$($tests $ctx filter TestT_failed)
echo "${lst}"
}
function BUnitResult_exit_status() {
# Overall exit status.
local ctx; is_ctx "${1}" && ctx="${1}" && shift
[ $# -ne 1 ] && { ctx_wn $ctx; return $EC; }
local -r res="${1}"
shift 1 || { ctx_wn $ctx; return $EC; }
$($res $ctx tests) $ctx any_match TestT_failed && return $EC
return 0
}
function BUnitResult_to_junitxml_echo() {
# Generate junitxml format for the result.
local ctx; is_ctx "${1}" && ctx="${1}" && shift
[ $# -ne 1 ] && { ctx_wn $ctx; return $EC; }
local -r res="${1}"
shift 1 || { ctx_wn $ctx; return $EC; }
function to_sec() {
echo "scale=3; $($1 duration) / 1000" | bc | $X_SED 's/^\.\(.*\)/0.\1/g'
}
echo '<?xml version="1.0" encoding="utf-8"?>'
echo '<testsuites>'
local -r tests=$($res $ctx tests)
local -r -i total=$($tests $ctx len)
local -r -i failed=$($($tests $ctx filter TestT_failed) $ctx len)
local -r -i skipped=$($($tests $ctx filter TestT_skipped) $ctx len)
echo ' <testsuite name="bunit"'
echo ' errors="0"'
echo " failures=\"${failed}\""
echo " skipped=\"${skipped}\""
echo " tests=\"${total}\""
echo " time=\"$(to_sec "$res")\""
echo " timestamp=\"$($res timestamp)\""
echo " hostname=\"$(hostname)\">"
local -i len
len=$($tests $ctx len)
local -i i
for (( i=0; i<${len}; i++ )); do
local testt=$($tests $ctx get ${i})
echo " <testcase classname=\"$($testt file)\""
echo " name=\"$($testt name)\""
echo " time=\"$(to_sec "$testt")\">"
if $testt $ctx failed; then
echo "<failure message=\"$($testt msg)\">$($testt msg)</failure>"
fi
if $testt $ctx skipped; then
echo "<skipped type=\"bunit\" message=\"$($testt msg)\">$($testt msg)</skipped>"
fi
echo " </testcase>"
done
echo ' </testsuite>'
echo '</testsuites>'
}
function BUnitResult_to_junitxml() {
# Generate junitxml format for the result.
local ctx; is_ctx "${1}" && ctx="${1}" && shift
[ $# -ne 2 ] && { ctx_wn $ctx; return $EC; }
local -r res="${1}"
local -r pathf="${2}"
shift 2 || { ctx_wn $ctx; return $EC; }
$res $ctx to_junitxml_echo > "${pathf}"
}