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