Config.pm untested

filter: Statementstmt 0.0% 0 / 209 Branchbran 0.0% 0 / 36 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 32 Podpod 0.0% 0 / 27 totaltotal 0.0% 0 / 304 CC 19 SCAR 20.0
CC
19
Cov
0%
new
20.0
_shiftBoolish
20.0
_shiftPool
12.0
CRAP 7.4
1 use strict;
2 use warnings;
3  
4 package SVN::Friendly::Config;
5 my $CLASS = __PACKAGE__;
6  
7 use SVN::Friendly::Utils qw(_shiftBoolean _shiftVisitor);
8 use SVN::Core;
9  
10 #------------------------------------------------------------------
11  
12 use SVN::Friendly::Exceptions;
13 my $EXCEPTIONS = 'SVN::Friendly::Exceptions';
14  
15 #------------------------------------------------------------------
16 # fill in missing constants
17  
18 {
19 # this 2 line wierdness is needed to keep Module::Build from adding
20 # this package to the META.yaml provides list
21 package
22 SVN::Core;
23 our $CONFIG_SECTION_AUTOPROPS = 'auto-props';
24 }
25  
26 #------------------------------------------------------------------
27  
28 our @CATEGORIES
29 = ($SVN::Core::CONFIG_CATEGORY_SERVERS
30 , $SVN::Core::CONFIG_CATEGORY_CONFIG
31 );
32  
33 our @SECTIONS
34 = ($SVN::Core::CONFIG_SECTION_GROUPS
35 , $SVN::Core::CONFIG_SECTION_GLOBAL
36 , $SVN::Core::CONFIG_SECTION_AUTH
37 , $SVN::Core::CONFIG_SECTION_HELPERS
38 , $SVN::Core::CONFIG_SECTION_MISCELLANY
39 , $SVN::Core::CONFIG_SECTION_TUNNELS
40 , $SVN::Core::CONFIG_SECTION_AUTOPROPS
41 );
42  
43 #==================================================================
44 # CLASS METHODS
45 #==================================================================
46  
47 sub new {
48 my ($sClass, $xNative, $oPool) = @_;
49 my $sRef = ref($xNative);
50 my $hConfig;
51 if ($sRef eq 'HASH') {
52 $hConfig = $xNative;
53 } elsif (!$sRef) {
54 $hConfig = SVN::Core::config_get_config($xNative);
55 } elsif ($sRef->isa(__PACKAGE__)) {
56 return $xNative; #already blessed
57 } else {
58 die $EXCEPTIONS->ERR_BAD_ARG->new(arg=>$xNative
59 , reason => 'value must be directory path, hash, or undefined');
60 }
61  
62 # Note: we don't create a pool because it is ok to use the
63 # global pool
64  
65 my $self = { config => $hConfig, pool => $oPool };
66 return bless($self, $sClass);
67 }
68  
69 #==================================================================
70 # OBJECT METHODS
71 #==================================================================
72  
73 #------------------------------------------------------------------
74  
75 sub enumerate1_1 {
76 my $self = shift;
77 my $oCategory = $self->_shiftCategory(\@_);
78 my $sSection = shift;
79 my $crVisit = _shiftVisitor(\@_);
80  
81 SVN::Core::config_enumerate($oCategory, $sSection, $crVisit);
82 }
83  
84 sub enumerate1_4 {
85 my $self = shift;
86 my $oCategory = $self->_shiftCategory(\@_);
87 my $sSection = shift;
88 my $crVisit = _shiftVisitor(\@_);
89 my $oPool = $self->_shiftPool(\@_);
90  
91 SVN::Core::config_enumerate2($oCategory, $sSection,$crVisit,$oPool);
92 }
93  
94 *enumerate1_5 = *enumerate1_4;
95 *enumerate1_6 = *enumerate1_4;
96 *enumerate1_7 = *enumerate1_4;
97 *enumerate = *enumerate1_1;
98 *visitOptions = *enumerate1_1;
99  
100 #------------------------------------------------------------------
101 # Note: this doesn't appear to be included in the 1.4.6 bindings
102  
103 sub enumerate_sections1_1 {
104 my $self = shift;
105 my $oCategory = $self->_shiftCategory(\@_);
106 my $crVisit = _shiftVisitor(\@_);
107  
108 #print STDERR "category=<$oCategory> visitor=<$crVisit>\n";
109 SVN::Core::config_enumerate_sections($oCategory, $crVisit);
110 }
111  
112 sub enumerate_sections1_4 {
113 my $self = shift;
114 my $oCategory = $self->_shiftCategory(\@_);
115 my $crVisit = _shiftVisitor(\@_);
116 my $oPool = $self->_shiftPool(\@_);
117 SVN::Core::config_enumerate_sections2($oCategory, $crVisit, $oPool);
118 }
119  
120 *enumerate_sections1_5 = *enumerate_sections1_4;
121 *enumerate_sections1_6 = *enumerate_sections1_4;
122 *enumerate_sections1_7 = *enumerate_sections1_4;
123 *enumerate_sections = *enumerate_sections1_1;
124  
125 #------------------------------------------------------------------
126 # looks up a server in the groups section and finds the section to
127 # which it belongs.
128  
129 sub find_group1_1 {
130 my $self = shift;
131 my $oCategory = $self->_shiftCategory(\@_);
132 my $sServer = shift;
133 my $sGroupSection = _shiftWildcardSection(\@_);
134 my $oPool = $self->_shiftPool(\@_);
135  
136 SVN::Core::config_find_group($oCategory, $sServer, $sGroupSection
137 , $oPool);
138 }
139  
140 *find_group1_4 = *find_group1_1;
141 *find_group1_5 = *find_group1_1;
142 *find_group1_6 = *find_group1_1;
143 *find_group1_7 = *find_group1_1;
144 *find_group = *find_group1_1;
145  
146 #------------------------------------------------------------------
147  
148 sub get1_1 {
149 my $self = shift;
150 my $oCategory = $self->_shiftCategory(\@_);
151 my ($sSection, $sOption, $xDefault) = @_;
152  
153 # force default value into string - SWIG won't do it for us
154  
155 # Need to use SVN::_Core because SVN::Base thinks anything ending
156 # in _get is an accessor method and defines it so that the "value"
157 # sets data rather than defining a default.
158  
159 return SVN::_Core::svn_config_get($oCategory, $sSection, $sOption
160 , defined($xDefault)?"$xDefault":undef);
161 }
162  
163 *get1_4 = *get1_1;
164 *get1_5 = *get1_1;
165 *get1_6 = *get1_1;
166 *get1_7 = *get1_1;
167 *get = *get1_1;
168  
169 #------------------------------------------------------------------
170  
171 sub get_bool1_1 {
172 my $self = shift;
173 my $oCategory = $self->_shiftCategory(\@_);
174 my $sSection = shift;
175 my $sOption = shift;
176 my $xDefault = _shiftBoolish(\@_);
177  
178 return SVN::Core::config_get_bool($oCategory, $sSection, $sOption
179 , $xDefault)?1:0;
180 }
181  
182 *get_bool1_4 = *get_bool1_1;
183 *get_bool1_5 = *get_bool1_1;
184 *get_bool1_6 = *get_bool1_1;
185 *get_bool1_7 = *get_bool1_1;
186 *get_bool = *get_bool1_1;
187  
188 #------------------------------------------------------------------
189  
190 sub getCategoryNames {
191 my $self = $_[0];
192 return [ keys %{$self->{config}} ];
193 }
194  
195 #------------------------------------------------------------------
196  
197 sub getOptionNames {
198 my ($self, $xCategory, $sSection) = @_;
199 my @aOptions;
200 my $crVisit = sub { push @aOptions, $_[0]};
201 $self->enumerate($xCategory, $sSection, $crVisit);
202 return \@aOptions;
203 }
204  
205 #------------------------------------------------------------------
206  
207 sub getSectionNames {
208 my ($self, $xCategory) = @_;
209  
210 my @aSections;
211  
212 my $crVisit = sub { push @aSections, $_[0]; };
213 $self->visitSections($xCategory, $crVisit);
214 return \@aSections;
215 }
216  
217 #------------------------------------------------------------------
218  
219 sub get_server_setting1_1 {
220 my $self = shift;
221 my $oCategory = $self->_shiftCategory(\@_);
222 my ($sServerGroup, $sOption, $xDefault) = @_;
223  
224 return SVN::Core::config_get_server_setting($oCategory
225 , $sServerGroup, $sOption
226 , defined($xDefault)?"$xDefault":undef);
227 }
228  
229 *get_server_setting1_4 = *get_server_setting1_1;
230 *get_server_setting1_5 = *get_server_setting1_1;
231 *get_server_setting1_6 = *get_server_setting1_1;
232 *get_server_setting1_7 = *get_server_setting1_1;
233 *get_server_setting = *get_server_setting1_1;
234  
235 #------------------------------------------------------------------
236  
237 sub get_server_setting_int1_1 {
238 my $self = shift;
239 my $oCategory = $self->_shiftCategory(\@_);
240 my ($sServerGroup, $sOption, $xDefault) = @_;
241  
242 return SVN::Core::config_get_server_setting_int
243 ($oCategory, $sServerGroup, $sOption, $xDefault);
244 }
245  
246 *get_server_setting_int1_4 = *get_server_setting_int1_1;
247 *get_server_setting_int1_5 = *get_server_setting_int1_1;
248 *get_server_setting_int1_6 = *get_server_setting_int1_1;
249 *get_server_setting_int1_7 = *get_server_setting_int1_1;
250 *get_server_setting_int = *get_server_setting_int1_1;
251  
252 #------------------------------------------------------------------
253  
254 sub getCategory { return $_[0]->{config}->{$_[1]}; }
255  
256 #------------------------------------------------------------------
257  
258 sub getCategoryHash { return $_[0]->{config} };
259  
260 #------------------------------------------------------------------
261  
262 sub hasSection1_1 {
263 my ($self, $xCategory, $sSection) = @_;
264  
265 # this method doesn't exist in 1_1 so we need to emulate it
266  
267 my $iOptionCount = 0;
268 $self->enumerate($xCategory, $sSection, sub { $iOptionCount++});
269 return $iOptionCount ? 1: 0;
270 }
271  
272 sub hasSection1_4 {
273 my $self = shift;
274 my $oCategory = $self->_shiftCategory(\@_);
275 my $sSection = shift;
276  
277 SVN::Core::config_has_section($oCategory, $sSection);
278 }
279  
280 *hasSection1_5 = *hasSection1_4;
281 *hasSection1_6 = *hasSection1_4;
282 *hasSection1_7 = *hasSection1_4;
283 *hasSection = *hasSection1_1;
284  
285 #------------------------------------------------------------------
286  
287 sub merge1_1 {
288 my $self = shift;
289 my $oCategory = $self->_shiftCategory(\@_);
290 my $sFile = shift;
291 my $bMustExist = _shiftBoolean(\@_);
292  
293 SVN::Core::config_merge($oCategory, $sFile, $bMustExist);
294 }
295  
296 *merge1_4 = *merge1_1;
297 *merge1_5 = *merge1_1;
298 *merge1_6 = *merge1_1;
299 *merge1_7 = *merge1_1;
300 *merge = *merge1_1;
301  
302 #------------------------------------------------------------------
303  
304 sub read1_1 {
305 my $self = shift;
306 my $sCategory = _shiftRequiredCategory(\@_);
307 my $sFile = shift;
308 my $bMustExist = _shiftBoolean(\@_);
309 my $oPool = $self->_shiftPool(\@_);
310  
311 my $oCategory = SVN::Core::config_read($sFile, $bMustExist,$oPool);
312 $self->{config}->{$sCategory} = $oCategory;
313 }
314  
315 *read1_4 = *read1_1;
316 *read1_5 = *read1_1;
317 *read1_6 = *read1_1;
318 *read1_7 = *read1_1;
319 *read = *read1_1;
320  
321 #------------------------------------------------------------------
322  
323 sub set1_1 {
324 my $self = shift;
325 my $oCategory = $self->_shiftCategory(\@_);
326 my ($sSection, $sOption, $sValue) = @_;
327  
328 # Need to use SVN::_Core because SVN::Base thinks anything ending
329 # in _set is a companion method to _get and refuses to import it.
330  
331 # need to quote value in case it is a number that needs to be
332 # converted to a value - SWIG won't do it for us.
333  
334 return SVN::_Core::svn_config_set($oCategory, $sSection, $sOption
335 , defined($sValue)?"$sValue":undef);
336 }
337  
338 *set1_4 = *set1_1;
339 *set1_5 = *set1_1;
340 *set1_6 = *set1_1;
341 *set1_7 = *set1_1;
342 *set = *set1_1;
343  
344 #------------------------------------------------------------------
345  
346 sub set_bool1_1 {
347 my $self = shift;
348 my $oCategory = $self->_shiftCategory(\@_);
349 my $sSection = shift;
350 my $sOption = shift;
351 my $bValue = _shiftBoolish(\@_);
352  
353 return SVN::Core::config_set_bool($oCategory, $sSection, $sOption
354 , $bValue);
355 }
356  
357 *set_bool1_4 = *set_bool1_1;
358 *set_bool1_5 = *set_bool1_1;
359 *set_bool1_6 = *set_bool1_1;
360 *set_bool1_7 = *set_bool1_1;
361 *set_bool = *set_bool1_1;
362  
363 #------------------------------------------------------------------
364  
365 sub visitSections {
366 my $self = shift;
367 my $oCategory = $self->_shiftCategory(\@_);
368 my $crVisit = _shiftVisitor(\@_);
369  
370 # Note: we can't use enumerate_sections because the thunk for that
371 # method doesn't appear to be set up. As a substitute we enumerate
372 # through the hard coded list of sections.
373 # BUT note that we will miss any user defined secitons.
374  
375 foreach my $sSection (@SECTIONS) {
376 next unless $self->hasSection($oCategory, $sSection);
377 $crVisit->($sSection);
378 }
379 }
380  
381 #------------------------------------------------------------------
382  
383 # 1.6 only
384 # - get_server_setting_bool
385 # - get_yes_no_ask
386 #
387 # tools
388 # - get_user_config_path($sDir, $sName)
389 # - ensure($sDir, $oPool)
390 # - read_auth_data($hAuthData, $sCredKind, $sRealm, $sDir, $oPool);
391 # - $hAuthData = write_auth_data($sCredKind, $sRealm, $sDir, $oPool);
392 # tools: 1.7 only
393 # - create($oPool)
394  
395 #==================================================================
396 # PRIVATE OBJECT METHODS
397 #==================================================================
398  
399 #------------------------------------------------------------------
400  
401 sub _shiftPool {
402 my $self = shift @_;
403 die "Not a $CLASS" unless $self->isa($CLASS);
404  
405 my $oPool = shift @{shift @_};
406 return defined($oPool) ? $oPool : $self->{pool};
407 }
408  
409 #------------------------------------------------------------------
410  
411 sub _shiftCategory {
412 my ($self, $aArgs) = @_;
413 my $xCategory = shift @$aArgs;
414 if (!defined($xCategory)) {
415 die $EXCEPTIONS->ERR_UNDEF_ARG->new(param=>'category');
416 }
417 return ref($xCategory)
418 ? $xCategory : $self->getCategory($xCategory);
419 }
420  
421 #==================================================================
422 # UTILITY METHODS
423 #==================================================================
424  
425 #------------------------------------------------------------------
426  
427 sub _shiftBoolish {
428 my $aArgs = $_[0];
429 my $sBool = shift @$aArgs;
430 return !defined($sBool)
431 ? 0
432 : ($sBool =~ m{^(?:true|yes|on|1)$}i)
433 ? 1
434 : ($sBool =~ m{^(?:false|no|off|0)}i)
435 ? 0 : $sBool;
436 }
437  
438 #------------------------------------------------------------------
439  
440 sub _shiftRequiredCategory {
441 my $aArgs = $_[0];
442 my $sCategory = shift @$aArgs;
443 if (!defined($sCategory)) {
444 die $EXCEPTIONS->ERR_UNDEF_ARG->new(param=>'category name');
445 }
446 return $sCategory;
447 }
448  
449 #------------------------------------------------------------------
450  
451 sub _shiftWildcardSection {
452 my $aArgs = $_[0];
453 my $sSection = shift @$aArgs;
454 return defined($sSection) ? $sSection : 'groups';
455 }
456  
457 #==================================================================
458 # MODULE INITIALIZATION
459 #==================================================================
460  
461 1;