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 ;  import Test::NoWarnings; 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            eval { require IO::Compress::Lzma } ;
127            skip "Method 'lzma' needs IO::Compress::Lzma\n", 8
128                if $@;
129        }
130
131        if ($method eq 'zstd')
132        {
133            eval { require IO::Compress::Zstd } ;
134            skip "Method 'zstd' needs IO::Compress::Zstd\n", 8
135                if $@;
136        }
137
138        if ($method eq 'xz')
139        {
140            eval { require IO::Compress::Xz } ;
141            skip "Method 'zstd' needs IO::Compress::Xz\n", 8
142                if $@;
143        }
144
145        {
146            title "streamzip method $method" ;
147
148            my ($infile, $outfile);
149            my $lex = LexFile->new( $infile, $outfile );
150
151            writeFile($infile, $hello1) ;
152            check "$Perl ${binDir}/streamzip -method $method <$infile >$outfile";
153
154            my $uncompressed ;
155            unzip $outfile => \$uncompressed;
156            is $uncompressed, $hello1;
157        }
158
159        {
160            title "streamzip $method- zipfile option" ;
161
162            my ($infile, $outfile);
163            my $lex = LexFile->new( $infile, $outfile );
164
165            writeFile($infile, $hello1) ;
166            check "$Perl ${binDir}/streamzip -zipfile $outfile -method $method <$infile";
167
168            my $uncompressed ;
169            unzip $outfile => \$uncompressed;
170            is $uncompressed, $hello1;
171        }
172    }
173}
174
175for my $level (0 ..9)
176{
177    {
178        title "streamzip level $level" ;
179
180        my ($infile, $outfile);
181        my $lex = LexFile->new( $infile, $outfile );
182
183        writeFile($infile, $hello1) ;
184        check "$Perl ${binDir}/streamzip -$level <$infile >$outfile";
185
186        my $uncompressed ;
187        unzip $outfile => \$uncompressed;
188        is $uncompressed, $hello1;
189    }
190
191    {
192        title "streamzip level $level- zipfile option" ;
193
194        my ($infile, $outfile);
195        my $lex = LexFile->new( $infile, $outfile );
196
197        writeFile($infile, $hello1) ;
198        check "$Perl ${binDir}/streamzip -zipfile $outfile -$level <$infile";
199
200        my $uncompressed ;
201        unzip $outfile => \$uncompressed;
202        is $uncompressed, $hello1;
203    }
204
205}
206