Skip to content

Commit 980315c

Browse files
committed
Introduce a quiet mode
1 parent 0e1131d commit 980315c

File tree

4 files changed

+60
-5
lines changed

4 files changed

+60
-5
lines changed

README.adoc

+5
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ _(by the way, the documentation you are reading is itself tested with bash-unit)
5353
specify an alternative output format.
5454
The only supported value is *tap*.
5555

56+
*-q*::
57+
quiet mode.
58+
Will only output the status of each test with no further
59+
information even in case of failure.
60+
5661
ifndef::backend-manpage[]
5762

5863
== How to install *bash_unit*

bash_unit

+34-4
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ fail() {
4141
local stderr=${3:-}
4242

4343
# shellcheck disable=2154
44-
notify_test_failed "$__bash_unit_current_test__" "$message"
44+
notify_test_failed "$__bash_unit_current_test__"
45+
notify_message "$message"
4546
[[ -n "$stdout" ]] && [ -s "$stdout" ] && notify_stdout < "$stdout"
4647
[[ -n "$stderr" ]] && [ -s "$stderr" ] && notify_stderr < "$stderr"
4748

@@ -372,12 +373,15 @@ text_format() {
372373
echo
373374
}
374375
notify_test_failed() {
375-
local message="$2"
376376
echo -n "FAILURE" | pretty_failure
377377
echo
378+
}
379+
notify_message() {
380+
local message="$1"
378381
# shellcheck disable=SC2059
379382
[[ -z "$message" ]] || printf -- "$message\n"
380383
}
384+
381385
notify_stdout() {
382386
"$SED" 's:^:out> :' | color "$GREEN"
383387
}
@@ -424,9 +428,11 @@ tap_format() {
424428
}
425429
notify_test_failed() {
426430
local test="$1"
427-
local message="$2"
428431
echo -n "not ok" | pretty_failure -
429432
echo "$test" | color "$BLUE"
433+
}
434+
notify_message() {
435+
local message="$1"
430436
[[ -z "$message" ]] || printf -- "%b\n" "$message" | "$SED" -u -e 's/^/# /'
431437
}
432438
notify_stdout() {
@@ -446,6 +452,21 @@ tap_format() {
446452
}
447453
}
448454

455+
quiet_mode() {
456+
notify_message() {
457+
:
458+
}
459+
notify_stdout() {
460+
:
461+
}
462+
notify_stderr() {
463+
:
464+
}
465+
notify_stack() {
466+
:
467+
}
468+
}
469+
449470
skip_if() {
450471
local condition="$1"
451472
local pattern="$2"
@@ -457,12 +478,13 @@ skip_if() {
457478
}
458479

459480
output_format=text
481+
verbosity=normal
460482
test_pattern=""
461483
test_pattern_separator=""
462484
skip_pattern=""
463485
skip_pattern_separator=""
464486
randomize=0
465-
while getopts "vp:s:f:r" option
487+
while getopts "vp:s:f:rq" option
466488
do
467489
case "$option" in
468490
p)
@@ -483,6 +505,9 @@ do
483505
echo "bash_unit $VERSION"
484506
exit
485507
;;
508+
q)
509+
verbosity=quiet
510+
;;
486511
?)
487512
usage
488513
;;
@@ -508,6 +533,11 @@ case "$output_format" in
508533
;;
509534
esac
510535

536+
if [[ "$verbosity" == quiet ]]
537+
then
538+
quiet_mode
539+
fi
540+
511541
#run tests received as parameters
512542
failure=0
513543
for test_file in "$@"

tests/test_cli.sh

+18
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,24 @@ Overall result: SUCCESS" \
114114
"$bash_unit_output"
115115
}
116116

117+
test_can_have_a_quiet_output() {
118+
bash_unit_output=$($BASH_UNIT -q \
119+
<(echo 'test_one() { echo -n ; }
120+
test_two() { fail "this test fails" ; }
121+
test_three() { fail ; }
122+
') \
123+
| "$SED" -e 's:/dev/fd/[0-9]*:test_file:' \
124+
)
125+
126+
assert_equals "\
127+
Running tests in test_file
128+
Running test_one ... SUCCESS
129+
Running test_three ... FAILURE
130+
Running test_two ... FAILURE
131+
Overall result: FAILURE" \
132+
"$bash_unit_output"
133+
}
134+
117135
test_fails_when_test_file_does_not_exist() {
118136
assert_fails "$BASH_UNIT /not_exist/not_exist"
119137
}

tests/test_core.sh

+3-1
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ unmute_logs() {
313313
notify_suite_starting() { echo "Running tests in $1" ; }
314314
notify_test_starting () { echo -e -n "\tRunning $1... " ; }
315315
notify_test_succeeded() { echo "SUCCESS" ; }
316-
notify_test_failed () { echo "FAILURE" ; echo "$2" ; }
316+
notify_test_failed () { echo "FAILURE" ; }
317+
notify_message () { echo "$1" ; }
317318
}
318319

319320
unmute_stack() {
@@ -333,6 +334,7 @@ mute() {
333334
notify_test_starting () { echo -n ; }
334335
notify_test_succeeded() { echo -n ; }
335336
notify_test_failed () { echo -n ; }
337+
notify_message () { echo -n ; }
336338
notify_stack () { echo -n ; }
337339
notify_stdout () { echo -n ; }
338340
notify_stderr () { echo -n ; }

0 commit comments

Comments
 (0)