Skip to content

Commit e23f436

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Reviewed-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cc4e48f commit e23f436

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

t/lib-terminal.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
#!/bin/sh
22

33
test_expect_success 'set up terminal for tests' '
4-
if test -t 1
4+
if test -t 1 && test -t 2
55
then
6-
>stdout_is_tty
6+
>have_tty
77
elif
88
test_have_prereq PERL &&
99
"$PERL_PATH" "$TEST_DIRECTORY"/test-terminal.perl \
10-
sh -c "test -t 1"
10+
sh -c "test -t 1 && test -t 2"
1111
then
1212
>test_terminal_works
1313
fi
1414
'
1515

16-
if test -e stdout_is_tty
16+
if test -e have_tty
1717
then
1818
test_terminal() { "$@"; }
1919
test_set_prereq TTY

t/test-terminal.perl

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
use IO::Pty;
55
use File::Copy;
66

7-
# Run @$argv in the background with stdout redirected to $out.
7+
# Run @$argv in the background with stdio redirected to $out and $err.
88
sub start_child {
9-
my ($argv, $out) = @_;
9+
my ($argv, $out, $err) = @_;
1010
my $pid = fork;
1111
if (not defined $pid) {
1212
die "fork failed: $!"
1313
} elsif ($pid == 0) {
1414
open STDOUT, ">&", $out;
15+
open STDERR, ">&", $err;
1516
close $out;
1617
exec(@$argv) or die "cannot exec '$argv->[0]': $!"
1718
}
@@ -47,12 +48,28 @@ sub xsendfile {
4748
copy($in, $out, 4096) or $!{EIO} or die "cannot copy from child: $!";
4849
}
4950

51+
sub copy_stdio {
52+
my ($out, $err) = @_;
53+
my $pid = fork;
54+
defined $pid or die "fork failed: $!";
55+
if (!$pid) {
56+
close($out);
57+
xsendfile(\*STDERR, $err);
58+
exit 0;
59+
}
60+
close($err);
61+
xsendfile(\*STDOUT, $out);
62+
finish_child($pid) == 0
63+
or exit 1;
64+
}
65+
5066
if ($#ARGV < 1) {
5167
die "usage: test-terminal program args";
5268
}
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);
69+
my $master_out = new IO::Pty;
70+
my $master_err = new IO::Pty;
71+
my $pid = start_child(\@ARGV, $master_out->slave, $master_err->slave);
72+
close $master_out->slave;
73+
close $master_err->slave;
74+
copy_stdio($master_out, $master_err);
5875
exit(finish_child($pid));

0 commit comments

Comments
 (0)