Skip to content

Commit 7989c24

Browse files
committed
ADD FORCE_COLOR environment variable
This is usefull if you want to force color output even if out of a tty. For instance when running tests in a gitlab-runner.
1 parent ffcb098 commit 7989c24

File tree

4 files changed

+30
-20
lines changed

4 files changed

+30
-20
lines changed

bash_unit

+26-16
Original file line numberDiff line numberDiff line change
@@ -165,25 +165,20 @@ usage() {
165165

166166
# Formating
167167

168-
color() {
169-
local color=$1
170-
shift
171-
if [ -t 1 ] ; then echo -en "$color" ; fi
172-
if [ $# -gt 0 ]
173-
then
174-
echo $*
175-
else
176-
cat
177-
fi
178-
if [ -t 1 ] ; then echo -en "$NOCOLOR" ; fi
168+
pretty_success() {
169+
pretty_format "$GREEN" "\u2713" "$1"
170+
}
171+
172+
pretty_failure() {
173+
pretty_format "$RED" "\u2717" "$1"
179174
}
180175

181176
pretty_format() {
182177
local color="$1"
183178
local pretty_symbol="$2"
184179
local alt_symbol="$3"
185180
local term_utf8=false
186-
if test -t 1 && [[ "$LANG" =~ .i*UTF-8.* ]]
181+
if is_terminal && [[ "$LANG" =~ .*UTF-8.* ]]
187182
then
188183
term_utf8=true
189184
fi
@@ -198,12 +193,27 @@ pretty_format() {
198193
) | color "$color"
199194
}
200195

201-
pretty_success() {
202-
pretty_format "$GREEN" "\u2713" "$1"
196+
color() {
197+
_start_color() {
198+
if is_terminal ; then echo -en "$color" ; fi
199+
}
200+
_stop_color() {
201+
if is_terminal ; then echo -en "$NOCOLOR" ; fi
202+
}
203+
local color=$1
204+
shift
205+
_start_color
206+
if [ $# -gt 0 ]
207+
then
208+
echo $*
209+
else
210+
cat
211+
fi
212+
_stop_color
203213
}
204214

205-
pretty_failure() {
206-
pretty_format "$RED" "\u2717" "$1"
215+
is_terminal() {
216+
[ -t 1 ] || [[ "${FORCE_COLOR}" == true ]]
207217
}
208218

209219
text_format() {

tests/test_bash_unit.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -313,4 +313,4 @@ mute() {
313313
notify_stderr () { echo -n ; }
314314
}
315315

316-
BASH_UNIT=../bash_unit
316+
BASH_UNIT="eval FORCE_COLOR=false ../bash_unit"

tests/test_doc.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
TEST_PATTERN='```bash|```test'
44
OUTPUT_PATTERN='```output'
5-
LANG=C
5+
LANG=C.UTF-8
66

7-
BASH_UNIT=./bash_unit
7+
BASH_UNIT="eval FORCE_COLOR=false ./bash_unit"
88

99
prepare_tests() {
1010
mkdir /tmp/$$

tests/test_tap_format

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,4 @@ bash_unit_out_for_code() {
9191
$BASH_UNIT -f tap <(cat) | sed -e 's:/dev/fd/[0-9]*:code:g'
9292
}
9393

94-
BASH_UNIT=../bash_unit
94+
BASH_UNIT="eval FORCE_COLOR=false ../bash_unit"

0 commit comments

Comments
 (0)