Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions t/yes/yes
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env perl
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to commit this file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello brian, many thanks. I certainly did not! What a howler. Fix imminent.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File removed.


# tryout script for running PPT's 'yes' in a child process
# and reading from it without blocking

use strict;
use warnings;

$|++; # autoflush both processes (superfluous?)

my ($pid, $child);

# default path to yes from PPT Git repo root
my $default_ppt_yes = './bin/yes';

# Update path to PPT yes if different to default,
# or to compare with other yes implementations,
# e.g. /usr/bin/yes.
my $ppt_yes_path = defined($ARGV[0]) ? $ARGV[0] : $default_ppt_yes;

die "Can't find 'yes' script at $ppt_yes_path\n"
unless (-e $ppt_yes_path && -f $ppt_yes_path);

# fork and run yes in child process via open '-|'
if ($pid = open($child, '-|', $ppt_yes_path)) {
# PARENT PROCESS
# run parent code, reading from child
print "Parent PID: $$\nChild PID: $pid\n";

my @lines;
for (1..10) {
# NOTE <> must be called in scalar context to prevent blocking.
my $line = <$child>;
push @lines, $line;
}
# Set output field separator to empty string
# to prevent outdenting of all but first 'y'.
local $" = '';
print "Child Output:-\n@lines";

close($child); # apparently gratuitous
} else {
die "cannot fork:$!\n" unless defined $pid;
# CHILD PROCESS
exit; # apparently gratuitous
}

61 changes: 61 additions & 0 deletions t/yes/yes.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use strict;
use warnings;

use File::Spec;
use Test::More;

$|++; # autoflush both processes (superfluous?)

subtest 'test yes' => sub {
# set default path to yes as seen from PPT root directory
my $yes_path = './bin/yes';
# Amend path to PPT yes, if required, by setting environment
# variable YESPATH. This may also be used to compare with
# other yes implementations, e.g. at /usr/bin/yes.
if (defined($ENV{YESPATH})) {
$yes_path = $ENV{YESPATH};
diag "Testing yes at $ENV{YESPATH}";
}

ok -e $yes_path && -f $yes_path, "found 'yes' program at $yes_path"
or return; # fail rest of script

subtest 'fork and run yes in child process' => sub {
SKIP: {
skip "Don't run fork test on Windows", 1 if $^O eq 'MSWin32';
fork_yes($yes_path);
fork_yes($yes_path, 'iluvperl');
}
};
};

sub fork_yes {
my ($yes_path, $yes_str) = @_;
my ($pid, $child);
$yes_str ||= 'y';
if ($pid = open($child, '-|', "$yes_path $yes_str")) {
# PARENT PROCESS
# read ten lines from child
my @lines;
for (1..10) {
# NOTE <> must be called in scalar context to prevent blocking.
my $line = <$child>;
push @lines, $line;
}

is $lines[0], "$yes_str\n", "First line is '$yes_str'.\n"; # superfluous?
is scalar(@lines), 10, 'Expected no. of output lines (10).';
my $count_of_ys = grep { /^$yes_str$/ } @lines;
note $count_of_ys;
is $count_of_ys, 10, "All 10 lines contain '$yes_str' only.\n";

close($child); # apparently superfluous
}
else {
die "cannot fork:$!\n" unless defined $pid;
# CHILD PROCESS
exit; # apparently superfluous
}
}

done_testing();