This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Refactor porting/diag.t and improve output format
[perl5.git] / dist / ExtUtils-Install / t / Installed.t
CommitLineData
39234879
MS
1#!/usr/bin/perl -w
2
3BEGIN {
fb78ba4b 4 unshift @INC, 't/lib/';
39234879 5}
431b0fc4
MS
6
7my $Is_VMS = $^O eq 'VMS';
f411ddcc 8
9use strict;
f411ddcc 10
f411ddcc 11use Config;
f411ddcc 12use Cwd;
13use File::Path;
f411ddcc 14use File::Basename;
f6d6199c 15use File::Spec;
f411ddcc 16
060fb22c 17use Test::More tests => 63;
f411ddcc 18
39234879 19BEGIN { use_ok( 'ExtUtils::Installed' ) }
f411ddcc 20
34dcf69d 21my $mandirs = !!$Config{man1direxp} + !!$Config{man3direxp};
64f50df3 22
f411ddcc 23# saves having to qualify package name for class methods
24my $ei = bless( {}, 'ExtUtils::Installed' );
25
060fb22c
YO
26# Make sure meta info is available
27$ei->{':private:'}{Config} = \%Config;
28$ei->{':private:'}{INC} = \@INC;
29
f411ddcc 30# _is_prefix
f6d6199c 31ok( $ei->_is_prefix('foo/bar', 'foo'),
41e5fcb0 32 '_is_prefix() should match valid path prefix' );
f6d6199c 33ok( !$ei->_is_prefix('\foo\bar', '\bar'),
41e5fcb0 34 '... should not match wrong prefix' );
f411ddcc 35
36# _is_type
f6d6199c 37ok( $ei->_is_type(0, 'all'), '_is_type() should be true for type of "all"' );
f411ddcc 38
34dcf69d 39foreach my $path (qw( man1dir man3dir )) {
41e5fcb0 40 SKIP: {
69a43907 41 my $dir = File::Spec->canonpath($Config{$path.'exp'});
34dcf69d
RB
42 skip("no man directory $path on this system", 2 ) unless $dir;
43
41e5fcb0
RGS
44 my $file = $dir . '/foo';
45 ok( $ei->_is_type($file, 'doc'), "... should find doc file in $path" );
46 ok( !$ei->_is_type($file, 'prog'), "... but not prog file in $path" );
34dcf69d 47 }
f411ddcc 48}
49
f6d6199c
MS
50# VMS 5.6.1 doesn't seem to have $Config{prefixexp}
51my $prefix = $Config{prefix} || $Config{prefixexp};
52
3a465856 53# You can concatenate /foo but not foo:, which defaults in the current
f6d6199c 54# directory
431b0fc4 55$prefix = VMS::Filespec::unixify($prefix) if $Is_VMS;
f6d6199c
MS
56
57# ActivePerl 5.6.1/631 has $Config{prefixexp} as 'p:' for some reason
58$prefix = $Config{prefix} if $prefix eq 'p:' && $^O eq 'MSWin32';
59
60ok( $ei->_is_type( File::Spec->catfile($prefix, 'bar'), 'prog'),
41e5fcb0 61 "... should find prog file under $prefix" );
64f50df3
JH
62
63SKIP: {
41e5fcb0 64 skip('no man directories on this system', 1) unless $mandirs;
3a465856 65 is( $ei->_is_type('bar', 'doc'), 0,
41e5fcb0 66 '... should not find doc file outside path' );
64f50df3
JH
67}
68
f6d6199c 69ok( !$ei->_is_type('bar', 'prog'),
41e5fcb0 70 '... nor prog file outside path' );
f6d6199c 71ok( !$ei->_is_type('whocares', 'someother'), '... nor other type anywhere' );
f411ddcc 72
73# _is_under
74ok( $ei->_is_under('foo'), '_is_under() should return true with no dirs' );
75
76my @under = qw( boo bar baz );
f6d6199c
MS
77ok( !$ei->_is_under('foo', @under), '... should find no file not under dirs');
78ok( $ei->_is_under('baz', @under), '... should find file under dir' );
f411ddcc 79
f411ddcc 80
d5d4ec93
MS
81rmtree 'auto/FakeMod';
82ok( mkpath('auto/FakeMod') );
b5f5ff40 83END { rmtree 'auto' }
57b1a898
MS
84
85ok(open(PACKLIST, '>auto/FakeMod/.packlist'));
86print PACKLIST 'list';
87close PACKLIST;
88
89ok(open(FAKEMOD, '>auto/FakeMod/FakeMod.pm'));
90
91print FAKEMOD <<'FAKE';
f411ddcc 92package FakeMod;
93use vars qw( $VERSION );
94$VERSION = '1.1.1';
951;
96FAKE
97
57b1a898 98close FAKEMOD;
f411ddcc 99
060fb22c 100my $fake_mod_dir = File::Spec->catdir(cwd(), 'auto', 'FakeMod');
57b1a898
MS
101{
102 # avoid warning and death by localizing glob
103 local *ExtUtils::Installed::Config;
57b1a898
MS
104 %ExtUtils::Installed::Config = (
105 %Config,
41e5fcb0
RGS
106 archlibexp => cwd(),
107 sitearchexp => $fake_mod_dir,
57b1a898 108 );
f411ddcc 109
41e5fcb0
RGS
110 # necessary to fool new()
111 push @INC, $fake_mod_dir;
f411ddcc 112
41e5fcb0 113 my $realei = ExtUtils::Installed->new();
57b1a898
MS
114 isa_ok( $realei, 'ExtUtils::Installed' );
115 isa_ok( $realei->{Perl}{packlist}, 'ExtUtils::Packlist' );
3a465856 116 is( $realei->{Perl}{version}, $Config{version},
57b1a898
MS
117 'new() should set Perl version from %Config' );
118
41e5fcb0
RGS
119 ok( exists $realei->{FakeMod}, 'new() should find modules with .packlists');
120 isa_ok( $realei->{FakeMod}{packlist}, 'ExtUtils::Packlist' );
3a465856 121 is( $realei->{FakeMod}{version}, '1.1.1',
41e5fcb0 122 '... should find version in modules' );
f411ddcc 123}
124
060fb22c
YO
125# Now try this using PERL5LIB
126{
127 local $ENV{PERL5LIB} = join $Config{path_sep}, $fake_mod_dir;
128 local *ExtUtils::Installed::Config;
129 %ExtUtils::Installed::Config = (
130 %Config,
131 archlibexp => cwd(),
132 sitearchexp => cwd(),
133 );
134
135 my $realei = ExtUtils::Installed->new();
136 isa_ok( $realei, 'ExtUtils::Installed' );
137 isa_ok( $realei->{Perl}{packlist}, 'ExtUtils::Packlist' );
138 is( $realei->{Perl}{version}, $Config{version},
139 'new() should set Perl version from %Config' );
140
141 ok( exists $realei->{FakeMod},
142 'new() should find modules with .packlists using PERL5LIB'
143 );
144 isa_ok( $realei->{FakeMod}{packlist}, 'ExtUtils::Packlist' );
145 is( $realei->{FakeMod}{version}, '1.1.1',
146 '... should find version in modules' );
147}
148
149# Do the same thing as the last block, but with overrides for
150# %Config and @INC.
151{
152 my $config_override = { %Config::Config };
153 $config_override->{archlibexp} = cwd();
154 $config_override->{sitearchexp} = $fake_mod_dir;
155 $config_override->{version} = 'fake_test_version';
156
157 my @inc_override = (@INC, $fake_mod_dir);
158
159 my $realei = ExtUtils::Installed->new(
160 'config_override' => $config_override,
161 'inc_override' => \@inc_override,
162 );
163 isa_ok( $realei, 'ExtUtils::Installed' );
164 isa_ok( $realei->{Perl}{packlist}, 'ExtUtils::Packlist' );
165 is( $realei->{Perl}{version}, 'fake_test_version',
166 'new(config_override => HASH) overrides %Config' );
167
168 ok( exists $realei->{FakeMod}, 'new() with overrides should find modules with .packlists');
169 isa_ok( $realei->{FakeMod}{packlist}, 'ExtUtils::Packlist' );
170 is( $realei->{FakeMod}{version}, '1.1.1',
171 '... should find version in modules' );
172}
173
174# Check if extra_libs works.
175{
176 my $realei = ExtUtils::Installed->new(
177 'extra_libs' => [ cwd() ],
178 );
179 isa_ok( $realei, 'ExtUtils::Installed' );
180 isa_ok( $realei->{Perl}{packlist}, 'ExtUtils::Packlist' );
181 ok( exists $realei->{FakeMod},
182 'new() with extra_libs should find modules with .packlists');
183
184 #{ use Data::Dumper; local $realei->{':private:'}{Config};
185 # warn Dumper($realei); }
186
187 isa_ok( $realei->{FakeMod}{packlist}, 'ExtUtils::Packlist' );
188 is( $realei->{FakeMod}{version}, '1.1.1',
189 '... should find version in modules' );
190}
191
f411ddcc 192# modules
193$ei->{$_} = 1 for qw( abc def ghi );
3a465856 194is( join(' ', $ei->modules()), 'abc def ghi',
41e5fcb0 195 'modules() should return sorted keys' );
f411ddcc 196
d5d4ec93
MS
197# This didn't work for a long time due to a sort in scalar context oddity.
198is( $ei->modules, 3, 'modules() in scalar context' );
199
f411ddcc 200# files
3a465856
SP
201$ei->{goodmod} = {
202 packlist => {
203 ($Config{man1direxp} ?
204 (File::Spec->catdir($Config{man1direxp}, 'foo') => 1) :
34dcf69d 205 ()),
3a465856
SP
206 ($Config{man3direxp} ?
207 (File::Spec->catdir($Config{man3direxp}, 'bar') => 1) :
34dcf69d 208 ()),
f6d6199c 209 File::Spec->catdir($prefix, 'foobar') => 1,
41e5fcb0
RGS
210 foobaz => 1,
211 },
f411ddcc 212};
213
214eval { $ei->files('badmod') };
215like( $@, qr/badmod is not installed/,'files() should croak given bad modname');
216eval { $ei->files('goodmod', 'badtype' ) };
217like( $@, qr/type must be/,'files() should croak given bad type' );
64f50df3
JH
218
219my @files;
220SKIP: {
3a465856
SP
221 skip('no man directory man1dir on this system', 2)
222 unless $Config{man1direxp};
34dcf69d
RB
223 @files = $ei->files('goodmod', 'doc', $Config{man1direxp});
224 is( scalar @files, 1, '... should find doc file under given dir' );
57b1a898 225 is( (grep { /foo$/ } @files), 1, '... checking file name' );
34dcf69d
RB
226}
227SKIP: {
228 skip('no man directories on this system', 1) unless $mandirs;
229 @files = $ei->files('goodmod', 'doc');
230 is( scalar @files, $mandirs, '... should find all doc files with no dir' );
64f50df3
JH
231}
232
f411ddcc 233@files = $ei->files('goodmod', 'prog', 'fake', 'fake2');
234is( scalar @files, 0, '... should find no doc files given wrong dirs' );
235@files = $ei->files('goodmod', 'prog');
236is( scalar @files, 1, '... should find doc file in correct dir' );
b4558e59 237like( $files[0], qr/foobar[>\]]?$/, '... checking file name' );
f411ddcc 238@files = $ei->files('goodmod');
34dcf69d 239is( scalar @files, 2 + $mandirs, '... should find all files with no type specified' );
62bfa7e0 240my %dirnames = map { lc($_) => dirname($_) } @files;
f411ddcc 241
242# directories
243my @dirs = $ei->directories('goodmod', 'prog', 'fake');
244is( scalar @dirs, 0, 'directories() should return no dirs if no files found' );
64f50df3
JH
245
246SKIP: {
34dcf69d
RB
247 skip('no man directories on this system', 1) unless $mandirs;
248 @dirs = $ei->directories('goodmod', 'doc');
249 is( scalar @dirs, $mandirs, '... should find all files files() would' );
250}
251@dirs = $ei->directories('goodmod');
252is( scalar @dirs, 2 + $mandirs, '... should find all files files() would, again' );
253@files = sort map { exists $dirnames{lc($_)} ? $dirnames{lc($_)} : '' } @files;
254is( join(' ', @files), join(' ', @dirs), '... should sort output' );
255
256# directory_tree
3a465856
SP
257my $expectdirs =
258 ($mandirs == 2) &&
34dcf69d
RB
259 (dirname($Config{man1direxp}) eq dirname($Config{man3direxp}))
260 ? 3 : 2;
3a465856 261
34dcf69d
RB
262SKIP: {
263 skip('no man directories on this system', 1) unless $mandirs;
264 @dirs = $ei->directory_tree('goodmod', 'doc', $Config{man1direxp} ?
265 dirname($Config{man1direxp}) : dirname($Config{man3direxp}));
3a465856 266 is( scalar @dirs, $expectdirs,
34dcf69d 267 'directory_tree() should report intermediate dirs to those requested' );
64f50df3 268}
f411ddcc 269
270my $fakepak = Fakepak->new(102);
271
3a465856 272$ei->{yesmod} = {
41e5fcb0
RGS
273 version => 101,
274 packlist => $fakepak,
f411ddcc 275};
276
277# these should all croak
278foreach my $sub (qw( validate packlist version )) {
41e5fcb0 279 eval { $ei->$sub('nomod') };
3a465856 280 like( $@, qr/nomod is not installed/,
41e5fcb0 281 "$sub() should croak when asked about uninstalled module" );
f411ddcc 282}
283
284# validate
3a465856 285is( $ei->validate('yesmod'), 'validated',
41e5fcb0 286 'validate() should return results of packlist validate() call' );
f411ddcc 287
288# packlist
3a465856 289is( ${ $ei->packlist('yesmod') }, 102,
41e5fcb0 290 'packlist() should report installed mod packlist' );
f411ddcc 291
292# version
3a465856 293is( $ei->version('yesmod'), 101,
41e5fcb0 294 'version() should report installed mod version' );
f411ddcc 295
f411ddcc 296
297package Fakepak;
298
299sub new {
41e5fcb0
RGS
300 my $class = shift;
301 bless(\(my $scalar = shift), $class);
f411ddcc 302}
303
304sub validate {
41e5fcb0 305 return 'validated'
f411ddcc 306}