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