Commit | Line | Data |
---|---|---|
0dc418cb | 1 | ### Module::Load::Conditional test suite ### |
3fa779ab | 2 | ### this should no longer be needed |
21501d15 | 3 | # BEGIN { |
3fa779ab | 4 | # if( $ENV{PERL_CORE} ) { |
21501d15 | 5 | # chdir '../lib/Module/Load/Conditional' |
3fa779ab JB |
6 | # if -d '../lib/Module/Load/Conditional'; |
7 | # unshift @INC, '../../../..'; | |
21501d15 | 8 | # |
3fa779ab JB |
9 | # ### fix perl location too |
10 | # $^X = '../../../../../t/' . $^X; | |
11 | # } | |
21501d15 | 12 | # } |
3fa779ab | 13 | |
c259ebfb | 14 | BEGIN { use FindBin; } |
0dc418cb JB |
15 | BEGIN { chdir 't' if -d 't' } |
16 | ||
17 | use strict; | |
0dc418cb | 18 | use File::Spec (); |
3fa779ab JB |
19 | use Test::More 'no_plan'; |
20 | ||
21 | use constant ON_VMS => $^O eq 'VMS'; | |
0dc418cb | 22 | |
9b31c40c SH |
23 | use lib File::Spec->catdir($FindBin::Bin, qw[.. lib] ); |
24 | use lib File::Spec->catdir($FindBin::Bin, q[to_load] ); | |
0dc418cb | 25 | |
91e53322 | 26 | use_ok( 'Module::Load::Conditional' ); |
0dc418cb JB |
27 | |
28 | ### stupid stupid warnings ### | |
21501d15 | 29 | { $Module::Load::Conditional::VERBOSE = |
0dc418cb JB |
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] ); | |
21501d15 | 47 | is( $rv->{version}, $Module::Load::Conditional::VERSION, |
0dc418cb | 48 | q[ Found proper version] ); |
9b31c40c | 49 | ok( $rv->{dir}, q[ Found directory information] ); |
21501d15 | 50 | |
687d0cf8 | 51 | { my $dir = File::Spec->canonpath( $rv->{dir} ); |
b8a4246c RGS |
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/\]//; | |
21501d15 CBW |
59 | } |
60 | ||
b8a4246c RGS |
61 | ### quote for Win32 paths, use | to avoid slash confusion |
62 | my $dir_re = qr|^\Q$dir\E|i; | |
687d0cf8 | 63 | like( File::Spec->canonpath( $rv->{file} ), $dir_re, |
9b31c40c SH |
64 | q[ Dir subset of file path] ); |
65 | } | |
0dc418cb | 66 | |
3fa779ab JB |
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'; | |
21501d15 | 73 | |
3fa779ab | 74 | my($vol, $path, $file) = $class->splitpath( $rv->{'file'} ); |
fd9d6e12 | 75 | |
3fa779ab | 76 | my @path = ($vol, $class->splitdir( $path ), $file ); |
fd9d6e12 | 77 | |
3fa779ab JB |
78 | ### First element could be blank for some system types like VMS |
79 | shift @path if $vol eq ''; | |
80 | ||
21501d15 | 81 | ### and return it |
3fa779ab JB |
82 | @path; |
83 | }; | |
596fddd5 SP |
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, | |
3fa779ab | 90 | File::Spec::Unix->catfile(@rv_path), |
0dc418cb JB |
91 | q[ Found proper file] |
92 | ); | |
21501d15 CBW |
93 | |
94 | ||
0dc418cb JB |
95 | |
96 | } | |
97 | ||
3fa779ab JB |
98 | ### the version may contain an _, which means perl will warn about 'not |
99 | ### numeric' -- turn off that warning here. | |
100 | { local $^W; | |
0dc418cb JB |
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}, | |
8c2265fd | 123 | q[Verify non-existent module] |
0dc418cb JB |
124 | ); |
125 | ||
126 | } | |
127 | ||
e163f9a0 RGS |
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 | ||
201db1c7 SH |
135 | ### test that no package statement means $VERSION is $main::VERSION |
136 | { | |
137 | my $rv = check_install( module => 'NotMain' ); | |
138 | ok( $rv, 'Testing $VERSION without package' ); | |
139 | is( $rv->{version}, undef, " No version info returned" ); | |
140 | } | |
141 | ||
142 | ### test that the right $VERSION is picked when there are several packages | |
143 | { | |
144 | my $rv = check_install( module => 'NotX' ); | |
145 | ok( $rv, 'Testing $VERSION with many packages' ); | |
146 | ok( $rv->{version}, " Version found" ); | |
147 | is( $rv->{version}, 3, " Version is correct" ); | |
148 | } | |
149 | ||
3fa779ab JB |
150 | ### test beta/developer release versions |
151 | { my $test_ver = $Module::Load::Conditional::VERSION; | |
21501d15 | 152 | |
3fa779ab JB |
153 | ### strip beta tags |
154 | $test_ver =~ s/_\d+//g; | |
155 | $test_ver .= '_99'; | |
21501d15 CBW |
156 | |
157 | my $rv = check_install( | |
158 | module => 'Module::Load::Conditional', | |
3fa779ab JB |
159 | version => $test_ver, |
160 | ); | |
161 | ||
162 | ok( $rv, "Checking beta versions" ); | |
163 | ok( !$rv->{'uptodate'}, " Beta version is higher" ); | |
21501d15 CBW |
164 | |
165 | } | |
3fa779ab | 166 | |
e163f9a0 | 167 | ### test $FIND_VERSION |
201db1c7 | 168 | { |
0dc418cb | 169 | local $Module::Load::Conditional::FIND_VERSION = 0; |
21501d15 | 170 | |
0dc418cb JB |
171 | my $rv = check_install( module => 'Module::Load::Conditional' ); |
172 | ||
173 | ok( $rv, 'Testing $FIND_VERSION' ); | |
174 | is( $rv->{version}, undef, " No version info returned" ); | |
175 | ok( $rv->{uptodate}, " Module marked as uptodate" ); | |
21501d15 | 176 | } |
0dc418cb | 177 | |
201db1c7 SH |
178 | ### test that check_install() picks up the first match |
179 | { | |
180 | my ($dir_a, $dir_b) = map File::Spec->catdir($FindBin::Bin, 'test_lib', $_), | |
181 | qw[a b]; | |
182 | my $x_pm = File::Spec->catfile($dir_a, 'X.pm'); | |
26968795 | 183 | $x_pm = VMS::Filespec::unixify($x_pm) if ON_VMS; |
201db1c7 SH |
184 | |
185 | local @INC = ($dir_a, $dir_b); | |
186 | ||
187 | my $rv = check_install( module => 'X' ); | |
188 | ||
189 | ok( $rv, 'Testing the file picked by check_install ($FIND_VERSION == 1)' ); | |
190 | is( $rv->{file}, $x_pm, " First file was picked" ); | |
191 | is( $rv->{version}, '0.01', " Correct version for first file" ); | |
192 | ||
193 | local $Module::Load::Conditional::FIND_VERSION = 0; | |
194 | ||
195 | $rv = check_install( module => 'X' ); | |
196 | ||
197 | ok( $rv, 'Testing the file picked by check_install ($FIND_VERSION == 0)' ); | |
198 | is( $rv->{file}, $x_pm, " First file was also picked" ); | |
199 | is( $rv->{version}, undef, " But its VERSION was not required" ); | |
200 | } | |
201 | ||
0dc418cb JB |
202 | ### test 'can_load' ### |
203 | ||
204 | { | |
205 | my $use_list = { 'LoadIt' => 1 }; | |
206 | my $bool = can_load( modules => $use_list ); | |
207 | ||
208 | ok( $bool, q[Load simple module] ); | |
209 | } | |
210 | ||
211 | { | |
212 | my $use_list = { 'Commented' => 2 }; | |
213 | my $bool = can_load( modules => $use_list ); | |
214 | ||
215 | ok( $bool, q[Load module with a second, commented-out $VERSION] ); | |
216 | } | |
217 | ||
218 | { | |
fc934b13 | 219 | my $use_list = { 'MustBe::Loaded' => 1 }; |
0dc418cb JB |
220 | my $bool = can_load( modules => $use_list ); |
221 | ||
222 | ok( !$bool, q[Detect out of date module] ); | |
223 | } | |
224 | ||
225 | { | |
226 | delete $INC{'LoadIt.pm'}; | |
fc934b13 | 227 | delete $INC{'MustBe/Loaded.pm'}; |
0dc418cb | 228 | |
fc934b13 | 229 | my $use_list = { 'LoadIt' => 1, 'MustBe::Loaded' => 1 }; |
0dc418cb JB |
230 | my $bool = can_load( modules => $use_list ); |
231 | ||
fc934b13 | 232 | ok( !$INC{'LoadIt.pm'} && !$INC{'MustBe/Loaded.pm'}, |
0dc418cb JB |
233 | q[Do not load if one prerequisite fails] |
234 | ); | |
235 | } | |
236 | ||
237 | ||
238 | ### test 'requires' ### | |
1dc5a1cd | 239 | SKIP:{ |
21501d15 | 240 | skip "Depends on \$^X, which doesn't work well when testing the Perl core", |
1dc5a1cd | 241 | 1 if $ENV{PERL_CORE}; |
0dc418cb | 242 | |
e163f9a0 | 243 | my %list = map { $_ => 1 } requires('Carp'); |
21501d15 | 244 | |
0dc418cb JB |
245 | my $flag; |
246 | $flag++ unless delete $list{'Exporter'}; | |
247 | ||
248 | ok( !$flag, q[Detecting requirements] ); | |
249 | } | |
250 | ||
0dc418cb JB |
251 | ### test using the %INC lookup for check_install |
252 | { local $Module::Load::Conditional::CHECK_INC_HASH = 1; | |
253 | local $Module::Load::Conditional::CHECK_INC_HASH = 1; | |
21501d15 CBW |
254 | |
255 | { package A::B::C::D; | |
256 | $A::B::C::D::VERSION = $$; | |
0dc418cb | 257 | $INC{'A/B/C/D.pm'} = $$.$$; |
21501d15 | 258 | |
3fa779ab JB |
259 | ### XXX this is no longer needed with M::Load 0.11_01 |
260 | #$INC{'[.A.B.C]D.pm'} = $$.$$ if $^O eq 'VMS'; | |
0dc418cb | 261 | } |
21501d15 | 262 | |
0dc418cb JB |
263 | my $href = check_install( module => 'A::B::C::D', version => 0 ); |
264 | ||
265 | ok( $href, 'Found package in %INC' ); | |
266 | is( $href->{'file'}, $$.$$, ' Found correct file' ); | |
267 | is( $href->{'version'}, $$, ' Found correct version' ); | |
268 | ok( $href->{'uptodate'}, ' Marked as uptodate' ); | |
269 | ok( can_load( modules => { 'A::B::C::D' => 0 } ), | |
270 | ' can_load successful' ); | |
271 | } | |
272 |