Skip to content

Commit

Permalink
Patch to ensure filehandles created by Archive::Zip::tempFile are clo…
Browse files Browse the repository at this point in the history
…sed properly
  • Loading branch information
antoniomonty committed Dec 1, 2015
1 parent cc9e250 commit 3d90cee
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions MANIFEST
Expand Up @@ -55,6 +55,7 @@ t/19_bug_101240.t
t/20_bug_github11.t
t/21_zip64.t
t/22_deflated_dir.t
t/23_closed_handler.t
t/badjpeg/expected.jpg
t/badjpeg/source.zip
t/common.pm
Expand Down
1 change: 1 addition & 0 deletions Makefile.PL
Expand Up @@ -37,6 +37,7 @@ WriteMakefile1(
},
BUILD_REQUIRES => {
'Test::More' => '0.88',
'Test::MockModule' => 0,
},
clean => {
FILES => join( ' ', qw{
Expand Down
1 change: 1 addition & 0 deletions lib/Archive/Zip.pm
Expand Up @@ -517,6 +517,7 @@ sub tempFile {
$dir ? (DIR => $dir) : ());
return (undef, undef) unless $fh;
my ($status, $newfh) = _newFileHandle($fh, 'w+');
$fh->close();
return ($newfh, $filename);
}

Expand Down
38 changes: 38 additions & 0 deletions t/23_closed_handler.t
@@ -0,0 +1,38 @@
#!/usr/bin/perl

# Test to make sure temporal filehandles created by Archive::Zip::tempFile are closed properly

use strict;
use warnings;

use Archive::Zip;
use Test::MockModule;

use Test::More tests => 2;

# array to store open filhandles
my @opened_filehandles;

# mocking File::Temp to store returned filehandles
my $mock_file_temp = Test::MockModule->new('File::Temp');

my $previous_tempfile_sub = \&File::Temp::tmpfile;
$mock_file_temp->mock(
tempfile => sub {
my ( $fh, $filename ) = $previous_tempfile_sub->(@_);
push( @opened_filehandles, $fh );
return ( $fh, $filename );
}
);

# calling method
Archive::Zip::tempFile();

# testing filehandles are closed
ok( scalar @opened_filehandles == 1, "One filehandle was created" );
ok( !defined $opened_filehandles[0]
|| !defined fileno( $opened_filehandles[0] )
|| fileno( $opened_filehandles[0] ) == -1,
"Filehandle is closed"
);

1 comment on commit 3d90cee

@redhotpenguin
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, 👍

Please sign in to comment.