tests: test terminal output to both stdout and stderr

Some outputs (like the pager) care whether stdout is a
terminal. Others (like progress meters) care about stderr.

This patch sets up both. Technically speaking, we could go
further and set up just one (because either the other goes
to a terminal, or because our tests are only interested in
one). This patch does both to keep the interface to
lib-terminal simple.

Signed-off-by: Jeff King <peff@peff.net>
Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jeff King
2010-10-17 02:36:57 +08:00
committed by Junio C Hamano
parent cc4e48fc1e
commit e23f436c35
2 changed files with 28 additions and 11 deletions

View File

@ -1,19 +1,19 @@
#!/bin/sh #!/bin/sh
test_expect_success 'set up terminal for tests' ' test_expect_success 'set up terminal for tests' '
if test -t 1 if test -t 1 && test -t 2
then then
>stdout_is_tty >have_tty
elif elif
test_have_prereq PERL && test_have_prereq PERL &&
"$PERL_PATH" "$TEST_DIRECTORY"/test-terminal.perl \ "$PERL_PATH" "$TEST_DIRECTORY"/test-terminal.perl \
sh -c "test -t 1" sh -c "test -t 1 && test -t 2"
then then
>test_terminal_works >test_terminal_works
fi fi
' '
if test -e stdout_is_tty if test -e have_tty
then then
test_terminal() { "$@"; } test_terminal() { "$@"; }
test_set_prereq TTY test_set_prereq TTY

View File

@ -4,14 +4,15 @@ use warnings;
use IO::Pty; use IO::Pty;
use File::Copy; use File::Copy;
# Run @$argv in the background with stdout redirected to $out. # Run @$argv in the background with stdio redirected to $out and $err.
sub start_child { sub start_child {
my ($argv, $out) = @_; my ($argv, $out, $err) = @_;
my $pid = fork; my $pid = fork;
if (not defined $pid) { if (not defined $pid) {
die "fork failed: $!" die "fork failed: $!"
} elsif ($pid == 0) { } elsif ($pid == 0) {
open STDOUT, ">&", $out; open STDOUT, ">&", $out;
open STDERR, ">&", $err;
close $out; close $out;
exec(@$argv) or die "cannot exec '$argv->[0]': $!" exec(@$argv) or die "cannot exec '$argv->[0]': $!"
} }
@ -47,12 +48,28 @@ sub xsendfile {
copy($in, $out, 4096) or $!{EIO} or die "cannot copy from child: $!"; copy($in, $out, 4096) or $!{EIO} or die "cannot copy from child: $!";
} }
sub copy_stdio {
my ($out, $err) = @_;
my $pid = fork;
defined $pid or die "fork failed: $!";
if (!$pid) {
close($out);
xsendfile(\*STDERR, $err);
exit 0;
}
close($err);
xsendfile(\*STDOUT, $out);
finish_child($pid) == 0
or exit 1;
}
if ($#ARGV < 1) { if ($#ARGV < 1) {
die "usage: test-terminal program args"; die "usage: test-terminal program args";
} }
my $master = new IO::Pty; my $master_out = new IO::Pty;
my $slave = $master->slave; my $master_err = new IO::Pty;
my $pid = start_child(\@ARGV, $slave); my $pid = start_child(\@ARGV, $master_out->slave, $master_err->slave);
close $slave; close $master_out->slave;
xsendfile(\*STDOUT, $master); close $master_err->slave;
copy_stdio($master_out, $master_err);
exit(finish_child($pid)); exit(finish_child($pid));