blob: 1911620b02bc541fa1d337f0d371410b71704ea5 [file] [log] [blame]
Akron85717512020-07-08 11:19:19 +02001use strict;
2use warnings;
3use Test::More;
Akron85717512020-07-08 11:19:19 +02004use File::Spec::Functions qw/catfile/;
Akron85717512020-07-08 11:19:19 +02005use IO::Uncompress::Unzip;
6
7use FindBin;
8BEGIN {
9 unshift @INC, "$FindBin::Bin/../lib";
10};
11
Peter Harders42e18a62020-07-21 02:43:26 +020012use Test::KorAP::XML::TEI qw!korap_tempfile!;
13
Akron85717512020-07-08 11:19:19 +020014require_ok('KorAP::XML::TEI::Zipper');
15
Akron3bdc0a32020-08-03 12:12:56 +020016subtest 'Create Zipper' => sub {
17 my $data;
18 my ($fh, $outzip) = korap_tempfile('zipper');
Akron85717512020-07-08 11:19:19 +020019
Akron3bdc0a32020-08-03 12:12:56 +020020 my $zip = KorAP::XML::TEI::Zipper->new('', $outzip);
21 $fh->close;
Akron85717512020-07-08 11:19:19 +020022
Akron3bdc0a32020-08-03 12:12:56 +020023 ok($zip, 'Zipper initialized');
Akron85717512020-07-08 11:19:19 +020024
Akron3bdc0a32020-08-03 12:12:56 +020025 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');
Akron85717512020-07-08 11:19:19 +020027
Akron3bdc0a32020-08-03 12:12:56 +020028 $zip->close;
Akron85717512020-07-08 11:19:19 +020029
Akron3bdc0a32020-08-03 12:12:56 +020030 ok(-e $outzip, 'Zip exists');
Akron85717512020-07-08 11:19:19 +020031
Akron3bdc0a32020-08-03 12:12:56 +020032 my $unzip = IO::Uncompress::Unzip->new($outzip, Name => 'data/file1.txt');
Akron85717512020-07-08 11:19:19 +020033
Akron3bdc0a32020-08-03 12:12:56 +020034 $data .= $unzip->getline while !$unzip->eof;
35 ok($unzip->close, 'Closed');
Akron85717512020-07-08 11:19:19 +020036
Akron3bdc0a32020-08-03 12:12:56 +020037 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};
Akron85717512020-07-08 11:19:19 +020047
48
Akron3bdc0a32020-08-03 12:12:56 +020049subtest 'Create Zipper with root dir "."' => sub {
50 my $data;
51 my ($fh, $outzip) = korap_tempfile('zipper');
Akron85717512020-07-08 11:19:19 +020052
Akron3bdc0a32020-08-03 12:12:56 +020053 my $zip = KorAP::XML::TEI::Zipper->new('.', $outzip);
54 $fh->close;
Akron85717512020-07-08 11:19:19 +020055
Akron3bdc0a32020-08-03 12:12:56 +020056 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
66subtest '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
83subtest '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
Akron85717512020-07-08 11:19:19 +0200100
101done_testing;