This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update ExtUtils-Install to CPAN version 2.14
[perl5.git] / cpan / ExtUtils-Install / t / Packlist.t
CommitLineData
39234879 1#!/usr/bin/perl -w
b0948e64 2
3BEGIN {
fb78ba4b 4 unshift @INC, 't/lib';
b0948e64 5}
6
ab7e0964 7use Test::More tests => 35;
b0948e64 8
ab7e0964 9BEGIN { use_ok( 'ExtUtils::Packlist' ); }
b0948e64 10
11is( ref(ExtUtils::Packlist::mkfh()), 'GLOB', 'mkfh() should return a FH' );
12
13# new calls tie()
14my $pl = ExtUtils::Packlist->new();
15isa_ok( $pl, 'ExtUtils::Packlist' );
16is( ref tied %$pl, 'ExtUtils::Packlist', 'obj should be tied underneath' );
17
18
19$pl = ExtUtils::Packlist::TIEHASH( 'tieclass', 'packfile' );
20is( ref($pl), 'tieclass', 'TIEHASH() should bless into class' );
21is( $pl->{packfile}, 'packfile', 'TIEHASH() should store packfile name' );
22
23
24ExtUtils::Packlist::STORE($pl, 'key', 'value');
25is( $pl->{data}{key}, 'value', 'STORE() should stuff stuff in data member' );
26
27
28$pl->{data}{foo} = 'bar';
29is( ExtUtils::Packlist::FETCH($pl, 'foo'), 'bar', 'check FETCH()' );
30
31
32# test FIRSTKEY and NEXTKEY
33SKIP: {
34 $pl->{data}{bar} = 'baz';
3a465856 35 skip('not enough keys to test FIRSTKEY', 2)
39234879 36 unless keys %{ $pl->{data} } > 2;
b0948e64 37
38 # get the first and second key
39 my ($first, $second) = keys %{ $pl->{data} };
40
41 # now get a couple of extra keys, to mess with the hash iterator
42 my $i = 0;
43 for (keys %{ $pl->{data} } ) {
44 last if $i++;
45 }
3a465856 46
b0948e64 47 # finally, see if it really can get the first key again
3a465856 48 is( ExtUtils::Packlist::FIRSTKEY($pl), $first,
b0948e64 49 'FIRSTKEY() should be consistent' );
50
51 is( ExtUtils::Packlist::NEXTKEY($pl), $second,
52 'and NEXTKEY() should also be consistent' );
53}
54
55
56ok( ExtUtils::Packlist::EXISTS($pl, 'bar'), 'EXISTS() should find keys' );
57
58
59ExtUtils::Packlist::DELETE($pl, 'bar');
60ok( !(exists $pl->{data}{bar}), 'DELETE() should delete cleanly' );
61
62
63ExtUtils::Packlist::CLEAR($pl);
64is( keys %{ $pl->{data} }, 0, 'CLEAR() should wipe out data' );
65
66
67# DESTROY does nothing...
68can_ok( 'ExtUtils::Packlist', 'DESTROY' );
69
70
71# write is a little more complicated
72eval { ExtUtils::Packlist::write({}) };
73like( $@, qr/No packlist filename/, 'write() should croak without packfile' );
74
75eval { ExtUtils::Packlist::write({}, 'eplist') };
76my $file_is_ready = $@ ? 0 : 1;
77ok( $file_is_ready, 'write() can write a file' );
78
79local *IN;
80
81SKIP: {
82 skip('cannot write files, some tests difficult', 3) unless $file_is_ready;
83
84 # set this file to read-only
85 chmod 0444, 'eplist';
86
e1eb1c15 87 SKIP: {
57b1a898 88 skip("cannot write readonly files", 1) if -w 'eplist';
e1eb1c15
JH
89
90 eval { ExtUtils::Packlist::write({}, 'eplist') };
91 like( $@, qr/Can't open file/, 'write() should croak on open failure' );
92 }
b0948e64 93
94 #'now set it back (tick here fixes vim syntax highlighting ;)
95 chmod 0777, 'eplist';
96
97 # and some test data to be read
98 $pl->{data} = {
99 single => 1,
100 hash => {
101 foo => 'bar',
102 baz => 'bup',
103 },
104 '/./abc' => '',
105 };
106 eval { ExtUtils::Packlist::write($pl, 'eplist') };
107 is( $@, '', 'write() should normally succeed' );
108 is( $pl->{packfile}, 'eplist', 'write() should set packfile name' );
109
110 $file_is_ready = open(IN, 'eplist');
111}
112
113
114eval { ExtUtils::Packlist::read({}) };
115like( $@, qr/^No packlist filename/, 'read() should croak without packfile' );
116
117
118eval { ExtUtils::Packlist::read({}, 'abadfilename') };
119like( $@, qr/^Can't open file/, 'read() should croak with bad packfile name' );
120#'open packfile for reading
121
122
123# and more read() tests
124SKIP: {
125 skip("cannot open file for reading: $!", 5) unless $file_is_ready;
126 my $file = do { local $/ = <IN> };
127
128 like( $file, qr/single\n/, 'key with value should be available' );
129 like( $file, qr!/\./abc\n!, 'key with no value should also be present' );
130 like( $file, qr/hash.+baz=bup/, 'key with hash value should be present' );
131 like( $file, qr/hash.+foo=bar/, 'second embedded hash value should appear');
132 close IN;
133
134 eval{ ExtUtils::Packlist::read($pl, 'eplist') };
135 is( $@, '', 'read() should normally succeed' );
136 is( $pl->{data}{single}, undef, 'single keys should have undef value' );
137 is( ref($pl->{data}{hash}), 'HASH', 'multivalue keys should become hashes');
138
139 is( $pl->{data}{hash}{foo}, 'bar', 'hash values should be set' );
140 ok( exists $pl->{data}{'/abc'}, 'read() should resolve /./ to / in keys' );
141
142 # give validate a valid and an invalid file to find
143 $pl->{data} = {
144 eplist => 1,
145 fake => undef,
146 };
147
148 is( ExtUtils::Packlist::validate($pl), 1,
149 'validate() should find missing files' );
150 ExtUtils::Packlist::validate($pl, 1);
3a465856 151 ok( !exists $pl->{data}{fake},
b0948e64 152 'validate() should remove missing files when prompted' );
3a465856 153
b0948e64 154 # one more new() test, to see if it calls read() successfully
155 $pl = ExtUtils::Packlist->new('eplist');
156}
157
158
159# packlist_file, $pl should be set from write test
160is( ExtUtils::Packlist::packlist_file({ packfile => 'pl' }), 'pl',
161 'packlist_file() should fetch packlist from passed hash' );
162is( ExtUtils::Packlist::packlist_file($pl), 'eplist',
163 'packlist_file() should fetch packlist from ExtUtils::Packlist object' );
164
ab7e0964
FC
165BEGIN {
166 # Call mkfh at BEGIN time, to make sure it does not trigger "Used
167 # once" warnings.
168 $SIG{__WARN__} = sub { ++$w; warn $_[0] };
169 ExtUtils::Packlist::mkfh();
170
171}
172INIT {
23368773 173 is $w, undef, '[perl #107410] no warnings from BEGIN-time mkfh';
ab7e0964
FC
174 delete $SIG{__WARN__};
175}
176
b0948e64 177END {
178 1 while unlink qw( eplist );
179}