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