Skip to content

Commit

Permalink
Merge pull request #28 from hoelzro/dev
Browse files Browse the repository at this point in the history
Fall back to error handler for warning if no warn handler is found
  • Loading branch information
petdance committed Jul 8, 2016
2 parents 04215e1 + 8e7e58e commit 9715697
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Next.pm
Expand Up @@ -333,7 +333,7 @@ sub from_file {

my ($parms,@queue) = _setup( \%files_defaults, @_ );
my $err = $parms->{error_handler};
my $warn = $parms->{warn_handler};
my $warn = $parms->{warning_handler};

my $filename = $queue[1];

Expand Down
45 changes: 44 additions & 1 deletion t/from_file.t
Expand Up @@ -2,11 +2,14 @@

use strict;
use warnings;
use Test::More tests => 10;
use Test::More tests => 17;

use lib 't';
use Util;

use File::Copy ();
use File::Temp;

use File::Next;

# use Test::Differences;
Expand Down Expand Up @@ -68,3 +71,43 @@ FROM_MISSING_FILE: {
ok( !defined($iter), 'Iterator should be null' );
ok( !defined($rc), 'Eval should fail' );
}

FROM_OK_FILE_BUT_MISSING: {
my $warn_called;
local $SIG{__WARN__} = sub { $warn_called = 1 };

my $tempfile = File::Temp->new;
File::Copy::copy('t/filelist.txt', $tempfile);
print {$tempfile} "t/non-existent-file.txt\n";
$tempfile->close;

my $iter = File::Next::from_file( $tempfile->filename );
isa_ok( $iter, 'CODE' );

my @actual = slurp( $iter );
sets_match( \@actual, \@expected, 'FROM_FILESYSTEM_FILE' );

ok($warn_called, 'CORE::warn() should be called if a warning occurs and no warning_handler is set');
}

FROM_OK_FILE_BUT_MISSING_WITH_HANDLER: {
my $warn_called;
local $SIG{__WARN__} = sub { $warn_called = 1 };

my $tempfile = File::Temp->new;
File::Copy::copy('t/filelist.txt', $tempfile);
print {$tempfile} "t/non-existent-file.txt\n";
$tempfile->close;

my $warning_handler_called;
my $iter = File::Next::from_file({
warning_handler => sub { $warning_handler_called = 1 },
}, $tempfile->filename);
isa_ok( $iter, 'CODE' );

my @actual = slurp( $iter );
sets_match( \@actual, \@expected, 'FROM_FILESYSTEM_FILE' );

ok(!$warn_called, 'CORE::warn() should be not called if a warning occurs but a warning_handler is set');
ok($warning_handler_called, 'The set warning handler should be called if a warning occurs');
}

0 comments on commit 9715697

Please sign in to comment.