From: Jeff King Date: Sat, 16 Oct 2010 18:36:56 +0000 (+0800) Subject: tests: factor out terminal handling from t7006 X-Git-Tag: v1.7.4-rc0~135^2~7 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=cc4e48fc1eafdb9bec037f10c22708a26fd25ef6;p=git.git tests: factor out terminal handling from t7006 Other tests besides the pager ones may want to check how we handle output to a terminal. This patch makes the code reusable. Signed-off-by: Jeff King Reviewed-by: Jonathan Nieder Signed-off-by: Junio C Hamano --- diff --git a/t/lib-terminal.sh b/t/lib-terminal.sh new file mode 100644 index 000000000..6fc33db6c --- /dev/null +++ b/t/lib-terminal.sh @@ -0,0 +1,28 @@ +#!/bin/sh + +test_expect_success 'set up terminal for tests' ' + if test -t 1 + then + >stdout_is_tty + elif + test_have_prereq PERL && + "$PERL_PATH" "$TEST_DIRECTORY"/test-terminal.perl \ + sh -c "test -t 1" + then + >test_terminal_works + fi +' + +if test -e stdout_is_tty +then + test_terminal() { "$@"; } + test_set_prereq TTY +elif test -e test_terminal_works +then + test_terminal() { + "$PERL_PATH" "$TEST_DIRECTORY"/test-terminal.perl "$@" + } + test_set_prereq TTY +else + say "# no usable terminal, so skipping some tests" +fi diff --git a/t/t7006-pager.sh b/t/t7006-pager.sh index fb744e3c4..17e54d3e6 100755 --- a/t/t7006-pager.sh +++ b/t/t7006-pager.sh @@ -4,42 +4,13 @@ test_description='Test automatic use of a pager.' . ./test-lib.sh . "$TEST_DIRECTORY"/lib-pager.sh +. "$TEST_DIRECTORY"/lib-terminal.sh cleanup_fail() { echo >&2 cleanup failed (exit 1) } -test_expect_success 'set up terminal for tests' ' - rm -f stdout_is_tty || - cleanup_fail && - - if test -t 1 - then - >stdout_is_tty - elif - test_have_prereq PERL && - "$PERL_PATH" "$TEST_DIRECTORY"/t7006/test-terminal.perl \ - sh -c "test -t 1" - then - >test_terminal_works - fi -' - -if test -e stdout_is_tty -then - test_terminal() { "$@"; } - test_set_prereq TTY -elif test -e test_terminal_works -then - test_terminal() { - "$PERL_PATH" "$TEST_DIRECTORY"/t7006/test-terminal.perl "$@" - } - test_set_prereq TTY -else - say "# no usable terminal, so skipping some tests" -fi - test_expect_success 'setup' ' unset GIT_PAGER GIT_PAGER_IN_USE; test_might_fail git config --unset core.pager && diff --git a/t/t7006/test-terminal.perl b/t/t7006/test-terminal.perl deleted file mode 100755 index 73ff80937..000000000 --- a/t/t7006/test-terminal.perl +++ /dev/null @@ -1,58 +0,0 @@ -#!/usr/bin/perl -use strict; -use warnings; -use IO::Pty; -use File::Copy; - -# Run @$argv in the background with stdout redirected to $out. -sub start_child { - my ($argv, $out) = @_; - my $pid = fork; - if (not defined $pid) { - die "fork failed: $!" - } elsif ($pid == 0) { - open STDOUT, ">&", $out; - close $out; - exec(@$argv) or die "cannot exec '$argv->[0]': $!" - } - return $pid; -} - -# Wait for $pid to finish. -sub finish_child { - # Simplified from wait_or_whine() in run-command.c. - my ($pid) = @_; - - my $waiting = waitpid($pid, 0); - if ($waiting < 0) { - die "waitpid failed: $!"; - } elsif ($? & 127) { - my $code = $? & 127; - warn "died of signal $code"; - return $code - 128; - } else { - return $? >> 8; - } -} - -sub xsendfile { - my ($out, $in) = @_; - - # Note: the real sendfile() cannot read from a terminal. - - # It is unspecified by POSIX whether reads - # from a disconnected terminal will return - # EIO (as in AIX 4.x, IRIX, and Linux) or - # end-of-file. Either is fine. - copy($in, $out, 4096) or $!{EIO} or die "cannot copy from child: $!"; -} - -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); -exit(finish_child($pid)); diff --git a/t/test-terminal.perl b/t/test-terminal.perl new file mode 100755 index 000000000..73ff80937 --- /dev/null +++ b/t/test-terminal.perl @@ -0,0 +1,58 @@ +#!/usr/bin/perl +use strict; +use warnings; +use IO::Pty; +use File::Copy; + +# Run @$argv in the background with stdout redirected to $out. +sub start_child { + my ($argv, $out) = @_; + my $pid = fork; + if (not defined $pid) { + die "fork failed: $!" + } elsif ($pid == 0) { + open STDOUT, ">&", $out; + close $out; + exec(@$argv) or die "cannot exec '$argv->[0]': $!" + } + return $pid; +} + +# Wait for $pid to finish. +sub finish_child { + # Simplified from wait_or_whine() in run-command.c. + my ($pid) = @_; + + my $waiting = waitpid($pid, 0); + if ($waiting < 0) { + die "waitpid failed: $!"; + } elsif ($? & 127) { + my $code = $? & 127; + warn "died of signal $code"; + return $code - 128; + } else { + return $? >> 8; + } +} + +sub xsendfile { + my ($out, $in) = @_; + + # Note: the real sendfile() cannot read from a terminal. + + # It is unspecified by POSIX whether reads + # from a disconnected terminal will return + # EIO (as in AIX 4.x, IRIX, and Linux) or + # end-of-file. Either is fine. + copy($in, $out, 4096) or $!{EIO} or die "cannot copy from child: $!"; +} + +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); +exit(finish_child($pid));