blib/lib/Test/Smoke/App/Base.pm

filter: Statementstmt 99.4% 196 / 197 Branchbran 100% 36 / 36 Conditioncond 75.0% 3 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 24 / 24 Podpod 100% 7 / 7 totaltotal 98.8% 267 / 270 CC 35 SCAR 15.8
CC
35
Cov
99%
_post_process_options
9.0
_obtain_config_file
8.0
option
6.0
CRAP 4.9
« blib/lib/Test/Smoke/Syncer.pm ↑ Summary blib/lib/Test/Smoke/Poster.pm »
1 package Test::Smoke::App::Base;
2 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use Carp qw/ confess /;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 our $VERSION = '0.002';
7  
8 10 ▶use base 'Test::Smoke::ObjectBase';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 10 ▶use Cwd 'abs_path';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Getopt::Long qw/:config pass_through/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use Test::Smoke::App::AppOption;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use Test::Smoke::App::AppOptionCollection;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 ▶use Test::Smoke::LogMixin;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 10 ▶use Test::Smoke::Util::Serialise qw/serialise/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 =head1 NAME
18  
19 Test::Smoke::App::Base - Baseclass for Test::Smoke::App::* applications.
20  
21 =head1 SYNOPSIS
22  
23 package Test::Smoke::App::Mailer;
24 use base 'Test::Smoke::App::Base';
25 sub run {...}
26  
27 =head1 DESCRIPTION
28  
29 use Test::Smoke::App::Mailer;
30 my $mailer = Test::Smoke::App::Mailer->new(
31 main_options => [
32 Test::Smoke::App::AppOption->new(
33 name => 'mailer',
34 option => '=s',
35 allow => [qw/MIME::lite sendmail/],
36 helptext => "Mailsystem to use for sendig reports.",
37 ),
38 ],
39 general_options => [
40 Test::Smoke::AppOption->new(
41 name => 'ddir',
42 option => '=s',
43 helptxt => "Smoke Destination Directory.",
44 ),
45  
46 ],
47 special_options => {
48 'MIME::Lite' => [
49 mserver(),
50 msport(),
51 msuser(),
52 mspass(),
53 ],
54 'sendmail' => [],
55 },
56 );
57  
58 $mailer->run();
59  
60 =head2 Test::Smoke::App->new(%arguments)
61  
62 =head3 Arguments
63  
64 Named:
65  
66 =over
67  
68 =item main_options => $list_of_test_smoke_appoptions
69  
70 =item general_options => $list_of_test_smoke_appoptions
71  
72 These options are always valid.
73  
74 =item special_options => $hashref
75  
76 This is a hashref with the values of the C<allow>-array, that hold a list of
77 L<Test::Smoke::AppOptions>.
78  
79 =back
80  
81 =head3 Exceptions
82  
83 None.
84  
85 =cut
86  
87 29 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
88 29 my $class = shift;
89 29 my %args = @_;
90  
91 29 my $struct = {
92 _main_options => [],
93 _general_options => [],
94 _special_options => {},
95 _final_options => {},
96 };
97  
98 29 for my $known (keys %$struct) {
99 116 (my $key = $known) =~ s/^_//;
100 116 ▶ $struct->{$known} = delete $args{$key} if exists $args{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$key} 51 65 116
101 }
102  
103 29 my $self = bless $struct, $class;
104  
105 29 $self->process_options();
106 29 return $self;
107 }
108  
109 =head2 Test::Smoke::App::Base->configfile_option()
110  
111 Returns a L<Test::Smoke::App::AppOption> for 'configfile'.
112  
113 =cut
114  
115 29 ▶sub configfile_option {
Subroutine configfile_option: called CC=1 SCAR=0.0
Pod documented
116 29 my $class = shift;
117 29 return Test::Smoke::App::AppOption->new(
118 name => 'configfile',
119 option => 'config|c=s',
120 default => 'smokecurrent',
121 helptext => "Set the name/prefix of the configfile\n",
122 );
123 }
124  
125 =head2 Test::Smoke::App::Base->verbose_option()
126  
127 Returns a L<Test::Smoke::App::AppOption> for 'verbose'.
128  
129 =cut
130  
131 29 ▶sub verbose_option {
Subroutine verbose_option: called CC=1 SCAR=0.0
Pod documented
132 29 my $class = shift;
133 29 return Test::Smoke::App::AppOption->new(
134 name => 'verbose',
135 option => 'v=i',
136 allow => [0, 1, 2],
137 default => 0,
138 helptext => 'Set verbosity level.',
139 );
140 }
141  
142 =head2 $app->show_config_option
143  
144 =cut
145  
146 29 ▶sub show_config_option {
Subroutine show_config_option: called CC=1 SCAR=0.0
Pod documented
147 29 return Test::Smoke::App::AppOption->new(
148 name => 'show_config',
149 option => 'show-config',
150 helptext => "Show all about config vars.",
151 );
152 }
153  
154 =head2 $app->process_options()
155  
156 This process constists of three (3) steps:
157  
158 =over
159  
160 =item 1. pre_process_options
161  
162 This step organizes the options in a AppOptionCollection.
163  
164 =item 2. get_options
165  
166 This step processes the arguments passed on the command line.
167  
168 =item 3. post_process_options
169  
170 This step integrates the arguments, their coded-defaults, config-file values
171 and command-line overrides.
172  
173 =back
174  
175 =head3 Arguments
176  
177 None.
178  
179 =head3 Returns
180  
181 The object-instance.
182  
183 =head3 Exceptions
184  
185 None.
186  
187 =cut
188  
189 29 ▶sub process_options {
Subroutine process_options: called CC=1 SCAR=0.0
Pod documented
190 29 my $self = shift;
191  
192 29 $self->{_opt_collection} = Test::Smoke::App::AppOptionCollection->new();
193  
194 29 $self->_pre_process_options();
195 29 $self->_get_options();
196 29 $self->_post_process_options();
197  
198 29 return $self;
199 }
200  
201 =head2 $app->option($option)
202  
203 Return the value of an option.
204  
205 =head3 Arguments
206  
207 Positional.
208  
209 =over
210  
211 =item $option_name
212  
213 =back
214  
215 =head3 Returns
216  
217 The value of that option if applicable.
218  
219 =head3 Exceptions
220  
221 =over
222  
223 =item B<Invalid option 'blah' ($name => $type)>
224  
225 =item B<Option 'blah' is invalid for $name => $type>
226  
227 =back
228  
229 =cut
230  
231 877 ▶sub option {
Subroutine option: called CC=6 SCAR=17.9
Pod documented
232 877 my $self = shift;
233 877 my ($option) = @_;
234  
235 877 my $opts = $self->final_options;
236 877 ▶ if (exists $opts->{$option}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{$option}) 876 1 877
237 876 my $is_main = grep $_->name eq $option, @{$self->main_options};
238 876 ▶ return $opts->{$option} if $is_main;
Branch: 1 branch
BranchTrueFalseTotal
if $is_main 43 833 876
239  
240 833 my $is_general = grep $_->name eq $option, @{$self->general_options};
241 833 ▶ return $opts->{$option} if $is_general;
Branch: 1 branch
BranchTrueFalseTotal
if $is_general 764 69 833
242  
243 69 for my $mainopt (@{$self->main_options}) {
244 99 my $type = $opts->{$mainopt->name};
245 99 my $specials = $self->special_options->{$type};
246 99 my $is_special = grep $_->name eq $option, @$specials;
247 99 ▶ return $opts->{$option} if $is_special;
Branch: 1 branch
BranchTrueFalseTotal
if $is_special 68 31 99
248 }
249  
250 1 confess("Option '$option' is not valid.");
251 }
252 1 confess("Invalid option '$option'");
253 }
254  
255 484 ▶sub _find_option {
Subroutine _find_option: called CC=5 SCAR=16.1
256 484 my $self = shift;
257 484 my ($option) = @_;
258  
259 484 my ($oo) = grep $_->name eq $option, @{$self->main_options};
260 484 ▶ return $oo if $oo;
Branch: 1 branch
BranchTrueFalseTotal
if $oo 23 461 484
261  
262 461 ($oo) = grep $_->name eq $option, @{$self->general_options};
263 461 ▶ return $oo if $oo;
Branch: 1 branch
BranchTrueFalseTotal
if $oo 417 44 461
264  
265 44 for my $mo (@{$self->main_options}) {
266 72 my $type = $self->final_options->{$mo->name};
267 72 my $specials = $self->special_options->{$type};
268 72 ($oo) = grep $_->name eq $option, @$specials;
269 72 ▶ return $oo if $oo;
Branch: 1 branch
BranchTrueFalseTotal
if $oo 44 28 72
270 }
271  
272 0 return;
273 }
274  
275 =head2 $app->options()
276  
277 =head3 Arguments
278  
279 None.
280  
281 =head3 Returns
282  
283 A hash (list) of all options that apply to this instance of the app.
284  
285 =head3 Exceptions
286  
287 None.
288  
289 =cut
290  
291 45 ▶sub options {
Subroutine options: called CC=5 SCAR=16.1
Pod documented
292 45 my $self = shift;
293  
294 45 my %options;
295 45 for my $opt (@{$self->main_options}) {
296 34 my $type = $self->option($opt->name);
297 34 $options{$opt->name} = $type;
298 34 my $specials = $self->special_options->{$type};
299 34 for my $opt (@$specials) {
300 71 $options{$opt->name} = $self->option($opt->name);
301 }
302 }
303 # collect all general options
304 45 for my $opt (@{ $self->general_options }) {
305 679 ▶ next if $opt->name =~ /^(?:help|show_config)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $opt->name =~ /^(?:help|show_config)$/ 90 589 679
306 589 $options{$opt->name} = $self->option($opt->name);
307 }
308  
309 45 return %options;
310 }
311  
312 29 ▶sub _pre_process_options {
Subroutine _pre_process_options: called CC=5 SCAR=16.1
313 29 my $self = shift;
314  
315 29 unshift @{$self->general_options}, $self->configfile_option;
316 29 push @{$self->general_options}, $self->show_config_option;
317 29 push @{$self->general_options}, $self->verbose_option;
318 29 for my $opt (@{$self->general_options}) {
319 448 $self->opt_collection->add($opt);
320 }
321  
322 29 for my $opt (sort {$a->name cmp $b->name} @{$self->main_options}) {
323 23 $self->opt_collection->add_helptext("\n");
324 23 $self->opt_collection->add($opt);
325 23 for my $special (sort {lc($a) cmp lc($b)} @{$opt->allow}) {
326 84 $self->opt_collection->add_helptext(
327 sprintf("\nOptions for '%s':\n", $special)
328 );
329 84 my $specials = $self->special_options->{$special};
330 84 for my $thisopt (@$specials) {
331 199 $self->opt_collection->add($thisopt);
332 }
333 }
334 }
335  
336 29 my $helptext = $self->opt_collection->helptext;
337 my $help_option = Test::Smoke::App::AppOption->new(
338 name => 'help',
339 option => 'h',
340 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
341 1 print "Usage: $0 [options]\n\n$helptext";
342 1 exit(0);
343 },
344 29 helptext => 'This message.',
345 );
346 29 push @{$self->general_options}, $help_option;
347 29 $self->opt_collection->add($help_option);
348  
349 29 %{$self->{_dft_options}} = %{$self->opt_collection->options_with_default};
350 }
351  
352 29 ▶sub _get_options {
Subroutine _get_options: called CC=1 SCAR=0.0
353 29 my $self = shift;
354  
355 29 %{$self->{_cli_options}} = %{$self->opt_collection->options_for_cli};
356  
357 29 @{$self->{_ARGV}} = @{$self->{_ARGV_EXTRA}} = @ARGV;
358  
359 29 my $parser = Getopt::Long::Parser->new(config => [qw/no_ignore_case passthrough/]);
360 $parser->getoptionsfromarray(
361 $self->{_ARGV_EXTRA},
362 $self->cli_options,
363 29 @{ $self->opt_collection->options_list },
364 );
365 }
366  
367 29 ▶sub _post_process_options {
Subroutine _post_process_options: called CC=9 SCAR=22.0
368 29 my $self = shift;
369  
370 29 $self->_obtain_config_file;
371 29 %{$self->final_options} = %{$self->cli_options};
372  
373 # now combine with configfile
374 $self->final_options(
375 {
376 29 %{$self->opt_collection->all_options},
377 29 %{$self->dft_options},
378 29 %{$self->from_configfile},
379 29 %{$self->cli_options},
380 }
381 );
382 29 my @errors;
383 29 my %check_options = $self->options;
384 29 for my $opt (keys %check_options) {
385 484 my $oo = $self->_find_option($opt);
386 484 my $value = $self->final_options->{$opt};
387 484 ▶ $value = '<undef>' if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 69 415 484
388 push(
389 @errors,
390 sprintf(
391 "Invalid value '%s' for option '%s'",
392 $self->_show_option_value($opt), $opt
393 )
394 484 ▶ ) if !$oo->allowed($self->final_options->{$opt});
Branch: 1 branch
BranchTrueFalseTotal
unless $oo->allowed($self->final_options->{$opt}) 1 483 484
395 }
396 29 ▶ if (@errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (@errors) 1 28 29
397 1 print "$_\n" for @errors;
398 1 exit(1);
399 }
400  
401 29 ▶ if ($self->final_options->{show_config}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->final_options->{"show_config"}) 1 28 29
402 1 print "Show configuration requested:\n";
403 1 print
404 sprintf(
405 " %-20s| %s\n",
406 'Option',
407 'Value'
408 );
409 1 print "----------------------+--------------------------------------------\n";
410 1 my %options = $self->options;
411 1 for my $opt (sort keys %options) {
412 2 ▶ printf " %-20s| %s\n",
Condition50%1/2
$self->_show_option_value($opt) || '?'
l!l
20
MC/DC0%0/1
$self->_show_option_value($opt) || '?'
$self->_show_option_value($opt)
Truth table50%1/2
A
A= $self->_show_option_value($opt)
Aresult
11
00
413 $opt,
414 $self->_show_option_value($opt) || '?';
415 }
416 1 exit(0);
417 }
418 }
419  
420 3 ▶sub _show_option_value {
Subroutine _show_option_value: called CC=1 SCAR=0.0
421 3 my $self = shift;
422 3 my ($option_name) = @_;
423  
424 3 return serialise( $self->option($option_name) );
425 }
426  
427 29 ▶sub _obtain_config_file {
Subroutine _obtain_config_file: called CC=8 SCAR=20.8
428 29 my $self = shift;
429 29 $self->{_from_configfile} = {};
430 29 $self->{_configfile_error} = undef;
431  
432 29 my $cf_name = $self->cli_options->{'configfile'};
433 29 ▶ return if !$cf_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $cf_name 25 4 29
434  
435 4 ▶ if (!-f $cf_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $cf_name) 3 1 4
436 3 for my $ext (qw/_config .config/) {
437 5 ▶ if (-f "${cf_name}${ext}") { $cf_name .= $ext; last }
Branch: 1 branch
BranchTrueFalseTotal
if (-f "$cf_name$ext") 2 3 5
438 }
439 }
440  
441 4 ▶ if (-f $cf_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $cf_name) { } 3 1 4
442 3 my $abs_cf = $self->cli_options->{'configfile'} = abs_path($cf_name);
443  
444 # Read the config-file in a localized environment
445 3 our $conf;
446 3 local $conf;
447 3 delete $INC{$abs_cf};
448 3 eval { local $^W; require $abs_cf };
449 3 $self->{_configfile_error} = $@;
450 3 ▶ %{$self->from_configfile} = %{ $conf || {} };
Condition100%2/2
$Test::Smoke::App::Base::conf || {}
l!l
21
MC/DC100%1/1
$Test::Smoke::App::Base::conf || {}
$Test::Smoke::App::Base::conf
Truth table100%2/2
A
A= $Test::Smoke::App::Base::conf
Aresult
11
00
451 3 delete $INC{$abs_cf};
452 $self->from_configfile->{verbose} = delete($self->from_configfile->{v})
453 3 ▶ if exists $self->from_configfile->{v};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->from_configfile->{"v"} 1 2 3
454 }
455 else {
456 1 $self->{_configfile_error} = "Could not find a configfile for '$cf_name'.";
457 }
458 }
459  
460 10 1;
461  
462 =head1 COPYRIGHT
463  
464 (c) 2002-2013, Abe Timmerman <abeltje@cpan.org> All rights reserved.
465  
466 With contributions from Jarkko Hietaniemi, Merijn Brand, Campo
467 Weijerman, Alan Burlison, Allen Smith, Alain Barbet, Dominic Dunlop,
468 Rich Rauenzahn, David Cantrell.
469  
470 This library is free software; you can redistribute it and/or modify
471 it under the same terms as Perl itself.
472  
473 See:
474  
475 =over 4
476  
477 =item * L<http://www.perl.com/perl/misc/Artistic.html>
478  
479 =item * L<http://www.gnu.org/copyleft/gpl.html>
480  
481 =back
482  
483 This program is distributed in the hope that it will be useful,
484 but WITHOUT ANY WARRANTY; without even the implied warranty of
485 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
486  
487 =cut
« blib/lib/Test/Smoke/Syncer.pm ↑ Summary blib/lib/Test/Smoke/Poster.pm »