Skip to content

Commit

Permalink
Introduce a quiet mode
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrange committed Mar 4, 2024
1 parent 0e1131d commit 777714e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
5 changes: 5 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ _(by the way, the documentation you are reading is itself tested with bash-unit)
specify an alternative output format.
The only supported value is *tap*.

*-q*::
quiet mode.
Will only output the status of each test with no further
information even in case of failure.

ifndef::backend-manpage[]

== How to install *bash_unit*
Expand Down
38 changes: 34 additions & 4 deletions bash_unit
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ fail() {
local stderr=${3:-}

# shellcheck disable=2154
notify_test_failed "$__bash_unit_current_test__" "$message"
notify_test_failed "$__bash_unit_current_test__"
notify_message "$message"
[[ -n "$stdout" ]] && [ -s "$stdout" ] && notify_stdout < "$stdout"
[[ -n "$stderr" ]] && [ -s "$stderr" ] && notify_stderr < "$stderr"

Expand Down Expand Up @@ -372,12 +373,15 @@ text_format() {
echo
}
notify_test_failed() {
local message="$2"
echo -n "FAILURE" | pretty_failure
echo
}
notify_message() {
local message="$1"
# shellcheck disable=SC2059
[[ -z "$message" ]] || printf -- "$message\n"
}

notify_stdout() {
"$SED" 's:^:out> :' | color "$GREEN"
}
Expand Down Expand Up @@ -424,9 +428,11 @@ tap_format() {
}
notify_test_failed() {
local test="$1"
local message="$2"
echo -n "not ok" | pretty_failure -
echo "$test" | color "$BLUE"
}
notify_message() {
local message="$1"
[[ -z "$message" ]] || printf -- "%b\n" "$message" | "$SED" -u -e 's/^/# /'
}
notify_stdout() {
Expand All @@ -446,6 +452,21 @@ tap_format() {
}
}

quiet_mode() {
notify_message() {
:
}
notify_stdout() {
:
}
notify_stderr() {
:
}
notify_stack() {
:
}
}

skip_if() {
local condition="$1"
local pattern="$2"
Expand All @@ -457,12 +478,13 @@ skip_if() {
}

output_format=text
verbosity=normal
test_pattern=""
test_pattern_separator=""
skip_pattern=""
skip_pattern_separator=""
randomize=0
while getopts "vp:s:f:r" option
while getopts "vp:s:f:rq" option
do
case "$option" in
p)
Expand All @@ -483,6 +505,9 @@ do
echo "bash_unit $VERSION"
exit
;;
q)
verbosity=quiet
;;
?)
usage
;;
Expand All @@ -508,6 +533,11 @@ case "$output_format" in
;;
esac

if [[ "$verbosity" == quiet ]]
then
quiet_mode
fi

#run tests received as parameters
failure=0
for test_file in "$@"
Expand Down
18 changes: 18 additions & 0 deletions tests/test_cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,24 @@ Overall result: SUCCESS" \
"$bash_unit_output"
}

test_can_have_a_quiet_output() {
bash_unit_output=$($BASH_UNIT -q \
<(echo 'test_one() { echo -n ; }
test_two() { fail "this test fails" ; }
test_three() { fail ; }
') \
| "$SED" -e 's:/dev/fd/[0-9]*:test_file:' \
)

assert_equals "\
Running tests in test_file
Running test_one ... SUCCESS
Running test_three ... FAILURE
Running test_two ... FAILURE
Overall result: FAILURE" \
"$bash_unit_output"
}

test_fails_when_test_file_does_not_exist() {
assert_fails "$BASH_UNIT /not_exist/not_exist"
}
Expand Down
4 changes: 3 additions & 1 deletion tests/test_core.sh
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,8 @@ unmute_logs() {
notify_suite_starting() { echo "Running tests in $1" ; }
notify_test_starting () { echo -e -n "\tRunning $1... " ; }
notify_test_succeeded() { echo "SUCCESS" ; }
notify_test_failed () { echo "FAILURE" ; echo "$2" ; }
notify_test_failed () { echo "FAILURE" ; }
notify_message () { echo "$1" ; }
}

unmute_stack() {
Expand All @@ -333,6 +334,7 @@ mute() {
notify_test_starting () { echo -n ; }
notify_test_succeeded() { echo -n ; }
notify_test_failed () { echo -n ; }
notify_message () { echo -n ; }
notify_stack () { echo -n ; }
notify_stdout () { echo -n ; }
notify_stderr () { echo -n ; }
Expand Down

0 comments on commit 777714e

Please sign in to comment.