Skip to content

Commit aa0b816

Browse files
committed
Merge branch 'rh/test-color-avoid-terminfo-in-original-home' into maint
An ancient test framework enhancement to allow color was not entirely correct; this makes it work even when tput needs to read from the ~/.terminfo under the user's real HOME directory. * rh/test-color-avoid-terminfo-in-original-home: test-lib.sh: fix color support when tput needs ~/.terminfo Revert "test-lib.sh: do tests for color support after changing HOME"
2 parents 7c69600 + d5c1b7c commit aa0b816

File tree

1 file changed

+47
-52
lines changed

1 file changed

+47
-52
lines changed

t/test-lib.sh

Lines changed: 47 additions & 52 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
@@ -181,8 +178,14 @@ export _x05 _x40 _z40 LF u200c
181178
# This test checks if command xyzzy does the right thing...
182179
# '
183180
# . ./test-lib.sh
181+
test "x$TERM" != "xdumb" && (
182+
test -t 1 &&
183+
tput bold >/dev/null 2>&1 &&
184+
tput setaf 1 >/dev/null 2>&1 &&
185+
tput sgr0 >/dev/null 2>&1
186+
) &&
187+
color=t
184188

185-
unset color
186189
while test "$#" -ne 0
187190
do
188191
case "$1" in
@@ -253,6 +256,44 @@ then
253256
verbose=t
254257
fi
255258

259+
if test -n "$color"
260+
then
261+
# Save the color control sequences now rather than run tput
262+
# each time say_color() is called. This is done for two
263+
# reasons:
264+
# * TERM will be changed to dumb
265+
# * HOME will be changed to a temporary directory and tput
266+
# might need to read ~/.terminfo from the original HOME
267+
# directory to get the control sequences
268+
# Note: This approach assumes the control sequences don't end
269+
# in a newline for any terminal of interest (command
270+
# substitutions strip trailing newlines). Given that most
271+
# (all?) terminals in common use are related to ECMA-48, this
272+
# shouldn't be a problem.
273+
say_color_error=$(tput bold; tput setaf 1) # bold red
274+
say_color_skip=$(tput setaf 4) # blue
275+
say_color_warn=$(tput setaf 3) # brown/yellow
276+
say_color_pass=$(tput setaf 2) # green
277+
say_color_info=$(tput setaf 6) # cyan
278+
say_color_reset=$(tput sgr0)
279+
say_color_="" # no formatting for normal text
280+
say_color () {
281+
test -z "$1" && test -n "$quiet" && return
282+
eval "say_color_color=\$say_color_$1"
283+
shift
284+
printf "%s\\n" "$say_color_color$*$say_color_reset"
285+
}
286+
else
287+
say_color() {
288+
test -z "$1" && test -n "$quiet" && return
289+
shift
290+
printf "%s\n" "$*"
291+
}
292+
fi
293+
294+
TERM=dumb
295+
export TERM
296+
256297
error () {
257298
say_color error "error: $*"
258299
GIT_EXIT_OK=t
@@ -829,52 +870,6 @@ HOME="$TRASH_DIRECTORY"
829870
GNUPGHOME="$HOME/gnupg-home-not-used"
830871
export HOME GNUPGHOME
831872

832-
# run the tput tests *after* changing HOME (in case ncurses needs
833-
# ~/.terminfo for $TERM)
834-
test -n "${color+set}" || test "x$ORIGINAL_TERM" != "xdumb" && (
835-
TERM=$ORIGINAL_TERM &&
836-
export TERM &&
837-
test -t 1 &&
838-
tput bold >/dev/null 2>&1 &&
839-
tput setaf 1 >/dev/null 2>&1 &&
840-
tput sgr0 >/dev/null 2>&1
841-
) &&
842-
color=t
843-
844-
if test -n "$color"
845-
then
846-
say_color () {
847-
(
848-
TERM=$ORIGINAL_TERM
849-
export TERM
850-
case "$1" in
851-
error)
852-
tput bold; tput setaf 1;; # bold red
853-
skip)
854-
tput setaf 4;; # blue
855-
warn)
856-
tput setaf 3;; # brown/yellow
857-
pass)
858-
tput setaf 2;; # green
859-
info)
860-
tput setaf 6;; # cyan
861-
*)
862-
test -n "$quiet" && return;;
863-
esac
864-
shift
865-
printf "%s" "$*"
866-
tput sgr0
867-
echo
868-
)
869-
}
870-
else
871-
say_color() {
872-
test -z "$1" && test -n "$quiet" && return
873-
shift
874-
printf "%s\n" "$*"
875-
}
876-
fi
877-
878873
if test -z "$TEST_NO_CREATE_REPO"
879874
then
880875
test_create_repo "$TRASH_DIRECTORY"

0 commit comments

Comments
 (0)