diff --git a/Changes b/Changes index 7ba531c..3985f0f 100644 --- a/Changes +++ b/Changes @@ -2,6 +2,10 @@ Revision history for Perl module Tee 0.13_51 + - moved guts of ptee to Tee::App; removed ptee seach logic from Tee.pm + + - changed to the Apache License, version 2.0; (it's clearer, relicensable, + and is explicit about contributions) 0.13 Thu Aug 17 13:09:03 EDT 2006 - customize Makefile.PL for auto/Tee diff --git a/MANIFEST b/MANIFEST index c639cc0..0f4895b 100644 --- a/MANIFEST +++ b/MANIFEST @@ -4,15 +4,18 @@ Changes inc/Module/Build/WikiDoc.pm INSTALL lib/Tee.pm +lib/Tee.pod +lib/Tee/App.pm +lib/Tee/App.pod LICENSE +Makefile.PL MANIFEST This list of files MANIFEST.SKIP +META.yml README t/01_Tee.t t/02_tee_script.t t/03_tee_function.t -t/98_pod.t -t/99_pod-coverage.t t/Expected.pm t/fatality.pl t/helloworld.pl @@ -23,6 +26,3 @@ xt/pod-coverage.t xt/pod-format.t xt/spelling.t xt/stopwords.txt -lib/Tee.pod -Makefile.PL -META.yml diff --git a/Todo b/Todo index 0ca1529..f15d624 100644 --- a/Todo +++ b/Todo @@ -1,9 +1,3 @@ -# Copyright (c) 2008 by David Golden. All rights reserved. -# Licensed under Apache License, Version 2.0 (the "License"). -# You may not use this file except in compliance with the License. -# A copy of the License was distributed with this file or you may obtain a -# copy of the License from http://www.apache.org/licenses/LICENSE-2.0 +Todo for Tee -- Write some code -- Write some tests -- Replace boilerplate docs with real documentation +- nothing right now diff --git a/bin/ptee b/bin/ptee index 6fa3e87..bd39b82 100644 --- a/bin/ptee +++ b/bin/ptee @@ -1,20 +1,17 @@ #!/usr/bin/perl - # ptee -- perl implementation of unix tee + $VERSION = '0.13_51'; use strict; -use File::Basename qw/basename/; -use Getopt::Long; -use IO::File; +use Tee::App qw/run/; +run; + +__END__ =head1 NAME ptee - emulate the GNU tee program with Perl -=cut - -my $help_text = <<'=cut'; - =head1 SYNOPSIS ptee [OPTIONS]... [FILENAMES]... @@ -30,65 +27,6 @@ my $help_text = <<'=cut'; --version or -V print the version number of this program -=cut - -$help_text =~ s/\A.+?( ptee.*)/$1/ms; - -#--------------------------------------------------------------------------# -# process command line -#--------------------------------------------------------------------------# - -my %opts; -GetOptions( \%opts, - 'version|V', - 'help|h|?', - 'append|a', -); - -#--------------------------------------------------------------------------# -# options -#--------------------------------------------------------------------------# - -if ($opts{version}) { - print basename($0), " $main::VERSION\n"; - exit 0; -} - -if ($opts{help}) { - print "Usage:\n$help_text"; - exit 1; -} - -my $mode = $opts{append} ? ">>" : ">"; - -#--------------------------------------------------------------------------# -# Setup list of filehandles -#--------------------------------------------------------------------------# - -my $stdout = IO::Handle->new->fdopen(fileno(STDOUT),"w"); -my @files = $stdout; - -for my $file ( @ARGV ) { - my $f = IO::File->new("$mode $file") - or die "Could't open '$file' for writing: $!'"; - push @files, $f; -} - -#--------------------------------------------------------------------------# -# Tee input to the filehandle list -#--------------------------------------------------------------------------# - -my $buffer_size = 1024; -my $buffer; - -while ( sysread( STDIN, $buffer, $buffer_size ) > 0 ) { - for my $fh ( @files ) { - syswrite $fh, $buffer; - } -} - -__END__ - =head1 DESCRIPTION C is a pure Perl emulation of the standard GNU tool C and is @@ -105,9 +43,8 @@ is not sufficiently portable. =head1 BUGS Please report any bugs or feature using the CPAN Request Tracker. -Bugs can be submitted by email to C or -through the web interface at -LErt.cpan.orgEPublicEDistEDisplay.html?Name=Tee> +Bugs can be submitted through the web interface at +L When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature. @@ -116,42 +53,21 @@ existing test-file that illustrates the bug or desired feature. David A. Golden (DAGOLDEN) -dagolden@cpan.org - -http:EEwww.dagolden.orgE - =head1 COPYRIGHT AND LICENSE -Copyright (c) 2006 by David A. Golden - -This program is free software; you can redistribute -it andEor modify it under the same terms as Perl itself. - -The full text of the license can be found in the -LICENSE file included with this module. - -=head1 DISCLAIMER OF WARRANTY - -BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY -FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN -OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS ANDEOR OTHER PARTIES -PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER -EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE -ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH -YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL -NECESSARY SERVICING, REPAIR, OR CORRECTION. - -IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING -WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY ANDEOR -REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE -LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, -OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE -THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING -RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A -FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF -SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF -SUCH DAMAGES. +Copyright (c) 2006-2008 by David A. Golden. All rights reserved. +Licensed under Apache License, Version 2.0 (the "License"). +You may not use this file except in compliance with the License. +A copy of the License was distributed with this file or you may obtain a +copy of the License from http:EEwww.apache.orgElicensesELICENSE-2.0 +Files produced as output though the use of this software, shall not be +considered Derivative Works, but shall be considered the original work of the +Licensor. +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/lib/Tee.pm b/lib/Tee.pm index b33249c..9a33696 100644 --- a/lib/Tee.pm +++ b/lib/Tee.pm @@ -1,57 +1,43 @@ -# Copyright (c) 2008 by David Golden. All rights reserved. -# Licensed under Apache License, Version 2.0 (the "License"). -# You may not use this file except in compliance with the License. -# A copy of the License was distributed with this file or you may obtain a -# copy of the License from http://www.apache.org/licenses/LICENSE-2.0 - package Tee; -use strict; $Tee::VERSION = '0.13_51'; $Tee::VERSION = eval $Tee::VERSION; ## no critic @Tee::ISA = qw (Exporter); @Tee::EXPORT = qw (tee); +use strict; use Exporter (); -use IO::File; +use Probe::Perl; +# use warnings; # only for Perl >= 5.6 + + +#--------------------------------------------------------------------------# +# Platform independent ptee invocation +#--------------------------------------------------------------------------# + +my $perl = Probe::Perl->find_perl_interpreter; +my $ptee_cmd = "$perl -MTee::App -e run --"; + +#--------------------------------------------------------------------------# +# Functions +#--------------------------------------------------------------------------# sub tee { my $command = shift; my $options; $options = shift if (ref $_[0] eq 'HASH'); - - my $mode = $options->{append} ? ">>" : ">"; + my $files = join(" ", @_); my $redirect = $options->{stderr} ? " 2>&1 " : q{}; - - my @files; - for my $file ( @_ ) { - my $f = IO::File->new("$mode $file") - or die "Could't open '$file' for writing: $!'"; - push @files, $f; - } - - local *COMMAND_FH; - open COMMAND_FH, "$command $redirect |" or die; - my $buffer; - my $buffer_size = 1024; - while ( sysread( COMMAND_FH, $buffer, $buffer_size ) > 0 ) { - for my $fh ( *STDOUT, @files ) { - syswrite $fh, $buffer; - } - } - - close COMMAND_FH; # to get $? - my $status = $?; - my $exit = $status ? 0 : 1; - - close for @files; - - return wantarray ? ($exit, $status) : $exit; + my $append = $options->{append} ? " -a " : q{}; + system( "$command $redirect | $ptee_cmd $append $files" ); } -1; +1; # modules must be true __END__ +#--------------------------------------------------------------------------# +# main pod documentation +#--------------------------------------------------------------------------# =begin wikidoc @@ -61,7 +47,7 @@ Tee - Pure Perl emulation of GNU tee = VERSION -This documentation describes version %%VERSION%%. +This documentation refers to version %%VERSION%% = SYNOPSIS @@ -74,15 +60,17 @@ This documentation describes version %%VERSION%%. = DESCRIPTION -The {Tee} distribution provides a pure Perl emulation of the standard GNU tool -{tee}. It is designed to be a platform-independent replacement for operating -systems without a native {tee} program. As with {tee}, it passes input -received on STDIN through to STDOUT while also writing a copy of the input to -one or more files. By default, files will be overwritten. +The {Tee} distribution provides the [ptee] program, a pure Perl emulation of +the standard GNU tool {tee}. It is designed to be a platform-independent +replacement for operating systems without a native {tee} program. As with +{tee}, it passes input received on STDIN through to STDOUT while also writing a +copy of the input to one or more files. By default, files will be overwritten. -In addition to this module, the distribution also provides the [ptee] program -for a command line replacement for {tee}. Unlike {tee}, {ptee} does not -support ignoring interrupts, as signal handling is not sufficiently portable. +Unlike {tee}, {ptee} does not support ignoring interrupts, as signal handling +is not sufficiently portable. + +The {Tee} module provides a convenience function that may be used in place of +{system()} to redirect commands through {ptee}. = USAGE @@ -91,10 +79,11 @@ support ignoring interrupts, as signal handling is not sufficiently portable. tee( $command, @filenames ); tee( $command, \%options, @filenames ); -Executes the given command, teeing output to STDOUT and a list of files. -Unlike with {system()}, the command must be a string as the command shell is -used for redirection and piping. It returns true if the command has an exit -status of zero and false otherwise. The exit status is preserved in {$?}. +Executes the given command via {system()}, but pipes it through [ptee] to copy +output to the list of files. Unlike with {system()}, the command must be a +string as the command shell is used for redirection and piping. The return +value of {system()} is passed through, but reflects the success of +the {ptee} command, which isn't very useful. The second argument may be a hash-reference of options. Recognized options include: @@ -111,6 +100,12 @@ portable alternative for capturing these streams from a command separately is [IPC::Run3], though it does not allow passing it through to a terminal at the same time. += SEE ALSO + +* [ptee] +* IPC::Run3 +* IO::Tee + = BUGS Please report any bugs or feature using the CPAN Request Tracker. @@ -120,12 +115,6 @@ Bugs can be submitted through the web interface at When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature. -= SEE ALSO - -* [ptee] -* IPC::Run3 -* IO::Tee - = AUTHOR David A. Golden (DAGOLDEN) @@ -152,4 +141,3 @@ limitations under the License. =end wikidoc =cut - diff --git a/lib/Tee.pod b/lib/Tee.pod index 19ad46c..094632c 100644 --- a/lib/Tee.pod +++ b/lib/Tee.pod @@ -8,7 +8,7 @@ Tee - Pure Perl emulation of GNU tee =head1 VERSION -This documentation describes version 0.13_51. +This documentation refers to version 0.13_51 =head1 SYNOPSIS @@ -21,15 +21,17 @@ This documentation describes version 0.13_51. =head1 DESCRIPTION -The C<<< Tee >>> distribution provides a pure Perl emulation of the standard GNU tool -C<<< tee >>>. It is designed to be a platform-independent replacement for operating -systems without a native C<<< tee >>> program. As with C<<< tee >>>, it passes input -received on STDIN through to STDOUT while also writing a copy of the input to -one or more files. By default, files will be overwritten. +The C<<< Tee >>> distribution provides the L program, a pure Perl emulation of +the standard GNU tool C<<< tee >>>. It is designed to be a platform-independent +replacement for operating systems without a native C<<< tee >>> program. As with +C<<< tee >>>, it passes input received on STDIN through to STDOUT while also writing a +copy of the input to one or more files. By default, files will be overwritten. -In addition to this module, the distribution also provides the L program -for a command line replacement for C<<< tee >>>. Unlike C<<< tee >>>, C<<< ptee >>> does not -support ignoring interrupts, as signal handling is not sufficiently portable. +Unlike C<<< tee >>>, C<<< ptee >>> does not support ignoring interrupts, as signal handling +is not sufficiently portable. + +The C<<< Tee >>> module provides a convenience function that may be used in place of +C<<< system() >>> to redirect commands through C<<< ptee >>>. =head1 USAGE @@ -38,10 +40,11 @@ support ignoring interrupts, as signal handling is not sufficiently portable. tee( $command, @filenames ); tee( $command, \%options, @filenames ); -Executes the given command, teeing output to STDOUT and a list of files. -Unlike with C<<< system() >>>, the command must be a string as the command shell is -used for redirection and piping. It returns true if the command has an exit -status of zero and false otherwise. The exit status is preserved in C<<< $? >>>. +Executes the given command via C<<< system() >>>, but pipes it through L to copy +output to the list of files. Unlike with C<<< system() >>>, the command must be a +string as the command shell is used for redirection and piping. The return +value of C<<< system() >>> is passed through, but reflects the success of +the C<<< ptee >>> command, which isn't very useful. The second argument may be a hash-reference of options. Recognized options include: @@ -67,15 +70,6 @@ portable alternative for capturing these streams from a command separately is L, though it does not allow passing it through to a terminal at the same time. -=head1 BUGS - -Please report any bugs or feature using the CPAN Request Tracker. -Bugs can be submitted through the web interface at -L - -When submitting a bug or request, please include a test-file or a patch to an -existing test-file that illustrates the bug or desired feature. - =head1 SEE ALSO =over @@ -94,6 +88,15 @@ IO::Tee =back +=head1 BUGS + +Please report any bugs or feature using the CPAN Request Tracker. +Bugs can be submitted through the web interface at +L + +When submitting a bug or request, please include a test-file or a patch to an +existing test-file that illustrates the bug or desired feature. + =head1 AUTHOR David A. Golden (DAGOLDEN) diff --git a/lib/Tee/App.pm b/lib/Tee/App.pm new file mode 100644 index 0000000..cfa1d10 --- /dev/null +++ b/lib/Tee/App.pm @@ -0,0 +1,155 @@ +# Copyright (c) 2008 by David Golden. All rights reserved. +# Licensed under Apache License, Version 2.0 (the "License"). +# You may not use this file except in compliance with the License. +# A copy of the License was distributed with this file or you may obtain a +# copy of the License from http://www.apache.org/licenses/LICENSE-2.0 + +package Tee::App; +use strict; +use Exporter (); +use File::Basename qw/basename/; +use Getopt::Long; +use IO::File; + +@Tee::App::ISA = 'Exporter'; +@Tee::App::EXPORT = qw/run/; +$Tee::App::VERSION = '0.13_51'; +$Tee::App::VERSION = eval $Tee::App::VERSION; ## no critic + +#--------------------------------------------------------------------------# +# define help text +#--------------------------------------------------------------------------# + +my $help_text = <<'END_HELP'; + + ptee [OPTIONS]... [FILENAMES]... + + OPTIONS: + + --append or -a + append to file(s) rather than overwrite + + --help or -h + give usage information + + --version or -V + print the version number of this program + +END_HELP + +$help_text =~ s/\A.+?( ptee.*)/$1/ms; + +sub run { + + #--------------------------------------------------------------------------# + # process command line + #--------------------------------------------------------------------------# + + my %opts; + GetOptions( \%opts, + 'version|V', + 'help|h|?', + 'append|a', + ); + + #--------------------------------------------------------------------------# + # options + #--------------------------------------------------------------------------# + + if ($opts{version}) { + print basename($0), " $main::VERSION\n"; + exit 0; + } + + if ($opts{help}) { + print "Usage:\n$help_text"; + exit 1; + } + + my $mode = $opts{append} ? ">>" : ">"; + + #--------------------------------------------------------------------------# + # Setup list of filehandles + #--------------------------------------------------------------------------# + + my $stdout = IO::Handle->new->fdopen(fileno(STDOUT),"w"); + my @files = $stdout; + + for my $file ( @ARGV ) { + my $f = IO::File->new("$mode $file") + or die "Could't open '$file' for writing: $!'"; + push @files, $f; + } + + #--------------------------------------------------------------------------# + # Tee input to the filehandle list + #--------------------------------------------------------------------------# + + my $buffer_size = 1024; + my $buffer; + + while ( sysread( STDIN, $buffer, $buffer_size ) > 0 ) { + for my $fh ( @files ) { + syswrite $fh, $buffer; + } + } + return; +} + +1; + +__END__ + +=begin wikidoc + += NAME + +Tee::App - Pure Perl emulation of GNU tee + += VERSION + +This documentation refers to version %%VERSION%% + += DESCRIPTION + +Guts of the {ptee} command. + += SEE ALSO + +* [ptee] + += BUGS + +Please report any bugs or feature using the CPAN Request Tracker. +Bugs can be submitted through the web interface at +[http://rt.cpan.org/Dist/Display.html?Queue=Tee] + +When submitting a bug or request, please include a test-file or a patch to an +existing test-file that illustrates the bug or desired feature. + += AUTHOR + +David A. Golden (DAGOLDEN) + += COPYRIGHT AND LICENSE + +Copyright (c) 2006-2008 by David A. Golden. All rights reserved. + +Licensed under Apache License, Version 2.0 (the "License"). +You may not use this file except in compliance with the License. +A copy of the License was distributed with this file or you may obtain a +copy of the License from http://www.apache.org/licenses/LICENSE-2.0 + +Files produced as output though the use of this software, shall not be +considered Derivative Works, but shall be considered the original work of the +Licensor. + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +=end wikidoc + +=cut diff --git a/lib/Tee/App.pod b/lib/Tee/App.pod new file mode 100644 index 0000000..368bccb --- /dev/null +++ b/lib/Tee/App.pod @@ -0,0 +1,59 @@ +# Generated by Pod::WikiDoc version 0.18 + +=pod + +=head1 NAME + +Tee::App - Pure Perl emulation of GNU tee + +=head1 VERSION + +This documentation refers to version 0.13_51 + +=head1 DESCRIPTION + +Guts of the C<<< ptee >>> command. + +=head1 SEE ALSO + +=over + +=item * + +L + +=back + +=head1 BUGS + +Please report any bugs or feature using the CPAN Request Tracker. +Bugs can be submitted through the web interface at +L + +When submitting a bug or request, please include a test-file or a patch to an +existing test-file that illustrates the bug or desired feature. + +=head1 AUTHOR + +David A. Golden (DAGOLDEN) + +=head1 COPYRIGHT AND LICENSE + +Copyright (c) 2006-2008 by David A. Golden. All rights reserved. + +Licensed under Apache License, Version 2.0 (the "License"). +You may not use this file except in compliance with the License. +A copy of the License was distributed with this file or you may obtain a +copy of the License from http:EEwww.apache.orgElicensesELICENSE-2.0 + +Files produced as output though the use of this software, shall not be +considered Derivative Works, but shall be considered the original work of the +Licensor. + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + + diff --git a/t/02_tee_script.t b/t/02_tee_script.t index d92c04f..04e8d9d 100644 --- a/t/02_tee_script.t +++ b/t/02_tee_script.t @@ -2,7 +2,8 @@ use strict; use File::Spec; use File::Temp; -use IPC::Run3; +use IO::CaptureOutput qw/ capture qxx /; +use IO::File; use Probe::Perl; use Test::More; use t::Expected; @@ -16,88 +17,112 @@ $|=1; select($stdout); $|=1; +#--------------------------------------------------------------------------# +# Declarations #--------------------------------------------------------------------------# -plan tests => 9 ; - -my $perl = Probe::Perl->find_perl_interpreter; +my $pp = Probe::Perl->new; +my $perl = $pp->find_perl_interpreter; my $hello = File::Spec->catfile(qw/t helloworld.pl/); -my $tee = File::Spec->catfile(qw/scripts ptee/); +my $tee = File::Spec->catfile(qw/bin ptee/); my $tempfh = File::Temp->new; +my $tempfh2 = File::Temp->new; my $tempname = $tempfh->filename; -my ($got_stdout, $got_stderr); +my $tempname2 = $tempfh2->filename; +my ($got_stdout, $got_stderr, $teed_stdout); + +my $stdout_regex = quotemeta(expected("STDOUT")); +my $stderr_regex = quotemeta(expected("STDERR")); + +sub _slurp { + my $fh = IO::File->new(shift, "r"); + local $/; + return scalar <$fh>; +} + +#--------------------------------------------------------------------------# +# Begin test plan +#--------------------------------------------------------------------------# + +plan tests => 14 ; + +require_ok( "Tee" ); +Tee->import; + +can_ok( "main", "tee" ); ok( -r $hello, "hello script readable" ); -ok( -r $tee, - "tee script readable" -); +# check direct output of helloworld -# check direct output of hello world -run3 "$perl $hello", undef, \$got_stdout, \$got_stderr; +($got_stdout, $got_stderr) = qxx("$perl $hello"); -is( $got_stdout, expected("STDOUT"), - "hello world program output (direct)" +is( $got_stdout, expected("STDOUT"), + "system(CMD) script STDOUT" +); +is( $got_stderr, expected("STDERR"), + "system(CMD) script STDERR" ); -# check output through ptee +# check tee of STDOUT truncate $tempfh, 0; -run3 "$perl $hello | $perl $tee $tempname", undef, \$got_stdout, \$got_stderr; -is( $got_stdout, expected("STDOUT"), - "hello world program output (tee stdout)" -); - -open FH, "< $tempname"; -$got_stdout = do { local $/; }; -close FH; +($got_stdout, $got_stderr) = qxx("$perl $hello | $perl $tee $tempname"); -is( $got_stdout, expected("STDOUT"), - "hello world program output (tee file)" +is( $got_stdout, expected("STDOUT"), + "tee(CMD,FILE) script STDOUT" +); +is( $got_stderr, expected("STDERR"), + "tee(CMD,FILE) script STDERR" ); -# check appended output -run3 "$perl $hello | $perl $tee -a $tempname", undef, \$got_stdout, \$got_stderr; +$teed_stdout = _slurp($tempname); +is( $teed_stdout, expected("STDOUT"), + "tee(CMD,FILE) script tee file" +); -open FH, "< $tempname"; -$got_stdout = do { local $/; }; -close FH; +## check tee of STDOUT to multiple files +truncate $tempfh, 0; +($got_stdout, $got_stderr) = qxx("$perl $hello | $perl $tee $tempname $tempname2"); -is( $got_stdout, expected("STDOUT") x 2, - "hello world program output (tee -a)" +$teed_stdout = _slurp($tempname); +is( $teed_stdout, expected("STDOUT"), + "tee(CMD,FILE1,FILE2) script tee file (1)" ); -run3 "$perl $hello | $perl $tee --append $tempname", undef, \$got_stdout, \$got_stderr; - -open FH, "< $tempname" or die "Can't open $tempname for reading"; +$teed_stdout = _slurp($tempname2); +is( $teed_stdout, expected("STDOUT"), + "tee(CMD,FILE1,FILE2) script tee file (2)" +); -$got_stdout = do { local $/; }; -close FH; +### check tee of both STDOUT and STDERR +truncate $tempfh, 0; +($got_stdout, $got_stderr) = qxx("$perl $hello 2>&1 | $perl $tee $tempname" ); + +$teed_stdout = _slurp($tempname); -is( $got_stdout, expected("STDOUT") x 3, - "hello world program output (tee --append)" +like( $teed_stdout, "/$stdout_regex/", + "tee(CMD,FILE) w/stderr script tee file (STDOUT)" +); +like( $teed_stdout, "/$stderr_regex/", + "tee(CMD,FILE) w/stderr script tee file (STDERR)" ); -# check multiple files -my $temp2 = File::Temp->new; -truncate $tempfh, 0; -run3 "$perl $hello | $perl $tee $tempname $temp2", undef, \$got_stdout, \$got_stderr; -open FH, "< $tempname"; -$got_stdout = do { local $/; }; -close FH; +### check tee of both with append +($got_stdout, $got_stderr) = qxx("$perl $hello 2>&1 | $perl -- $tee --append $tempname" ); -is( $got_stdout, expected("STDOUT"), - "hello world program output (tee file1 file2 [1])" -); +$teed_stdout = _slurp($tempname); -open FH, "< $temp2"; -$got_stdout = do { local $/; }; -close FH; +my $saw_stdout = () = ( $teed_stdout =~ /($stdout_regex)/gms ); +my $saw_stderr = () = ( $teed_stdout =~ /($stderr_regex)/gms ); -is( $got_stdout, expected("STDOUT"), - "hello world program output (tee file1 file2 [2])" +is( $saw_stdout, 2, + "tee(CMD,FILE) w/stderr+append script tee file (STDOUT)" +); +is( $saw_stderr, 2, + "tee(CMD,FILE) w/stderr+append script tee file (STDERR)" ); diff --git a/t/03_tee_function.t b/t/03_tee_function.t index 4bef172..56be3bb 100644 --- a/t/03_tee_function.t +++ b/t/03_tee_function.t @@ -45,7 +45,7 @@ sub _slurp { # Begin test plan #--------------------------------------------------------------------------# -plan tests => 18 ; +plan tests => 14 ; require_ok( "Tee" ); Tee->import; @@ -74,17 +74,6 @@ capture { $rc = tee("$perl $hello", $tempname) } \$got_stdout, \$got_stderr; -$status = $? >> 8; - - -is( $rc, 1, - "tee(CMD,FILE) returns 1 on successful execution" -); - -is( $status, 0, - "tee(CMD,FILE) \$? >> 8 is 0 on successful execution" -); - is( $got_stdout, expected("STDOUT"), "tee(CMD,FILE) script STDOUT" ); @@ -147,20 +136,3 @@ is( $saw_stderr, 2, "tee(CMD,FILE) w/stderr+append script tee file (STDERR)" ); -## check tee with fatal error code -truncate $tempfh, 0; -capture { - $rc = tee("$perl $fatality", { stderr => 1 }, $tempname); -} \$got_stdout, \$got_stderr; - -$status = $? >> 8; - -is( $rc, 0, - "teeing fatal script returns 0" -); - -is( $status, 1, - "\$? >> 8 is 1 on fatal execution" -); - -