Skip to content

Commit

Permalink
Added tests for STDIN, and fix the STDIN reading along the way
Browse files Browse the repository at this point in the history
  • Loading branch information
petdance committed May 21, 2012
1 parent 9b0b528 commit ccb32d1
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 39 deletions.
15 changes: 11 additions & 4 deletions Next.pm
Expand Up @@ -84,7 +84,8 @@ whatever.
=head2 from_file( [ \%options, ] $filename )
Returns an iterator that iterates over each of the files specified
in I<$filename>. If I<$filename> is C<->, then STDIN is opened.
in I<$filename>. If I<$filename> is C<->, then the files are read
from STDIN.
The files are assumed to be in the file one filename per line. If
I<$null_separated> is passed, then the files are assumed to be
Expand Down Expand Up @@ -331,9 +332,15 @@ sub from_file {
$err->( 'Must pass a filename to from_file()' );
}

open( my $fh, '<', $filename );
if ( !$fh ) {
$err->( "Unable to open $filename: $!" );
my $fh;
if ( $filename eq '-' ) {
$fh = \*STDIN;
}
else {
open( $fh, '<', $filename );
if ( !$fh ) {
$err->( "Unable to open $filename: $!" );
}
}
my $filter = $parms->{file_filter};

Expand Down
2 changes: 2 additions & 0 deletions t/Util.pm
@@ -1,5 +1,7 @@
package main;

use File::Next;

sub slurp {
my $iter = shift;
my @files;
Expand Down
2 changes: 2 additions & 0 deletions t/dot.t
Expand Up @@ -27,13 +27,15 @@ NO_PARMS: {
filelist-nul.txt
follow.t
from_file.t
from_stdin.t
give-me-a-process-pipe.pl
methods.t
parms.t
pod-coverage.t
pod.t
process-substitution.t
sort.t
stdin-iterator.pl
Util.pm
zero.t
swamp/a/a1
Expand Down
2 changes: 2 additions & 0 deletions t/everything.t
Expand Up @@ -28,13 +28,15 @@ NO_PARMS: {
t/filelist-nul.txt
t/follow.t
t/from_file.t
t/from_stdin.t
t/give-me-a-process-pipe.pl
t/methods.t
t/parms.t
t/pod-coverage.t
t/pod.t
t/process-substitution.t
t/sort.t
t/stdin-iterator.pl
t/swamp
t/swamp/0
t/swamp/a
Expand Down
1 change: 1 addition & 0 deletions t/filelist.txt
Expand Up @@ -12,4 +12,5 @@ t/pod-coverage.t
t/pod.t
t/process-substitution.t
t/sort.t
t/swamp/perl-test.t
t/zero.t
55 changes: 20 additions & 35 deletions t/from_file.t
Expand Up @@ -12,28 +12,31 @@ use File::Next;
# use Test::Differences;
# eq_or_diff \@got, [qw( a b c )], "testing arrays";

my @expected = qw(
t/00-load.t
t/api.t
t/basic.t
t/dirs.t
t/dot.t
t/everything.t
t/follow.t
t/from_file.t
t/methods.t
t/parms.t
t/pod-coverage.t
t/pod.t
t/process-substitution.t
t/sort.t
t/swamp/perl-test.t
t/zero.t
);


FROM_FILESYSTEM_FILE: {
my $iter = File::Next::from_file( 't/filelist.txt' );
isa_ok( $iter, 'CODE' );

my @actual = slurp( $iter );
my @expected = qw(
t/00-load.t
t/api.t
t/basic.t
t/dirs.t
t/dot.t
t/everything.t
t/follow.t
t/from_file.t
t/methods.t
t/parms.t
t/pod-coverage.t
t/pod.t
t/process-substitution.t
t/sort.t
t/zero.t
);
sets_match( \@actual, \@expected, 'FROM_FILESYSTEM_FILE' );
}

Expand All @@ -43,23 +46,5 @@ FROM_NUL_FILE: {
isa_ok( $iter, 'CODE' );

my @actual = slurp( $iter );
my @expected = qw(
t/00-load.t
t/api.t
t/basic.t
t/dirs.t
t/dot.t
t/everything.t
t/follow.t
t/from_file.t
t/methods.t
t/parms.t
t/pod-coverage.t
t/pod.t
t/process-substitution.t
t/sort.t
t/swamp/perl-test.t
t/zero.t
);
sets_match( \@actual, \@expected, 'FROM_NUL_FILE' );
}
43 changes: 43 additions & 0 deletions t/from_stdin.t
@@ -0,0 +1,43 @@
#!perl

use strict;
use warnings;
use Test::More tests => 2;

use lib 't';
use Util;

my $CAT = "$^X -pe1";

my @expected = qw(
t/00-load.t
t/api.t
t/basic.t
t/dirs.t
t/dot.t
t/everything.t
t/follow.t
t/from_file.t
t/methods.t
t/parms.t
t/pod-coverage.t
t/pod.t
t/process-substitution.t
t/sort.t
t/swamp/perl-test.t
t/zero.t
);

FROM_STDIN: {
# Pipe stuff into the iterator
my @actual = `$CAT t/filelist.txt | $^X -Mblib t/stdin-iterator.pl`;
chomp @actual;
sets_match( \@actual, \@expected, 'FROM_STDIN' );
}

FROM_STDIN_NUL: {
# Pipe nul-separated stuff into the iterator that handles nul-separated
my @actual = `$CAT t/filelist-nul.txt | $^X -Mblib t/stdin-iterator.pl 1`;
chomp @actual;
sets_match( \@actual, \@expected, 'FROM_STDIN_NUL' );
}
13 changes: 13 additions & 0 deletions t/stdin-iterator.pl
@@ -0,0 +1,13 @@
#!/usr/bin/env perl

use strict;
use warnings;

use File::Next;

my $nul = shift;

my $iter = File::Next::from_file( { nul_separated => $nul }, '-' );
while ( my $file = $iter->() ) {
print "$file\n";
}

0 comments on commit ccb32d1

Please sign in to comment.