Cannot portably split on $Config{path_sep} -- use quotemeta($Config{path_sep}).
[p5sagit/p5-mst-13.2.git] / dist / ExtUtils-Install / t / Installed.t
1 #!/usr/bin/perl -w
2
3 BEGIN {
4     unshift @INC, 't/lib/';
5 }
6
7 my $Is_VMS = $^O eq 'VMS';
8
9 use strict;
10
11 use Config;
12 use Cwd;
13 use File::Path;
14 use File::Basename;
15 use File::Spec;
16
17 use Test::More tests => 63;
18
19 BEGIN { use_ok( 'ExtUtils::Installed' ) }
20
21 my $mandirs =  !!$Config{man1direxp} + !!$Config{man3direxp};
22
23 # saves having to qualify package name for class methods
24 my $ei = bless( {}, 'ExtUtils::Installed' );
25
26 # Make sure meta info is available
27 $ei->{':private:'}{Config} = \%Config;
28 $ei->{':private:'}{INC} = \@INC;
29
30 # _is_prefix
31 ok( $ei->_is_prefix('foo/bar', 'foo'),
32         '_is_prefix() should match valid path prefix' );
33 ok( !$ei->_is_prefix('\foo\bar', '\bar'),
34         '... should not match wrong prefix' );
35
36 # _is_type
37 ok( $ei->_is_type(0, 'all'), '_is_type() should be true for type of "all"' );
38
39 foreach my $path (qw( man1dir man3dir )) {
40     SKIP: {
41         my $dir = File::Spec->canonpath($Config{$path.'exp'});
42         skip("no man directory $path on this system", 2 ) unless $dir;
43
44         my $file = $dir . '/foo';
45         ok( $ei->_is_type($file, 'doc'),   "... should find doc file in $path" );
46         ok( !$ei->_is_type($file, 'prog'), "... but not prog file in $path" );
47     }
48 }
49
50 # VMS 5.6.1 doesn't seem to have $Config{prefixexp}
51 my $prefix = $Config{prefix} || $Config{prefixexp};
52
53 # You can concatenate /foo but not foo:, which defaults in the current
54 # directory
55 $prefix = VMS::Filespec::unixify($prefix) if $Is_VMS;
56
57 # ActivePerl 5.6.1/631 has $Config{prefixexp} as 'p:' for some reason
58 $prefix = $Config{prefix} if $prefix eq 'p:' && $^O eq 'MSWin32';
59
60 ok( $ei->_is_type( File::Spec->catfile($prefix, 'bar'), 'prog'),
61         "... should find prog file under $prefix" );
62
63 SKIP: {
64     skip('no man directories on this system', 1) unless $mandirs;
65     is( $ei->_is_type('bar', 'doc'), 0,
66         '... should not find doc file outside path' );
67 }
68
69 ok( !$ei->_is_type('bar', 'prog'),
70         '... nor prog file outside path' );
71 ok( !$ei->_is_type('whocares', 'someother'), '... nor other type anywhere' );
72
73 # _is_under
74 ok( $ei->_is_under('foo'), '_is_under() should return true with no dirs' );
75
76 my @under = qw( boo bar baz );
77 ok( !$ei->_is_under('foo', @under), '... should find no file not under dirs');
78 ok( $ei->_is_under('baz', @under),  '... should find file under dir' );
79
80
81 rmtree 'auto/FakeMod';
82 ok( mkpath('auto/FakeMod') );
83 END { rmtree 'auto' }
84
85 ok(open(PACKLIST, '>auto/FakeMod/.packlist'));
86 print PACKLIST 'list';
87 close PACKLIST;
88
89 ok(open(FAKEMOD, '>auto/FakeMod/FakeMod.pm'));
90
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
100 my $fake_mod_dir = File::Spec->catdir(cwd(), 'auto', 'FakeMod');
101 {
102     # avoid warning and death by localizing glob
103     local *ExtUtils::Installed::Config;
104     %ExtUtils::Installed::Config = (
105         %Config,
106         archlibexp         => cwd(),
107         sitearchexp        => $fake_mod_dir,
108     );
109
110     # necessary to fool new()
111     push @INC, $fake_mod_dir;
112
113     my $realei = ExtUtils::Installed->new();
114     isa_ok( $realei, 'ExtUtils::Installed' );
115     isa_ok( $realei->{Perl}{packlist}, 'ExtUtils::Packlist' );
116     is( $realei->{Perl}{version}, $Config{version},
117         'new() should set Perl version from %Config' );
118
119     ok( exists $realei->{FakeMod}, 'new() should find modules with .packlists');
120     isa_ok( $realei->{FakeMod}{packlist}, 'ExtUtils::Packlist' );
121     is( $realei->{FakeMod}{version}, '1.1.1',
122         '... should find version in modules' );
123 }
124
125 # Now try this using PERL5LIB
126 {
127     local $ENV{PERL5LIB} = join $Config{path_sep}, $fake_mod_dir;
128     local *ExtUtils::Installed::Config;
129     %ExtUtils::Installed::Config = (
130         %Config,
131         archlibexp         => cwd(),
132         sitearchexp        => cwd(),
133     );
134
135     my $realei = ExtUtils::Installed->new();
136     isa_ok( $realei, 'ExtUtils::Installed' );
137     isa_ok( $realei->{Perl}{packlist}, 'ExtUtils::Packlist' );
138     is( $realei->{Perl}{version}, $Config{version},
139         'new() should set Perl version from %Config' );
140
141     ok( exists $realei->{FakeMod},
142         'new() should find modules with .packlists using PERL5LIB'
143     );
144     isa_ok( $realei->{FakeMod}{packlist}, 'ExtUtils::Packlist' );
145     is( $realei->{FakeMod}{version}, '1.1.1',
146         '... should find version in modules' );
147 }
148
149 # Do the same thing as the last block, but with overrides for
150 # %Config and @INC.
151 {
152     my $config_override = { %Config::Config };
153     $config_override->{archlibexp} = cwd();
154     $config_override->{sitearchexp} = $fake_mod_dir;
155     $config_override->{version} = 'fake_test_version';
156
157     my @inc_override = (@INC, $fake_mod_dir);
158
159     my $realei = ExtUtils::Installed->new(
160         'config_override' => $config_override,
161         'inc_override' => \@inc_override,
162     );
163     isa_ok( $realei, 'ExtUtils::Installed' );
164     isa_ok( $realei->{Perl}{packlist}, 'ExtUtils::Packlist' );
165     is( $realei->{Perl}{version}, 'fake_test_version',
166         'new(config_override => HASH) overrides %Config' );
167
168     ok( exists $realei->{FakeMod}, 'new() with overrides should find modules with .packlists');
169     isa_ok( $realei->{FakeMod}{packlist}, 'ExtUtils::Packlist' );
170     is( $realei->{FakeMod}{version}, '1.1.1',
171         '... should find version in modules' );
172 }
173
174 # Check if extra_libs works.
175 {
176     my $realei = ExtUtils::Installed->new(
177         'extra_libs' => [ cwd() ],
178     );
179     isa_ok( $realei, 'ExtUtils::Installed' );
180     isa_ok( $realei->{Perl}{packlist}, 'ExtUtils::Packlist' );
181     ok( exists $realei->{FakeMod}, 
182         'new() with extra_libs should find modules with .packlists');
183     
184     #{ use Data::Dumper; local $realei->{':private:'}{Config};
185     #  warn Dumper($realei); }
186     
187     isa_ok( $realei->{FakeMod}{packlist}, 'ExtUtils::Packlist' );
188     is( $realei->{FakeMod}{version}, '1.1.1',
189         '... should find version in modules' );
190 }
191
192 # modules
193 $ei->{$_} = 1 for qw( abc def ghi );
194 is( join(' ', $ei->modules()), 'abc def ghi',
195     'modules() should return sorted keys' );
196
197 # This didn't work for a long time due to a sort in scalar context oddity.
198 is( $ei->modules, 3,    'modules() in scalar context' );
199
200 # files
201 $ei->{goodmod} = {
202         packlist => {
203                 ($Config{man1direxp} ?
204                     (File::Spec->catdir($Config{man1direxp}, 'foo') => 1) :
205                         ()),
206                 ($Config{man3direxp} ?
207                     (File::Spec->catdir($Config{man3direxp}, 'bar') => 1) :
208                         ()),
209                 File::Spec->catdir($prefix, 'foobar') => 1,
210                 foobaz  => 1,
211         },
212 };
213
214 eval { $ei->files('badmod') };
215 like( $@, qr/badmod is not installed/,'files() should croak given bad modname');
216 eval { $ei->files('goodmod', 'badtype' ) };
217 like( $@, qr/type must be/,'files() should croak given bad type' );
218
219 my @files;
220 SKIP: {
221     skip('no man directory man1dir on this system', 2)
222       unless $Config{man1direxp};
223     @files = $ei->files('goodmod', 'doc', $Config{man1direxp});
224     is( scalar @files, 1, '... should find doc file under given dir' );
225     is( (grep { /foo$/ } @files), 1, '... checking file name' );
226 }
227 SKIP: {
228     skip('no man directories on this system', 1) unless $mandirs;
229     @files = $ei->files('goodmod', 'doc');
230     is( scalar @files, $mandirs, '... should find all doc files with no dir' );
231 }
232
233 @files = $ei->files('goodmod', 'prog', 'fake', 'fake2');
234 is( scalar @files, 0, '... should find no doc files given wrong dirs' );
235 @files = $ei->files('goodmod', 'prog');
236 is( scalar @files, 1, '... should find doc file in correct dir' );
237 like( $files[0], qr/foobar[>\]]?$/, '... checking file name' );
238 @files = $ei->files('goodmod');
239 is( scalar @files, 2 + $mandirs, '... should find all files with no type specified' );
240 my %dirnames = map { lc($_) => dirname($_) } @files;
241
242 # directories
243 my @dirs = $ei->directories('goodmod', 'prog', 'fake');
244 is( scalar @dirs, 0, 'directories() should return no dirs if no files found' );
245
246 SKIP: {
247     skip('no man directories on this system', 1) unless $mandirs;
248     @dirs = $ei->directories('goodmod', 'doc');
249     is( scalar @dirs, $mandirs, '... should find all files files() would' );
250 }
251 @dirs = $ei->directories('goodmod');
252 is( scalar @dirs, 2 + $mandirs, '... should find all files files() would, again' );
253 @files = sort map { exists $dirnames{lc($_)} ? $dirnames{lc($_)} : '' } @files;
254 is( join(' ', @files), join(' ', @dirs), '... should sort output' );
255
256 # directory_tree
257 my $expectdirs =
258        ($mandirs == 2) &&
259        (dirname($Config{man1direxp}) eq dirname($Config{man3direxp}))
260        ? 3 : 2;
261
262 SKIP: {
263     skip('no man directories on this system', 1) unless $mandirs;
264     @dirs = $ei->directory_tree('goodmod', 'doc', $Config{man1direxp} ?
265        dirname($Config{man1direxp}) : dirname($Config{man3direxp}));
266     is( scalar @dirs, $expectdirs,
267         'directory_tree() should report intermediate dirs to those requested' );
268 }
269
270 my $fakepak = Fakepak->new(102);
271
272 $ei->{yesmod} = {
273         version         => 101,
274         packlist        => $fakepak,
275 };
276
277 # these should all croak
278 foreach my $sub (qw( validate packlist version )) {
279     eval { $ei->$sub('nomod') };
280     like( $@, qr/nomod is not installed/,
281           "$sub() should croak when asked about uninstalled module" );
282 }
283
284 # validate
285 is( $ei->validate('yesmod'), 'validated',
286         'validate() should return results of packlist validate() call' );
287
288 # packlist
289 is( ${ $ei->packlist('yesmod') }, 102,
290         'packlist() should report installed mod packlist' );
291
292 # version
293 is( $ei->version('yesmod'), 101,
294         'version() should report installed mod version' );
295
296
297 package Fakepak;
298
299 sub new {
300     my $class = shift;
301     bless(\(my $scalar = shift), $class);
302 }
303
304 sub validate {
305     return 'validated'
306 }