@matheusbsbs / matheus-bagliano / commits / e23f436c35
Fork 0

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>

Jeff King committed Oct 17, 2010 at 02:36 UTC e23f436c35c3e7fd12feeed783064e08aaf27869
2 files changed +28 -11
t/lib-terminal.sh
+4 -4
@@ -1,19 +1,19 @@
1 #!/bin/sh
2
3 test_expect_success 'set up terminal for tests' '
4 - if test -t 1
4 + if test -t 1 && test -t 2
5 then
6 - >stdout_is_tty
6 + >have_tty
7 elif
8 test_have_prereq PERL &&
9 "$PERL_PATH" "$TEST_DIRECTORY"/test-terminal.perl \
10 - sh -c "test -t 1"
10 + sh -c "test -t 1 && test -t 2"
11 then
12 >test_terminal_works
13 fi
14 '
15
16 -if test -e stdout_is_tty
16 +if test -e have_tty
17 then
18 test_terminal() { "$@"; }
19 test_set_prereq TTY
t/test-terminal.perl
+24 -7
@@ -4,14 +4,15 @@ use warnings;
4 use IO::Pty;
5 use File::Copy;
6
7 -# Run @$argv in the background with stdout redirected to $out.
7 +# Run @$argv in the background with stdio redirected to $out and $err.
8 sub start_child {
9 - my ($argv, $out) = @_;
9 + my ($argv, $out, $err) = @_;
10 my $pid = fork;
11 if (not defined $pid) {
12 die "fork failed: $!"
13 } elsif ($pid == 0) {
14 open STDOUT, ">&", $out;
15 + open STDERR, ">&", $err;
16 close $out;
17 exec(@$argv) or die "cannot exec '$argv->[0]': $!"
18 }
@@ -47,12 +48,28 @@ sub xsendfile {
48 copy($in, $out, 4096) or $!{EIO} or die "cannot copy from child: $!";
49 }
50
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 +
66 if ($#ARGV < 1) {
67 die "usage: test-terminal program args";
68 }
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);
75 exit(finish_child($pid));