File Coverage

blib/lib/Test2/Tools/LoadModule.pm
Criterion Covered Total %
statement 212 216 98.1
branch 65 72 90.2
condition 27 30 90.0
subroutine 45 45 100.0
pod 7 7 100.0
total 356 370 96.2


line stmt bran cond sub pod time code
1             package Test2::Tools::LoadModule;
2              
3 10     10   2578068 use 5.008001;
  10         46  
4              
5 10     10   70 use strict;
  10         28  
  10         350  
6 10     10   49 use warnings;
  10         33  
  10         722  
7              
8             # OK, the following is probably paranoia. But if Perl 7 decides to
9             # change this particular default I'm ready. Unless they eliminate $].
10 10     10   89 no if $] ge '5.020', feature => qw{ signatures };
  10         25  
  10         5670  
11              
12 10     10   74 use Carp;
  10         18  
  10         926  
13 10     10   67 use Exporter 5.567; # Comes with Perl 5.8.1.
  10         176  
  10         532  
14             # use File::Find ();
15             # use File::Spec ();
16             # use Getopt::Long 2.34; # Comes with Perl 5.8.1.
17 10     10   127 use Test2::API 1.302096 ();
  10         226  
  10         391  
18 10     10   80 use Test2::API::Context 1.302096 (); # for pass_and_release().
  10         245  
  10         321  
19 10     10   53 use Test2::Util 1.302096 ();
  10         252  
  10         372  
20              
21 10     10   56 use base qw{ Exporter };
  10         28  
  10         3630  
22              
23             our $VERSION = '0.009';
24             $VERSION =~ s/ _ //smxg;
25              
26             {
27             my @test2 = qw{
28             all_modules_tried_ok
29             clear_modules_tried
30             load_module_ok
31             load_module_or_skip
32             load_module_or_skip_all
33             };
34              
35             my @more = qw{
36             require_ok
37             use_ok
38             };
39              
40             my @private = qw{
41             __build_load_eval
42             __get_hint_hash
43             DEFAULT_LOAD_ERROR
44             ERR_IMPORT_BAD
45             ERR_MODULE_UNDEF
46             ERR_OPTION_BAD
47             ERR_SKIP_NUM_BAD
48             ERR_VERSION_BAD
49             HINTS_AVAILABLE
50             TEST_MORE_ERROR_CONTEXT
51             TEST_MORE_LOAD_ERROR
52             };
53              
54             our @EXPORT_OK = ( @test2, @more, @private );
55              
56             our %EXPORT_TAGS = (
57             all => [ @test2, @more ],
58             default => \@test2,
59             more => \@more,
60             private => \@private,
61             test2 => \@test2,
62             );
63              
64             our @EXPORT = @{ $EXPORT_TAGS{default} }; ## no critic (ProhibitAutomaticExportation)
65             }
66              
67 10     10   76 use constant ARRAY_REF => ref [];
  10         48  
  10         843  
68 10     10   80 use constant HASH_REF => ref {};
  10         18  
  10         634  
69              
70 10     10   55 use constant CALLER_HINT_HASH => 10;
  10         20  
  10         656  
71              
72 10     10   78 use constant DEFAULT_LOAD_ERROR => '%s';
  10         82  
  10         667  
73              
74 10         589 use constant ERR_IMPORT_BAD =>
75 10     10   82 'Import list must be an array reference, or undef';
  10         77  
76 10     10   71 use constant ERR_MODULE_UNDEF => 'Module name must be defined';
  10         28  
  10         588  
77 10     10   54 use constant ERR_OPTION_BAD => 'Bad option';
  10         45  
  10         675  
78 10         501 use constant ERR_SKIP_NUM_BAD =>
79 10     10   57 'Number of skipped tests must be an unsigned integer';
  10         18  
80 10     10   51 use constant ERR_VERSION_BAD => q/Version '%s' is invalid/;
  10         18  
  10         603  
81              
82 10     10   64 use constant HINTS_AVAILABLE => $] ge '5.010';
  10         16  
  10         1805  
83              
84             # The following cribbed shamelessly from version::regex 0.9924,
85             # after being munged to suit by tools/version_regex 0.000_010.
86             # This technical debt is incurred to avoid having to require a version
87             # of the version module large enough to export the is_lax() subroutine.
88 10         697 use constant LAX_VERSION => qr/(?x: (?x:
89             v (?-x:[0-9]+) (?-x: (?-x:\.[0-9]+)+ (?-x:_[0-9]+)? )?
90             |
91             (?-x:[0-9]+)? (?-x:\.[0-9]+){2,} (?-x:_[0-9]+)?
92             ) | (?x: (?-x:[0-9]+) (?-x: (?-x:\.[0-9]+) | \. )? (?-x:_[0-9]+)?
93             |
94             (?-x:\.[0-9]+) (?-x:_[0-9]+)?
95 10     10   86 ) )/;
  10         20  
96              
97 10     10   57 use constant TEST_MORE_ERROR_CONTEXT => q/Tried to %s '%s'./;
  10         26  
  10         614  
98 10     10   58 use constant TEST_MORE_LOAD_ERROR => 'Error: %s';
  10         17  
  10         648  
99 10         11593 use constant TEST_MORE_OPT => {
100             load_error => TEST_MORE_LOAD_ERROR,
101             require => 1,
102 10     10   84 };
  10         28  
103              
104             {
105             my %module_tried;
106              
107             sub load_module_ok (@) { ## no critic (RequireArgUnpacking)
108 16     16 1 336268 my @arg = _validate_args( 0, @_ );
109              
110             # We do this now in case _load_module_ok() throws an uncaught
111             # exception, just so we have SOME record we tried.
112 12         58 $module_tried{ $arg[1] } = undef;
113              
114 12         46 my $ctx = Test2::API::context();
115              
116 12         1506 my $rslt = _load_module_ok( @arg );
117              
118 12         2428 $module_tried{ $arg[1] } = $rslt;
119              
120 12         49 $ctx->release();
121              
122 12         529 return $rslt;
123             }
124              
125             sub all_modules_tried_ok (@) {
126 3     3 1 12287 my @where = @_;
127             @where
128 3 50       21 or @where = ( 'blib/lib', 'blib/arch' );
129              
130 3         36 require File::Find;
131 3         12 require File::Spec;
132              
133 3         6 my @not_tried;
134 3         9 foreach my $d ( @where ) {
135             File::Find::find( sub {
136 15 100   15   1177 m/ [.] pm \z /smx
137             or return;
138 3         406 my ( undef, $dir, $name ) = File::Spec->splitpath(
139             File::Spec->abs2rel( $File::Find::name, $d ) );
140 3         19 my @dir = File::Spec->splitdir( $dir );
141 3 50       12 $dir[-1]
142             or pop @dir;
143 3         21 ( my $module = join '::', @dir, $name ) =~ s/ [.] pm //smx;
144 3 100       121 exists $module_tried{$module}
145             or push @not_tried, $module;
146 6         530 }, $d );
147             }
148              
149 3 100       12 if ( @not_tried ) {
150              
151 2         10 my $ctx = Test2::API::context();
152              
153 2         219 $ctx->fail( "Module $_ not tried" ) for sort @not_tried;
154              
155 2         461 $ctx->release();
156              
157 2         71 return 0;
158             }
159             }
160              
161             sub clear_modules_tried () {
162 1     1 1 777 %module_tried = ();
163 1         3 return;
164             }
165             }
166              
167             sub _load_module_ok {
168 21     21   66 my ( $opt, $module, $version, $import, $name, @diag ) = @_;
169              
170 21         39 local $@ = undef;
171              
172 21         85 my $eval = __build_load_eval( $opt, $module, $version, $import );
173              
174 21 100       116 defined $name
175             or $name = $eval;
176              
177 21         61 my $ctx = Test2::API::context();
178              
179 21 100       1670 _eval_in_pkg( $eval, $ctx->trace()->call() )
180             and return $ctx->pass_and_release( $name );
181              
182 10         36 chomp $@;
183              
184             $opt->{load_error}
185 10 100       90 and push @diag, sprintf $opt->{load_error}, $@;
186              
187 10         54 return $ctx->fail_and_release( $name, @diag );
188             }
189              
190             sub load_module_or_skip (@) { ## no critic (RequireArgUnpacking,RequireFinalReturn)
191 11     11 1 30493 my ( $opt, $module, $version, $import, $name, $num ) = _validate_args( 5, @_ );
192              
193 6 100       27 _load_module( $opt, $module, $version, $import )
194             and return;
195              
196 4 50       37 defined $name
197             or $name = sprintf 'Unable to %s',
198             __build_load_eval( $opt, $module, $version, $import );
199 4 100 100     186 defined $num
200             and $num =~ m/ [^0-9] /smx
201             and croak ERR_SKIP_NUM_BAD;
202              
203 3         9 my $ctx = Test2::API::context();
204 3   100     275 $num ||= 1;
205 3         21 $ctx->skip( 'skipped test', $name ) for 1 .. $num;
206              
207 3         1012 $ctx->release();
208 10     10   116 no warnings qw{ exiting };
  10         19  
  10         24923  
209 3         97 last SKIP;
210             }
211              
212             sub load_module_or_skip_all (@) { ## no critic (RequireArgUnpacking)
213 10     10 1 21886 my ( $opt, $module, $version, $import, $name ) = _validate_args( 4, @_ );
214              
215 5 100       15 _load_module( $opt, $module, $version, $import )
216             and return;
217              
218 3 50       13 defined $name
219             or $name = sprintf 'Unable to %s',
220             __build_load_eval( $opt, $module, $version, $import );
221              
222 3         9 my $ctx = Test2::API::context();
223 3         262 $ctx->plan( 0, SKIP => $name );
224 0         0 $ctx->release();
225              
226 0         0 return;
227             }
228              
229             sub _load_module {
230 11     11   31 my ( $opt, $module, $version, $import ) = @_;
231              
232 11         20 local $@ = undef;
233              
234 11         53 my $eval = __build_load_eval( $opt, $module, $version, $import );
235              
236 11         33 return _eval_in_pkg( $eval, _get_call_info() )
237             }
238              
239             {
240             my $psr;
241              
242             # Because we want to work with Perl 5.8.1 we are limited to
243             # Getopt::Long 2.34, and therefore getoptions(). So we expect the
244             # arguments to be in a suitably-localized @ARGV. The optional
245             # argument is a reference to a hash into which we place the option
246             # values. If omitted, we create a reference to a new hash. Either
247             # way the hash reference gets returned.
248             sub _parse_opts {
249 50     50   108 my ( $opt ) = @_;
250 50   50     140 $opt ||= {};
251             {
252 50 100       84 unless ( $psr ) {
  50         163  
253 9         8765 require Getopt::Long;
254 9         130926 Getopt::Long->VERSION( 2.34 );
255 9         264 $psr = Getopt::Long::Parser->new();
256 9         11910 $psr->configure( qw{ posix_default } );
257             }
258              
259 50         879 my $opt_err;
260 50     3   384 local $SIG{__WARN__} = sub { $opt_err = $_[0] };
  3         1174  
261             $psr->getoptions( $opt, qw{
262             load_error=s
263             require|req!
264             },
265 50 100       300 ) or do {
266 3 50       237 if ( defined $opt_err ) {
267 3         8 chomp $opt_err;
268 3         1317 croak $opt_err;
269             } else {
270 0         0 croak ERR_OPTION_BAD;
271             }
272             };
273             }
274 47 100       21375 if ( $opt->{load_error} ) {
275             $opt->{load_error} =~ m/ ( %+ ) [ #0+-]* [0-9]* s /smx
276             and length( $1 ) % 2
277 34 100 66     397 or $opt->{load_error} = '%s';
278             }
279 47         156 return $opt;
280             }
281             }
282              
283             sub import { ## no critic (RequireArgUnpacking,ProhibitBuiltinHomonyms)
284 18     18   14230 ( my $class, local @ARGV ) = @_; # See _parse_opts
285 18 100       81 if ( @ARGV ) {
286 13         26 my %opt;
287 13         44 _parse_opts( \%opt );
288 13         27 if ( HINTS_AVAILABLE ) {
289 13         48 $^H{ _make_pragma_key() } = $opt{$_} for keys %opt;
290             } else {
291             keys %opt
292             and carp "Import options ignored under Perl $]";
293             }
294             @ARGV
295 13 100       5553 or return;
296             }
297 14         6563 return $class->export_to_level( 1, $class, @ARGV );
298             }
299              
300             sub require_ok ($) {
301 4     4 1 33020 my ( $module ) = @_;
302 4 100       172 defined $module
303             or croak ERR_MODULE_UNDEF;
304 3         10 my $ctx = Test2::API::context();
305 3         361 my $rslt = _load_module_ok( TEST_MORE_OPT,
306             $module, undef, undef, "require $module;",
307             sprintf( TEST_MORE_ERROR_CONTEXT, require => $module ),
308             );
309 3         824 $ctx->release();
310 3         172 return $rslt;
311             }
312              
313             sub use_ok ($;@) {
314 7     7 1 19461 my ( $module, @arg ) = @_;
315 7 100       136 defined $module
316             or croak ERR_MODULE_UNDEF;
317 6 100 100     50 my $version = ( defined $arg[0] && $arg[0] =~ LAX_VERSION ) ?
318             shift @arg : undef;
319 6         13 my $ctx = Test2::API::context();
320 6         366 my $rslt = _load_module_ok( TEST_MORE_OPT,
321             $module, $version, \@arg, undef,
322             sprintf( TEST_MORE_ERROR_CONTEXT, use => $module ),
323             );
324 6         903 $ctx->release();
325 6         186 return $rslt;
326             }
327              
328             sub _make_pragma_key {
329 4     4   24 return join '', __PACKAGE__, '/', $_;
330             }
331              
332             sub _caller_class {
333 4     4   11 my ( $lvl ) = @_;
334 4   50     31 my ( $pkg ) = caller( $lvl || 1 );
335 4 100       558 my $code = $pkg->can( 'CLASS' )
336             or croak ERR_MODULE_UNDEF;
337 1         5 return $code->();
338             }
339              
340             {
341              
342             my %default_hint = (
343             load_error => DEFAULT_LOAD_ERROR,
344             );
345              
346             sub __get_hint_hash {
347 42     42   241264 my ( $level ) = @_;
348 42   100     170 $level ||= 0;
349 42         259 my $hint_hash = ( caller( $level ) )[ CALLER_HINT_HASH ];
350 42         199 my %rslt = %default_hint;
351 42         82 if ( HINTS_AVAILABLE ) {
352 42         70 foreach ( keys %{ $hint_hash } ) {
  42         149  
353 4         45 my ( $hint_pkg, $hint_key ) = split qr< / >smx;
354             __PACKAGE__ eq $hint_pkg
355 4 50       56 and $rslt{$hint_key} = $hint_hash->{$_};
356             }
357             }
358 42         214 return \%rslt;
359             }
360             }
361              
362             sub __build_load_eval {
363 73     73   297616 my @arg = @_;
364 73 100       275 HASH_REF eq ref $arg[0]
365             or unshift @arg, {};
366 73         222 my ( $opt, $module, $version, $import ) = @arg;
367 73         183 my @eval = "use $module";
368              
369 73 100       194 defined $version
370             and push @eval, $version;
371              
372 73 100 100     380 if ( $import && @{ $import } ) {
  29 100 100     136  
373 19         36 push @eval, "qw{ @{ $import } }";
  19         57  
374             } elsif ( defined $import xor not $opt->{require} ) {
375             # Do nothing.
376             } else {
377 12         46 push @eval, '()';
378             }
379              
380 73         508 return "@eval;";
381             }
382              
383             sub _validate_args {
384 37     37   163 ( my $max_arg, local @ARGV ) = @_;
385 37         113 my $opt = _parse_opts( __get_hint_hash( 2 ) );
386              
387 34 100 100     163 if ( $max_arg && @ARGV > $max_arg ) {
388 2         22 ( my $sub_name = ( caller 1 )[3] ) =~ s/ .* :: //smx;
389 2         337 croak sprintf '%s() takes at most %d arguments', $sub_name, $max_arg;
390             }
391              
392 32         108 my ( $module, $version, $import, $name, @diag ) = @ARGV;
393              
394 32 100       133 defined $module
395             or $module = _caller_class( 2 );
396              
397 29 100       80 if ( defined $version ) {
398 7 100       584 $version =~ LAX_VERSION
399             or croak sprintf ERR_VERSION_BAD, $version;
400             }
401              
402 26 100 100     614 not defined $import
403             or ARRAY_REF eq ref $import
404             or croak ERR_IMPORT_BAD;
405              
406 23         116 return ( $opt, $module, $version, $import, $name, @diag );
407             }
408              
409             sub _eval_in_pkg {
410 32     32   305 my ( $eval, $pkg, $file, $line ) = @_;
411              
412 32         101 my $e = <<"EOD";
413             package $pkg;
414             #line $line "$file"
415             $eval;
416             1;
417             EOD
418              
419             # We need the stringy eval() so we can mess with Perl's concept of
420             # what the current file and line number are for the purpose of
421             # formatting the exception, AND as a convenience to get symbols
422             # imported.
423 32         2780 my $rslt = eval $e; ## no critic (ProhibitStringyEval)
424              
425 32         6438 return $rslt;
426             }
427              
428             sub _get_call_info {
429 11     11   47 my $lvl = 0;
430 11         139 while ( my @info = caller $lvl++ ) {
431 33 100       177 __PACKAGE__ eq $info[0]
432             and next;
433 11 50       86 $info[1] =~ m/ \A [(] eval \b /smx # )
434             or return @info;
435             }
436 0           confess 'Bug - Unable to determine caller';
437             }
438              
439             1;
440              
441             __END__
442              
443             =head1 NAME
444              
445             Test2::Tools::LoadModule - Test whether a module can be successfully loaded.
446              
447             =head1 SYNOPSIS
448              
449             use Test2::V0;
450             use Test2::Tools::LoadModule;
451            
452             load_module_ok 'My::Module';
453            
454             done_testing();
455              
456             =head1 DESCRIPTION
457              
458             This L<Test2::Tools|Test2::Tools> module tests whether a module can be
459             loaded, and optionally whether it has at least a given version, and
460             exports specified symbols. It can also skip tests, or skip all tests,
461             based on these criteria.
462              
463             L<Test2::Manual::Testing::Migrating|Test2::Manual::Testing::Migrating>
464             deals with migrating from L<Test::More|Test::More> to
465             L<Test2::V0|Test2::V0>. It states that instead of C<require_ok()> you
466             should simply use the C<require()> built-in, since a failure to load the
467             required module or file will cause the test script to fail anyway. The
468             same is said for C<use_ok()>.
469              
470             In my perhaps-not-so-humble opinion this overlooks the fact that if you
471             can not load the module you are testing, it may make sense to abort not
472             just the individual test script but the entire test run. Put another
473             way, the absence of an analogue to L<Test::More|Test::More>'s
474             C<require_ok()> means there is no analogue to
475              
476             require_ok( 'My::Module' ) or BAIL_OUT();
477              
478             This module restores that functionality.
479              
480             B<Note> that if you are using this module with testing tools that are
481             not based on L<Test2::V0|Test2::V0> you may have to tweak the load order
482             of modules. I ran into this in the early phases of implementation, and
483             fixed it for my own use by initializing the testing system as late as
484             possible, but I can not promise that all such problems have been
485             eliminated.
486              
487             =head1 CAVEAT
488              
489             Accurately testing whether a module can be loaded is more complicated
490             than it might first appear. One known issue is that you can get a false
491             pass if the module under test forgets to load a module it needs, but
492             this module loads it for its own use.
493              
494             Ideally this module would use nothing that C<Test2> does not, but that
495             seems to require a fair amount of wheel-reinventing. What this module
496             does try to do is to load the extra modules only if it really needs
497             them. Specifically:
498              
499             L<Getopt::Long|Getopt::Long> is loaded only if arguments are passed to
500             the C<use Test::Tools::LoadModule> statement.
501              
502             L<File::Find|File::Find> and L<File::Spec|File::Spec> are loaded only if
503             L<all_modules_tried_ok()|/all_modules_tried_ok> is called.
504              
505             Because L<Carp|Carp> and L<Exporter|Exporter> are used by
506             L<Test2::API|Test2::API> (at least as of version C<1.302181>), this
507             module makes no attempt to avoid their use.
508              
509             =head1 SUBROUTINES
510              
511             All subroutines documented below are exportable, either by name or using
512             one of the following tags:
513              
514             =over
515              
516             =item :all exports all public exports;
517              
518             =item :default exports the default exports (i.e. :test2);
519              
520             =item :more exports require_ok() and use_ok();
521              
522             =item :test2 exports load_module_*(), and is the default.
523              
524             =back
525              
526             =head2 load_module_ok
527              
528             load_module_ok $module, $ver, $import, $name, @diag;
529              
530             Prototype: C<(@)>.
531              
532             This subroutine tests whether the specified module (B<not> file) can be
533             loaded. All arguments are optional. The arguments are:
534              
535             =over
536              
537             =item $module - the module name
538              
539             This is the name of the module to be loaded. If unspecified or specified
540             as C<undef>, it defaults to the caller's C<CLASS> if that exists;
541             otherwise an exception is thrown.
542              
543             =item $ver - the desired version number, or undef
544              
545             If defined, the test fails if the installed module is not at least this
546             version. An exception is thrown if the version number is invalid.
547              
548             If C<undef>, no version check is done.
549              
550             =item $import - the import list as an array ref, or undef
551              
552             This argument specifies the import list. C<undef> means to import the
553             default symbols, C<[]> means not to import anything, and a non-empty
554             array reference means to import the specified symbols.
555              
556             =item $name - the test name, or undef
557              
558             If C<undef>, the name defaults to the code used to load the module.
559             B<Note> that this code, and therefore the default name, may change
560             without notice.
561              
562             =item @diag - the desired diagnostics
563              
564             Diagnostics are only issued on failure.
565              
566             =back
567              
568             Argument validation failures are signalled by C<croak()>.
569              
570             The module is loaded, and version checks and imports are done if
571             specified. The test passes if all these succeed, and fails otherwise.
572              
573             B<Note> that any imports from the loaded module take place when this
574             subroutine is called, which is normally at run time. Imported
575             subroutines will be callable, provided you do not make use of prototypes
576             or attributes.
577              
578             If you want anything imported from the loaded module to be available for
579             subsequent compilation (e.g. variables, subroutine prototypes) you will
580             need to put the call to this subroutine in a C<BEGIN { }> block:
581              
582             BEGIN { load_module_ok 'My::Module'; }
583              
584             By default, C<$@> is appended to the diagnostics issued in the event of
585             a load failure. If you want to omit this, or embed the value in your own
586             text, see L<CONFIGURATION|/CONFIGURATION>, below.
587              
588             As a side effect, the names of all modules tried with this test are
589             recorded, along with test results (pass/fail) for the use of
590             L<all_modules_tried_ok()|/all_modules_tried_ok>.
591              
592             =head2 load_module_or_skip
593              
594             load_module_or_skip $module, $ver, $import, $name, $num;
595              
596             Prototype: C<(@)>.
597              
598             This subroutine performs the same loading actions as
599             L<load_module_ok()|/load_module_ok>, but no tests are performed.
600             Instead, the specified number of tests is skipped if the load fails.
601              
602             The arguments are the same as L<load_module_ok()|/load_module_ok>,
603             except that the fifth argument (C<$num> in the example) is the number of
604             tests to skip, defaulting to C<1>.
605              
606             The C<$name> argument gives the skip message, and defaults to
607             C<"Unable to ..."> where the ellipsis is the code used to load the
608             module.
609              
610             =head2 load_module_or_skip_all
611              
612             load_module_or_skip_all $module, $ver, $import, $name;
613              
614             Prototype: C<(@)>.
615              
616             This subroutine performs the same loading actions as
617             L<load_module_ok()|/load_module_ok>, but no tests are performed.
618             Instead, all tests are skipped if any part of the load fails.
619              
620             The arguments are the same as L<load_module_ok()|/load_module_ok>,
621             except for the fact that diagnostics are not specified.
622              
623             The C<$name> argument gives the skip message, and defaults to
624             C<"Unable to ..."> where the ellipsis is the code used to load the
625             module.
626              
627             This subroutine can be called either at the top level or in a subtest,
628             but either way it B<must> be called before any actual tests in the file
629             or subtest.
630              
631             =head2 all_modules_tried_ok
632              
633             all_modules_tried_ok
634              
635             Added in version C<0.002>.
636              
637             Prototype: C<(@)>.
638              
639             This test traverses any directories specified as arguments looking for
640             Perl modules (defined as files whose names end in F<.pm>). A failing
641             test is generated for any such file not previously tested using
642             L<load_module_ok()|/load_module_ok>.
643              
644             If no directory is specified, the default is
645             C< ( '/blib/lib', '/blib/arch' ) >.
646              
647             B<NOTE> that no attempt is made to parse the file and determine its
648             module name. The module is assumed from the part of the file path below
649             the specified directory. So an explicit specification of F<lib/> will
650             work (if that is where the modules are stored) but an explicit F<blib/>
651             will not.
652              
653             =head2 clear_modules_tried
654              
655             clear_modules_tried
656              
657             Added in version C<0.002>.
658              
659             Prototype: C<()>.
660              
661             This is not a test. It clears the record of modules tried by
662             L<load_module_ok()|/load_module_ok>.
663              
664             =head2 require_ok
665              
666             require_ok $module;
667              
668             Prototype: C<($)>.
669              
670             This subroutine is more or less the same as the L<Test::More|Test::More>
671             subroutine of the same name. The argument is the name of the module to
672             load.
673              
674             =head2 use_ok
675              
676             use_ok $module, @imports;
677             use_ok $module, $version, @imports;
678              
679             Prototype: C<($;@)>.
680              
681             This subroutine is more or less the same as the L<Test::More|Test::More>
682             subroutine of the same name. The arguments are the name of the module to
683             load, and optional version (recognized by the equivalent of
684             C<version::is_lax()>, and optional imports.
685              
686             =head1 CONFIGURATION
687              
688             The action of the C<load_module_*()> subroutines is configurable using
689             POSIX-style options. If used as subroutine arguments they apply only to
690             that subroutine call. If used as arguments to C<use()>, they apply to
691             everything in the scope of the C<use()>, though this requires Perl 5.10
692             or above, and you must specify any desired imports.
693              
694             These options are parsed by L<Getopt::Long|Getopt::Long> (q.v.) in POSIX
695             mode, so they must appear before non-option arguments. They are all
696             documented double-dashed. A single leading dash is tolerated except in
697             the form C<--option=argument>, where the double dash is required.
698              
699             The following configuration options are available.
700              
701             =head2 --require
702              
703             If asserted, this possibly-badly-named Boolean option specifies that an
704             C<undef> or unspecified import list imports nothing, while C<[]> does
705             the default import.
706              
707             The default is C<-norequire>, which is the other way around. This is the
708             way C<use()> works, which is what inspired the name of the option.
709              
710             =head2 --req
711              
712             This is just a shorter synonym for L<--require|/--require>.
713              
714             =head2 --load_error
715              
716             --load_error 'Error: %s'
717              
718             This option specifies the formatting of the load error for those
719             subroutines that append it to the diagnostics. The value is interpreted
720             as follows:
721              
722             =over
723              
724             =item A string containing C<'%s'>
725              
726             or anything that looks like an C<sprintf()> string substitution is
727             interpreted verbatim as the L<sprintf> format to use to format the
728             error;
729              
730             =item Any other true value (e.g. C<1>)
731              
732             specifies the default, C<'%s'>;
733              
734             =item Any false value (e.g. C<0>)
735              
736             specifies that C<$@> should not be appended to the diagnostics at all.
737              
738             =back
739              
740             For example, if you want your diagnostics to look like the
741             L<Test::More|Test::More> C<require_ok()> diagnostics, you can do
742             something like this (at least under Perl 5.10 or above):
743              
744             { # Begin scope
745             use Test2::Tools::LoadModule -load_error => 'Error: %s';
746             load_module_ok $my_module, undef, undef,
747             "require $my_module;", "Tried to require '$my_module'.";
748             ...
749             }
750             # -load_error reverts to whatever it was before.
751              
752             If you want your code to work under Perl 5.8, you can equivalently do
753              
754             load_module_ok -load_error => 'Error: %s',
755             $my_module, undef, undef, "require $my_module;"
756             "Tried to require '$my_module'.";
757              
758             B<Note> that, while you can specify options on your initial load,
759             if you do so you must specify your desired imports explicitly, as (e.g.)
760              
761             use Test2::Tools::LoadModule
762             -load_error => 'Bummer! %s', ':default';
763              
764             =head1 SEE ALSO
765              
766             L<Test::More|Test::More>
767              
768             L<Test2::V0|Test2::V0>
769              
770             L<Test2::Require|Test2::Require>, especially
771             L<Test2::Require::Module|Test2::Require::Module>.
772              
773             L<Test2::Manual::Testing::Migrating|Test2::Manual::Testing::Migrating>
774              
775             L<Test2::Plugin::BailOnFail|Test2::Plugin::BailOnFail>
776              
777             =head1 SUPPORT
778              
779             Support is by the author. Please file bug reports at
780             L<https://rt.cpan.org/Public/Dist/Display.html?Name=Test2-Tools-LoadModule>,
781             L<https://github.com/trwyant/perl-Test2-Tools-LoadModule/issues>, or in
782             electronic mail to the author.
783              
784             =head1 AUTHOR
785              
786             Thomas R. Wyant, III F<wyant at cpan dot org>
787              
788             =head1 COPYRIGHT AND LICENSE
789              
790             Copyright (C) 2019-2026 by Thomas R. Wyant, III
791              
792             This program is free software; you can redistribute it and/or modify it
793             under the same terms as Perl 5.10.0. For more details, see the full text
794             of the licenses in the files F<LICENSE-Artistic> and F<LICENSE-GPL>.
795              
796             This program is distributed in the hope that it will be useful, but
797             without any warranty; without even the implied warranty of
798             merchantability or fitness for a particular purpose.
799              
800             =cut
801              
802             # ex: set textwidth=72 :