Move IO::Compress from ext/ to cpan/
[p5sagit/p5-mst-13.2.git] / cpan / IO-Compress / t / 050interop-gzip.t
CommitLineData
25f0751f 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);
9use strict;
10use warnings;
11use bytes;
12
13use Test::More ;
25139ca2 14use CompTestUtils;
25f0751f 15
16my $GZIP ;
17
25f0751f 18
25139ca2 19sub ExternalGzipWorks
20{
21 my $lex = new LexFile my $outfile;
22 my $content = qq {
23Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut tempus odio id
24 dolor. Camelus perlus. Larrius in lumen numen. Dolor en quiquum filia
25 est. Quintus cenum parat.
26};
25f0751f 27
25139ca2 28 writeWithGzip($outfile, $content)
29 or return 0;
25f0751f 30
25139ca2 31 my $got ;
32 readWithGzip($outfile, $got)
33 or return 0;
25f0751f 34
25139ca2 35 if ($content ne $got)
36 {
37 diag "Uncompressed content is wrong";
38 return 0 ;
39 }
25f0751f 40
25139ca2 41 return 1 ;
25f0751f 42}
43
25f0751f 44sub readWithGzip
45{
46 my $file = shift ;
47
cb7abd7f 48 my $lex = new LexFile my $outfile;
49
25f0751f 50 my $comp = "$GZIP -dc" ;
51
25139ca2 52 if ( system("$comp $file >$outfile") == 0 )
53 {
54 $_[0] = readFile($outfile);
55 return 1
56 }
25f0751f 57
25139ca2 58 diag "'$comp' failed: $?";
59 return 0 ;
25f0751f 60}
61
62sub getGzipInfo
63{
64 my $file = shift ;
65}
66
67sub writeWithGzip
68{
69 my $file = shift ;
70 my $content = shift ;
71 my $options = shift || '';
72
cb7abd7f 73 my $lex = new LexFile my $infile;
74 writeFile($infile, $content);
75
25f0751f 76 unlink $file ;
25139ca2 77 my $comp = "$GZIP -c $options $infile >$file" ;
25f0751f 78
25139ca2 79 return 1
80 if system($comp) == 0 ;
81
82 diag "'$comp' failed: $?";
83 return 0 ;
84}
85
86BEGIN {
87
88 # Check external gzip is available
4e7676c7 89 my $name = $^O =~ /mswin/i ? 'gzip.exe' : 'gzip';
90 my $split = $^O =~ /mswin/i ? ";" : ":";
91
92 for my $dir (reverse split $split, $ENV{PATH})
25139ca2 93 {
94 $GZIP = "$dir/$name"
95 if -x "$dir/$name" ;
96 }
97
98 plan(skip_all => "Cannot find $name")
99 if ! $GZIP ;
100
101 plan(skip_all => "$name doesn't work as expected")
102 if ! ExternalGzipWorks();
103
104
105 # use Test::NoWarnings, if available
106 my $extra = 0 ;
107 $extra = 1
108 if eval { require Test::NoWarnings ; import Test::NoWarnings; 1 };
109
110 plan tests => 7 + $extra ;
111
112 use_ok('IO::Compress::Gzip', ':all') ;
113 use_ok('IO::Uncompress::Gunzip', ':all') ;
25f0751f 114
25f0751f 115}
116
117
118{
119 title "Test interop with $GZIP" ;
120
121 my $file;
122 my $file1;
123 my $lex = new LexFile $file, $file1;
44033ed4 124 my $content = qq {
125Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut tempus odio id
126 dolor. Camelus perlus. Larrius in lumen numen. Dolor en quiquum filia
127 est. Quintus cenum parat.
128};
25f0751f 129 my $got;
130
25139ca2 131 ok writeWithGzip($file, $content), "writeWithGzip ok";
25f0751f 132
133 gunzip $file => \$got ;
cb7abd7f 134 is $got, $content, "got content";
25f0751f 135
136
137 gzip \$content => $file1;
138 $got = '';
25139ca2 139 ok readWithGzip($file1, $got), "readWithGzip ok";
25f0751f 140 is $got, $content, "got content";
141}
142
143