Skip to content

Commit 18d8c26

Browse files
pyokagangitster
authored andcommitted
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 <[email protected]> Cc: Jeff King <[email protected]> Signed-off-by: Paul Tan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e97a5e7 commit 18d8c26

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

t/test-terminal.perl

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55
use IO::Pty;
66
use File::Copy;
77

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.
99
sub start_child {
10-
my ($argv, $out, $err) = @_;
10+
my ($argv, $in, $out, $err) = @_;
1111
my $pid = fork;
1212
if (not defined $pid) {
1313
die "fork failed: $!"
1414
} elsif ($pid == 0) {
15+
open STDIN, "<&", $in;
1516
open STDOUT, ">&", $out;
1617
open STDERR, ">&", $err;
18+
close $in;
1719
close $out;
1820
exec(@$argv) or die "cannot exec '$argv->[0]': $!"
1921
}
@@ -49,6 +51,17 @@ sub xsendfile {
4951
copy($in, $out, 4096) or $!{EIO} or die "cannot copy from child: $!";
5052
}
5153

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+
5265
sub copy_stdio {
5366
my ($out, $err) = @_;
5467
my $pid = fork;
@@ -67,14 +80,25 @@ sub copy_stdio {
6780
if ($#ARGV < 1) {
6881
die "usage: test-terminal program args";
6982
}
83+
my $master_in = new IO::Pty;
7084
my $master_out = new IO::Pty;
7185
my $master_err = new IO::Pty;
86+
$master_in->set_raw();
7287
$master_out->set_raw();
7388
$master_err->set_raw();
89+
$master_in->slave->set_raw();
7490
$master_out->slave->set_raw();
7591
$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;
7794
close $master_out->slave;
7895
close $master_err->slave;
96+
my $in_pid = copy_stdin($master_in);
7997
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);

0 commit comments

Comments
 (0)