From 777714ef2ed17e8891770701459af72cf3185eef Mon Sep 17 00:00:00 2001 From: Pascal Grange Date: Mon, 4 Mar 2024 10:04:31 +0100 Subject: [PATCH] Introduce a quiet mode --- README.adoc | 5 +++++ bash_unit | 38 ++++++++++++++++++++++++++++++++++---- tests/test_cli.sh | 18 ++++++++++++++++++ tests/test_core.sh | 4 +++- 4 files changed, 60 insertions(+), 5 deletions(-) diff --git a/README.adoc b/README.adoc index d72bccc..b6a18d5 100644 --- a/README.adoc +++ b/README.adoc @@ -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* diff --git a/bash_unit b/bash_unit index 0bf91e9..253b2a2 100755 --- a/bash_unit +++ b/bash_unit @@ -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" @@ -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" } @@ -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() { @@ -446,6 +452,21 @@ tap_format() { } } +quiet_mode() { + notify_message() { + : + } + notify_stdout() { + : + } + notify_stderr() { + : + } + notify_stack() { + : + } +} + skip_if() { local condition="$1" local pattern="$2" @@ -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) @@ -483,6 +505,9 @@ do echo "bash_unit $VERSION" exit ;; + q) + verbosity=quiet + ;; ?) usage ;; @@ -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 "$@" diff --git a/tests/test_cli.sh b/tests/test_cli.sh index 4f04e01..bcf0fca 100644 --- a/tests/test_cli.sh +++ b/tests/test_cli.sh @@ -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" } diff --git a/tests/test_core.sh b/tests/test_core.sh index 9db9c05..42d9fab 100644 --- a/tests/test_core.sh +++ b/tests/test_core.sh @@ -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() { @@ -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 ; }