Skip to content

Commit 2d3ca21

Browse files
jrngitster
authored andcommitted
t7006-pager: if stdout is not a terminal, make a new one
Testing pagination requires (fake or real) access to a terminal so we can see whether the pagination automatically kicks in, which makes it hard to get good coverage when running tests without --verbose. There are a number of ways to work around that: - Replace all isatty calls with calls to a custom xisatty wrapper that usually checks for a terminal but can be overridden for tests. This would be workable, but it would require implementing xisatty separately in three languages (C, shell, and perl) and making sure that any code that is to be tested always uses the wrapper. - Redirect stdout to /dev/tty. This would be problematic because there might be no terminal available, and even if a terminal is available, it might not be appropriate to spew output to it. - Create a new pseudo-terminal on the fly and capture its output. This patch implements the third approach. The new test-terminal.perl helper uses IO::Pty from Expect.pm to create a terminal and executes the program specified by its arguments with that terminal as stdout. If the IO::Pty module is missing or not working on a system, the test script will maintain its old behavior (skipping most of its tests unless GIT_TEST_OPTS includes --verbose). Signed-off-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 60b6e22 commit 2d3ca21

File tree

2 files changed

+82
-11
lines changed

2 files changed

+82
-11
lines changed

t/t7006-pager.sh

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,31 @@ test_description='Test automatic use of a pager.'
55
. ./test-lib.sh
66

77
rm -f stdout_is_tty
8-
test_expect_success 'is stdout a terminal?' '
8+
test_expect_success 'set up terminal for tests' '
99
if test -t 1
1010
then
1111
: > stdout_is_tty
12+
elif
13+
test_have_prereq PERL &&
14+
"$PERL_PATH" "$TEST_DIRECTORY"/t7006/test-terminal.perl \
15+
sh -c "test -t 1"
16+
then
17+
: > test_terminal_works
1218
fi
1319
'
1420

1521
if test -e stdout_is_tty
1622
then
23+
test_terminal() { "$@"; }
24+
test_set_prereq TTY
25+
elif test -e test_terminal_works
26+
then
27+
test_terminal() {
28+
"$PERL_PATH" "$TEST_DIRECTORY"/t7006/test-terminal.perl "$@"
29+
}
1730
test_set_prereq TTY
1831
else
19-
say stdout is not a terminal, so skipping some tests.
32+
say no usable terminal, so skipping some tests
2033
fi
2134

2235
unset GIT_PAGER GIT_PAGER_IN_USE
@@ -30,13 +43,13 @@ test_expect_success 'setup' '
3043

3144
rm -f paginated.out
3245
test_expect_success TTY 'some commands use a pager' '
33-
git log &&
46+
test_terminal git log &&
3447
test -e paginated.out
3548
'
3649

3750
rm -f paginated.out
3851
test_expect_success TTY 'some commands do not use a pager' '
39-
git rev-list HEAD &&
52+
test_terminal git rev-list HEAD &&
4053
! test -e paginated.out
4154
'
4255

@@ -54,7 +67,7 @@ test_expect_success 'no pager when stdout is a regular file' '
5467

5568
rm -f paginated.out
5669
test_expect_success TTY 'git --paginate rev-list uses a pager' '
57-
git --paginate rev-list HEAD &&
70+
test_terminal git --paginate rev-list HEAD &&
5871
test -e paginated.out
5972
'
6073

@@ -66,7 +79,7 @@ test_expect_success 'no pager even with --paginate when stdout is a pipe' '
6679

6780
rm -f paginated.out
6881
test_expect_success TTY 'no pager with --no-pager' '
69-
git --no-pager log &&
82+
test_terminal git --no-pager log &&
7083
! test -e paginated.out
7184
'
7285

@@ -95,7 +108,7 @@ test_expect_success 'no color when stdout is a regular file' '
95108
rm -f paginated.out
96109
git config color.ui auto
97110
test_expect_success TTY 'color when writing to a pager' '
98-
TERM=vt100 git log &&
111+
TERM=vt100 test_terminal git log &&
99112
colorful paginated.out
100113
'
101114

@@ -127,7 +140,7 @@ test_expect_success SIMPLEPAGER 'default pager is used by default' '
127140
: > default_pager_used
128141
EOF
129142
chmod +x $less &&
130-
PATH=.:$PATH git log &&
143+
PATH=.:$PATH test_terminal git log &&
131144
test -e default_pager_used
132145
'
133146

@@ -137,7 +150,7 @@ rm -f PAGER_used
137150
test_expect_success TTY 'PAGER overrides default pager' '
138151
PAGER=": > PAGER_used" &&
139152
export PAGER &&
140-
git log &&
153+
test_terminal git log &&
141154
test -e PAGER_used
142155
'
143156

@@ -147,7 +160,7 @@ test_expect_success TTY 'core.pager overrides PAGER' '
147160
PAGER=: &&
148161
export PAGER &&
149162
git config core.pager ": > core.pager_used" &&
150-
git log &&
163+
test_terminal git log &&
151164
test -e core.pager_used
152165
'
153166

@@ -156,7 +169,7 @@ test_expect_success TTY 'GIT_PAGER overrides core.pager' '
156169
git config core.pager : &&
157170
GIT_PAGER=": > GIT_PAGER_used" &&
158171
export GIT_PAGER &&
159-
git log &&
172+
test_terminal git log &&
160173
test -e GIT_PAGER_used
161174
'
162175

t/t7006/test-terminal.perl

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/perl
2+
use strict;
3+
use warnings;
4+
use IO::Pty;
5+
use File::Copy;
6+
7+
# Run @$argv in the background with stdout redirected to $out.
8+
sub start_child {
9+
my ($argv, $out) = @_;
10+
my $pid = fork;
11+
if (not defined $pid) {
12+
die "fork failed: $!"
13+
} elsif ($pid == 0) {
14+
open STDOUT, ">&", $out;
15+
close $out;
16+
exec(@$argv) or die "cannot exec '$argv->[0]': $!"
17+
}
18+
return $pid;
19+
}
20+
21+
# Wait for $pid to finish.
22+
sub finish_child {
23+
# Simplified from wait_or_whine() in run-command.c.
24+
my ($pid) = @_;
25+
26+
my $waiting = waitpid($pid, 0);
27+
if ($waiting < 0) {
28+
die "waitpid failed: $!";
29+
} elsif ($? & 127) {
30+
my $code = $? & 127;
31+
warn "died of signal $code";
32+
return $code - 128;
33+
} else {
34+
return $? >> 8;
35+
}
36+
}
37+
38+
sub xsendfile {
39+
my ($out, $in) = @_;
40+
41+
# Note: the real sendfile() cannot read from a terminal.
42+
43+
# It is unspecified by POSIX whether reads
44+
# from a disconnected terminal will return
45+
# EIO (as in AIX 4.x, IRIX, and Linux) or
46+
# end-of-file. Either is fine.
47+
copy($in, $out, 4096) or $!{EIO} or die "cannot copy from child: $!";
48+
}
49+
50+
if ($#ARGV < 1) {
51+
die "usage: test-terminal program args";
52+
}
53+
my $master = new IO::Pty;
54+
my $slave = $master->slave;
55+
my $pid = start_child(\@ARGV, $slave);
56+
close $slave;
57+
xsendfile(\*STDOUT, $master);
58+
exit(finish_child($pid));

0 commit comments

Comments
 (0)