[PATCH 2/3] tests: test terminal output to both stdout and stderr

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxx>
---
 t/lib-terminal.sh    |    8 ++++----
 t/test-terminal.perl |   31 ++++++++++++++++++++++++-------
 2 files changed, 28 insertions(+), 11 deletions(-)

diff --git a/t/lib-terminal.sh b/t/lib-terminal.sh
index 6fc33db..3258b8f 100644
--- a/t/lib-terminal.sh
+++ b/t/lib-terminal.sh
@@ -1,19 +1,19 @@
 #!/bin/sh
 
 test_expect_success 'set up terminal for tests' '
-	if test -t 1
+	if test -t 1 && test -t 2
 	then
-		>stdout_is_tty
+		>have_tty
 	elif
 		test_have_prereq PERL &&
 		"$PERL_PATH" "$TEST_DIRECTORY"/test-terminal.perl \
-			sh -c "test -t 1"
+			sh -c "test -t 1 && test -t 2"
 	then
 		>test_terminal_works
 	fi
 '
 
-if test -e stdout_is_tty
+if test -e have_tty
 then
 	test_terminal() { "$@"; }
 	test_set_prereq TTY
diff --git a/t/test-terminal.perl b/t/test-terminal.perl
index 73ff809..c2e9dac 100755
--- a/t/test-terminal.perl
+++ b/t/test-terminal.perl
@@ -4,14 +4,15 @@ use warnings;
 use IO::Pty;
 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 {
-	my ($argv, $out) = @_;
+	my ($argv, $out, $err) = @_;
 	my $pid = fork;
 	if (not defined $pid) {
 		die "fork failed: $!"
 	} elsif ($pid == 0) {
 		open STDOUT, ">&", $out;
+		open STDERR, ">&", $err;
 		close $out;
 		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: $!";
 }
 
+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) {
 	die "usage: test-terminal program args";
 }
-my $master = new IO::Pty;
-my $slave = $master->slave;
-my $pid = start_child(\@ARGV, $slave);
-close $slave;
-xsendfile(\*STDOUT, $master);
+my $master_out = new IO::Pty;
+my $master_err = new IO::Pty;
+my $pid = start_child(\@ARGV, $master_out->slave, $master_err->slave);
+close $master_out->slave;
+close $master_err->slave;
+copy_stdio($master_out, $master_err);
 exit(finish_child($pid));
-- 
1.7.3.1.204.g337d6.dirty

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]