Bump version numbers of moules affected by change #22258
[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     else {
9         unshift @INC, 't/lib/';
10     }
11 }
12
13 my $Is_VMS = $^O eq 'VMS';
14 chdir($Is_VMS ? 'BFD_TEST_ROOT:[t]' : 't');
15
16
17 use strict;
18
19 use Config;
20 use Cwd;
21 use File::Path;
22 use File::Basename;
23 use File::Spec;
24
25 use Test::More tests => 46;
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 ok( $ei->_is_prefix('foo/bar', 'foo'),
36         '_is_prefix() should match valid path prefix' );
37 ok( !$ei->_is_prefix('\foo\bar', '\bar'),
38         '... should not match wrong prefix' );
39
40 # _is_type
41 ok( $ei->_is_type(0, 'all'), '_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         ok( $ei->_is_type($file, 'doc'),   "... should find doc file in $path" );
50         ok( !$ei->_is_type($file, 'prog'), "... but not prog file in $path" );
51     }
52 }
53
54 # VMS 5.6.1 doesn't seem to have $Config{prefixexp}
55 my $prefix = $Config{prefix} || $Config{prefixexp};
56
57 # You can concatenate /foo but not foo:, which defaults in the current 
58 # directory
59 $prefix = VMS::Filespec::unixify($prefix) if $Is_VMS;
60
61 # ActivePerl 5.6.1/631 has $Config{prefixexp} as 'p:' for some reason
62 $prefix = $Config{prefix} if $prefix eq 'p:' && $^O eq 'MSWin32';
63
64 ok( $ei->_is_type( File::Spec->catfile($prefix, 'bar'), 'prog'),
65         "... should find prog file under $prefix" );
66
67 SKIP: {
68         skip('no man directories on this system', 1) unless $mandirs;
69         is( $ei->_is_type('bar', 'doc'), 0, 
70                 '... should not find doc file outside path' );
71 }
72
73 ok( !$ei->_is_type('bar', 'prog'),
74         '... nor prog file outside path' );
75 ok( !$ei->_is_type('whocares', 'someother'), '... nor other type anywhere' );
76
77 # _is_under
78 ok( $ei->_is_under('foo'), '_is_under() should return true with no dirs' );
79
80 my @under = qw( boo bar baz );
81 ok( !$ei->_is_under('foo', @under), '... should find no file not under dirs');
82 ok( $ei->_is_under('baz', @under),  '... should find file under dir' );
83
84
85 rmtree 'auto/FakeMod';
86 ok( mkpath('auto/FakeMod') );
87 END { rmtree 'auto' }
88
89 ok(open(PACKLIST, '>auto/FakeMod/.packlist'));
90 print PACKLIST 'list';
91 close PACKLIST;
92
93 ok(open(FAKEMOD, '>auto/FakeMod/FakeMod.pm'));
94
95 print FAKEMOD <<'FAKE';
96 package FakeMod;
97 use vars qw( $VERSION );
98 $VERSION = '1.1.1';
99 1;
100 FAKE
101
102 close FAKEMOD;
103
104 {
105     # avoid warning and death by localizing glob
106     local *ExtUtils::Installed::Config;
107     my $fake_mod_dir = File::Spec->catdir(cwd(), 'auto', 'FakeMod');
108     %ExtUtils::Installed::Config = (
109         %Config,
110         archlibexp         => cwd(),
111         sitearchexp        => $fake_mod_dir,
112     );
113
114         # necessary to fool new()
115         push @INC, $fake_mod_dir;
116
117         my $realei = ExtUtils::Installed->new();
118     isa_ok( $realei, 'ExtUtils::Installed' );
119     isa_ok( $realei->{Perl}{packlist}, 'ExtUtils::Packlist' );
120     is( $realei->{Perl}{version}, $Config{version}, 
121         'new() should set Perl version from %Config' );
122
123         ok( exists $realei->{FakeMod}, 'new() should find modules with .packlists');
124         isa_ok( $realei->{FakeMod}{packlist}, 'ExtUtils::Packlist' );
125         is( $realei->{FakeMod}{version}, '1.1.1', 
126                 '... should find version in modules' );
127 }
128
129 # modules
130 $ei->{$_} = 1 for qw( abc def ghi );
131 is( join(' ', $ei->modules()), 'abc def ghi', 
132         'modules() should return sorted keys' );
133
134 # This didn't work for a long time due to a sort in scalar context oddity.
135 is( $ei->modules, 3,    'modules() in scalar context' );
136
137 # files
138 $ei->{goodmod} = { 
139         packlist => { 
140                 ($Config{man1direxp} ? 
141                     (File::Spec->catdir($Config{man1direxp}, 'foo') => 1) : 
142                         ()),
143                 ($Config{man3direxp} ? 
144                     (File::Spec->catdir($Config{man3direxp}, 'bar') => 1) : 
145                         ()),
146                 File::Spec->catdir($prefix, 'foobar') => 1,
147                 foobaz  => 1,
148         },
149 };
150
151 eval { $ei->files('badmod') };
152 like( $@, qr/badmod is not installed/,'files() should croak given bad modname');
153 eval { $ei->files('goodmod', 'badtype' ) };
154 like( $@, qr/type must be/,'files() should croak given bad type' );
155
156 my @files;
157 SKIP: {
158     skip('no man directory man1dir on this system', 2) 
159       unless $Config{man1direxp}; 
160     @files = $ei->files('goodmod', 'doc', $Config{man1direxp});
161     is( scalar @files, 1, '... should find doc file under given dir' );
162     is( (grep { /foo$/ } @files), 1, '... checking file name' );
163 }
164 SKIP: {
165     skip('no man directories on this system', 1) unless $mandirs;
166     @files = $ei->files('goodmod', 'doc');
167     is( scalar @files, $mandirs, '... should find all doc files with no dir' );
168 }
169
170 @files = $ei->files('goodmod', 'prog', 'fake', 'fake2');
171 is( scalar @files, 0, '... should find no doc files given wrong dirs' );
172 @files = $ei->files('goodmod', 'prog');
173 is( scalar @files, 1, '... should find doc file in correct dir' );
174 like( $files[0], qr/foobar[>\]]?$/, '... checking file name' );
175 @files = $ei->files('goodmod');
176 is( scalar @files, 2 + $mandirs, '... should find all files with no type specified' );
177 my %dirnames = map { lc($_) => dirname($_) } @files;
178
179 # directories
180 my @dirs = $ei->directories('goodmod', 'prog', 'fake');
181 is( scalar @dirs, 0, 'directories() should return no dirs if no files found' );
182
183 SKIP: {
184     skip('no man directories on this system', 1) unless $mandirs;
185     @dirs = $ei->directories('goodmod', 'doc');
186     is( scalar @dirs, $mandirs, '... should find all files files() would' );
187 }
188 @dirs = $ei->directories('goodmod');
189 is( scalar @dirs, 2 + $mandirs, '... should find all files files() would, again' );
190 @files = sort map { exists $dirnames{lc($_)} ? $dirnames{lc($_)} : '' } @files;
191 is( join(' ', @files), join(' ', @dirs), '... should sort output' );
192
193 # directory_tree
194 my $expectdirs = 
195        ($mandirs == 2) && 
196        (dirname($Config{man1direxp}) eq dirname($Config{man3direxp}))
197        ? 3 : 2;
198  
199 SKIP: {
200     skip('no man directories on this system', 1) unless $mandirs;
201     @dirs = $ei->directory_tree('goodmod', 'doc', $Config{man1direxp} ?
202        dirname($Config{man1direxp}) : dirname($Config{man3direxp}));
203     is( scalar @dirs, $expectdirs, 
204         'directory_tree() should report intermediate dirs to those requested' );
205 }
206
207 my $fakepak = Fakepak->new(102);
208
209 $ei->{yesmod} = { 
210         version         => 101,
211         packlist        => $fakepak,
212 };
213
214 # these should all croak
215 foreach my $sub (qw( validate packlist version )) {
216         eval { $ei->$sub('nomod') };
217         like( $@, qr/nomod is not installed/, 
218                 "$sub() should croak when asked about uninstalled module" );
219 }
220
221 # validate
222 is( $ei->validate('yesmod'), 'validated', 
223         'validate() should return results of packlist validate() call' );
224
225 # packlist
226 is( ${ $ei->packlist('yesmod') }, 102, 
227         'packlist() should report installed mod packlist' );
228
229 # version
230 is( $ei->version('yesmod'), 101, 
231         'version() should report installed mod version' );
232
233
234 package Fakepak;
235
236 sub new {
237         my $class = shift;
238         bless(\(my $scalar = shift), $class);
239 }
240
241 sub validate {
242         'validated'
243 }