Forgot from #14641.
[p5sagit/p5-mst-13.2.git] / lib / ExtUtils / t / Installed.t
1 #!/usr/bin/perl -w
2
3 BEGIN {
4     if( $ENV{PERL_CORE} ) {
5         chdir 't' if -d 't';
6         @INC = '../lib';
7     }
8 }
9 chdir 't';
10
11
12 use strict;
13 use warnings;
14
15 # for _is_type() tests
16 use Config;
17
18 # for new() tests
19 use Cwd;
20 use File::Path;
21
22 # for directories() tests
23 use File::Basename;
24
25 use Test::More tests => 43;
26
27 BEGIN { use_ok( 'ExtUtils::Installed' ) }
28
29 my $noman = ! ($Config{installman1dir} && $Config{installman3dir});
30
31 # saves having to qualify package name for class methods
32 my $ei = bless( {}, 'ExtUtils::Installed' );
33
34 # _is_prefix
35 is( $ei->_is_prefix('foo/bar', 'foo'), 1, 
36         '_is_prefix() should match valid path prefix' );
37 is( $ei->_is_prefix('\foo\bar', '\bar'), 0, 
38         '... should not match wrong prefix' );
39
40 # _is_type
41 is( $ei->_is_type(0, 'all'), 1, '_is_type() should be true for type of "all"' );
42
43 foreach my $path (qw( installman1dir installman3dir )) {
44         my $file = $Config{$path} . '/foo';
45         is( $ei->_is_type($file, 'doc'), 1, "... should find doc file in $path" );
46         is( $ei->_is_type($file, 'prog'), 0, "... but not prog file in $path" );
47 }
48
49 is( $ei->_is_type($Config{prefix} . '/bar', 'prog'), 1, 
50         "... should find prog file under $Config{prefix}" );
51
52 SKIP: {
53         skip('no man directories on this system', 1) if $noman;
54         is( $ei->_is_type('bar', 'doc'), 0, 
55                 '... should not find doc file outside path' );
56 }
57
58 is( $ei->_is_type('bar', 'prog'), 0, 
59         '... nor prog file outside path' );
60 is( $ei->_is_type('whocares', 'someother'), 0, '... nor other type anywhere' );
61
62 # _is_under
63 ok( $ei->_is_under('foo'), '_is_under() should return true with no dirs' );
64
65 my @under = qw( boo bar baz );
66 is( $ei->_is_under('foo', @under), 0, '... should find no file not under dirs');
67 is( $ei->_is_under('baz', @under), 1, '... should find file under dir' );
68
69 # new
70 my $realei;
71 {
72     # We're going to get warnings about not being able to find install
73     # directories if we're not installed.
74     local $SIG{__WARN__} = sub {
75         warn @_ unless $ENV{PERL_CORE} && $_[0] =~ /^Can't stat/;
76     };
77     $realei = ExtUtils::Installed->new();
78 }
79
80 isa_ok( $realei, 'ExtUtils::Installed' );
81 isa_ok( $realei->{Perl}{packlist}, 'ExtUtils::Packlist' );
82 is( $realei->{Perl}{version}, $Config{version}, 
83         'new() should set Perl version from %Config' );
84
85 my $wrotelist;
86 if (mkpath('auto/FakeMod')) {
87         if (open(PACKLIST, '>', 'auto/FakeMod/.packlist')) {
88                 print PACKLIST 'list';
89                 close PACKLIST;
90                 if (open(FAKEMOD, '>', 'auto/FakeMod/FakeMod.pm')) {
91                         print FAKEMOD <<'FAKE';
92 package FakeMod;
93 use vars qw( $VERSION );
94 $VERSION = '1.1.1';
95 1;
96 FAKE
97
98                         close FAKEMOD;
99                         $wrotelist = 1;
100                 }
101         }
102 }
103
104
105 SKIP: {
106         skip( "could not write packlist: $!", 3 ) unless $wrotelist;
107
108         # avoid warning and death by localizing glob
109         local *ExtUtils::Installed::Config;
110     my $fake_mod_dir = File::Spec->catdir(cwd(), 'auto', 'FakeMod');
111         %ExtUtils::Installed::Config = (
112                 archlib            => cwd(),
113         installarchlib => cwd(),
114                 sitearch           => $fake_mod_dir,
115         );
116
117         # necessary to fool new()
118         push @INC, $fake_mod_dir;
119
120         my $realei = ExtUtils::Installed->new();
121         ok( exists $realei->{FakeMod}, 'new() should find modules with .packlists');
122         isa_ok( $realei->{FakeMod}{packlist}, 'ExtUtils::Packlist' );
123         is( $realei->{FakeMod}{version}, '1.1.1', 
124                 '... should find version in modules' );
125 }
126
127 # modules
128 $ei->{$_} = 1 for qw( abc def ghi );
129 is( join(' ', $ei->modules()), 'abc def ghi', 
130         'modules() should return sorted keys' );
131
132 # files
133 $ei->{goodmod} = { 
134         packlist => { 
135                 File::Spec->catdir($Config{installman1dir}, 'foo') => 1,
136                 File::Spec->catdir($Config{installman3dir}, 'bar') => 1,
137                 File::Spec->catdir($Config{prefix}, 'foobar') => 1,
138                 foobaz  => 1,
139         },
140 };
141
142 eval { $ei->files('badmod') };
143 like( $@, qr/badmod is not installed/,'files() should croak given bad modname');
144 eval { $ei->files('goodmod', 'badtype' ) };
145 like( $@, qr/type must be/,'files() should croak given bad type' );
146
147 my @files;
148 SKIP: {
149         skip('no man directories on this system', 3) if $noman;
150         
151         @files = $ei->files('goodmod', 'doc', $Config{installman1dir});
152         is( scalar @files, 1, '... should find doc file under given dir' );
153         is( grep({ /foo$/ } @files), 1, '... checking file name' );
154         @files = $ei->files('goodmod', 'doc');
155         is( scalar @files, 2, '... should find all doc files with no dir' );
156 }
157
158 @files = $ei->files('goodmod', 'prog', 'fake', 'fake2');
159 is( scalar @files, 0, '... should find no doc files given wrong dirs' );
160 @files = $ei->files('goodmod', 'prog');
161 is( scalar @files, 1, '... should find doc file in correct dir' );
162 like( $files[0], qr/foobar$/, '... checking file name' );
163 @files = $ei->files('goodmod');
164 is( scalar @files, 4, '... should find all files with no type specified' );
165 my %dirnames = map { lc($_) => dirname(lc($_)) } @files;
166
167 # directories
168 my @dirs = $ei->directories('goodmod', 'prog', 'fake');
169 is( scalar @dirs, 0, 'directories() should return no dirs if no files found' );
170
171 SKIP: {
172         skip('no man directories on this system', 4) if $noman;
173
174         @dirs = $ei->directories('goodmod', 'doc');
175         is( scalar @dirs, 2, '... should find all files files() would' );
176         @dirs = $ei->directories('goodmod');
177         is( scalar @dirs, 4, '... should find all files files() would, again' );
178         @files = sort map { exists $dirnames{lc($_)} ? $dirnames{lc($_)} : '' } 
179                 @files;
180         is( join(' ', @files), join(' ', @dirs), '... should sort output' );
181
182         # directory_tree
183         my $expectdirs = dirname($Config{installman1dir}) eq 
184                 dirname($Config{installman3dir}) ? 3 :2;
185
186         @dirs = $ei->directory_tree('goodmod', 'doc', 
187                 dirname($Config{installman1dir}));
188         is( scalar @dirs, $expectdirs, 
189                 'directory_tree() should report intermediate dirs to those requested' );
190 }
191
192 my $fakepak = Fakepak->new(102);
193
194 $ei->{yesmod} = { 
195         version         => 101,
196         packlist        => $fakepak,
197 };
198
199 # these should all croak
200 foreach my $sub (qw( validate packlist version )) {
201         eval { $ei->$sub('nomod') };
202         like( $@, qr/nomod is not installed/, 
203                 "$sub() should croak when asked about uninstalled module" );
204 }
205
206 # validate
207 is( $ei->validate('yesmod'), 'validated', 
208         'validate() should return results of packlist validate() call' );
209
210 # packlist
211 is( ${ $ei->packlist('yesmod') }, 102, 
212         'packlist() should report installed mod packlist' );
213
214 # version
215 is( $ei->version('yesmod'), 101, 
216         'version() should report installed mod version' );
217
218 # needs a DESTROY, for some reason
219 can_ok( $ei, 'DESTROY' );
220
221 END {
222         if ($wrotelist) {
223                 for my $file (qw( .packlist FakePak.pm )) {
224                         1 while unlink $file;
225                 }
226                 File::Path::rmtree('auto') or warn "Couldn't rmtree auto: $!";
227         }
228 }
229
230 package Fakepak;
231
232 sub new {
233         my $class = shift;
234         bless(\(my $scalar = shift), $class);
235 }
236
237 sub validate {
238         'validated'
239 }