-
Notifications
You must be signed in to change notification settings - Fork 54
Add test for 'yes' #992
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add test for 'yes' #992
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| #!/usr/bin/env perl | ||
|
|
||
| # 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 | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
File removed.