| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | =head1 test User Intercept Methods | ||
| 4 | |||
| 5 | This performs very minimal testing and was written to test | ||
| 6 | some minor changes I made to Proc::Reliable so that STDERR | ||
| 7 | was properly routed to user methods AND any left over strings | ||
| 8 | were passed (those not terminated by an ending newline). | ||
| 9 | |||
| 10 | Written by Robb Canfield <robb at canfield dot com> | ||
| 11 | |||
| 12 | Date 2003-11-21 | ||
| 13 | |||
| 14 | =end | ||
| 15 | |||
| 16 | =cut | ||
| 17 | |||
| 18 | |||
| 19 | use lib qw/../; | ||
| 20 | |||
| 21 | use Proc::Reliable; | ||
| 22 | use Test::More qw/no_plan/; | ||
| 23 | use strict; | ||
| 24 | |||
| 25 | # test that capturing works for STDERR and STDOUT | ||
| 26 | { | ||
| 27 | my $proc = Proc::Reliable->new(); | ||
| 28 | |||
| 29 | my $buffer_out; | ||
| 30 | my $buffer_err; | ||
| 31 | |||
| 32 | # Default for single line is TRUE which routes ALL data to STDOUT leaving STDERR empty | ||
| 33 | # Change to FALSE so I can test each separate stream. | ||
| 34 | # | ||
| 35 | # I feel that internally Proc::Reliable should keep three buffers and return | ||
| 36 | # the proper ones as requested. But that would make it backward incompatible. | ||
| 37 | # - STDERR | ||
| 38 | # - STDOUT | ||
| 39 | # - mingled as they occurred | ||
| 40 | # | ||
| 41 | # Of course it would be easy enough to create a module based on Proc::Reliable that | ||
| 42 | # did exactly that! | ||
| 43 | $proc->want_single_list(0); | ||
| 44 | |||
| 45 | # Not very efficeient but makes tests easier. | ||
| 46 | # - $_[0] is the type as a string 'STDERR', 'STDOUT' this is NOT documented and I do not need it. | ||
| 47 | $proc->stdout_cb(sub {shift; $buffer_out .= join('', @_);}); | ||
| 48 | $proc->stderr_cb(sub {shift; $buffer_err .= join('', @_);}); | ||
| 49 | |||
| 50 | $proc->run(sub { | ||
| 51 | print "STDOUT Output\n"; | ||
| 52 | print STDERR "STDERR Output\n"; | ||
| 53 | print "STDOUT Output: No Newline"; | ||
| 54 | print STDERR "STDERR Output: No Newline"; | ||
| 55 | }); | ||
| 56 | |||
| 57 | # Make sure each buffer is separate | ||
| 58 | ok( | ||
| 59 | $buffer_err =~ /STDERR Output\n/ && $buffer_err !~ /^STDOUT Output/, | ||
| 60 | "STDERR captured" | ||
| 61 | ) or die("Unexpected output: $buffer_err"); | ||
| 62 | |||
| 63 | ok( | ||
| 64 | $buffer_out =~ /STDOUT Output\n/ && $buffer_out !~ /^STDERR Output/, | ||
| 65 | "STDOUT captured" | ||
| 66 | ) or die("Unexpected output: $buffer_out"); | ||
| 67 | |||
| 68 | # make sure ending data is in buffers | ||
| 69 | ok( | ||
| 70 | $buffer_err =~ /STDERR.*No Newline$/s, | ||
| 71 | "STDOUT captured data without a newline" | ||
| 72 | ) or die("Unexpected output: $buffer_err"); | ||
| 73 | |||
| 74 | ok( | ||
| 75 | $buffer_out =~ /STDOUT.*No Newline$/, | ||
| 76 | "STDOUT captured data without a newline" | ||
| 77 | ) or die("Unexpected output: $buffer_out"); | ||
| 78 | } | ||
| 79 | |||
| 80 |