]> Sergey Matveev's repositories - public-inbox.git/blob - t/spawn.t
public-inbox 1.1.0-pre1
[public-inbox.git] / t / spawn.t
1 # Copyright (C) 2015-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
6 use PublicInbox::Spawn qw(which spawn popen_rd);
7
8 {
9         my $true = which('true');
10         ok($true, "'true' command found with which()");
11 }
12
13 {
14         my $pid = spawn(['true']);
15         ok($pid, 'spawned process');
16         is(waitpid($pid, 0), $pid, 'waitpid succeeds on spawned process');
17         is($?, 0, 'true exited successfully');
18 }
19
20 {
21         my ($r, $w);
22         pipe $r, $w or die "pipe failed: $!";
23         my $pid = spawn(['echo', 'hello world'], undef, { 1 => fileno($w) });
24         close $w or die "close pipe[1] failed: $!";
25         is(<$r>, "hello world\n", 'read stdout of spawned from pipe');
26         is(waitpid($pid, 0), $pid, 'waitpid succeeds on spawned process');
27         is($?, 0, 'true exited successfully');
28 }
29
30 {
31         my ($r, $w);
32         pipe $r, $w or die "pipe failed: $!";
33         my $pid = spawn(['sh', '-c', 'echo $HELLO'],
34                 { 'HELLO' => 'world' }, { 1 => fileno($w) });
35         close $w or die "close pipe[1] failed: $!";
36         is(<$r>, "world\n", 'read stdout of spawned from pipe');
37         is(waitpid($pid, 0), $pid, 'waitpid succeeds on spawned process');
38         is($?, 0, 'sh exited successfully');
39 }
40
41 {
42         my ($r, $w);
43         pipe $r, $w or die "pipe failed: $!";
44         my $pid = spawn(['env'], {}, { -env => 1, 1 => fileno($w) });
45         close $w or die "close pipe[1] failed: $!";
46         ok(!defined(<$r>), 'read stdout of spawned from pipe');
47         is(waitpid($pid, 0), $pid, 'waitpid succeeds on spawned process');
48         is($?, 0, 'env(1) exited successfully');
49 }
50
51 {
52         my $fh = popen_rd([qw(echo hello)]);
53         ok(fileno($fh) >= 0, 'tied fileno works');
54         my $l = <$fh>;
55         is($l, "hello\n", 'tied readline works');
56         $l = <$fh>;
57         ok(!$l, 'tied readline works for EOF');
58 }
59
60 {
61         my $fh = popen_rd([qw(printf foo\nbar)]);
62         ok(fileno($fh) >= 0, 'tied fileno works');
63         my @line = <$fh>;
64         is_deeply(\@line, [ "foo\n", 'bar' ], 'wantarray works on readline');
65 }
66
67 {
68         my $fh = popen_rd([qw(echo hello)]);
69         my $buf;
70         is(sysread($fh, $buf, 6), 6, 'sysread got 6 bytes');
71         is($buf, "hello\n", 'tied gets works');
72         is(sysread($fh, $buf, 6), 0, 'sysread got EOF');
73         $? = 1;
74         close $fh;
75         is($?, 0, '$? set properly');
76 }
77
78 {
79         my $fh = popen_rd([qw(false)]);
80         close $fh;
81         isnt($?, 0, '$? set properly: '.$?);
82 }
83
84 {
85         my ($fh, $pid) = popen_rd([qw(sleep 60)], undef, { Blocking => 0 });
86         ok(defined $pid && $pid > 0, 'returned pid when array requested');
87         is(kill(0, $pid), 1, 'child process is running');
88         ok(!defined(sysread($fh, my $buf, 1)) && $!{EAGAIN},
89            'sysread returned quickly with EAGAIN');
90         is(kill(9, $pid), 1, 'child process killed early');
91         is(waitpid($pid, 0), $pid, 'child process reapable');
92         isnt($?, 0, '$? set properly: '.$?);
93 }
94
95 done_testing();
96
97 1;