From 587752f9f55abccf8c39d0d740efc051fea71556 Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Sun, 27 Jul 2025 18:03:16 +0200 Subject: [PATCH 1/5] feat: add --quiet|--failures-only option --- CHANGELOG.md | 1 + bashunit | 3 +++ docs/command-line.md | 14 ++++++++++++++ docs/configuration.md | 14 ++++++++++++++ src/console_header.sh | 3 +++ src/env.sh | 7 +++++++ src/state.sh | 4 ++++ ...bashunit_without_path_env_nor_argument.snapshot | 3 +++ ...t_sh.test_bashunit_should_display_help.snapshot | 3 +++ 9 files changed, 52 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a6ea559d..a71d0cd0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Update docs mocks usage - Add support for `.bash` test files +- Add `-q|--quiet|--failures-only` mode ## [0.22.3](https://github.com/TypedDevs/bashunit/compare/0.22.2...0.22.3) - 2025-07-27 diff --git a/bashunit b/bashunit index bc420270..b157dedf 100755 --- a/bashunit +++ b/bashunit @@ -94,6 +94,9 @@ while [[ $# -gt 0 ]]; do --no-output) export BASHUNIT_NO_OUTPUT=true ;; + -q|--quiet|--failures-only) + export BASHUNIT_FAILURES_ONLY=true + ;; -vvv|--verbose) export BASHUNIT_VERBOSE=true ;; diff --git a/docs/command-line.md b/docs/command-line.md index 959a0320..eccac5f5 100644 --- a/docs/command-line.md +++ b/docs/command-line.md @@ -352,6 +352,18 @@ Run tests without printing any output. Exit code will be `0` if all tests pass o ``` ::: +## Failures only + +> `bashunit --failures-only` + +Suppress all per-test output. Failures and summary are shown only after all tests finish. + +::: code-group +```bash [Example] +./bashunit tests --failures-only +``` +::: + ## Version > `bashunit --version` @@ -442,6 +454,8 @@ Options: Suppress all console output; rely on exit code. -s, --simple | --detailed Choose console output style (default: detailed). + -q, --quiet | --failures-only + Suppress per-test output and show failures at the end. -S, --stop-on-failure Stop execution immediately on the first failing test. --upgrade diff --git a/docs/configuration.md b/docs/configuration.md index ddafc6f5..44f8992e 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -266,6 +266,20 @@ BASHUNIT_NO_OUTPUT=true ``` ::: +## Failures only + +> `BASHUNIT_FAILURES_ONLY=true|false` + +Suppress all per-test output. Failures and summary are shown only after all tests finish. Defaults to `false`. + +Similar as using `--failures-only` option on the [command line](/command-line#failures-only). + +::: code-group +```bash [Example] +BASHUNIT_FAILURES_ONLY=true +``` +::: + diff --git a/src/console_header.sh b/src/console_header.sh index 8a6265c2..bed894d7 100644 --- a/src/console_header.sh +++ b/src/console_header.sh @@ -100,6 +100,9 @@ Options: -s, --simple | --detailed Choose console output style (default: detailed). + -q, --quiet | --failures-only + Suppress per-test output and show failures at the end. + -S, --stop-on-failure Stop execution immediately on the first failing test. diff --git a/src/env.sh b/src/env.sh index d13b552a..67dd601e 100644 --- a/src/env.sh +++ b/src/env.sh @@ -30,6 +30,7 @@ _DEFAULT_VERBOSE="false" _DEFAULT_BENCH_MODE="false" _DEFAULT_NO_OUTPUT="false" _DEFAULT_INTERNAL_LOG="false" +_DEFAULT_FAILURES_ONLY="false" : "${BASHUNIT_PARALLEL_RUN:=${PARALLEL_RUN:=$_DEFAULT_PARALLEL_RUN}}" : "${BASHUNIT_SHOW_HEADER:=${SHOW_HEADER:=$_DEFAULT_SHOW_HEADER}}" @@ -41,6 +42,7 @@ _DEFAULT_INTERNAL_LOG="false" : "${BASHUNIT_BENCH_MODE:=${BENCH_MODE:=$_DEFAULT_BENCH_MODE}}" : "${BASHUNIT_NO_OUTPUT:=${NO_OUTPUT:=$_DEFAULT_NO_OUTPUT}}" : "${BASHUNIT_INTERNAL_LOG:=${INTERNAL_LOG:=$_DEFAULT_INTERNAL_LOG}}" +: "${BASHUNIT_FAILURES_ONLY:=${FAILURES_ONLY:=$_DEFAULT_FAILURES_ONLY}}" function env::is_parallel_run_enabled() { [[ "$BASHUNIT_PARALLEL_RUN" == "true" ]] @@ -86,6 +88,10 @@ function env::is_no_output_enabled() { [[ "$BASHUNIT_NO_OUTPUT" == "true" ]] } +function env::is_failures_only_enabled() { + [[ "$BASHUNIT_FAILURES_ONLY" == "true" ]] +} + function env::active_internet_connection() { if [[ "${BASHUNIT_NO_NETWORK:-}" == "true" ]]; then return 1 @@ -134,6 +140,7 @@ function env::print_verbose() { "BASHUNIT_STOP_ON_FAILURE" "BASHUNIT_SHOW_EXECUTION_TIME" "BASHUNIT_VERBOSE" + "BASHUNIT_FAILURES_ONLY" ) local max_length=0 diff --git a/src/state.sh b/src/state.sh index 8d8fc7ed..d89a5fe1 100644 --- a/src/state.sh +++ b/src/state.sh @@ -193,6 +193,10 @@ function state::print_line() { state::add_test_output "[$type]$line" + if env::is_failures_only_enabled; then + return + fi + if ! env::is_simple_output_enabled; then printf "%s\n" "$line" return diff --git a/tests/acceptance/snapshots/bashunit_path_test_sh.test_bashunit_without_path_env_nor_argument.snapshot b/tests/acceptance/snapshots/bashunit_path_test_sh.test_bashunit_without_path_env_nor_argument.snapshot index 94532329..72d57969 100644 --- a/tests/acceptance/snapshots/bashunit_path_test_sh.test_bashunit_without_path_env_nor_argument.snapshot +++ b/tests/acceptance/snapshots/bashunit_path_test_sh.test_bashunit_without_path_env_nor_argument.snapshot @@ -43,6 +43,9 @@ Options: -s, --simple | --detailed Choose console output style (default: detailed). + -q, --quiet | --failures-only + Suppress per-test output and show failures at the end. + -S, --stop-on-failure Stop execution immediately on the first failing test. diff --git a/tests/acceptance/snapshots/bashunit_test_sh.test_bashunit_should_display_help.snapshot b/tests/acceptance/snapshots/bashunit_test_sh.test_bashunit_should_display_help.snapshot index a3ba73ab..064fbd38 100644 --- a/tests/acceptance/snapshots/bashunit_test_sh.test_bashunit_should_display_help.snapshot +++ b/tests/acceptance/snapshots/bashunit_test_sh.test_bashunit_should_display_help.snapshot @@ -42,6 +42,9 @@ Options: -s, --simple | --detailed Choose console output style (default: detailed). + -q, --quiet | --failures-only + Suppress per-test output and show failures at the end. + -S, --stop-on-failure Stop execution immediately on the first failing test. From 0783b9fde74d204f9a5b3a8c44ee5f27f2785fc3 Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Sun, 27 Jul 2025 18:10:04 +0200 Subject: [PATCH 2/5] fix: do not render file header when is_failures_only_enabled --- src/runner.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runner.sh b/src/runner.sh index b6937620..305df259 100755 --- a/src/runner.sh +++ b/src/runner.sh @@ -165,7 +165,7 @@ function runner::render_running_file_header() { internal_log "Running file" "$script" - if parallel::is_enabled; then + if parallel::is_enabled || env::is_failures_only_enabled; then return fi From ef5f28028c48af75b0ecd51ed4f0db47b8cf6c9a Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Sun, 27 Jul 2025 18:51:10 +0200 Subject: [PATCH 3/5] feat: skip new lines when failures only enabled --- src/runner.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/runner.sh b/src/runner.sh index 305df259..217b72bb 100755 --- a/src/runner.sh +++ b/src/runner.sh @@ -126,7 +126,7 @@ function runner::call_test_functions() { unset fn_name done - if ! env::is_simple_output_enabled; then + if ! env::is_simple_output_enabled && ! env::is_failures_only_enabled; then echo "" fi } @@ -155,7 +155,7 @@ function runner::call_bench_functions() { unset fn_name done - if ! env::is_simple_output_enabled; then + if ! env::is_simple_output_enabled && ! env::is_failures_only_enabled; then echo "" fi } From 9cc3f1aaa82116c5294262e5bbc6778172bda80d Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Sun, 27 Jul 2025 19:07:32 +0200 Subject: [PATCH 4/5] fix: failures only flag within test exec result --- src/runner.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/runner.sh b/src/runner.sh index 217b72bb..c98033d0 100755 --- a/src/runner.sh +++ b/src/runner.sh @@ -227,7 +227,10 @@ function runner::run_test() { # 2>&1: Redirects the std-error (FD 2) to the std-output (FD 1). # points to the original std-output. + local failures_only_backup="$BASHUNIT_FAILURES_ONLY" + export BASHUNIT_FAILURES_ONLY=false "$fn_name" "$@" 2>&1 + export BASHUNIT_FAILURES_ONLY="$failures_only_backup" ) From d64814d892a48843f9d91135a71883f7e141f7ff Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Sun, 27 Jul 2025 21:00:12 +0200 Subject: [PATCH 5/5] fix: ensured exit codes are preserved when running individual tests --- src/runner.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/runner.sh b/src/runner.sh index c98033d0..165a9e54 100755 --- a/src/runner.sh +++ b/src/runner.sh @@ -230,8 +230,9 @@ function runner::run_test() { local failures_only_backup="$BASHUNIT_FAILURES_ONLY" export BASHUNIT_FAILURES_ONLY=false "$fn_name" "$@" 2>&1 + local exit_code=$? export BASHUNIT_FAILURES_ONLY="$failures_only_backup" - + exit "$exit_code" ) # Closes FD 3, which was used temporarily to hold the original stdout.