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