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