Skip to content

Commit b1c4b5b

Browse files
committed
FIX #28
Implement tap output format for bash_unit. bash_unit -f tap
1 parent 2b253d0 commit b1c4b5b

File tree

2 files changed

+123
-18
lines changed

2 files changed

+123
-18
lines changed

bash_unit

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,11 @@ fail() {
3838
local stdout=$2
3939
local stderr=$3
4040

41-
format "$RED" "FAILURE" >&$LOG
42-
[[ -z $message ]] || printf -- "$message\n" >&$LOG
43-
[[ ! -z $stdout ]] && [ -s $stdout ] && cat $stdout | sed 's:^:out> :' | format $GREEN >&$OUT
44-
[[ ! -z $stderr ]] && [ -s $stderr ] && cat $stderr | sed 's:^:err> :' | format $RED >&$ERR
41+
notify_test_failed $__bash_unit_current_test__ "$message"
42+
[[ ! -z $stdout ]] && [ -s $stdout ] && cat $stdout | notify_stdout
43+
[[ ! -z $stderr ]] && [ -s $stderr ] && cat $stderr | notify_stderr
4544

46-
print_stack | grep -v ^$BASH_SOURCE | format "$YELLOW" >&$STACK
45+
print_stack | grep -v ^$BASH_SOURCE | notify_stack
4746
exit 1
4847
}
4948

@@ -137,7 +136,7 @@ run_test_suite() {
137136
(
138137
local status=0
139138
declare -F | grep ' setup$' >/dev/null && setup
140-
(run_test $test) || status=$?
139+
(__bash_unit_current_test__="$test" run_test) || status=$?
141140
declare -F | grep ' teardown$' >/dev/null && teardown
142141
exit $status
143142
)
@@ -148,9 +147,8 @@ run_test_suite() {
148147

149148
run_test() {
150149
set -e
151-
local TEST=$1
152-
notify_test_starting "$TEST"
153-
$TEST && notify_test_succeeded "$TEST" >&$LOG
150+
notify_test_starting "$__bash_unit_current_test__"
151+
$__bash_unit_current_test__ && notify_test_succeeded "$__bash_unit_current_test__"
154152
}
155153

156154
usage() {
@@ -198,13 +196,30 @@ notify_test_starting() {
198196
}
199197

200198
notify_test_succeeded() {
201-
local test="$1"
202199
format "$GREEN" "SUCCESS" >&$LOG
203200
}
204201

202+
notify_test_failed() {
203+
local message="$2"
204+
format "$RED" "FAILURE" >&$LOG
205+
[[ -z $message ]] || printf -- "$message\n" >&$LOG
206+
}
207+
208+
notify_stdout() {
209+
sed 's:^:out> :' | format $GREEN >&$OUT
210+
}
211+
212+
notify_stderr() {
213+
sed 's:^:err> :' | format $RED >&$ERR
214+
}
215+
216+
notify_stack() {
217+
format "$YELLOW" >&$STACK
218+
}
219+
205220
tap_notify_suite_starting() {
206221
test_file="$1"
207-
echo "# Running tests in $test_file" >&$LOG
222+
echo "# Running tests in $test_file" >&$LOG
208223
}
209224

210225
tap_notify_test_starting() {
@@ -214,8 +229,29 @@ tap_notify_test_starting() {
214229
tap_notify_test_succeeded() {
215230
local test="$1"
216231
echo -n "ok" | format "$GREEN" >&$LOG
217-
echo -n " " >&$LOG
232+
echo -n ' - ' >&$LOG
233+
echo "$test" | format "$BLUE" >&$LOG
234+
}
235+
236+
tap_notify_test_failed() {
237+
local test="$1"
238+
local message="$2"
239+
echo -n "not ok" | format "$RED" >&$LOG
240+
echo -n " - " >&$LOG
218241
echo "$test" >&$LOG
242+
[[ -z $message ]] || printf -- "$message\n" | sed -u -e 's/^/# /' >&$LOG
243+
}
244+
245+
tap_notify_stdout() {
246+
sed 's:^:# out> :' | format $GREEN >&$OUT
247+
}
248+
249+
tap_notify_stderr() {
250+
sed 's:^:# err> :' | format $RED >&$ERR
251+
}
252+
253+
tap_notify_stack() {
254+
sed 's:^:# :' | format "$YELLOW" >&$STACK
219255
}
220256

221257
output_format=default
@@ -250,9 +286,13 @@ case "$output_format" in
250286
default)
251287
;;
252288
tap)
253-
notify_suite_starting() { tap_notify_suite_starting "$@"; }
254-
notify_test_starting() { tap_notify_test_starting "$@"; }
255-
notify_test_succeeded() { tap_notify_test_succeeded "$@";}
289+
notify_suite_starting() { tap_notify_suite_starting "$@" ; }
290+
notify_test_starting() { tap_notify_test_starting "$@" ; }
291+
notify_test_succeeded() { tap_notify_test_succeeded "$@" ; }
292+
notify_test_failed() { tap_notify_test_failed "$@" ; }
293+
notify_stdout() { tap_notify_stdout "$@" ; }
294+
notify_stderr() { tap_notify_stderr "$@" ; }
295+
notify_stack() { tap_notify_stack "$@" ; }
256296
;;
257297
*)
258298
usage "unsupproted output format: $output_format"

tests/test_tap_format

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ test_bash_unit_rejects_invalid_format() {
1111
test_tap_format_for_one_succesfull_test() {
1212
assert_equals \
1313
"\
14-
# Running tests in code
15-
ok test_ok" \
16-
\
14+
# Running tests in code
15+
ok - test_ok\
16+
" \
1717
"$(bash_unit_out_for_code <<EOF
1818
test_ok() {
1919
assert true
@@ -22,6 +22,71 @@ EOF
2222
)"
2323
}
2424

25+
test_tap_format_for_one_failing_test() {
26+
assert_equals \
27+
"\
28+
# Running tests in code
29+
not ok - test_not_ok
30+
# code:2:test_not_ok()\
31+
" \
32+
"$(bash_unit_out_for_code <<EOF
33+
test_not_ok() {
34+
assert false
35+
}
36+
EOF
37+
)"
38+
}
39+
40+
test_tap_format_for_failing_test_with_stdout_stderr_outputs() {
41+
assert_equals \
42+
"\
43+
# Running tests in code
44+
not ok - test_not_ok
45+
# out> message on stdout
46+
# err> message on stderr
47+
# code:2:test_not_ok()\
48+
" \
49+
"$(bash_unit_out_for_code <<EOF
50+
test_not_ok() {
51+
assert_fail "echo message on stdout ; echo message on stderr >&2"
52+
}
53+
EOF
54+
)"
55+
}
56+
57+
test_assertion_message_is_tap_formatted() {
58+
assert_equals \
59+
"\
60+
# Running tests in code
61+
not ok - test_not_ok
62+
# obvious failure
63+
# code:2:test_not_ok()\
64+
" \
65+
"$(bash_unit_out_for_code <<EOF
66+
test_not_ok() {
67+
assert_fail true "obvious failure"
68+
}
69+
EOF
70+
)"
71+
}
72+
73+
test_multi_lines_assertion_message_is_tap_formatted() {
74+
assert_equals \
75+
"\
76+
# Running tests in code
77+
not ok - test_not_ok
78+
# obvious failure
79+
# on multiple lines
80+
# code:2:test_not_ok()\
81+
" \
82+
"$(bash_unit_out_for_code <<EOF
83+
test_not_ok() {
84+
assert_fail true "obvious failure\non multiple lines"
85+
}
86+
EOF
87+
)"
88+
}
89+
2590
bash_unit_out_for_code() {
2691
$BASH_UNIT -f tap <(cat) | sed -e 's:/dev/fd/[0-9]*:code:g'
2792
}

0 commit comments

Comments
 (0)