xref: /openbsd-src/gnu/usr.bin/perl/cpan/Module-Load-Conditional/t/01_Module_Load_Conditional.t (revision ac9b4aacc1da35008afea06a5d23c2f2dea9b93e)
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-existant 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 beta/developer release versions
136{   my $test_ver = $Module::Load::Conditional::VERSION;
137
138    ### strip beta tags
139    $test_ver =~ s/_\d+//g;
140    $test_ver .= '_99';
141
142    my $rv = check_install(
143                    module  => 'Module::Load::Conditional',
144                    version => $test_ver,
145                );
146
147    ok( $rv,                "Checking beta versions" );
148    ok( !$rv->{'uptodate'}, "   Beta version is higher" );
149
150}
151
152### test $FIND_VERSION
153{   local $Module::Load::Conditional::FIND_VERSION = 0;
154    local $Module::Load::Conditional::FIND_VERSION = 0;
155
156    my $rv = check_install( module  => 'Module::Load::Conditional' );
157
158    ok( $rv,                        'Testing $FIND_VERSION' );
159    is( $rv->{version}, undef,      "   No version info returned" );
160    ok( $rv->{uptodate},            "   Module marked as uptodate" );
161}
162
163### test 'can_load' ###
164
165{
166    my $use_list = { 'LoadIt' => 1 };
167    my $bool = can_load( modules => $use_list );
168
169    ok( $bool, q[Load simple module] );
170}
171
172{
173    my $use_list = { 'Commented' => 2 };
174    my $bool = can_load( modules => $use_list );
175
176    ok( $bool, q[Load module with a second, commented-out $VERSION] );
177}
178
179{
180    my $use_list = { 'MustBe::Loaded' => 1 };
181    my $bool = can_load( modules => $use_list );
182
183    ok( !$bool, q[Detect out of date module] );
184}
185
186{
187    delete $INC{'LoadIt.pm'};
188    delete $INC{'MustBe/Loaded.pm'};
189
190    my $use_list = { 'LoadIt' => 1, 'MustBe::Loaded' => 1 };
191    my $bool = can_load( modules => $use_list );
192
193    ok( !$INC{'LoadIt.pm'} && !$INC{'MustBe/Loaded.pm'},
194        q[Do not load if one prerequisite fails]
195    );
196}
197
198
199### test 'requires' ###
200SKIP:{
201    skip "Depends on \$^X, which doesn't work well when testing the Perl core",
202        1 if $ENV{PERL_CORE};
203
204    my %list = map { $_ => 1 } requires('Carp');
205
206    my $flag;
207    $flag++ unless delete $list{'Exporter'};
208
209    ok( !$flag, q[Detecting requirements] );
210}
211
212### test using the %INC lookup for check_install
213{   local $Module::Load::Conditional::CHECK_INC_HASH = 1;
214    local $Module::Load::Conditional::CHECK_INC_HASH = 1;
215
216    {   package A::B::C::D;
217        $A::B::C::D::VERSION = $$;
218        $INC{'A/B/C/D.pm'}   = $$.$$;
219
220        ### XXX this is no longer needed with M::Load 0.11_01
221        #$INC{'[.A.B.C]D.pm'} = $$.$$ if $^O eq 'VMS';
222    }
223
224    my $href = check_install( module => 'A::B::C::D', version => 0 );
225
226    ok( $href,                  'Found package in %INC' );
227    is( $href->{'file'}, $$.$$, '   Found correct file' );
228    is( $href->{'version'}, $$, '   Found correct version' );
229    ok( $href->{'uptodate'},    '   Marked as uptodate' );
230    ok( can_load( modules => { 'A::B::C::D' => 0 } ),
231                                '   can_load successful' );
232}
233
234