Skip to content

Commit d5c1b7c

Browse files
rhansengitster
authored andcommitted
test-lib.sh: fix color support when tput needs ~/.terminfo
If tput needs ~/.terminfo for the current $TERM, then tput will succeed before HOME is changed to $TRASH_DIRECTORY (causing color to be set to 't') but fail afterward. One possible way to fix this is to treat HOME like TERM: back up the original value and temporarily restore it before say_color() runs tput. Instead, pre-compute and save the color control sequences before changing either TERM or HOME. Use the saved control sequences in say_color() rather than call tput each time. This avoids the need to back up and restore the TERM and HOME variables, and it avoids the overhead of a subshell and two invocations of tput per call to say_color(). Signed-off-by: Richard Hansen <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ca92a66 commit d5c1b7c

File tree

1 file changed

+28
-29
lines changed

1 file changed

+28
-29
lines changed

t/test-lib.sh

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
# You should have received a copy of the GNU General Public License
1616
# along with this program. If not, see http://www.gnu.org/licenses/ .
1717

18-
# Keep the original TERM for say_color
19-
ORIGINAL_TERM=$TERM
20-
2118
# Test the binaries we have just built. The tests are kept in
2219
# t/ subdirectory and are run in 'trash directory' subdirectory.
2320
if test -z "$TEST_DIRECTORY"
@@ -68,12 +65,12 @@ done,*)
6865
esac
6966

7067
# For repeatability, reset the environment to known value.
68+
# TERM is sanitized below, after saving color control sequences.
7169
LANG=C
7270
LC_ALL=C
7371
PAGER=cat
7472
TZ=UTC
75-
TERM=dumb
76-
export LANG LC_ALL PAGER TERM TZ
73+
export LANG LC_ALL PAGER TZ
7774
EDITOR=:
7875
# A call to "unset" with no arguments causes at least Solaris 10
7976
# /usr/xpg4/bin/sh and /bin/ksh to bail out. So keep the unsets
@@ -184,9 +181,7 @@ export _x05 _x40 _z40 LF u200c
184181
# This test checks if command xyzzy does the right thing...
185182
# '
186183
# . ./test-lib.sh
187-
test "x$ORIGINAL_TERM" != "xdumb" && (
188-
TERM=$ORIGINAL_TERM &&
189-
export TERM &&
184+
test "x$TERM" != "xdumb" && (
190185
test -t 1 &&
191186
tput bold >/dev/null 2>&1 &&
192187
tput setaf 1 >/dev/null 2>&1 &&
@@ -260,29 +255,30 @@ fi
260255

261256
if test -n "$color"
262257
then
258+
# Save the color control sequences now rather than run tput
259+
# each time say_color() is called. This is done for two
260+
# reasons:
261+
# * TERM will be changed to dumb
262+
# * HOME will be changed to a temporary directory and tput
263+
# might need to read ~/.terminfo from the original HOME
264+
# directory to get the control sequences
265+
# Note: This approach assumes the control sequences don't end
266+
# in a newline for any terminal of interest (command
267+
# substitutions strip trailing newlines). Given that most
268+
# (all?) terminals in common use are related to ECMA-48, this
269+
# shouldn't be a problem.
270+
say_color_error=$(tput bold; tput setaf 1) # bold red
271+
say_color_skip=$(tput setaf 4) # blue
272+
say_color_warn=$(tput setaf 3) # brown/yellow
273+
say_color_pass=$(tput setaf 2) # green
274+
say_color_info=$(tput setaf 6) # cyan
275+
say_color_reset=$(tput sgr0)
276+
say_color_="" # no formatting for normal text
263277
say_color () {
264-
(
265-
TERM=$ORIGINAL_TERM
266-
export TERM
267-
case "$1" in
268-
error)
269-
tput bold; tput setaf 1;; # bold red
270-
skip)
271-
tput setaf 4;; # blue
272-
warn)
273-
tput setaf 3;; # brown/yellow
274-
pass)
275-
tput setaf 2;; # green
276-
info)
277-
tput setaf 6;; # cyan
278-
*)
279-
test -n "$quiet" && return;;
280-
esac
278+
test -z "$1" && test -n "$quiet" && return
279+
eval "say_color_color=\$say_color_$1"
281280
shift
282-
printf "%s" "$*"
283-
tput sgr0
284-
echo
285-
)
281+
printf "%s\\n" "$say_color_color$*$say_color_reset"
286282
}
287283
else
288284
say_color() {
@@ -292,6 +288,9 @@ else
292288
}
293289
fi
294290

291+
TERM=dumb
292+
export TERM
293+
295294
error () {
296295
say_color error "error: $*"
297296
GIT_EXIT_OK=t

0 commit comments

Comments
 (0)