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 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
150### test beta/developer release versions
151{   my $test_ver = $Module::Load::Conditional::VERSION;
152
153    ### strip beta tags
154    $test_ver =~ s/_\d+//g;
155    $test_ver .= '_99';
156
157    my $rv = check_install(
158                    module  => 'Module::Load::Conditional',
159                    version => $test_ver,
160                );
161
162    ok( $rv,                "Checking beta versions" );
163    ok( !$rv->{'uptodate'}, "   Beta version is higher" );
164
165}
166
167### test $FIND_VERSION
168{
169    local $Module::Load::Conditional::FIND_VERSION = 0;
170
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" );
176}
177
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');
183    $x_pm = VMS::Filespec::unixify($x_pm) if ON_VMS;
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
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{
219    my $use_list = { 'MustBe::Loaded' => 1 };
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'};
227    delete $INC{'MustBe/Loaded.pm'};
228
229    my $use_list = { 'LoadIt' => 1, 'MustBe::Loaded' => 1 };
230    my $bool = can_load( modules => $use_list );
231
232    ok( !$INC{'LoadIt.pm'} && !$INC{'MustBe/Loaded.pm'},
233        q[Do not load if one prerequisite fails]
234    );
235}
236
237
238### test 'requires' ###
239SKIP:{
240    skip "Depends on \$^X, which doesn't work well when testing the Perl core",
241        1 if $ENV{PERL_CORE};
242
243    my %list = map { $_ => 1 } requires('Carp');
244
245    my $flag;
246    $flag++ unless delete $list{'Exporter'};
247
248    ok( !$flag, q[Detecting requirements] );
249}
250
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;
254
255    {   package A::B::C::D;
256        $A::B::C::D::VERSION = $$;
257        $INC{'A/B/C/D.pm'}   = $$.$$;
258
259        ### XXX this is no longer needed with M::Load 0.11_01
260        #$INC{'[.A.B.C]D.pm'} = $$.$$ if $^O eq 'VMS';
261    }
262
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
273