From f2446f11632dfb520a1bf331f1ce4c12eb51fdf1 Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Sun, 13 Jul 2025 00:16:13 +0200 Subject: [PATCH 1/2] feat: add --no-color option for enabling or disabling colorized output --- CHANGELOG.md | 1 + bashunit | 3 +++ docs/command-line.md | 12 +++++++++ docs/configuration.md | 12 +++++++++ src/colors.sh | 17 +++++++++++++ src/console_header.sh | 3 +++ src/env.sh | 7 ++++++ ...nit_without_path_env_nor_argument.snapshot | 3 +++ ...test_bashunit_should_display_help.snapshot | 3 +++ tests/unit/colors_test.sh | 25 +++++++++++++++++++ 10 files changed, 86 insertions(+) create mode 100644 tests/unit/colors_test.sh diff --git a/CHANGELOG.md b/CHANGELOG.md index bb99c930..355f5f47 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased - Add `--init` option to get you started quickly +- Add `--no-color` option for enabling or disabling colorized output - Fix process time always shows as 0 ms - Fixed terminal width detection first tput and fall back stty - Refactor clock optimizing the implementation used to get the time diff --git a/bashunit b/bashunit index 7be55d73..f0f8a3b2 100755 --- a/bashunit +++ b/bashunit @@ -78,6 +78,9 @@ while [[ $# -gt 0 ]]; do --no-parallel) export BASHUNIT_PARALLEL_RUN=false ;; + --no-color) + export BASHUNIT_COLOR=false + ;; -e|--env|--boot) # shellcheck disable=SC1090 source "$2" diff --git a/docs/command-line.md b/docs/command-line.md index ae5889e5..1d398997 100644 --- a/docs/command-line.md +++ b/docs/command-line.md @@ -340,6 +340,18 @@ Duration: 48 ms ``` ::: +## No color + +> `bashunit --no-color` + +Disables colored output. + +::: code-group +```bash [Example] +./bashunit ./tests --no-color +``` +::: + ## Version > `bashunit --version` diff --git a/docs/configuration.md b/docs/configuration.md index 59819fdc..baa69b56 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -247,6 +247,18 @@ BASHUNIT_VERBOSE=true ``` ::: +## Colors + +> `BASHUNIT_COLOR=true|false` + +Specify if you want to display colored output. `true` by default. + +::: code-group +```bash [Without colors] +BASHUNIT_COLOR=false +``` +::: + diff --git a/src/colors.sh b/src/colors.sh index 5ec2e202..d8b1edec 100644 --- a/src/colors.sh +++ b/src/colors.sh @@ -31,3 +31,20 @@ _COLOR_RETURN_SKIPPED="$(sgr 43)$_COLOR_BLACK$_COLOR_BOLD" _COLOR_RETURN_INCOMPLETE="$(sgr 46)$_COLOR_BLACK$_COLOR_BOLD" _COLOR_RETURN_SNAPSHOT="$(sgr 44)$_COLOR_BLACK$_COLOR_BOLD" _COLOR_DEFAULT="$(sgr 0)" + +if ! env::is_color_enabled; then + _COLOR_BOLD="" + _COLOR_FAINT="" + _COLOR_BLACK="" + _COLOR_FAILED="" + _COLOR_PASSED="" + _COLOR_SKIPPED="" + _COLOR_INCOMPLETE="" + _COLOR_SNAPSHOT="" + _COLOR_RETURN_ERROR="" + _COLOR_RETURN_SUCCESS="" + _COLOR_RETURN_SKIPPED="" + _COLOR_RETURN_INCOMPLETE="" + _COLOR_RETURN_SNAPSHOT="" + _COLOR_DEFAULT="" +fi diff --git a/src/console_header.sh b/src/console_header.sh index 0e912e72..b2950196 100644 --- a/src/console_header.sh +++ b/src/console_header.sh @@ -84,6 +84,9 @@ Options: -S, --stop-on-failure Force to stop the runner right after encountering one failing test. + --no-color + Disable colored output. + --debug Print all executed shell commands to the terminal. If a file-path is passed, it will redirect the output to that file. diff --git a/src/env.sh b/src/env.sh index ef2af138..3de63959 100644 --- a/src/env.sh +++ b/src/env.sh @@ -28,6 +28,7 @@ _DEFAULT_STOP_ON_FAILURE="false" _DEFAULT_SHOW_EXECUTION_TIME="true" _DEFAULT_VERBOSE="false" _DEFAULT_BENCH_MODE="false" +_DEFAULT_COLOR="true" : "${BASHUNIT_PARALLEL_RUN:=${PARALLEL_RUN:=$_DEFAULT_PARALLEL_RUN}}" : "${BASHUNIT_SHOW_HEADER:=${SHOW_HEADER:=$_DEFAULT_SHOW_HEADER}}" @@ -37,6 +38,7 @@ _DEFAULT_BENCH_MODE="false" : "${BASHUNIT_SHOW_EXECUTION_TIME:=${SHOW_EXECUTION_TIME:=$_DEFAULT_SHOW_EXECUTION_TIME}}" : "${BASHUNIT_VERBOSE:=${VERBOSE:=$_DEFAULT_VERBOSE}}" : "${BASHUNIT_BENCH_MODE:=${BENCH_MODE:=$_DEFAULT_BENCH_MODE}}" +: "${BASHUNIT_COLOR:=${COLOR:=$_DEFAULT_COLOR}}" function env::is_parallel_run_enabled() { [[ "$BASHUNIT_PARALLEL_RUN" == "true" ]] @@ -74,6 +76,10 @@ function env::is_bench_mode_enabled() { [[ "$BASHUNIT_BENCH_MODE" == "true" ]] } +function env::is_color_enabled() { + [[ "$BASHUNIT_COLOR" == "true" ]] +} + function env::active_internet_connection() { if ping -c 1 -W 3 google.com &> /dev/null; then return 0 @@ -111,6 +117,7 @@ function env::print_verbose() { "BASHUNIT_STOP_ON_FAILURE" "BASHUNIT_SHOW_EXECUTION_TIME" "BASHUNIT_VERBOSE" + "BASHUNIT_COLOR" ) local max_length=0 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 b43fb469..7b9c92f9 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 @@ -31,6 +31,9 @@ Options: -S, --stop-on-failure Force to stop the runner right after encountering one failing test. + --no-color + Disable colored output. + --debug Print all executed shell commands to the terminal. If a file-path is passed, it will redirect the output to that file. 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 43dd8ec2..a09b14b4 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 @@ -30,6 +30,9 @@ Options: -S, --stop-on-failure Force to stop the runner right after encountering one failing test. + --no-color + Disable colored output. + --debug Print all executed shell commands to the terminal. If a file-path is passed, it will redirect the output to that file. diff --git a/tests/unit/colors_test.sh b/tests/unit/colors_test.sh new file mode 100644 index 00000000..e11e9494 --- /dev/null +++ b/tests/unit/colors_test.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash + +function test_colors_disabled_via_env() { + local output + output=$(bash -c ' + export BASHUNIT_COLOR=false + source "$BASHUNIT_ROOT_DIR/src/globals.sh" + source "$BASHUNIT_ROOT_DIR/src/env.sh" + source "$BASHUNIT_ROOT_DIR/src/colors.sh" + printf "%s%s%s" "$_COLOR_PASSED" "$_COLOR_FAILED" "$_COLOR_DEFAULT" + ') + assert_empty "$output" +} + +function test_colors_enabled_by_default() { + local output + output=$(bash -c ' + unset BASHUNIT_COLOR + source "$BASHUNIT_ROOT_DIR/src/globals.sh" + source "$BASHUNIT_ROOT_DIR/src/env.sh" + source "$BASHUNIT_ROOT_DIR/src/colors.sh" + printf "%s" "$_COLOR_PASSED" + ') + assert_not_empty "$output" +} From 1e3ce4f0d38a5e2df8d008ad319b32606ea717a8 Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Sun, 13 Jul 2025 00:45:09 +0200 Subject: [PATCH 2/2] feat: add --color option --- CHANGELOG.md | 2 +- bashunit | 3 +++ docs/command-line.md | 12 ++++++++++++ src/console_header.sh | 4 ++-- ...t_bashunit_without_path_env_nor_argument.snapshot | 4 ++-- ...est_sh.test_bashunit_should_display_help.snapshot | 4 ++-- 6 files changed, 22 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 355f5f47..30fc41ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ## Unreleased - Add `--init` option to get you started quickly -- Add `--no-color` option for enabling or disabling colorized output +- Add `--color|--no-color` option for enabling or disabling colorized output - Fix process time always shows as 0 ms - Fixed terminal width detection first tput and fall back stty - Refactor clock optimizing the implementation used to get the time diff --git a/bashunit b/bashunit index f0f8a3b2..146fdb26 100755 --- a/bashunit +++ b/bashunit @@ -81,6 +81,9 @@ while [[ $# -gt 0 ]]; do --no-color) export BASHUNIT_COLOR=false ;; + --color) + export BASHUNIT_COLOR=true + ;; -e|--env|--boot) # shellcheck disable=SC1090 source "$2" diff --git a/docs/command-line.md b/docs/command-line.md index 1d398997..c91786ff 100644 --- a/docs/command-line.md +++ b/docs/command-line.md @@ -352,6 +352,18 @@ Disables colored output. ``` ::: +## Color + +> `bashunit --color` + +Forces colored output, overriding `.env` configuration. + +::: code-group +```bash [Example] +./bashunit ./tests --color +``` +::: + ## Version > `bashunit --version` diff --git a/src/console_header.sh b/src/console_header.sh index b2950196..6709750f 100644 --- a/src/console_header.sh +++ b/src/console_header.sh @@ -84,8 +84,8 @@ Options: -S, --stop-on-failure Force to stop the runner right after encountering one failing test. - --no-color - Disable colored output. + --color|--no-color + Toggle colored output. --debug Print all executed shell commands to the terminal. 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 7b9c92f9..710579f3 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 @@ -31,8 +31,8 @@ Options: -S, --stop-on-failure Force to stop the runner right after encountering one failing test. - --no-color - Disable colored output. + --color|--no-color + Toggle colored output. --debug Print all executed shell commands to the terminal. 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 a09b14b4..3a524199 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 @@ -30,8 +30,8 @@ Options: -S, --stop-on-failure Force to stop the runner right after encountering one failing test. - --no-color - Disable colored output. + --color|--no-color + Toggle colored output. --debug Print all executed shell commands to the terminal.