Syncing with bleadperl
[p5sagit/p5-mst-13.2.git] / lib / ExtUtils / t / Installed.t
1 #!/usr/bin/perl -w
2
3 BEGIN {
4     if( $ENV{PERL_CORE} ) {
5         chdir 't' if -d 't';
6         @INC = '../lib';
7     }
8 }
9 chdir 't';
10
11
12 use strict;
13 use warnings;
14
15 # for _is_type() tests
16 use Config;
17
18 # for new() tests
19 use Cwd;
20 use File::Path;
21
22 # for directories() tests
23 use File::Basename;
24
25 use Test::More tests => 43;
26
27 BEGIN { use_ok( 'ExtUtils::Installed' ) }
28
29 # saves having to qualify package name for class methods
30 my $ei = bless( {}, 'ExtUtils::Installed' );
31
32 # _is_prefix
33 is( $ei->_is_prefix('foo/bar', 'foo'), 1, 
34         '_is_prefix() should match valid path prefix' );
35 is( $ei->_is_prefix('\foo\bar', '\bar'), 0, 
36         '... should not match wrong prefix' );
37
38 # _is_type
39 is( $ei->_is_type(0, 'all'), 1, '_is_type() should be true for type of "all"' );
40
41 foreach 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
47 is( $ei->_is_type($Config{prefix} . '/bar', 'prog'), 1, 
48         "... should find prog file under $Config{prefix}" );
49 is( $ei->_is_type('bar', 'doc'), 0, 
50         '... should not find doc file outside path' );
51 is( $ei->_is_type('bar', 'prog'), 0, 
52         '... nor prog file outside path' );
53 is( $ei->_is_type('whocares', 'someother'), 0, '... nor other type anywhere' );
54
55 # _is_under
56 ok( $ei->_is_under('foo'), '_is_under() should return true with no dirs' );
57
58 my @under = qw( boo bar baz );
59 is( $ei->_is_under('foo', @under), 0, '... should find no file not under dirs');
60 is( $ei->_is_under('baz', @under), 1, '... should find file under dir' );
61
62 # new
63 my $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 }
72
73 isa_ok( $realei, 'ExtUtils::Installed' );
74 isa_ok( $realei->{Perl}{packlist}, 'ExtUtils::Packlist' );
75 is( $realei->{Perl}{version}, $Config{version}, 
76         'new() should set Perl version from %Config' );
77
78 my $wrotelist;
79 if (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';
85 package FakeMod;
86 use vars qw( $VERSION );
87 $VERSION = '1.1.1';
88 1;
89 FAKE
90
91                         close FAKEMOD;
92                         $wrotelist = 1;
93                 }
94         }
95 }
96
97
98 SKIP: {
99         skip( "could not write packlist: $!", 3 ) unless $wrotelist;
100
101         # avoid warning and death by localizing glob
102         local *ExtUtils::Installed::Config;
103     my $fake_mod_dir = File::Spec->catdir(cwd(), 'auto', 'FakeMod');
104         %ExtUtils::Installed::Config = (
105                 archlib            => cwd(),
106         installarchlib => cwd(),
107                 sitearch           => $fake_mod_dir,
108         );
109
110         # necessary to fool new()
111         push @INC, $fake_mod_dir;
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 );
122 is( join(' ', $ei->modules()), 'abc def ghi', 
123         'modules() should return sorted keys' );
124
125 # files
126 $ei->{goodmod} = { 
127         packlist => { 
128                 File::Spec->catdir($Config{installman1dir}, 'foo') => 1,
129                 File::Spec->catdir($Config{installman3dir}, 'bar') => 1,
130                 File::Spec->catdir($Config{prefix}, 'foobar') => 1,
131                 foobaz  => 1,
132         },
133 };
134
135 eval { $ei->files('badmod') };
136 like( $@, qr/badmod is not installed/,'files() should croak given bad modname');
137 eval { $ei->files('goodmod', 'badtype' ) };
138 like( $@, qr/type must be/,'files() should croak given bad type' );
139 my @files = $ei->files('goodmod', 'doc', $Config{installman1dir});
140 is( scalar @files, 1, '... should find doc file under given dir' );
141 like( $files[0], qr/foo$/, '... checking file name' );
142 @files = $ei->files('goodmod', 'doc');
143 is( scalar @files, 2, '... should find all doc files with no dir' );
144 @files = $ei->files('goodmod', 'prog', 'fake', 'fake2');
145 is( scalar @files, 0, '... should find no doc files given wrong dirs' );
146 @files = $ei->files('goodmod', 'prog');
147 is( scalar @files, 1, '... should find doc file in correct dir' );
148 like( $files[0], qr/foobar$/, '... checking file name' );
149 @files = $ei->files('goodmod');
150 is( scalar @files, 4, '... should find all files with no type specified' );
151 my %dirnames = map { $_ => dirname($_) } @files;
152
153 # directories
154 my @dirs = $ei->directories('goodmod', 'prog', 'fake');
155 is( scalar @dirs, 0, 'directories() should return no dirs if no files found' );
156 @dirs = $ei->directories('goodmod', 'doc');
157 is( scalar @dirs, 2, '... should find all files files() would' );
158 @dirs = $ei->directories('goodmod');
159 is( scalar @dirs, 4, '... should find all files files() would, again' );
160 @files = sort map { exists $dirnames{$_} ? $dirnames{$_} : '' } @files;
161 is( join(' ', @files), join(' ', @dirs), '... should sort output' );
162
163 # directory_tree
164 my $expectdirs = 
165         dirname($Config{installman1dir}) eq dirname($Config{installman3dir}) ? 3 :2;
166
167 @dirs = $ei->directory_tree('goodmod', 'doc', dirname($Config{installman1dir}));
168 is( scalar @dirs, $expectdirs, 
169         'directory_tree() should report intermediate dirs to those requested' );
170
171 my $fakepak = Fakepak->new(102);
172
173 $ei->{yesmod} = { 
174         version         => 101,
175         packlist        => $fakepak,
176 };
177
178 # these should all croak
179 foreach 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
186 is( $ei->validate('yesmod'), 'validated', 
187         'validate() should return results of packlist validate() call' );
188
189 # packlist
190 is( ${ $ei->packlist('yesmod') }, 102, 
191         'packlist() should report installed mod packlist' );
192
193 # version
194 is( $ei->version('yesmod'), 101, 
195         'version() should report installed mod version' );
196
197 # needs a DESTROY, for some reason
198 can_ok( $ei, 'DESTROY' );
199
200 END {
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
209 package Fakepak;
210
211 sub new {
212         my $class = shift;
213         bless(\(my $scalar = shift), $class);
214 }
215
216 sub validate {
217         'validated'
218 }