test_terminal: redirect child process' stdin to a pty

When resuming, git-am detects if we are trying to feed it patches or not by checking if stdin is a TTY. However, the test library redirects stdin to /dev/null. This makes it difficult, for instance, to test the behavior of "git am -3" when resuming, as git-am will think we are trying to feed it patches and error out. Support this use case by extending test-terminal.perl to create a pseudo-tty for the child process' standard input as well. Note that due to the way the code is structured, the child's stdin pseudo-tty will be closed when we finish reading from our stdin. This means that in the common case, where our stdin is attached to /dev/null, the child's stdin pseudo-tty will be closed immediately. Some operations like isatty(), which git-am uses, require the file descriptor to be open, and hence if the success of the command depends on such functions, test_terminal's stdin should be redirected to a source with large amount of data to ensure that the child's stdin is not closed, e.g. test_terminal git am --3way </dev/zero Cc: Jonathan Nieder <jrnieder@gmail.com> Cc: Jeff King <peff@peff.net> Signed-off-by: Paul Tan <pyokagan@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Paul Tan committed Aug 4, 2015 at 22:08 UTC 18d8c26930acbba1627d2a8b7323be30ac2bd9bb
1 file changed +28 -4
t/test-terminal.perl
+28 -4
@@ -5,15 +5,17 @@ use warnings;
5 use IO::Pty;
6 use File::Copy;
7
8 -# Run @$argv in the background with stdio redirected to $out and $err.
8 +# Run @$argv in the background with stdio redirected to $in, $out and $err.
9 sub start_child {
10 - my ($argv, $out, $err) = @_;
10 + my ($argv, $in, $out, $err) = @_;
11 my $pid = fork;
12 if (not defined $pid) {
13 die "fork failed: $!"
14 } elsif ($pid == 0) {
15 + open STDIN, "<&", $in;
16 open STDOUT, ">&", $out;
17 open STDERR, ">&", $err;
18 + close $in;
19 close $out;
20 exec(@$argv) or die "cannot exec '$argv->[0]': $!"
21 }
@@ -49,6 +51,17 @@ sub xsendfile {
51 copy($in, $out, 4096) or $!{EIO} or die "cannot copy from child: $!";
52 }
53
54 +sub copy_stdin {
55 + my ($in) = @_;
56 + my $pid = fork;
57 + if (!$pid) {
58 + xsendfile($in, \*STDIN);
59 + exit 0;
60 + }
61 + close($in);
62 + return $pid;
63 +}
64 +
65 sub copy_stdio {
66 my ($out, $err) = @_;
67 my $pid = fork;
@@ -67,14 +80,25 @@ sub copy_stdio {
80 if ($#ARGV < 1) {
81 die "usage: test-terminal program args";
82 }
83 +my $master_in = new IO::Pty;
84 my $master_out = new IO::Pty;
85 my $master_err = new IO::Pty;
86 +$master_in->set_raw();
87 $master_out->set_raw();
88 $master_err->set_raw();
89 +$master_in->slave->set_raw();
90 $master_out->slave->set_raw();
91 $master_err->slave->set_raw();
76 -my $pid = start_child(\@ARGV, $master_out->slave, $master_err->slave);
92 +my $pid = start_child(\@ARGV, $master_in->slave, $master_out->slave, $master_err->slave);
93 +close $master_in->slave;
94 close $master_out->slave;
95 close $master_err->slave;
96 +my $in_pid = copy_stdin($master_in);
97 copy_stdio($master_out, $master_err);
80 -exit(finish_child($pid));
98 +my $ret = finish_child($pid);
99 +# If the child process terminates before our copy_stdin() process is able to
100 +# write all of its data to $master_in, the copy_stdin() process could stall.
101 +# Send SIGTERM to it to ensure it terminates.
102 +kill 'TERM', $in_pid;
103 +finish_child($in_pid);
104 +exit($ret);