blib/lib/Module/Mask.pm

filter: Statementstmt 100% 63 / 63 Branchbran 100% 10 / 10 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 19 / 19 Podpod 100% 8 / 8 totaltotal 100% 110 / 110 CC 9 SCAR 5.5
CC
9
Cov
100%
mask_modules
3.0
_is_listed
3.0
_path
2.0
CRAP 1.7
↑ Summary
1 package Module::Mask;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 our $VERSION = '0.06';
7  
8 =head1 NAME
9  
10 Module::Mask - Pretend certain modules are not installed
11  
12 =head1 SYNOPSIS
13  
14 use Module::Mask;
15  
16 {
17 my $mask = new Module::Mask ('My::Module');
18 eval { require My::Module };
19 if ($@) {
20 # ... should be called
21 }
22 else {
23 warn "require succeeded unexpectedly"
24 }
25 }
26
27 # The mask is out of scope, this should now work.
28 eval { require My::Module };
29  
30 # There's also an inverted version:
31 {
32 my $mask = new Module::Mask::Inverted qw( Foo Bar );
33  
34 # Now only Foo and Bar can be loaded by require:
35 eval {require Baz};
36 }
37  
38 =head1 DESCRIPTION
39  
40 Sometimes you need to test what happens when a given module is not installed.
41 This module provides a way of temporarily hiding installed modules from perl's
42 require mechanism. The Module::Mask object adds itself to @INC and blocks
43 require calls to restricted modules.
44  
45 Module::Mask will not affect modules already loaded at time of instantiation.
46  
47 =cut
48  
49 4 ▶use Module::Util qw( module_path );
Subroutine BEGIN: called CC=1 SCAR=0.0
50 4 ▶use Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
51 4 ▶use Carp qw( shortmess );
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 # Don't want this to be loaded inside INC by calling shortmess
54 4 require Carp::Heavy;
55  
56 =head1 METHODS
57  
58 =head2 import
59  
60 use Module::Mask @modules;
61  
62 $class->import(@modules);
63  
64 Globally masks modules. This can be used to block optional modules for testing
65 purposes.
66  
67 perl -MModule::Mask=MyOptionalModule my_test.pl
68  
69 =cut
70  
71 3 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
72 3 my $class = shift;
73 3 our $Mask = $class->new(@_);
74 }
75  
76 =head2 new
77  
78 $obj = $class->new(@modules);
79  
80 Returns a new instance of this class. Any arguments are passed to mask_modules.
81  
82 =cut
83  
84 7 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
85 7 my $class = shift;
86 7 my $self = bless {}, $class;
87  
88 7 $self->mask_modules(@_);
89 7 $self->set_mask;
90  
91 7 return $self;
92 }
93  
94 4 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
95 4 my $self = shift;
96  
97 4 $self->clear_mask();
98 }
99  
100 =head2 mask_modules
101  
102 $obj = $obj->mask_modules(@modules)
103  
104 Add the given modules to the mask. Arguments can be paths or module names,
105 module names will be stored internally as relative paths. So there's no
106 difference between the following statements:
107  
108 $mask->mask_modules('My::Module');
109 $mask->mask_modules('My/Module.pm');
110  
111 =cut
112  
113 9 ▶sub mask_modules {
Subroutine mask_modules: called CC=3 SCAR=11.0
Pod documented
114 9 my ($self, @modules) = @_;
115  
116 9 for my $module (@modules) {
117 7 ▶ next unless defined $module;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $module 1 6 7
118 6 $self->_mask_module($module);
119 }
120  
121 9 return $self;
122 }
123  
124 87 ▶sub _path {
Subroutine _path: called CC=2 SCAR=6.9
125 87 my ($self, $module) = @_;
126  
127 87 ▶ return module_path $module || $module;
Condition100%3/3
&module_path($module) || $module
l!l&&r!l&&!r
6792
MC/DC100%2/2
&module_path($module) || $module
&module_path($module) $module
Truth table100%3/3
A || B
A= &module_path($module)B= $module
ABresult
1X1
011
000
128 }
129  
130 # internal method to allow overriding
131 6 ▶sub _mask_module {
Subroutine _mask_module: called CC=2 SCAR=6.9
132 6 my ($self, $module) = @_;
133 6 ▶ my $path = $self->_path($module) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $path = $self->_path($module) or return 5 1 6
134  
135 5 $self->{$path} = 1;
136 }
137  
138 =head2 clear_mask
139  
140 $obj = $obj->clear_mask()
141  
142 Stops the object from masking modules by removing it from @INC. This is
143 automatically called when object goes out of scope.
144  
145 =cut
146  
147 11 ▶sub clear_mask {
Subroutine clear_mask: called CC=2 SCAR=6.9
Pod documented
148 11 my $self = shift;
149  
150 11 ▶ @INC = grep { !ref $_ or $_ != $self } @INC;
Condition100%3/3
!ref($_) || $_ != $self
l!l&&r!l&&!r
9084
MC/DC100%2/2
!ref($_) || $_ != $self
!ref($_) $_ != $self
Truth table100%3/3
not(A) || B
A= !ref($_)B= $_ != $self
ABresult
1X1
011
000
151  
152 11 return $self;
153 }
154  
155 =head2 set_mask
156  
157 $obj = $obj->set_mask()
158  
159 Makes the object start masking modules by adding it to @INC. This is called by
160 new().
161  
162 This also has the effect of moving the object to the front of @INC again, which
163 could prove useful if @INC has been manipulated since the object was first
164 instantiated.
165  
166 Calling this method on an object already in @INC won't cause multiple copies to
167 appear.
168  
169 =cut
170  
171 7 ▶sub set_mask {
Subroutine set_mask: called CC=1 SCAR=0.0
Pod documented
172 7 my $self = shift;
173  
174 # We might already be in @INC
175 7 $self->clear_mask;
176  
177 7 unshift @INC, $self;
178 7 weaken $INC[0]; # don't let @INC keep us alive.
179  
180 7 return $self;
181 }
182  
183 =head2 is_masked
184  
185 $bool = $obj->is_masked($module)
186  
187 Returns true if the mask object is currently masking the given module, false
188 otherwise.
189  
190 Module::Mask::Inverted objects have the opposite behaviour.
191  
192 =cut
193  
194 77 ▶sub is_masked {
Subroutine is_masked: called CC=1 SCAR=0.0
Pod documented
195 77 my ($self, $module) = @_;
196  
197 77 return $self->_is_listed($module);
198 }
199  
200 # internal method to determine whether a module is listed in the mask or not.
201 81 ▶sub _is_listed {
Subroutine _is_listed: called CC=3 SCAR=11.0
202 81 my ($self, $module) = @_;
203 81 ▶ my $path = $self->_path($module) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $path = $self->_path($module) or return 0 80 1 81
204  
205 80 ▶ return $self->{$path} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{$path} ? : 8 72 80
206 }
207  
208 4 @Module::Mask::Inverted::ISA = qw( Module::Mask );
209  
210 4 ▶sub Module::Mask::Inverted::is_masked {
Subroutine is_masked: called CC=1 SCAR=0.0
211 4 my $self = shift;
212  
213 4 return ! $self->_is_listed(@_);
214 }
215  
216 =head2 list_masked
217  
218 @modules = $obj->list_masked()
219  
220 Returns a list of modules that are being masked. These are in the form of relative file paths, as found in %INC.
221  
222 =cut
223  
224 1 ▶sub list_masked { keys %{$_[0]} }
Subroutine list_masked: called CC=1 SCAR=0.0
Pod documented
225  
226 =head2 INC
227  
228 Implements the hook in @INC. See perldoc -f require
229  
230 =cut
231  
232 # INC gets forced into main unless explicitly qualified
233 77 ▶sub Module::Mask::INC {
Subroutine INC: called CC=2 SCAR=6.9
Pod documented
234 77 my ($self, $filename) = @_;
235  
236 77 ▶ if ($self->is_masked($filename)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_masked($filename)) { } 7 70 77
237 7 die $self->message($filename);
238 }
239 else {
240 70 return;
241 }
242 }
243  
244 =head2 message
245  
246 $message = $obj->message($filename)
247  
248 Returns the error message to be used when the filename is not found. This is
249 normally "$filename masked by $class", but can be overridden in subclasses if
250 necessary. Carp's L<shortmess|Carp/shortmess> is used to make this message
251 appear to come from the caller, i.e. the C<require> or C<use> statement
252 attempting to load the file.
253  
254 One possible application of this would be to make the error message look more
255 like perl's native "Could not find $filename in \@INC ...".
256  
257 =cut
258  
259 6 ▶sub message {
Subroutine message: called CC=1 SCAR=0.0
Pod documented
260 6 my ($self, $filename) = @_;
261 6 my $class = ref $self;
262  
263 6 return shortmess("$filename masked by $class");
264 }
265  
266 4 1;
267  
268 __END__
↑ Summary