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