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