Akron | 8571751 | 2020-07-08 11:19:19 +0200 | [diff] [blame] | 1 | use strict; |
| 2 | use warnings; |
| 3 | use Test::More; |
Akron | 8571751 | 2020-07-08 11:19:19 +0200 | [diff] [blame] | 4 | use File::Spec::Functions qw/catfile/; |
Peter Harders | 57c884e | 2020-07-16 01:28:52 +0200 | [diff] [blame^] | 5 | use File::Temp qw/tempfile/; |
Akron | 8571751 | 2020-07-08 11:19:19 +0200 | [diff] [blame] | 6 | use IO::Uncompress::Unzip; |
| 7 | |
| 8 | use FindBin; |
| 9 | BEGIN { |
| 10 | unshift @INC, "$FindBin::Bin/../lib"; |
| 11 | }; |
| 12 | |
Peter Harders | 57c884e | 2020-07-16 01:28:52 +0200 | [diff] [blame^] | 13 | our %ENV; |
| 14 | # default: remove temp. file created by func. tempfile |
| 15 | # to keep temp. files use e.g. 'KORAPXMLTEI_DONTUNLINK=1 prove -lr t/script.t' |
| 16 | my $_UNLINK = $ENV{KORAPXMLTEI_DONTUNLINK}?0:1; |
| 17 | |
Akron | 8571751 | 2020-07-08 11:19:19 +0200 | [diff] [blame] | 18 | require_ok('KorAP::XML::TEI::Zipper'); |
| 19 | |
| 20 | my $data; |
Peter Harders | 57c884e | 2020-07-16 01:28:52 +0200 | [diff] [blame^] | 21 | my ($fh, $outzip) = tempfile("KorAP-XML-TEI_zipper_XXXXXXXXXX", SUFFIX => ".tmp", TMPDIR => 1, UNLINK => $_UNLINK); |
Akron | 8571751 | 2020-07-08 11:19:19 +0200 | [diff] [blame] | 22 | |
| 23 | my $zip = KorAP::XML::TEI::Zipper->new($outzip); |
Peter Harders | 57c884e | 2020-07-16 01:28:52 +0200 | [diff] [blame^] | 24 | $fh->close; |
Akron | 8571751 | 2020-07-08 11:19:19 +0200 | [diff] [blame] | 25 | |
| 26 | ok($zip, 'Zipper initialized'); |
| 27 | |
| 28 | ok($zip->new_stream('data/file1.txt')->print('hello'), 'Write to initial stream'); |
| 29 | ok($zip->new_stream('data/file2.txt')->print('world'), 'Write to appended stream'); |
| 30 | |
| 31 | $zip->close; |
| 32 | |
| 33 | ok(-e $outzip, 'Zip exists'); |
| 34 | |
| 35 | # Uncompress GOE/header.xml from zip file |
| 36 | my $unzip = IO::Uncompress::Unzip->new($outzip, Name => 'data/file1.txt'); |
| 37 | |
| 38 | $data .= $unzip->getline while !$unzip->eof; |
| 39 | ok($unzip->close, 'Closed'); |
| 40 | |
| 41 | is($data, 'hello', 'Data correct'); |
| 42 | |
| 43 | |
| 44 | # Uncompress GOE/header.xml from zip file |
| 45 | $unzip = IO::Uncompress::Unzip->new($outzip, Name => 'data/file2.txt'); |
| 46 | |
| 47 | $data = ''; |
| 48 | $data .= $unzip->getline while !$unzip->eof; |
| 49 | ok($unzip->close, 'Closed'); |
| 50 | |
| 51 | is($data, 'world', 'Data correct'); |
| 52 | |
| 53 | done_testing; |