xref: /openbsd-src/gnu/usr.bin/perl/cpan/Module-Load-Conditional/t/01_Module_Load_Conditional.t (revision de8cc8edbc71bd3e3bc7fbffa27ba0e564c37d8b)
1b39c5158Smillert### Module::Load::Conditional test suite ###
2b39c5158Smillert### this should no longer be needed
3b39c5158Smillert# BEGIN {
4b39c5158Smillert#     if( $ENV{PERL_CORE} ) {
5b39c5158Smillert#         chdir '../lib/Module/Load/Conditional'
6b39c5158Smillert#             if -d '../lib/Module/Load/Conditional';
7b39c5158Smillert#         unshift @INC, '../../../..';
8b39c5158Smillert#
9b39c5158Smillert#         ### fix perl location too
10b39c5158Smillert#         $^X = '../../../../../t/' . $^X;
11b39c5158Smillert#     }
12b39c5158Smillert# }
13b39c5158Smillert
14b39c5158SmillertBEGIN { use FindBin; }
15b39c5158SmillertBEGIN { chdir 't' if -d 't' }
16b39c5158Smillert
17b39c5158Smillertuse strict;
18b39c5158Smillertuse File::Spec ();
19b39c5158Smillertuse Test::More 'no_plan';
20b39c5158Smillert
21b39c5158Smillertuse constant ON_VMS     => $^O eq 'VMS';
22b39c5158Smillert
23b39c5158Smillertuse lib File::Spec->catdir($FindBin::Bin, qw[.. lib] );
24b39c5158Smillertuse lib File::Spec->catdir($FindBin::Bin, q[to_load] );
25b39c5158Smillert
26b39c5158Smillertuse_ok( 'Module::Load::Conditional' );
27b39c5158Smillert
28b39c5158Smillert### stupid stupid warnings ###
29b39c5158Smillert{   $Module::Load::Conditional::VERBOSE =
30b39c5158Smillert    $Module::Load::Conditional::VERBOSE = 0;
31b39c5158Smillert
32b39c5158Smillert    *can_load       = *Module::Load::Conditional::can_load
33b39c5158Smillert                    = *Module::Load::Conditional::can_load;
34b39c5158Smillert    *check_install  = *Module::Load::Conditional::check_install
35b39c5158Smillert                    = *Module::Load::Conditional::check_install;
36b39c5158Smillert    *requires       = *Module::Load::Conditional::requires
37b39c5158Smillert                    = *Module::Load::Conditional::requires;
38b39c5158Smillert}
39b39c5158Smillert
40b39c5158Smillert{
41b39c5158Smillert    my $rv = check_install(
42b39c5158Smillert                        module  => 'Module::Load::Conditional',
43b39c5158Smillert                        version => $Module::Load::Conditional::VERSION,
44b39c5158Smillert                );
45b39c5158Smillert
46b39c5158Smillert    ok( $rv->{uptodate},    q[Verify self] );
47b39c5158Smillert    is( $rv->{version}, $Module::Load::Conditional::VERSION,
48b39c5158Smillert                            q[  Found proper version] );
49b39c5158Smillert    ok( $rv->{dir},         q[  Found directory information] );
50b39c5158Smillert
51b39c5158Smillert    {   my $dir = File::Spec->canonpath( $rv->{dir} );
52b39c5158Smillert
53b39c5158Smillert        ### special rules apply on VMS, as always...
54b39c5158Smillert        if (ON_VMS) {
55b39c5158Smillert            ### Need path syntax for VMS compares.
56b39c5158Smillert            $dir = VMS::Filespec::pathify($dir);
57b39c5158Smillert            ### Remove the trailing VMS specific directory delimiter
58b39c5158Smillert            $dir =~ s/\]//;
59b39c5158Smillert        }
60b39c5158Smillert
61b39c5158Smillert        ### quote for Win32 paths, use | to avoid slash confusion
62b39c5158Smillert        my $dir_re = qr|^\Q$dir\E|i;
63b39c5158Smillert        like( File::Spec->canonpath( $rv->{file} ), $dir_re,
64b39c5158Smillert                            q[      Dir subset of file path] );
65b39c5158Smillert    }
66b39c5158Smillert
67b39c5158Smillert    ### break up the specification
68b39c5158Smillert    my @rv_path = do {
69b39c5158Smillert
70b39c5158Smillert        ### Use the UNIX specific method, as the VMS one currently
71b39c5158Smillert        ### converts the file spec back to VMS format.
72b39c5158Smillert        my $class = ON_VMS ? 'File::Spec::Unix' : 'File::Spec';
73b39c5158Smillert
74b39c5158Smillert        my($vol, $path, $file) = $class->splitpath( $rv->{'file'} );
75b39c5158Smillert
76b39c5158Smillert        my @path = ($vol, $class->splitdir( $path ), $file );
77b39c5158Smillert
78b39c5158Smillert        ### First element could be blank for some system types like VMS
79b39c5158Smillert        shift @path if $vol eq '';
80b39c5158Smillert
81b39c5158Smillert        ### and return it
82b39c5158Smillert        @path;
83b39c5158Smillert    };
84b39c5158Smillert    my $inc_path = $INC{'Module/Load/Conditional.pm'};
85b39c5158Smillert    if ( $^O eq 'MSWin32' ) {
86b39c5158Smillert        $inc_path = File::Spec->canonpath( $inc_path );
87b39c5158Smillert        $inc_path =~ s{\\}{/}g; # to meet with unix path
88b39c5158Smillert    }
89b39c5158Smillert    is( $inc_path,
90b39c5158Smillert            File::Spec::Unix->catfile(@rv_path),
91b39c5158Smillert                            q[  Found proper file]
92b39c5158Smillert    );
93b39c5158Smillert
94b39c5158Smillert
95b39c5158Smillert
96b39c5158Smillert}
97b39c5158Smillert
98b39c5158Smillert### the version may contain an _, which means perl will warn about 'not
99b39c5158Smillert### numeric' -- turn off that warning here.
100b39c5158Smillert{   local $^W;
101b39c5158Smillert    my $rv = check_install(
102b39c5158Smillert                        module  => 'Module::Load::Conditional',
103b39c5158Smillert                        version => $Module::Load::Conditional::VERSION + 1,
104b39c5158Smillert                );
105b39c5158Smillert
106b39c5158Smillert    ok( !$rv->{uptodate} && $rv->{version} && $rv->{file},
107b39c5158Smillert        q[Verify out of date module]
108b39c5158Smillert    );
109b39c5158Smillert}
110b39c5158Smillert
111b39c5158Smillert{
112b39c5158Smillert    my $rv = check_install( module  => 'Module::Load::Conditional' );
113b39c5158Smillert
114b39c5158Smillert    ok( $rv->{uptodate} && $rv->{version} && $rv->{file},
115b39c5158Smillert        q[Verify any module]
116b39c5158Smillert    );
117b39c5158Smillert}
118b39c5158Smillert
119b39c5158Smillert{
120b39c5158Smillert    my $rv = check_install( module  => 'Module::Does::Not::Exist' );
121b39c5158Smillert
122b39c5158Smillert    ok( !$rv->{uptodate} && !$rv->{version} && !$rv->{file},
123898184e3Ssthen        q[Verify non-existent module]
124b39c5158Smillert    );
125b39c5158Smillert
126b39c5158Smillert}
127b39c5158Smillert
128b39c5158Smillert### test finding a version of a module that mentions $VERSION in pod
129b39c5158Smillert{   my $rv = check_install( module => 'InPod' );
130b39c5158Smillert    ok( $rv,                        'Testing $VERSION in POD' );
131b39c5158Smillert    ok( $rv->{version},             "   Version found" );
132b39c5158Smillert    is( $rv->{version}, 2,          "   Version is correct" );
133b39c5158Smillert}
134b39c5158Smillert
135*de8cc8edSafresh1### test finding a version of a module that has a VERSION error in a HereDoc
136*de8cc8edSafresh1{   my $rv = check_install( module => 'HereDoc' );
137*de8cc8edSafresh1    ok( $rv,                        'Testing $VERSION in HEREDOC' );
138*de8cc8edSafresh1    ok( !$rv->{version},            "   No Version found" );
139*de8cc8edSafresh1    is( $rv->{version}, undef,      "   Version is correct" );
140*de8cc8edSafresh1}
141*de8cc8edSafresh1
14291f110e0Safresh1### test that no package statement means $VERSION is $main::VERSION
14391f110e0Safresh1{
14491f110e0Safresh1    my $rv = check_install( module => 'NotMain' );
14591f110e0Safresh1    ok( $rv,                   'Testing $VERSION without package' );
14691f110e0Safresh1    is( $rv->{version}, undef, "   No version info returned" );
14791f110e0Safresh1}
14891f110e0Safresh1
14991f110e0Safresh1### test that the right $VERSION is picked when there are several packages
15091f110e0Safresh1{
15191f110e0Safresh1    my $rv = check_install( module => 'NotX' );
15291f110e0Safresh1    ok( $rv,               'Testing $VERSION with many packages' );
15391f110e0Safresh1    ok( $rv->{version},    "   Version found" );
15491f110e0Safresh1    is( $rv->{version}, 3, "   Version is correct" );
15591f110e0Safresh1}
15691f110e0Safresh1
157b39c5158Smillert### test beta/developer release versions
158b39c5158Smillert{   my $test_ver = $Module::Load::Conditional::VERSION;
159b39c5158Smillert
160b39c5158Smillert    ### strip beta tags
161b39c5158Smillert    $test_ver =~ s/_\d+//g;
162b39c5158Smillert    $test_ver .= '_99';
163b39c5158Smillert
164b39c5158Smillert    my $rv = check_install(
165b39c5158Smillert                    module  => 'Module::Load::Conditional',
166b39c5158Smillert                    version => $test_ver,
167b39c5158Smillert                );
168b39c5158Smillert
169b39c5158Smillert    ok( $rv,                "Checking beta versions" );
170b39c5158Smillert    ok( !$rv->{'uptodate'}, "   Beta version is higher" );
171b39c5158Smillert
172b39c5158Smillert}
173b39c5158Smillert
174b39c5158Smillert### test $FIND_VERSION
17591f110e0Safresh1{
176b39c5158Smillert    local $Module::Load::Conditional::FIND_VERSION = 0;
177b39c5158Smillert
178b39c5158Smillert    my $rv = check_install( module  => 'Module::Load::Conditional' );
179b39c5158Smillert
180b39c5158Smillert    ok( $rv,                        'Testing $FIND_VERSION' );
181b39c5158Smillert    is( $rv->{version}, undef,      "   No version info returned" );
182b39c5158Smillert    ok( $rv->{uptodate},            "   Module marked as uptodate" );
183b39c5158Smillert}
184b39c5158Smillert
18591f110e0Safresh1### test that check_install() picks up the first match
18691f110e0Safresh1{
18791f110e0Safresh1    my ($dir_a, $dir_b) = map File::Spec->catdir($FindBin::Bin, 'test_lib', $_),
18891f110e0Safresh1                              qw[a b];
18991f110e0Safresh1    my $x_pm = File::Spec->catfile($dir_a, 'X.pm');
19091f110e0Safresh1    $x_pm = VMS::Filespec::unixify($x_pm) if ON_VMS;
19191f110e0Safresh1
19291f110e0Safresh1    local @INC = ($dir_a, $dir_b);
19391f110e0Safresh1
19491f110e0Safresh1    my $rv = check_install( module => 'X' );
19591f110e0Safresh1
19691f110e0Safresh1    ok( $rv,                    'Testing the file picked by check_install ($FIND_VERSION == 1)' );
19791f110e0Safresh1    is( $rv->{file},    $x_pm,  "   First file was picked" );
19891f110e0Safresh1    is( $rv->{version}, '0.01', "   Correct version for first file" );
19991f110e0Safresh1
20091f110e0Safresh1    local $Module::Load::Conditional::FIND_VERSION = 0;
20191f110e0Safresh1
20291f110e0Safresh1    $rv = check_install( module => 'X' );
20391f110e0Safresh1
20491f110e0Safresh1    ok( $rv,                    'Testing the file picked by check_install ($FIND_VERSION == 0)' );
20591f110e0Safresh1    is( $rv->{file},    $x_pm,  "   First file was also picked" );
20691f110e0Safresh1    is( $rv->{version}, undef,  "   But its VERSION was not required" );
20791f110e0Safresh1}
20891f110e0Safresh1
209b39c5158Smillert### test 'can_load' ###
210b39c5158Smillert
211b39c5158Smillert{
212b39c5158Smillert    my $use_list = { 'LoadIt' => 1 };
213b39c5158Smillert    my $bool = can_load( modules => $use_list );
214b39c5158Smillert
215b39c5158Smillert    ok( $bool, q[Load simple module] );
216b39c5158Smillert}
217b39c5158Smillert
218b39c5158Smillert{
219b39c5158Smillert    my $use_list = { 'Commented' => 2 };
220b39c5158Smillert    my $bool = can_load( modules => $use_list );
221b39c5158Smillert
222b39c5158Smillert    ok( $bool, q[Load module with a second, commented-out $VERSION] );
223b39c5158Smillert}
224b39c5158Smillert
225b39c5158Smillert{
226b39c5158Smillert    my $use_list = { 'MustBe::Loaded' => 1 };
227b39c5158Smillert    my $bool = can_load( modules => $use_list );
228b39c5158Smillert
229b39c5158Smillert    ok( !$bool, q[Detect out of date module] );
230b39c5158Smillert}
231b39c5158Smillert
232b39c5158Smillert{
233b39c5158Smillert    delete $INC{'LoadIt.pm'};
234b39c5158Smillert    delete $INC{'MustBe/Loaded.pm'};
235b39c5158Smillert
236b39c5158Smillert    my $use_list = { 'LoadIt' => 1, 'MustBe::Loaded' => 1 };
237b39c5158Smillert    my $bool = can_load( modules => $use_list );
238b39c5158Smillert
239b39c5158Smillert    ok( !$INC{'LoadIt.pm'} && !$INC{'MustBe/Loaded.pm'},
240b39c5158Smillert        q[Do not load if one prerequisite fails]
241b39c5158Smillert    );
242b39c5158Smillert}
243b39c5158Smillert
2446fb12b70Safresh1# test for autoload
2456fb12b70Safresh1
2466fb12b70Safresh1# autoload
2476fb12b70Safresh1{
2486fb12b70Safresh1    my $use_list = { 'AutoLoad' => 0 };
2496fb12b70Safresh1    my $bool = can_load( modules => $use_list, autoload => 1 );
2506fb12b70Safresh1    ok( $bool, q[autoloaded] );
2516fb12b70Safresh1
2526fb12b70Safresh1    eval { func1(); };
2536fb12b70Safresh1    is( $@, '', q[exported function] );
2546fb12b70Safresh1
2556fb12b70Safresh1    eval { func2(); };
2566fb12b70Safresh1    ok( $@, q[not exported function] );
2576fb12b70Safresh1}
2586fb12b70Safresh1
2596fb12b70Safresh1# not autoload
2606fb12b70Safresh1{
2616fb12b70Safresh1    my $use_list = { 'NotAutoLoad' => 0 };
2626fb12b70Safresh1    my $bool = can_load( modules => $use_list );
2636fb12b70Safresh1    ok( $bool, q[not autoloaded] );
2646fb12b70Safresh1
2656fb12b70Safresh1    eval { func3(); };
2666fb12b70Safresh1    ok( $@, q[not exported function - func3] );
2676fb12b70Safresh1
2686fb12b70Safresh1    eval { func4(); };
2696fb12b70Safresh1    ok( $@, q[not exported function - func4] );
2706fb12b70Safresh1}
2716fb12b70Safresh1
272b39c5158Smillert
273b39c5158Smillert### test 'requires' ###
274b39c5158SmillertSKIP:{
275b39c5158Smillert    skip "Depends on \$^X, which doesn't work well when testing the Perl core",
276b39c5158Smillert        1 if $ENV{PERL_CORE};
277b39c5158Smillert
278b39c5158Smillert    my %list = map { $_ => 1 } requires('Carp');
279b39c5158Smillert
280b39c5158Smillert    my $flag;
281b39c5158Smillert    $flag++ unless delete $list{'Exporter'};
282b39c5158Smillert
283b39c5158Smillert    ok( !$flag, q[Detecting requirements] );
284b39c5158Smillert}
285b39c5158Smillert
286b39c5158Smillert### test using the %INC lookup for check_install
287b39c5158Smillert{   local $Module::Load::Conditional::CHECK_INC_HASH = 1;
288b39c5158Smillert    local $Module::Load::Conditional::CHECK_INC_HASH = 1;
289b39c5158Smillert
290b39c5158Smillert    {   package A::B::C::D;
2916fb12b70Safresh1        $A::B::C::D::VERSION = "$$";
2926fb12b70Safresh1        $INC{'A/B/C/D.pm'}   = "$$"."$$";
293b39c5158Smillert
294b39c5158Smillert        ### XXX this is no longer needed with M::Load 0.11_01
295b39c5158Smillert        #$INC{'[.A.B.C]D.pm'} = $$.$$ if $^O eq 'VMS';
296b39c5158Smillert    }
297b39c5158Smillert
298b39c5158Smillert    my $href = check_install( module => 'A::B::C::D', version => 0 );
299b39c5158Smillert
300b39c5158Smillert    ok( $href,                  'Found package in %INC' );
3016fb12b70Safresh1    is( $href->{'file'}, "$$"."$$", '   Found correct file' );
3026fb12b70Safresh1    is( $href->{'version'}, "$$", '   Found correct version' );
303b39c5158Smillert    ok( $href->{'uptodate'},    '   Marked as uptodate' );
304b39c5158Smillert    ok( can_load( modules => { 'A::B::C::D' => 0 } ),
305b39c5158Smillert                                '   can_load successful' );
306b39c5158Smillert}
307b39c5158Smillert
308