File Coverage

blib/lib/Mock/Config.pm
Criterion Covered Total %
statement 21 28 75.0
branch 4 12 33.3
condition 1 6 16.6
subroutine 6 7 85.7
pod n/a
total 32 53 60.3


line stmt bran cond sub pod time code
1             package Mock::Config;
2              
3 1     1   12692 use 5.006;
  1         3  
4 1     1   7 use Config ();
  1         1  
  1         129  
5              
6             our %MockConfig;
7              
8             =head1 NAME
9              
10             Mock::Config - temporarily set Config or XSConfig values
11              
12             =head1 VERSION
13              
14             Version 0.02
15              
16             =cut
17              
18             our $VERSION = '0.03';
19              
20             =head1 SYNOPSIS
21              
22             XSConfig is readonly, so workaround that.
23              
24             use Mock::Config d_fork => 0, perl_patchlevel => '';
25              
26             The importer works only dynamically, not lexically yet.
27              
28             use Mock::Config;
29             Mock::Config->import(startperl => '');
30             print $Config{startperl}, ' mocked to empty';
31             Mock::Config->unimport;
32              
33             =head1 SUBROUTINES
34              
35             =head2 import
36              
37             Set pair of Config values, even for the readonly XSConfig implementation,
38             as used in cperl.
39              
40             It does not store the mocked overrides lexically, just dynamically.
41              
42             =cut
43              
44             sub _set {
45 1     1   3 my ($key, $val) = @_;
46             # string context only?
47 1 50 33     8 if (!exists $MockConfig{$key} or $MockConfig{$key} ne $val) {
48 1 50       5 if (exists &Config::KEYS) { # compiled XSConfig
49 0         0 $MockConfig{$key} = $val; # cache new value
50             } else {
51 1         4 $MockConfig{$key} = tied(%Config::Config)->{$key}; # store the old value
52 1         7 tied(%Config::Config)->{$key} = $val; # set uncompiled Config
53             }
54             }
55             }
56              
57             sub import {
58 2     2   78706 my $class = shift;
59 2 50       9 if (exists &Config::KEYS) { # compiled XSConfig
60             # initialize the mocker
61 0 0       0 if (!exists &Config_FETCHorig) {
62 0         0 *Config_FETCHorig = \&Config::FETCH;
63 1     1   4 no warnings 'redefine';
  1         4  
  1         163  
64             *Config::FETCH = sub {
65 0 0 0 0   0 if ($_[0] and exists $MockConfig{$_[1]}) {
66 0         0 return $MockConfig{$_[1]};
67             } else {
68 0         0 return Config_FETCHorig(@_);
69             }
70             }
71 0         0 }
72             }
73 2         891 _set(shift, shift) while @_;
74             }
75              
76             =head2 unimport
77              
78             This is unstacked and not lexical.
79             It undoes all imported Config values at once.
80              
81             =cut
82              
83             sub unimport {
84 1     1   585 my $class = shift;
85 1 50       6 if (!exists &Config::KEYS) {
86 1         5 for (keys %MockConfig) {
87 1         4 tied(%Config::Config)->{$_} = $MockConfig{$_};
88             }
89             }
90 1         5 %MockConfig = ();
91             }
92              
93              
94             =head1 AUTHOR
95              
96             Reini Urban, C<< >>
97              
98             =head1 BUGS
99              
100             Please report any bugs or feature requests at
101             L.
102              
103             We will be notified, and then you'll automatically be notified of
104             progress on your request as we make changes.
105              
106             =head1 SUPPORT
107              
108             You can find documentation for this module with the perldoc command.
109              
110             perldoc Mock::Config
111              
112             You can also look for information at:
113              
114             =over 4
115              
116             =item * RT: CPAN's request tracker (report bugs here)
117              
118             L
119              
120             =item * AnnoCPAN: Annotated CPAN documentation
121              
122             L
123              
124             =item * CPAN Ratings
125              
126             L
127              
128             =item * Search CPAN
129              
130             L
131              
132             =back
133              
134              
135             =head1 ACKNOWLEDGEMENTS
136              
137              
138             =head1 LICENSE AND COPYRIGHT
139              
140             Copyright 2016 cPanel Inc.
141              
142             This program is free software; you can redistribute it and/or modify it
143             under the terms of the the Artistic License (2.0). You may obtain a
144             copy of the full license at:
145              
146             L
147              
148             Any use, modification, and distribution of the Standard or Modified
149             Versions is governed by this Artistic License. By using, modifying or
150             distributing the Package, you accept this license. Do not use, modify,
151             or distribute the Package, if you do not accept this license.
152              
153             If your Modified Version has been derived from a Modified Version made
154             by someone other than you, you are nevertheless required to ensure that
155             your Modified Version complies with the requirements of this license.
156              
157             This license does not grant you the right to use any trademark, service
158             mark, tradename, or logo of the Copyright Holder.
159              
160             This license includes the non-exclusive, worldwide, free-of-charge
161             patent license to make, have made, use, offer to sell, sell, import and
162             otherwise transfer the Package with respect to any patent claims
163             licensable by the Copyright Holder that are necessarily infringed by the
164             Package. If you institute patent litigation (including a cross-claim or
165             counterclaim) against any party alleging that the Package constitutes
166             direct or contributory patent infringement, then this Artistic License
167             to you shall terminate on the date that such litigation is filed.
168              
169             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
170             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
171             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
172             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
173             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
174             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
175             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
176             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
177              
178              
179             =cut
180              
181             1; # End of Mock::Config