xref: /openbsd-src/gnu/usr.bin/perl/cpan/IO-Zlib/t/external.t (revision f2a19305cfc49ea4d1a5feb55cd6c283c6f1e031)
1use strict;
2use warnings;
3
4use IO::Zlib;
5
6sub ok
7{
8    my ($no, $ok) = @_ ;
9    print "ok $no\n" if $ok ;
10    print "not ok $no\n" unless $ok ;
11}
12
13# Test this only iff we have an executable /usr/bin/gzip
14# AND we have /usr/bin in our PATH
15# AND we have a useable /usr/bin directory.
16# This limits the testing to UNIX-like
17# systems but that should be enough.
18
19my $gzip = "/usr/bin/gzip";
20
21unless (-x $gzip &&
22        ":$ENV{PATH}:" =~ m!:/usr/bin:! &&
23        -d "/usr/bin" && -x "/usr/bin")
24{
25    print "1..0 # Skip: no $gzip\n";
26    exit 0;
27}
28
29my $hasCompressZlib;
30
31BEGIN {
32    eval { require Compress::Zlib };
33    $hasCompressZlib = $@ ? 0 : 1;
34}
35
36print "1..35\n";
37
38ok(1, $hasCompressZlib == IO::Zlib::has_Compress_Zlib());
39
40eval "use IO::Zlib qw(:gzip_external)";
41
42ok(2, $@ =~ /^IO::Zlib::import: ':gzip_external' requires an argument /);
43
44eval "use IO::Zlib";
45
46ok(3, !$@);
47ok(4, $hasCompressZlib || IO::Zlib::gzip_used());
48ok(5, !defined IO::Zlib::gzip_external());
49ok(6, IO::Zlib::gzip_read_open() eq 'gzip -dc %s |');
50ok(7, IO::Zlib::gzip_write_open() eq '| gzip > %s');
51ok(8, \&IO::Zlib::gzopen == \&IO::Zlib::gzopen_external ||
52      ($hasCompressZlib && \&IO::Zlib::gzopen == \&Compress::Zlib::gzopen));
53
54eval "use IO::Zlib qw(:gzip_external 0)";
55
56ok(9, !IO::Zlib::gzip_external());
57ok(10, ($hasCompressZlib && \&IO::Zlib::gzopen == \&Compress::Zlib::gzopen) ||
58       (!$hasCompressZlib && $@ =~ /^IO::Zlib::import: no Compress::Zlib and no external gzip /));
59
60eval "use IO::Zlib qw(:gzip_external 1)";
61
62ok(11, IO::Zlib::gzip_used());
63ok(12, IO::Zlib::gzip_external());
64ok(13, \&IO::Zlib::gzopen == \&IO::Zlib::gzopen_external);
65
66eval 'IO::Zlib->new("foo", "xyz")';
67
68ok(14, $@ =~ /^IO::Zlib::gzopen_external: mode 'xyz' is illegal /);
69
70# The following is a copy of the basic.t, shifted up by 14 tests,
71# the difference being that now we should be using the external gzip.
72
73my $name="test_external_$$.gz";
74
75my $hello = <<EOM ;
76hello world
77this is a test
78EOM
79
80my $file;
81
82ok(15, $file = IO::Zlib->new($name, "wb"));
83ok(16, $file->print($hello));
84ok(17, $file->opened());
85ok(18, $file->close());
86ok(19, !$file->opened());
87
88my $uncomp;
89
90ok(20, $file = IO::Zlib->new());
91ok(21, $file->open($name, "rb"));
92ok(22, !$file->eof());
93ok(23, $file->read($uncomp, 1024) == length($hello));
94ok(24, $uncomp eq $hello);
95ok(25, $file->eof());
96ok(26, $file->opened());
97ok(27, $file->close());
98ok(28, !$file->opened());
99
100$file = IO::Zlib->new($name, "rb");
101ok(29, $file->read($uncomp, 1024, length($uncomp)) == length($hello));
102ok(30, $uncomp eq $hello . $hello);
103$file->close();
104
105unlink($name);
106
107ok(31, !defined(IO::Zlib->new($name, "rb")));
108
109# Then finally test modifying the open commands.
110
111my $new_read = 'gzip.exe /d /c %s |';
112
113eval "use IO::Zlib ':gzip_read_open' => '$new_read'";
114
115ok(32, IO::Zlib::gzip_read_open() eq $new_read);
116
117eval "use IO::Zlib ':gzip_read_open' => 'bad'";
118
119ok(33, $@ =~ /^IO::Zlib::import: ':gzip_read_open' 'bad' is illegal /);
120
121my $new_write = '| gzip.exe %s';
122
123eval "use IO::Zlib ':gzip_write_open' => '$new_write'";
124
125ok(34, IO::Zlib::gzip_write_open() eq $new_write);
126
127eval "use IO::Zlib ':gzip_write_open' => 'bad'";
128
129ok(35, $@ =~ /^IO::Zlib::import: ':gzip_write_open' 'bad' is illegal /);
130