4 if( $ENV{PERL_CORE} ) {
15 # for _is_type() tests
22 # for directories() tests
25 use Test::More tests => 43;
27 BEGIN { use_ok( 'ExtUtils::Installed' ) }
29 # saves having to qualify package name for class methods
30 my $ei = bless( {}, 'ExtUtils::Installed' );
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' );
39 is( $ei->_is_type(0, 'all'), 1, '_is_type() should be true for type of "all"' );
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" );
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' );
56 ok( $ei->_is_under('foo'), '_is_under() should return true with no dirs' );
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' );
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/;
70 $realei = ExtUtils::Installed->new();
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' );
79 if (mkpath('auto/FakeMod')) {
80 if (open(PACKLIST, '>', 'auto/FakeMod/.packlist')) {
81 print PACKLIST 'list';
83 if (open(FAKEMOD, '>', 'auto/FakeMod/FakeMod.pm')) {
84 print FAKEMOD <<'FAKE';
86 use vars qw( $VERSION );
99 skip( "could not write packlist: $!", 3 ) unless $wrotelist;
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 = (
106 installarchlib => cwd(),
107 sitearch => $fake_mod_dir,
110 # necessary to fool new()
111 push @INC, $fake_mod_dir;
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' );
121 $ei->{$_} = 1 for qw( abc def ghi );
122 is( join(' ', $ei->modules()), 'abc def ghi',
123 'modules() should return sorted keys' );
128 File::Spec->catdir($Config{installman1dir}, 'foo') => 1,
129 File::Spec->catdir($Config{installman3dir}, 'bar') => 1,
130 File::Spec->catdir($Config{prefix}, 'foobar') => 1,
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;
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' );
165 dirname($Config{installman1dir}) eq dirname($Config{installman3dir}) ? 3 :2;
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' );
171 my $fakepak = Fakepak->new(102);
175 packlist => $fakepak,
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" );
186 is( $ei->validate('yesmod'), 'validated',
187 'validate() should return results of packlist validate() call' );
190 is( ${ $ei->packlist('yesmod') }, 102,
191 'packlist() should report installed mod packlist' );
194 is( $ei->version('yesmod'), 101,
195 'version() should report installed mod version' );
197 # needs a DESTROY, for some reason
198 can_ok( $ei, 'DESTROY' );
202 for my $file (qw( .packlist FakePak.pm )) {
203 1 while unlink $file;
205 File::Path::rmtree('auto') or warn "Couldn't rmtree auto: $!";
213 bless(\(my $scalar = shift), $class);