Skip to content

Commit eb4253a

Browse files
committed
Manage tap format (#28) for succesfull tests only.
bash_unit -t tap
1 parent ce3fa2a commit eb4253a

File tree

2 files changed

+92
-11
lines changed

2 files changed

+92
-11
lines changed

bash_unit

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,22 @@ run_test_suite() {
148148
run_test() {
149149
set -e
150150
local TEST=$1
151-
echo -n "Running $TEST... " | format "$BLUE"
152-
$TEST && format "$GREEN" "SUCCESS"
151+
notify_test_starting "$TEST"
152+
$TEST && notify_test_succeeded "$TEST" >&$LOG
153153
}
154154

155+
usage() {
156+
echo "$1" >&2
157+
echo "$0 [-f <output format<] [-p <pattern1>] [-p <pattern2>]... <test_file1> <test_file2>..." >&2
158+
echo >&2
159+
echo "Runs tests in test files that match <pattern>s" >&2
160+
echo "<output format> is optionnal only supported value is tap" >&2
161+
echo "See https://github.com/pgrange/bash_unit" >&2
162+
exit 1
163+
}
164+
165+
# Formating
166+
155167
print_stack() {
156168
local i=1
157169
while ! [ -z ${BASH_SOURCE[$i]} ]
@@ -174,24 +186,50 @@ format() {
174186
if [ -t 1 ] ; then echo -en "$NOCOLOR" ; fi
175187
}
176188

177-
usage() {
178-
echo "$1" >&2
179-
echo "$0 [-p <pattern1>] [-p <pattern2>]... <test_file1> <test_file2>..." >&2
180-
echo >&2
181-
echo "Runs tests in test files that match patterns" >&2
182-
echo "See https://github.com/pgrange/bash_unit" >&2
183-
exit 1
189+
notify_suite_starting() {
190+
test_file="$1"
191+
echo "Running tests in $test_file" >&$LOG
184192
}
185193

194+
notify_test_starting() {
195+
local test="$1"
196+
echo -n "Running $test... " | format "$BLUE" >&$LOG
197+
}
198+
199+
notify_test_succeeded() {
200+
local test="$1"
201+
format "$GREEN" "SUCCESS" >&$LOG
202+
}
203+
204+
tap_notify_suite_starting() {
205+
test_file="$1"
206+
echo "# Running tests in $test_file" >&$LOG
207+
}
208+
209+
tap_notify_test_starting() {
210+
echo -n
211+
}
212+
213+
tap_notify_test_succeeded() {
214+
local test="$1"
215+
echo -n "ok" | format "$GREEN" >&$LOG
216+
echo -n " " >&$LOG
217+
echo "$test" >&$LOG
218+
}
219+
220+
output_format=default
186221
test_pattern=""
187222
separator=""
188-
while getopts "p:" option
223+
while getopts "p:f:" option
189224
do
190225
case "$option" in
191226
p)
192227
test_pattern="${test_pattern}${separator}${OPTARG}"
193228
separator="|"
194229
;;
230+
f)
231+
output_format="${OPTARG}"
232+
;;
195233
?|:)
196234
usage
197235
;;
@@ -206,11 +244,25 @@ do
206244
test -r $test_file || usage "can not read file: $test_file"
207245
done
208246

247+
#set output format
248+
case "$output_format" in
249+
default)
250+
;;
251+
tap)
252+
notify_suite_starting() { tap_notify_suite_starting "$@"; }
253+
notify_test_starting() { tap_notify_test_starting "$@"; }
254+
notify_test_succeeded() { tap_notify_test_succeeded "$@";}
255+
;;
256+
*)
257+
usage "unsupproted output format: $output_format"
258+
;;
259+
esac
260+
209261
#run tests received as parameters
210262
failure=0
211263
for test_file in "$@"
212264
do
213-
echo "Running tests in $test_file"
265+
notify_suite_starting "$test_file"
214266
(
215267
source "$test_file"
216268
cd $(dirname "$test_file")

tests/test_tap_format

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
3+
test_bash_unit_accepts_tap_format_option() {
4+
assert "$BASH_UNIT -f tap"
5+
}
6+
7+
test_bash_unit_rejects_invalid_format() {
8+
assert_fail "$BASH_UNIT -f invalid_format"
9+
}
10+
11+
test_tap_format_for_one_succesfull_test() {
12+
assert_equals \
13+
"\
14+
# Running tests in code
15+
ok test_ok" \
16+
\
17+
"$(bash_unit_out_for_code <<EOF
18+
test_ok() {
19+
assert true
20+
}
21+
EOF
22+
)"
23+
}
24+
25+
bash_unit_out_for_code() {
26+
$BASH_UNIT -f tap <(cat) | sed -e 's:/dev/fd/[0-9]*:code:g'
27+
}
28+
29+
BASH_UNIT=../bash_unit

0 commit comments

Comments
 (0)