|
| 1 | +use strict; |
| 2 | +use warnings; |
| 3 | + |
| 4 | +use File::Spec; |
| 5 | +use Test::More; |
| 6 | + |
| 7 | +$|++; # autoflush both processes (superfluous?) |
| 8 | + |
| 9 | +subtest 'test yes' => sub { |
| 10 | + # set default path to yes as seen from PPT root directory |
| 11 | + my $yes_path = './bin/yes'; |
| 12 | + # Amend path to PPT yes, if required, by setting environment |
| 13 | + # variable YESPATH. This may also be used to compare with |
| 14 | + # other yes implementations, e.g. at /usr/bin/yes. |
| 15 | + if (defined($ENV{YESPATH})) { |
| 16 | + $yes_path = $ENV{YESPATH}; |
| 17 | + diag "Testing yes at $ENV{YESPATH}"; |
| 18 | + } |
| 19 | + |
| 20 | + ok -e $yes_path && -f $yes_path, "found 'yes' program at $yes_path" |
| 21 | + or return; # fail rest of script |
| 22 | + |
| 23 | + subtest 'fork and run yes in child process' => sub { |
| 24 | + SKIP: { |
| 25 | + skip "Don't run fork test on Windows", 1 if $^O eq 'MSWin32'; |
| 26 | + fork_yes($yes_path); |
| 27 | + fork_yes($yes_path, 'iluvperl'); |
| 28 | + } |
| 29 | + }; |
| 30 | + }; |
| 31 | + |
| 32 | +sub fork_yes { |
| 33 | + my ($yes_path, $yes_str) = @_; |
| 34 | + my ($pid, $child); |
| 35 | + $yes_str ||= 'y'; |
| 36 | + if ($pid = open($child, '-|', "$yes_path $yes_str")) { |
| 37 | + # PARENT PROCESS |
| 38 | + # read ten lines from child |
| 39 | + my @lines; |
| 40 | + for (1..10) { |
| 41 | + # NOTE <> must be called in scalar context to prevent blocking. |
| 42 | + my $line = <$child>; |
| 43 | + push @lines, $line; |
| 44 | + } |
| 45 | + |
| 46 | + is $lines[0], "$yes_str\n", "First line is '$yes_str'.\n"; # superfluous? |
| 47 | + is scalar(@lines), 10, 'Expected no. of output lines (10).'; |
| 48 | + my $count_of_ys = grep { /^$yes_str$/ } @lines; |
| 49 | + note $count_of_ys; |
| 50 | + is $count_of_ys, 10, "All 10 lines contain '$yes_str' only.\n"; |
| 51 | + |
| 52 | + close($child); # apparently superfluous |
| 53 | + } |
| 54 | + else { |
| 55 | + die "cannot fork:$!\n" unless defined $pid; |
| 56 | + # CHILD PROCESS |
| 57 | + exit; # apparently superfluous |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | +done_testing(); |
0 commit comments