assorted VMS test fix-ups, $Config{prefixexp} revisited
[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 $mandirs =  !!$Config{man1direxp} + !!$Config{man3direxp};
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( man1dir man3dir )) {
44 SKIP: {
45         my $dir = $Config{$path.'exp'};
46         skip("no man directory $path on this system", 2 ) unless $dir;
47
48         my $file = $dir . '/foo';
49         is( $ei->_is_type($file, 'doc'), 1, "... should find doc file in $path" );
50         is( $ei->_is_type($file, 'prog'), 0, "... but not prog file in $path" );
51     }
52 }
53
54 is( $ei->_is_type($Config{prefixexp} . '/bar', 'prog'), 1, 
55         "... should find prog file under $Config{prefixexp}" );
56
57 SKIP: {
58         skip('no man directories on this system', 1) unless $mandirs;
59         is( $ei->_is_type('bar', 'doc'), 0, 
60                 '... should not find doc file outside path' );
61 }
62
63 is( $ei->_is_type('bar', 'prog'), 0, 
64         '... nor prog file outside path' );
65 is( $ei->_is_type('whocares', 'someother'), 0, '... nor other type anywhere' );
66
67 # _is_under
68 ok( $ei->_is_under('foo'), '_is_under() should return true with no dirs' );
69
70 my @under = qw( boo bar baz );
71 is( $ei->_is_under('foo', @under), 0, '... should find no file not under dirs');
72 is( $ei->_is_under('baz', @under), 1, '... should find file under dir' );
73
74 # new
75 my $realei;
76 {
77     # We're going to get warnings about not being able to find install
78     # directories if we're not installed.
79     local $SIG{__WARN__} = sub {
80         warn @_ unless $ENV{PERL_CORE} && $_[0] =~ /^Can't stat/;
81     };
82     $realei = ExtUtils::Installed->new();
83 }
84
85 isa_ok( $realei, 'ExtUtils::Installed' );
86 isa_ok( $realei->{Perl}{packlist}, 'ExtUtils::Packlist' );
87 is( $realei->{Perl}{version}, $Config{version}, 
88         'new() should set Perl version from %Config' );
89
90 my $wrotelist;
91 if (mkpath('auto/FakeMod')) {
92         if (open(PACKLIST, '>', 'auto/FakeMod/.packlist')) {
93                 print PACKLIST 'list';
94                 close PACKLIST;
95                 if (open(FAKEMOD, '>', 'auto/FakeMod/FakeMod.pm')) {
96                         print FAKEMOD <<'FAKE';
97 package FakeMod;
98 use vars qw( $VERSION );
99 $VERSION = '1.1.1';
100 1;
101 FAKE
102
103                         close FAKEMOD;
104                         $wrotelist = 1;
105                 }
106         }
107 }
108
109
110 SKIP: {
111     TODO: {
112         skip("could not write packlist: $!", 3 ) unless $wrotelist;
113
114        local $TODO = "new() attempts to derive package name from filename"
115            if $^O eq 'VMS';
116
117         # avoid warning and death by localizing glob
118         local *ExtUtils::Installed::Config;
119         my $fake_mod_dir = File::Spec->catdir(cwd(), 'auto', 'FakeMod');
120         %ExtUtils::Installed::Config = (
121                 archlibexp         => cwd(),
122                 sitearchexp        => $fake_mod_dir,
123         );
124
125         # necessary to fool new()
126         push @INC, $fake_mod_dir;
127
128         my $realei = ExtUtils::Installed->new();
129         ok( exists $realei->{FakeMod}, 'new() should find modules with .packlists');
130         isa_ok( $realei->{FakeMod}{packlist}, 'ExtUtils::Packlist' );
131         is( $realei->{FakeMod}{version}, '1.1.1', 
132                 '... should find version in modules' );
133     }
134 }
135
136 # modules
137 $ei->{$_} = 1 for qw( abc def ghi );
138 is( join(' ', $ei->modules()), 'abc def ghi', 
139         'modules() should return sorted keys' );
140
141 # files
142 $ei->{goodmod} = { 
143         packlist => { 
144                 ($Config{man1direxp} ? 
145                     (File::Spec->catdir($Config{man1direxp}, 'foo') => 1) : 
146                         ()),
147                 ($Config{man3direxp} ? 
148                     (File::Spec->catdir($Config{man3direxp}, 'bar') => 1) : 
149                         ()),
150                 File::Spec->catdir($Config{prefixexp}, 'foobar') => 1,
151                 foobaz  => 1,
152         },
153 };
154
155 eval { $ei->files('badmod') };
156 like( $@, qr/badmod is not installed/,'files() should croak given bad modname');
157 eval { $ei->files('goodmod', 'badtype' ) };
158 like( $@, qr/type must be/,'files() should croak given bad type' );
159
160 my @files;
161 SKIP: {
162     skip('no man directory man1dir on this system', 2) unless $Config{man1direxp}; 
163     @files = $ei->files('goodmod', 'doc', $Config{man1direxp});
164     is( scalar @files, 1, '... should find doc file under given dir' );
165     is( grep({ /foo$/ } @files), 1, '... checking file name' );
166 }
167 SKIP: {
168     skip('no man directories on this system', 1) unless $mandirs;
169     @files = $ei->files('goodmod', 'doc');
170     is( scalar @files, $mandirs, '... should find all doc files with no dir' );
171 }
172
173 @files = $ei->files('goodmod', 'prog', 'fake', 'fake2');
174 is( scalar @files, 0, '... should find no doc files given wrong dirs' );
175 @files = $ei->files('goodmod', 'prog');
176 is( scalar @files, 1, '... should find doc file in correct dir' );
177 like( $files[0], qr/foobar[>\]]?$/, '... checking file name' );
178 @files = $ei->files('goodmod');
179 is( scalar @files, 2 + $mandirs, '... should find all files with no type specified' );
180 my %dirnames = map { lc($_) => dirname($_) } @files;
181
182 # directories
183 my @dirs = $ei->directories('goodmod', 'prog', 'fake');
184 is( scalar @dirs, 0, 'directories() should return no dirs if no files found' );
185
186 SKIP: {
187     skip('no man directories on this system', 1) unless $mandirs;
188     @dirs = $ei->directories('goodmod', 'doc');
189     is( scalar @dirs, $mandirs, '... should find all files files() would' );
190 }
191 @dirs = $ei->directories('goodmod');
192 is( scalar @dirs, 2 + $mandirs, '... should find all files files() would, again' );
193 @files = sort map { exists $dirnames{lc($_)} ? $dirnames{lc($_)} : '' } @files;
194 is( join(' ', @files), join(' ', @dirs), '... should sort output' );
195
196 # directory_tree
197 my $expectdirs = 
198        ($mandirs == 2) && 
199        (dirname($Config{man1direxp}) eq dirname($Config{man3direxp}))
200        ? 3 : 2;
201  
202 SKIP: {
203     skip('no man directories on this system', 1) unless $mandirs;
204     @dirs = $ei->directory_tree('goodmod', 'doc', $Config{man1direxp} ?
205        dirname($Config{man1direxp}) : dirname($Config{man3direxp}));
206     is( scalar @dirs, $expectdirs, 
207         'directory_tree() should report intermediate dirs to those requested' );
208 }
209
210 my $fakepak = Fakepak->new(102);
211
212 $ei->{yesmod} = { 
213         version         => 101,
214         packlist        => $fakepak,
215 };
216
217 # these should all croak
218 foreach my $sub (qw( validate packlist version )) {
219         eval { $ei->$sub('nomod') };
220         like( $@, qr/nomod is not installed/, 
221                 "$sub() should croak when asked about uninstalled module" );
222 }
223
224 # validate
225 is( $ei->validate('yesmod'), 'validated', 
226         'validate() should return results of packlist validate() call' );
227
228 # packlist
229 is( ${ $ei->packlist('yesmod') }, 102, 
230         'packlist() should report installed mod packlist' );
231
232 # version
233 is( $ei->version('yesmod'), 101, 
234         'version() should report installed mod version' );
235
236 # needs a DESTROY, for some reason
237 can_ok( $ei, 'DESTROY' );
238
239 END {
240         if ($wrotelist) {
241                 for my $file (qw( .packlist FakePak.pm )) {
242                         1 while unlink $file;
243                 }
244                 File::Path::rmtree('auto') or warn "Couldn't rmtree auto: $!";
245         }
246 }
247
248 package Fakepak;
249
250 sub new {
251         my $class = shift;
252         bless(\(my $scalar = shift), $class);
253 }
254
255 sub validate {
256         'validated'
257 }