| 1 | package Test::Without::Module; | ||||||||||||||||||||
| 2 | 7 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 7 | ▶ | use Carp qw( croak ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 7 | ▶ | use vars qw( $VERSION ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 7 | $VERSION = '0.23'; | |||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 7 | ▶ | use vars qw(%forbidden); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 178 | ▶ | sub get_forbidden_list { | ||||||||||||||||||
|
Subroutine
get_forbidden_list: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 11 | 178 | \%forbidden | |||||||||||||||||||
| 12 | }; | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 8 | ▶ | sub import { | ||||||||||||||||||
|
Subroutine
import: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 15 | 8 | my ($self,@forbidden_modules) = @_; | |||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 8 | my $forbidden = get_forbidden_list; | |||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 8 | for (@forbidden_modules) { | |||||||||||||||||||
| 20 | 5 | my $file = module2file($_); | |||||||||||||||||||
| 21 | 5 | $forbidden->{$file} = delete $INC{$file}; | |||||||||||||||||||
| 22 | }; | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | # Scrub %INC, so that loaded modules disappear | ||||||||||||||||||||
| 25 | 8 | for my $module (@forbidden_modules) { | |||||||||||||||||||
| 26 | 5 | scrub( $module ); | |||||||||||||||||||
| 27 | }; | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 8 | ▶ | @INC = (\&fake_module, grep { !ref || $_ != \&fake_module } @INC); | ||||||||||||||||||
|
Condition66%2/3
!ref($_) || $_ != \&Test::Without::Module::fake_module
MC/DC50%1/2
!ref($_) || $_ != \&Test::Without::Module::fake_module
!ref($_)
$_ != \&Test::Without::Module::fake_module
Truth table66%2/3
not(A) || B
A= !ref($_)B= $_ != \&Test::Without::Module::fake_module
| |||||||||||||||||||||
| 30 | }; | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 166 | ▶ | sub fake_module { | ||||||||||||||||||
|
Subroutine
fake_module: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 33 | 166 | my ($self,$module_file,$member_only) = @_; | |||||||||||||||||||
| 34 | # Don't touch $@, or .al files will not load anymore???? | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 166 | ▶ | if (exists get_forbidden_list->{$module_file}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 37 | 3 | my $module_name = file2module($module_file); | |||||||||||||||||||
| 38 | 3 | croak "Can't locate $module_file in \@INC (you may need to install the $module_name module) (\@INC contains: @INC)"; | |||||||||||||||||||
| 39 | }; | ||||||||||||||||||||
| 40 | }; | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 2 | ▶ | sub unimport { | ||||||||||||||||||
|
Subroutine
unimport: called CC=4 SCAR=14.0
| |||||||||||||||||||||
| 43 | 2 | my ($self,@list) = @_; | |||||||||||||||||||
| 44 | 2 | my $module; | |||||||||||||||||||
| 45 | 2 | my $forbidden = get_forbidden_list; | |||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 2 | for $module (@list) { | |||||||||||||||||||
| 48 | 2 | my $file = module2file($module); | |||||||||||||||||||
| 49 | 2 | ▶ | if (exists $forbidden->{$file}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 50 | 2 | my $path = delete $forbidden->{$file}; | |||||||||||||||||||
| 51 | 2 | ▶ | if (defined $path) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 52 | 1 | $INC{ $file } = $path; | |||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | } else { | ||||||||||||||||||||
| 55 | 0 | croak "Can't allow non-forbidden module $module"; | |||||||||||||||||||
| 56 | }; | ||||||||||||||||||||
| 57 | }; | ||||||||||||||||||||
| 58 | }; | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 3 | ▶ | sub file2module { | ||||||||||||||||||
|
Subroutine
file2module: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 61 | 3 | my ($mod) = @_; | |||||||||||||||||||
| 62 | 3 | $mod =~ s!/!::!g; | |||||||||||||||||||
| 63 | 3 | $mod =~ s!\.pm$!!; | |||||||||||||||||||
| 64 | 3 | $mod; | |||||||||||||||||||
| 65 | }; | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 12 | ▶ | sub module2file { | ||||||||||||||||||
|
Subroutine
module2file: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 68 | 12 | my ($mod) = @_; | |||||||||||||||||||
| 69 | 12 | $mod =~ s!::|'!/!g; | |||||||||||||||||||
| 70 | 12 | $mod .= ".pm"; | |||||||||||||||||||
| 71 | 12 | $mod; | |||||||||||||||||||
| 72 | }; | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 5 | ▶ | sub scrub { | ||||||||||||||||||
|
Subroutine
scrub: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 75 | 5 | my ($module) = @_; | |||||||||||||||||||
| 76 | 5 | delete $INC{module2file($module)}; | |||||||||||||||||||
| 77 | }; | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 7 | 1; | |||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | =head1 NAME | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | Test::Without::Module - Test fallback behaviour in absence of modules | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | =head1 SYNOPSIS | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | use Test::Without::Module qw( My::Module ); | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | # Now, loading of My::Module fails : | ||||||||||||||||||||
| 90 | eval { require My::Module; }; | ||||||||||||||||||||
| 91 | warn $@ if $@; | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | # Now it works again | ||||||||||||||||||||
| 94 | eval q{ no Test::Without::Module qw( My::Module ) }; | ||||||||||||||||||||
| 95 | eval { require My::Module; }; | ||||||||||||||||||||
| 96 | print "Found My::Module" unless $@; | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | =head1 DESCRIPTION | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | This module allows you to deliberately hide modules from a program | ||||||||||||||||||||
| 101 | even though they are installed. This is mostly useful for testing modules | ||||||||||||||||||||
| 102 | that have a fallback when a certain dependency module is not installed. | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | =head2 EXPORT | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | None. All magic is done via C<use Test::Without::Module LIST> and | ||||||||||||||||||||
| 107 | C<no Test::Without::Module LIST>. | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | =head2 Test::Without::Module::get_forbidden_list | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | This function returns a reference to a copy of the current hash of forbidden | ||||||||||||||||||||
| 112 | modules or an empty hash if none are currently forbidden. This is convenient | ||||||||||||||||||||
| 113 | if you are testing and/or debugging this module. | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | =cut | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | =head1 ONE LINER | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | A neat trick for using this module from the command line | ||||||||||||||||||||
| 120 | was mentioned to me by NUFFIN and by Jerrad Pierce: | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | perl -MTest::Without::Module=Some::Module -w -Iblib/lib t/SomeModule.t | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | That way, you can easily see how your module or test file behaves | ||||||||||||||||||||
| 125 | when a certain module is unavailable. | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | =head1 BUGS | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | =over 4 | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | =item * | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | There is no lexical scoping | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | =back | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | =head1 CREDITS | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | Much improvement must be thanked to Aristotle from PerlMonks, he pointed me | ||||||||||||||||||||
| 140 | to a much less convoluted way to fake a module at | ||||||||||||||||||||
| 141 | L<https://perlmonks.org?node=192635>. | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | I also discussed with him an even more elegant way of overriding | ||||||||||||||||||||
| 144 | CORE::GLOBAL::require, but the parsing of the overridden subroutine | ||||||||||||||||||||
| 145 | didn't work out the way I wanted it - CORE::require didn't recognize | ||||||||||||||||||||
| 146 | barewords as such anymore. | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | NUFFIN and Jerrad Pierce pointed out the convenient | ||||||||||||||||||||
| 149 | use from the command line to interactively watch the | ||||||||||||||||||||
| 150 | behaviour of the test suite and module in absence | ||||||||||||||||||||
| 151 | of a module. | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | =head1 AUTHOR | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | Copyright (c) 2003-2024 Max Maischein, E<lt>corion@cpan.orgE<gt> | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | =head1 LICENSE | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | This module is released under the same terms as Perl itself. | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | =head1 REPOSITORY | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | The public repository of this module is | ||||||||||||||||||||
| 164 | L<https://github.com/Corion/test-without-module>. | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | =head1 SUPPORT | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | The public support forum of this module is | ||||||||||||||||||||
| 169 | L<https://perlmonks.org/>. | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | =head1 BUG TRACKER | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | Please report bugs in this module via the RT CPAN bug queue at | ||||||||||||||||||||
| 174 | L<https://rt.cpan.org/Public/Dist/Display.html?Name=Test-Without-Module> | ||||||||||||||||||||
| 175 | or via mail to L<test-without-module-Bugs@rt.cpan.org>. | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | =head1 SEE ALSO | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | L<Devel::Hide>, L<Acme::Intraweb>, L<PAR>, L<perlfunc> | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | =cut | ||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | __END__ | ||||||||||||||||||||