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