diff --git a/CHANGELOG.md b/CHANGELOG.md index 64ddd6de..fdef9491 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 - Add runtime check for Bash >= 3.2 ## [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 38e22b94..bd2e34f4 100755 --- a/bashunit +++ b/bashunit @@ -120,6 +120,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/runner.sh b/src/runner.sh index b6937620..165a9e54 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 } @@ -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 @@ -227,8 +227,12 @@ 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 - + 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. 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.