1### Module::Load::Conditional test suite ### 2### this should no longer be needed 3# BEGIN { 4# if( $ENV{PERL_CORE} ) { 5# chdir '../lib/Module/Load/Conditional' 6# if -d '../lib/Module/Load/Conditional'; 7# unshift @INC, '../../../..'; 8# 9# ### fix perl location too 10# $^X = '../../../../../t/' . $^X; 11# } 12# } 13 14BEGIN { use FindBin; } 15BEGIN { chdir 't' if -d 't' } 16 17use strict; 18use File::Spec (); 19use Test::More 'no_plan'; 20 21use constant ON_VMS => $^O eq 'VMS'; 22 23use lib File::Spec->catdir($FindBin::Bin, qw[.. lib] ); 24use lib File::Spec->catdir($FindBin::Bin, q[to_load] ); 25 26use_ok( 'Module::Load::Conditional' ); 27 28### stupid stupid warnings ### 29{ $Module::Load::Conditional::VERBOSE = 30 $Module::Load::Conditional::VERBOSE = 0; 31 32 *can_load = *Module::Load::Conditional::can_load 33 = *Module::Load::Conditional::can_load; 34 *check_install = *Module::Load::Conditional::check_install 35 = *Module::Load::Conditional::check_install; 36 *requires = *Module::Load::Conditional::requires 37 = *Module::Load::Conditional::requires; 38} 39 40{ 41 my $rv = check_install( 42 module => 'Module::Load::Conditional', 43 version => $Module::Load::Conditional::VERSION, 44 ); 45 46 ok( $rv->{uptodate}, q[Verify self] ); 47 is( $rv->{version}, $Module::Load::Conditional::VERSION, 48 q[ Found proper version] ); 49 ok( $rv->{dir}, q[ Found directory information] ); 50 51 { my $dir = File::Spec->canonpath( $rv->{dir} ); 52 53 ### special rules apply on VMS, as always... 54 if (ON_VMS) { 55 ### Need path syntax for VMS compares. 56 $dir = VMS::Filespec::pathify($dir); 57 ### Remove the trailing VMS specific directory delimiter 58 $dir =~ s/\]//; 59 } 60 61 ### quote for Win32 paths, use | to avoid slash confusion 62 my $dir_re = qr|^\Q$dir\E|i; 63 like( File::Spec->canonpath( $rv->{file} ), $dir_re, 64 q[ Dir subset of file path] ); 65 } 66 67 ### break up the specification 68 my @rv_path = do { 69 70 ### Use the UNIX specific method, as the VMS one currently 71 ### converts the file spec back to VMS format. 72 my $class = ON_VMS ? 'File::Spec::Unix' : 'File::Spec'; 73 74 my($vol, $path, $file) = $class->splitpath( $rv->{'file'} ); 75 76 my @path = ($vol, $class->splitdir( $path ), $file ); 77 78 ### First element could be blank for some system types like VMS 79 shift @path if $vol eq ''; 80 81 ### and return it 82 @path; 83 }; 84 my $inc_path = $INC{'Module/Load/Conditional.pm'}; 85 if ( $^O eq 'MSWin32' ) { 86 $inc_path = File::Spec->canonpath( $inc_path ); 87 $inc_path =~ s{\\}{/}g; # to meet with unix path 88 } 89 is( $inc_path, 90 File::Spec::Unix->catfile(@rv_path), 91 q[ Found proper file] 92 ); 93 94 95 96} 97 98### the version may contain an _, which means perl will warn about 'not 99### numeric' -- turn off that warning here. 100{ local $^W; 101 my $rv = check_install( 102 module => 'Module::Load::Conditional', 103 version => $Module::Load::Conditional::VERSION + 1, 104 ); 105 106 ok( !$rv->{uptodate} && $rv->{version} && $rv->{file}, 107 q[Verify out of date module] 108 ); 109} 110 111{ 112 my $rv = check_install( module => 'Module::Load::Conditional' ); 113 114 ok( $rv->{uptodate} && $rv->{version} && $rv->{file}, 115 q[Verify any module] 116 ); 117} 118 119{ 120 my $rv = check_install( module => 'Module::Does::Not::Exist' ); 121 122 ok( !$rv->{uptodate} && !$rv->{version} && !$rv->{file}, 123 q[Verify non-existent module] 124 ); 125 126} 127 128### test finding a version of a module that mentions $VERSION in pod 129{ my $rv = check_install( module => 'InPod' ); 130 ok( $rv, 'Testing $VERSION in POD' ); 131 ok( $rv->{version}, " Version found" ); 132 is( $rv->{version}, 2, " Version is correct" ); 133} 134 135### test finding a version of a module that has a VERSION error in a HereDoc 136{ my $rv = check_install( module => 'HereDoc' ); 137 ok( $rv, 'Testing $VERSION in HEREDOC' ); 138 ok( !$rv->{version}, " No Version found" ); 139 is( $rv->{version}, undef, " Version is correct" ); 140} 141 142### test that no package statement means $VERSION is $main::VERSION 143{ 144 my $rv = check_install( module => 'NotMain' ); 145 ok( $rv, 'Testing $VERSION without package' ); 146 is( $rv->{version}, undef, " No version info returned" ); 147} 148 149### test that the right $VERSION is picked when there are several packages 150{ 151 my $rv = check_install( module => 'NotX' ); 152 ok( $rv, 'Testing $VERSION with many packages' ); 153 ok( $rv->{version}, " Version found" ); 154 is( $rv->{version}, 3, " Version is correct" ); 155} 156 157### test beta/developer release versions 158{ my $test_ver = $Module::Load::Conditional::VERSION; 159 160 ### strip beta tags 161 $test_ver =~ s/_\d+//g; 162 $test_ver .= '_99'; 163 164 my $rv = check_install( 165 module => 'Module::Load::Conditional', 166 version => $test_ver, 167 ); 168 169 ok( $rv, "Checking beta versions" ); 170 ok( !$rv->{'uptodate'}, " Beta version is higher" ); 171 172} 173 174### test $FIND_VERSION 175{ 176 local $Module::Load::Conditional::FIND_VERSION = 0; 177 178 my $rv = check_install( module => 'Module::Load::Conditional' ); 179 180 ok( $rv, 'Testing $FIND_VERSION' ); 181 is( $rv->{version}, undef, " No version info returned" ); 182 ok( $rv->{uptodate}, " Module marked as uptodate" ); 183} 184 185### test that check_install() picks up the first match 186{ 187 my ($dir_a, $dir_b) = map File::Spec->catdir($FindBin::Bin, 'test_lib', $_), 188 qw[a b]; 189 my $x_pm = File::Spec->catfile($dir_a, 'X.pm'); 190 $x_pm = VMS::Filespec::unixify($x_pm) if ON_VMS; 191 192 local @INC = ($dir_a, $dir_b); 193 194 my $rv = check_install( module => 'X' ); 195 196 ok( $rv, 'Testing the file picked by check_install ($FIND_VERSION == 1)' ); 197 is( $rv->{file}, $x_pm, " First file was picked" ); 198 is( $rv->{version}, '0.01', " Correct version for first file" ); 199 200 local $Module::Load::Conditional::FIND_VERSION = 0; 201 202 $rv = check_install( module => 'X' ); 203 204 ok( $rv, 'Testing the file picked by check_install ($FIND_VERSION == 0)' ); 205 is( $rv->{file}, $x_pm, " First file was also picked" ); 206 is( $rv->{version}, undef, " But its VERSION was not required" ); 207} 208 209### test 'can_load' ### 210 211{ 212 my $use_list = { 'LoadIt' => 1 }; 213 my $bool = can_load( modules => $use_list ); 214 215 ok( $bool, q[Load simple module] ); 216} 217 218{ 219 my $use_list = { 'Commented' => 2 }; 220 my $bool = can_load( modules => $use_list ); 221 222 ok( $bool, q[Load module with a second, commented-out $VERSION] ); 223} 224 225{ 226 my $use_list = { 'MustBe::Loaded' => 1 }; 227 my $bool = can_load( modules => $use_list ); 228 229 ok( !$bool, q[Detect out of date module] ); 230} 231 232{ 233 delete $INC{'LoadIt.pm'}; 234 delete $INC{'MustBe/Loaded.pm'}; 235 236 my $use_list = { 'LoadIt' => 1, 'MustBe::Loaded' => 1 }; 237 my $bool = can_load( modules => $use_list ); 238 239 ok( !$INC{'LoadIt.pm'} && !$INC{'MustBe/Loaded.pm'}, 240 q[Do not load if one prerequisite fails] 241 ); 242} 243 244# test for autoload 245 246# autoload 247{ 248 my $use_list = { 'AutoLoad' => 0 }; 249 my $bool = can_load( modules => $use_list, autoload => 1 ); 250 ok( $bool, q[autoloaded] ); 251 252 eval { func1(); }; 253 is( $@, '', q[exported function] ); 254 255 eval { func2(); }; 256 ok( $@, q[not exported function] ); 257} 258 259# not autoload 260{ 261 my $use_list = { 'NotAutoLoad' => 0 }; 262 my $bool = can_load( modules => $use_list ); 263 ok( $bool, q[not autoloaded] ); 264 265 eval { func3(); }; 266 ok( $@, q[not exported function - func3] ); 267 268 eval { func4(); }; 269 ok( $@, q[not exported function - func4] ); 270} 271 272 273### test 'requires' ### 274SKIP:{ 275 skip "Depends on \$^X, which doesn't work well when testing the Perl core", 276 1 if $ENV{PERL_CORE}; 277 278 my %list = map { $_ => 1 } requires('Carp'); 279 280 my $flag; 281 $flag++ unless delete $list{'Exporter'}; 282 283 ok( !$flag, q[Detecting requirements] ); 284} 285 286### test using the %INC lookup for check_install 287{ local $Module::Load::Conditional::CHECK_INC_HASH = 1; 288 local $Module::Load::Conditional::CHECK_INC_HASH = 1; 289 290 { package A::B::C::D; 291 $A::B::C::D::VERSION = "$$"; 292 $INC{'A/B/C/D.pm'} = "$$"."$$"; 293 294 ### XXX this is no longer needed with M::Load 0.11_01 295 #$INC{'[.A.B.C]D.pm'} = $$.$$ if $^O eq 'VMS'; 296 } 297 298 my $href = check_install( module => 'A::B::C::D', version => 0 ); 299 300 ok( $href, 'Found package in %INC' ); 301 is( $href->{'file'}, "$$"."$$", ' Found correct file' ); 302 is( $href->{'version'}, "$$", ' Found correct version' ); 303 ok( $href->{'uptodate'}, ' Marked as uptodate' ); 304 ok( can_load( modules => { 'A::B::C::D' => 0 } ), 305 ' can_load successful' ); 306} 307 308