@matheusbsbs / matheus-bagliano / commits / 2d3ca21677
Fork 0

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 <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Nieder committed Feb 20, 2010 at 02:50 UTC 2d3ca21677597902f66bf2f2b2cf1b4a623f1e4f
2 files changed +82 -11
t/t7006-pager.sh
+24 -11
@@ -5,18 +5,31 @@ test_description='Test automatic use of a pager.'
5 . ./test-lib.sh
6
7 rm -f stdout_is_tty
8 -test_expect_success 'is stdout a terminal?' '
8 +test_expect_success 'set up terminal for tests' '
9 if test -t 1
10 then
11 : > 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
18 fi
19 '
20
21 if test -e stdout_is_tty
22 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 + }
30 test_set_prereq TTY
31 else
19 - say stdout is not a terminal, so skipping some tests.
32 + say no usable terminal, so skipping some tests
33 fi
34
35 unset GIT_PAGER GIT_PAGER_IN_USE
@@ -30,13 +43,13 @@ test_expect_success 'setup' '
43
44 rm -f paginated.out
45 test_expect_success TTY 'some commands use a pager' '
33 - git log &&
46 + test_terminal git log &&
47 test -e paginated.out
48 '
49
50 rm -f paginated.out
51 test_expect_success TTY 'some commands do not use a pager' '
39 - git rev-list HEAD &&
52 + test_terminal git rev-list HEAD &&
53 ! test -e paginated.out
54 '
55
@@ -54,7 +67,7 @@ test_expect_success 'no pager when stdout is a regular file' '
67
68 rm -f paginated.out
69 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 &&
71 test -e paginated.out
72 '
73
@@ -66,7 +79,7 @@ test_expect_success 'no pager even with --paginate when stdout is a pipe' '
79
80 rm -f paginated.out
81 test_expect_success TTY 'no pager with --no-pager' '
69 - git --no-pager log &&
82 + test_terminal git --no-pager log &&
83 ! test -e paginated.out
84 '
85
@@ -95,7 +108,7 @@ test_expect_success 'no color when stdout is a regular file' '
108 rm -f paginated.out
109 git config color.ui auto
110 test_expect_success TTY 'color when writing to a pager' '
98 - TERM=vt100 git log &&
111 + TERM=vt100 test_terminal git log &&
112 colorful paginated.out
113 '
114
@@ -127,7 +140,7 @@ test_expect_success SIMPLEPAGER 'default pager is used by default' '
140 : > default_pager_used
141 EOF
142 chmod +x $less &&
130 - PATH=.:$PATH git log &&
143 + PATH=.:$PATH test_terminal git log &&
144 test -e default_pager_used
145 '
146
@@ -137,7 +150,7 @@ rm -f PAGER_used
150 test_expect_success TTY 'PAGER overrides default pager' '
151 PAGER=": > PAGER_used" &&
152 export PAGER &&
140 - git log &&
153 + test_terminal git log &&
154 test -e PAGER_used
155 '
156
@@ -147,7 +160,7 @@ test_expect_success TTY 'core.pager overrides PAGER' '
160 PAGER=: &&
161 export PAGER &&
162 git config core.pager ": > core.pager_used" &&
150 - git log &&
163 + test_terminal git log &&
164 test -e core.pager_used
165 '
166
@@ -156,7 +169,7 @@ test_expect_success TTY 'GIT_PAGER overrides core.pager' '
169 git config core.pager : &&
170 GIT_PAGER=": > GIT_PAGER_used" &&
171 export GIT_PAGER &&
159 - git log &&
172 + test_terminal git log &&
173 test -e GIT_PAGER_used
174 '
175
t/t7006/test-terminal.perl new
+58
@@ -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));