Skip to content
Snippets Groups Projects
Commit 1c20e6a2 authored by Alessandro Vullo's avatar Alessandro Vullo
Browse files

bzip2 based array slurping and corresponding tests.

parent 9ea373a2
No related branches found
No related tags found
No related merge requests found
......@@ -317,14 +317,14 @@ sub slurp_to_array {
return $contents;
}
=head2 gz_slurp_to_array()
=head2 gz_slurp_to_array
Arg [1] : string $file
Arg [2] : boolean $chomp
Arg [3] : HashRef arguments to pass into IO compression layers
Description : Sends the contents of the given gzipped file into an ArrayRef
Returntype : ArrayRef
Example : my $contents_array = slurp_to_array('/tmp/file.txt.gz');
Example : my $contents_array = gz_slurp_to_array('/tmp/file.txt.gz');
Exceptions : If the file did not exist or was not readable
Status : Stable
......@@ -341,7 +341,31 @@ sub gz_slurp_to_array {
return $contents;
}
=head2 fh_to_array()
=head2 bz_slurp_to_array
Arg [1] : string $file
Arg [2] : boolean $chomp
Arg [3] : HashRef arguments to pass into IO compression layers
Description : Sends the contents of the given bzipped file into an ArrayRef
Returntype : ArrayRef
Example : my $contents_array = bz_slurp_to_array('/tmp/file.txt.bz2');
Exceptions : If the file did not exist or was not readable
Status : Stable
=cut
sub bz_slurp_to_array {
my ($file, $chomp, $args) = @_;
my $contents;
bz_work_with_file($file, 'r', sub {
my ($fh) = @_;
$contents = fh_to_array($fh, $chomp);
return;
}, $args);
return $contents;
}
=head2 fh_to_array
Arg [1] : Glob/IO::Handle $fh
Arg [2] : boolean $chomp
......
......@@ -69,6 +69,12 @@ SKIP: {
my $content = bz_slurp($bz2tmpfile->filename);
like($content, qr/test data/, "Bzip2: correct content");
like($content, qr/more data/, "Bzip2: more correct content");
# test bz_slurp_to_array
my $content_array = bz_slurp_to_array($bz2tmpfile->filename, 1);
ok($content_array->[0] eq 'test data', "Bzip2 slurped file first element");
ok($content_array->[1] eq 'some more data.', "Bzip2 slurped file second element");
}
my $ZIP_OK = 0;
......@@ -80,12 +86,12 @@ eval {
SKIP: {
skip "Cannot run Zip/Unzip tests, install related IO::[Un]Compress modules first",
2 unless $BZIP2_OK;
2 unless $ZIP_OK;
# send the content of the tmpfile to another
# bzip2 compressed file
my $file_content = slurp($tmpfilename);
my $bz2tmpfile = File::Temp->new(DIR => $dirname, SUFFIX => '.bz2');
my $bz2tmpfile = File::Temp->new(DIR => $dirname, SUFFIX => '.zip');
zip_work_with_file($bz2tmpfile->filename, 'w', sub {
my ($fh) = @_;
......@@ -97,6 +103,7 @@ SKIP: {
my $content = zip_slurp($bz2tmpfile->filename);
like($content, qr/test data/, "Zip: correct content");
like($content, qr/more data/, "Zip: more correct content");
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment