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/; |
Akron | 8571751 | 2020-07-08 11:19:19 +0200 | [diff] [blame] | 5 | use IO::Uncompress::Unzip; |
| 6 | |
| 7 | use FindBin; |
| 8 | BEGIN { |
| 9 | unshift @INC, "$FindBin::Bin/../lib"; |
| 10 | }; |
| 11 | |
Peter Harders | 42e18a6 | 2020-07-21 02:43:26 +0200 | [diff] [blame] | 12 | use Test::KorAP::XML::TEI qw!korap_tempfile!; |
| 13 | |
Akron | 8571751 | 2020-07-08 11:19:19 +0200 | [diff] [blame] | 14 | require_ok('KorAP::XML::TEI::Zipper'); |
| 15 | |
Akron | 3bdc0a3 | 2020-08-03 12:12:56 +0200 | [diff] [blame^] | 16 | subtest 'Create Zipper' => sub { |
| 17 | my $data; |
| 18 | my ($fh, $outzip) = korap_tempfile('zipper'); |
Akron | 8571751 | 2020-07-08 11:19:19 +0200 | [diff] [blame] | 19 | |
Akron | 3bdc0a3 | 2020-08-03 12:12:56 +0200 | [diff] [blame^] | 20 | my $zip = KorAP::XML::TEI::Zipper->new('', $outzip); |
| 21 | $fh->close; |
Akron | 8571751 | 2020-07-08 11:19:19 +0200 | [diff] [blame] | 22 | |
Akron | 3bdc0a3 | 2020-08-03 12:12:56 +0200 | [diff] [blame^] | 23 | ok($zip, 'Zipper initialized'); |
Akron | 8571751 | 2020-07-08 11:19:19 +0200 | [diff] [blame] | 24 | |
Akron | 3bdc0a3 | 2020-08-03 12:12:56 +0200 | [diff] [blame^] | 25 | ok($zip->new_stream('data/file1.txt')->print('hello'), 'Write to initial stream'); |
| 26 | ok($zip->new_stream('data/file2.txt')->print('world'), 'Write to appended stream'); |
Akron | 8571751 | 2020-07-08 11:19:19 +0200 | [diff] [blame] | 27 | |
Akron | 3bdc0a3 | 2020-08-03 12:12:56 +0200 | [diff] [blame^] | 28 | $zip->close; |
Akron | 8571751 | 2020-07-08 11:19:19 +0200 | [diff] [blame] | 29 | |
Akron | 3bdc0a3 | 2020-08-03 12:12:56 +0200 | [diff] [blame^] | 30 | ok(-e $outzip, 'Zip exists'); |
Akron | 8571751 | 2020-07-08 11:19:19 +0200 | [diff] [blame] | 31 | |
Akron | 3bdc0a3 | 2020-08-03 12:12:56 +0200 | [diff] [blame^] | 32 | my $unzip = IO::Uncompress::Unzip->new($outzip, Name => 'data/file1.txt'); |
Akron | 8571751 | 2020-07-08 11:19:19 +0200 | [diff] [blame] | 33 | |
Akron | 3bdc0a3 | 2020-08-03 12:12:56 +0200 | [diff] [blame^] | 34 | $data .= $unzip->getline while !$unzip->eof; |
| 35 | ok($unzip->close, 'Closed'); |
Akron | 8571751 | 2020-07-08 11:19:19 +0200 | [diff] [blame] | 36 | |
Akron | 3bdc0a3 | 2020-08-03 12:12:56 +0200 | [diff] [blame^] | 37 | is($data, 'hello', 'Data correct'); |
| 38 | |
| 39 | $unzip = IO::Uncompress::Unzip->new($outzip, Name => 'data/file2.txt'); |
| 40 | |
| 41 | $data = ''; |
| 42 | $data .= $unzip->getline while !$unzip->eof; |
| 43 | ok($unzip->close, 'Closed'); |
| 44 | |
| 45 | is($data, 'world', 'Data correct'); |
| 46 | }; |
Akron | 8571751 | 2020-07-08 11:19:19 +0200 | [diff] [blame] | 47 | |
| 48 | |
Akron | 3bdc0a3 | 2020-08-03 12:12:56 +0200 | [diff] [blame^] | 49 | subtest 'Create Zipper with root dir "."' => sub { |
| 50 | my $data; |
| 51 | my ($fh, $outzip) = korap_tempfile('zipper'); |
Akron | 8571751 | 2020-07-08 11:19:19 +0200 | [diff] [blame] | 52 | |
Akron | 3bdc0a3 | 2020-08-03 12:12:56 +0200 | [diff] [blame^] | 53 | my $zip = KorAP::XML::TEI::Zipper->new('.', $outzip); |
| 54 | $fh->close; |
Akron | 8571751 | 2020-07-08 11:19:19 +0200 | [diff] [blame] | 55 | |
Akron | 3bdc0a3 | 2020-08-03 12:12:56 +0200 | [diff] [blame^] | 56 | ok($zip, 'Zipper initialized'); |
| 57 | |
| 58 | ok($zip->new_stream('data/file1.txt')->print('hello'), 'Write to initial stream'); |
| 59 | $zip->close; |
| 60 | ok(-e $outzip, 'Zip exists'); |
| 61 | |
| 62 | ok(IO::Uncompress::Unzip->new($outzip, Name => 'data/file1.txt'), 'File exists'); |
| 63 | }; |
| 64 | |
| 65 | |
| 66 | subtest 'Create Zipper with root dir "subdir"' => sub { |
| 67 | my $data; |
| 68 | my ($fh, $outzip) = korap_tempfile('zipper'); |
| 69 | |
| 70 | my $zip = KorAP::XML::TEI::Zipper->new('subdir', $outzip); |
| 71 | $fh->close; |
| 72 | |
| 73 | ok($zip, 'Zipper initialized'); |
| 74 | |
| 75 | ok($zip->new_stream('data/file1.txt')->print('hello'), 'Write to initial stream'); |
| 76 | $zip->close; |
| 77 | ok(-e $outzip, 'Zip exists'); |
| 78 | |
| 79 | ok(IO::Uncompress::Unzip->new($outzip, Name => 'subdir/data/file1.txt'), 'File exists'); |
| 80 | ok(!IO::Uncompress::Unzip->new($outzip, Name => 'data/file1.txt'), 'File exists not'); |
| 81 | }; |
| 82 | |
| 83 | subtest 'Create Zipper with root dir "./"' => sub { |
| 84 | my $data; |
| 85 | my ($fh, $outzip) = korap_tempfile('zipper'); |
| 86 | |
| 87 | my $zip = KorAP::XML::TEI::Zipper->new('./', $outzip); |
| 88 | $fh->close; |
| 89 | |
| 90 | ok($zip, 'Zipper initialized'); |
| 91 | |
| 92 | ok($zip->new_stream('data/file1.txt')->print('hello'), 'Write to initial stream'); |
| 93 | $zip->close; |
| 94 | ok(-e $outzip, 'Zip exists'); |
| 95 | |
| 96 | # Uncompress GOE/header.xml from zip file |
| 97 | ok(IO::Uncompress::Unzip->new($outzip, Name => 'data/file1.txt'), 'File exists'); |
| 98 | }; |
| 99 | |
Akron | 8571751 | 2020-07-08 11:19:19 +0200 | [diff] [blame] | 100 | |
| 101 | done_testing; |