Skip to content

Add --no-color option #441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions bashunit
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
24 changes: 24 additions & 0 deletions docs/command-line.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
12 changes: 12 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
:::

<script setup>
import pkg from '../package.json'
</script>
17 changes: 17 additions & 0 deletions src/colors.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions src/console_header.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 <?file-path>
Print all executed shell commands to the terminal.
If a file-path is passed, it will redirect the output to that file.
Expand Down
7 changes: 7 additions & 0 deletions src/env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}}"
Expand All @@ -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" ]]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -111,6 +117,7 @@ function env::print_verbose() {
"BASHUNIT_STOP_ON_FAILURE"
"BASHUNIT_SHOW_EXECUTION_TIME"
"BASHUNIT_VERBOSE"
"BASHUNIT_COLOR"
)

local max_length=0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <?file-path>
Print all executed shell commands to the terminal.
If a file-path is passed, it will redirect the output to that file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <?file-path>
Print all executed shell commands to the terminal.
If a file-path is passed, it will redirect the output to that file.
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/colors_test.sh
Original file line number Diff line number Diff line change
@@ -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"
}
Loading