diff --git a/CHANGELOG.md b/CHANGELOG.md index bb99c930..30fc41ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased - Add `--init` option to get you started quickly +- 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 7be55d73..146fdb26 100755 --- a/bashunit +++ b/bashunit @@ -78,6 +78,12 @@ while [[ $# -gt 0 ]]; do --no-parallel) export BASHUNIT_PARALLEL_RUN=false ;; + --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 ae5889e5..c91786ff 100644 --- a/docs/command-line.md +++ b/docs/command-line.md @@ -340,6 +340,30 @@ Duration: 48 ms ``` ::: +## No color + +> `bashunit --no-color` + +Disables colored output. + +::: code-group +```bash [Example] +./bashunit ./tests --no-color +``` +::: + +## Color + +> `bashunit --color` + +Forces colored output, overriding `.env` configuration. + +::: code-group +```bash [Example] +./bashunit ./tests --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..6709750f 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. + --color|--no-color + Toggle 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..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,6 +31,9 @@ Options: -S, --stop-on-failure Force to stop the runner right after encountering one failing test. + --color|--no-color + Toggle 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..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,6 +30,9 @@ Options: -S, --stop-on-failure Force to stop the runner right after encountering one failing test. + --color|--no-color + Toggle 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" +}