xref: /openbsd-src/gnu/usr.bin/perl/cpan/IO-Compress/t/011-streamzip.t (revision 5486feefcc8cb79b19e014ab332cc5dfd05b3b33)
1BEGIN {
2    if ($ENV{PERL_CORE}) {
3	chdir 't' if -d 't';
4	@INC = ("../lib", "lib/compress");
5    }
6}
7
8use lib qw(t t/compress);
9
10use strict;
11use warnings;
12use bytes;
13
14use Test::More ;
15use CompTestUtils;
16use IO::Uncompress::Unzip 'unzip' ;
17
18BEGIN
19{
20    plan(skip_all => "Needs Perl 5.005 or better - you have Perl $]" )
21        if $] < 5.005 ;
22
23    # use Test::NoWarnings, if available
24    my $extra = 0 ;
25    $extra = 1
26        if eval { require Test::NoWarnings ;  Test::NoWarnings->import; 1 };
27
28    plan tests => 136 + $extra ;
29}
30
31
32my $Inc = join " ", map qq["-I$_"] => @INC;
33$Inc = '"-MExtUtils::testlib"'
34    if ! $ENV{PERL_CORE} && eval " require ExtUtils::testlib; " ;
35
36my $Perl = ($ENV{'FULLPERL'} or $^X or 'perl') ;
37$Perl = qq["$Perl"] if $^O eq 'MSWin32' ;
38
39$Perl = "$Perl $Inc -w" ;
40#$Perl .= " -Mblib " ;
41my $binDir = $ENV{PERL_CORE} ? "../ext/IO-Compress/bin/"
42                             : "./bin/";
43
44my $hello1 = <<EOM ;
45hello
46this is
47a test
48message
49x ttttt
50xuuuuuu
51the end
52EOM
53
54
55
56
57my $lex = LexFile->new( my $stderr );
58
59
60sub check
61{
62    my $command = shift ;
63    my $expected = shift ;
64
65    my $lex = LexFile->new( my $stderr );
66
67    my $cmd = "$command 2>$stderr";
68    my $stdout = `$cmd` ;
69
70    my $aok = 1 ;
71
72    $aok &= is $?, 0, "  exit status is 0" ;
73
74    $aok &= is readFile($stderr), '', "  no stderr" ;
75
76    $aok &= is $stdout, $expected, "  expected content is ok"
77        if defined $expected ;
78
79    if (! $aok) {
80        diag "Command line: $cmd";
81        my ($file, $line) = (caller)[1,2];
82        diag "Test called from $file, line $line";
83    }
84
85    1 while unlink $stderr;
86}
87
88
89# streamzip
90# #########
91
92{
93    title "streamzip" ;
94
95    my ($infile, $outfile);
96    my $lex = LexFile->new( $infile, $outfile );
97
98    writeFile($infile, $hello1) ;
99    check "$Perl ${binDir}/streamzip <$infile >$outfile";
100
101    my $uncompressed ;
102    unzip $outfile => \$uncompressed;
103    is $uncompressed, $hello1;
104}
105
106{
107    title "streamzip - zipfile option" ;
108
109    my ($infile, $outfile);
110    my $lex = LexFile->new( $infile, $outfile );
111
112    writeFile($infile, $hello1) ;
113    check "$Perl ${binDir}/streamzip -zipfile $outfile <$infile";
114
115    my $uncompressed ;
116    unzip $outfile => \$uncompressed;
117    is $uncompressed, $hello1;
118}
119
120for my $method (qw(store deflate bzip2 lzma xz zstd))
121{
122    SKIP:
123    {
124        if ($method eq 'lzma')
125        {
126            no warnings;
127            eval { require IO::Compress::Lzma && defined &{ 'IO::Compress::Adapter::Bzip2::mkRawZipCompObject' } } ;
128            skip "Method 'lzma' needs IO::Compress::Lzma\n", 8
129                if $@;
130        }
131
132        if ($method eq 'zstd')
133        {
134            no warnings;
135            eval { require IO::Compress::Zstd && defined &{ 'IO::Compress::Adapter::Zstd::mkRawZipCompObject' }} ;
136            skip "Method 'zstd' needs IO::Compress::Zstd\n", 8
137                if $@;
138        }
139
140        if ($method eq 'xz')
141        {
142            no warnings;
143            eval { require IO::Compress::Xz && defined &{ 'IO::Compress::Adapter::Xz::mkRawZipCompObject' }} ;
144            skip "Method 'xz' needs IO::Compress::Xz\n", 8
145                if $@;
146        }
147
148        {
149            title "streamzip method $method" ;
150
151            skip "streaming unzip not supported with zstd\n", 7
152                if $method eq 'zstd' ;
153
154            my ($infile, $outfile);
155            my $lex = LexFile->new( $infile, $outfile );
156
157            writeFile($infile, $hello1) ;
158            check "$Perl ${binDir}/streamzip -method $method <$infile >$outfile";
159
160            my $uncompressed ;
161            unzip $outfile => \$uncompressed;
162            is $uncompressed, $hello1;
163        }
164
165        {
166            title "streamzip $method- zipfile option" ;
167
168            my ($infile, $outfile);
169            my $lex = LexFile->new( $infile, $outfile );
170
171            writeFile($infile, $hello1) ;
172            check "$Perl ${binDir}/streamzip -zipfile $outfile -method $method <$infile";
173
174            my $uncompressed ;
175            unzip $outfile => \$uncompressed;
176            is $uncompressed, $hello1;
177        }
178    }
179}
180
181for my $level (0 ..9)
182{
183    {
184        title "streamzip level $level" ;
185
186        my ($infile, $outfile);
187        my $lex = LexFile->new( $infile, $outfile );
188
189        writeFile($infile, $hello1) ;
190        check "$Perl ${binDir}/streamzip -$level <$infile >$outfile";
191
192        my $uncompressed ;
193        unzip $outfile => \$uncompressed;
194        is $uncompressed, $hello1;
195    }
196
197    {
198        title "streamzip level $level- zipfile option" ;
199
200        my ($infile, $outfile);
201        my $lex = LexFile->new( $infile, $outfile );
202
203        writeFile($infile, $hello1) ;
204        check "$Perl ${binDir}/streamzip -zipfile $outfile -$level <$infile";
205
206        my $uncompressed ;
207        unzip $outfile => \$uncompressed;
208        is $uncompressed, $hello1;
209    }
210
211}
212