blib/lib/Tapper/CLI/Precondition.pm

filter: Statementstmt 70.6% 135 / 191 Branchbran 56.0% 37 / 66 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 94.1% 16 / 17 Podpod 83.3% 10 / 12 totaltotal 67.2% 201 / 299 CC 45 SCAR 22.8
CC
45
Cov
66%
b_list
18.9
hr_init_precondition_commands
14.2
s_read_condition_file
12.0
CRAP 9.8
« blib/lib/Tapper/CLI/Init.pm ↑ Summary blib/lib/Tapper/CLI/DbDeploy/Command/saveschema.pm »
1 package Tapper::CLI::Precondition;
2 72 our $AUTHORITY = 'cpan:TAPPER';
3 72 $Tapper::CLI::Precondition::VERSION = '5.0.8';
4  
5 72 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 72 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 72 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 72 ▶use feature qw/ say /;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 72 ▶use English qw/ -no_match_vars /;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # list limit default value
12 72 my $i_limit_default = 50;
13  
14  
15 1 ▶sub b_print_single_precondition {
Subroutine b_print_single_precondition: called CC=8 SCAR=22.4
Pod documented
16  
17 1 my ( $or_precondition, $hr_options ) = @_;
18  
19 1 ▶ my %h_print_elements = (
Condition50%1/2
$or_precondition->timeout || ''
l!l
01
MC/DC0%0/1
$or_precondition->timeout || ''
$or_precondition->timeout
Truth table50%1/2
A
A= $or_precondition->timeout
Aresult
11
00
20 'Id' => [ 1, $or_precondition->id, ],
21 'Shortname' => [ 2, $or_precondition->shortname, ],
22 'Precondition' => [ 3, $or_precondition->precondition, ],
23 'Timeout' => [ 4, $or_precondition->timeout || q##, ],
24 );
25  
26 1 ▶ if ( $hr_options->{nonewlines} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hr_options->{"nonewlines"}) 0 1 1
27 0 $h_print_elements{Precondition}[1] =~ s/\n/\\n/mg;
28 }
29 1 ▶ if ( $hr_options->{quotevalues} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hr_options->{"quotevalues"}) 0 1 1
30 0 require Data::Dumper;
31 0 for my $s_key ( keys %h_print_elements ) {
32 $h_print_elements{$s_key}[1] =
33 Data::Dumper
34 0 ->new([$h_print_elements{$s_key}[1]])
35 ->Terse(1)
36 ->Indent(0)
37 ->Dump
38 ;
39 }
40 }
41  
42 1 print "\n";
43 1 my $i_key_length = 0;
44 1 for my $s_key ( keys %h_print_elements ) {
45 4 ▶ if ( length $s_key > $i_key_length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $s_key > $i_key_length) 2 2 4
46 2 $i_key_length = length $s_key;
47 }
48 }
49 1 for my $s_key (
50 sort {
51 5 $h_print_elements{$a}[0] <=> $h_print_elements{$b}[0]
52 } keys %h_print_elements
53 ) {
54 4 printf ' %' . $i_key_length . "s: %s\n", $s_key, $h_print_elements{$s_key}[1];
55 }
56  
57 1 return 1;
58  
59 }
60  
61  
62 1 ▶sub b_print_preconditions {
Subroutine b_print_preconditions: called CC=3 SCAR=11.3
Pod documented
63  
64 1 my ( $or_preconditions, $hr_options ) = @_;
65  
66 1 ▶ if ( $or_preconditions->isa('DBIx::Class::ResultSet') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($or_preconditions->isa('DBIx::Class::ResultSet')) { } 1 0 1
67 1 for my $or_precondition ( $or_preconditions->all ) {
68 1 b_print_single_precondition(
69 $or_precondition,
70 $hr_options,
71 );
72 }
73 }
74 else {
75 0 b_print_single_precondition(
76 $or_preconditions,
77 $hr_options,
78 );
79 }
80 1 print "\n";
81  
82 1 return 1;
83  
84 }
85  
86  
87 3 ▶sub ar_get_list_parameters {
Subroutine ar_get_list_parameters: called CC=1 SCAR=0.0
Pod undocumented
88 return [
89 3 [ 'id|i=i@' , 'list particular preconditions by id', 'Can be given multiple times.', ],
90 [ 'testrun|t=i@' , 'list particular preconditions by testrun id', 'Can be given multiple times.', ],
91 [ 'limit|l=i' , "limit the number of testruns (default = $i_limit_default). A value smaller than 1 deactivates the limit.", ],
92 [ 'verbose|v' , 'print all output, without only print ids', ],
93 [ 'nonewlines|n' , 'no newslines in precondition ( available in verbose mode )', ],
94 [ 'quotevalues|q' , 'quote every value ( available in verbose mode )', ],
95 [ 'help|?' , 'Print this help message and exit.', ],
96 ];
97 }
98  
99  
100 3 ▶sub b_list {
Subroutine b_list: called CC=11 SCAR=29.4
Pod documented
101  
102 3 my ( $or_app_rad ) = @_;
103  
104 3 my $or_querylog;
105  
106 3 require Tapper::Model;
107 3 my $or_schema = Tapper::Model::model('TestrunDB');
108  
109 3 ▶ if ( $ENV{DEBUG} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"DEBUG"}) 0 3 3
110 0 require DBIx::Class::QueryLog;
111 0 $or_querylog = DBIx::Class::QueryLog->new();
112 0 $or_schema->storage->debugobj( $or_querylog );
113 0 $or_schema->storage->debug( 1 );
114 }
115  
116 3 my $ar_parameters = ar_get_list_parameters();
117 3 $or_app_rad->getopt( map { $_->[0] } @{$ar_parameters} );
118 3 my %h_options = %{$or_app_rad->options};
119  
120 3 ▶ if ( $h_options{help} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h_options{"help"}) 0 3 3
121 0 say {*STDERR} "Usage: $PROGRAM_NAME precondition-list [options]";
122 0 require Tapper::CLI::Base;
123 0 Tapper::CLI::Base::b_print_help( $ar_parameters );
124 0 return;
125 }
126  
127 3 my $hr_search;
128 3 my $hr_search_options = {
129 order_by => { -desc => 'me.id' },
130 };
131  
132 3 ▶ if ( exists $h_options{limit} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $h_options{'limit'}) { } 0 3 3
133 0 ▶ if ( $h_options{limit} > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h_options{"limit"} > 0) 0 0 0
134 0 $hr_search_options->{rows} = $h_options{limit};
135 }
136 }
137 else {
138 3 $hr_search_options->{rows} = $i_limit_default;
139 }
140  
141 3 ▶ if ( my $ar_precondition_ids = $h_options{id} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $ar_precondition_ids = $h_options{'id'}) { } 1 2 3
elsif (my $ar_testrun_ids = $h_options{'testrun'}) { } 1 1 2
142  
143 1 ▶ if ( $h_options{testrun} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h_options{"testrun"}) 0 1 1
144 0 die "error: filter 'testrun' doesn't make sense with filter 'id'\n";
145 }
146  
147 # filter 'id' doesn't make sense without verbose
148 1 $h_options{verbose} = 1;
149  
150 1 $hr_search = { 'me.id' => $ar_precondition_ids }
151 }
152 elsif ( my $ar_testrun_ids = $h_options{testrun} ) {
153  
154 1 $hr_search = { 'testrun_precondition.testrun_id' => $ar_testrun_ids };
155 1 $hr_search_options->{'join'} = 'testrun_precondition';
156  
157 }
158  
159 3 my $or_precondition_rs =
160 $or_schema
161 ->resultset('Precondition')
162 ->search( $hr_search, $hr_search_options )
163 ;
164  
165 3 ▶ if ( $h_options{verbose} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h_options{'verbose'}) { } 1 2 3
166 1 b_print_preconditions( $or_precondition_rs, \%h_options, );
167 }
168 else {
169 2 for my $i_precondition_id ( $or_precondition_rs->get_column('id')->all ) {
170 12 say $i_precondition_id;
171 }
172 }
173  
174 3 ▶ if ( $ENV{DEBUG} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"DEBUG"}) 0 3 3
175  
176 0 require DBIx::Class::QueryLog::Analyzer;
177 0 my $or_analyzer = DBIx::Class::QueryLog::Analyzer->new({
178 querylog => $or_querylog,
179 });
180  
181 0 require Data::Dumper;
182 0 say {*STDERR} "Query count: " . scalar( @{$or_analyzer->get_sorted_queries} );
183 0 say {*STDERR} Data::Dumper::Dumper([
184 $or_analyzer->get_sorted_queries
185 ]);
186  
187 }
188  
189 3 return;
190  
191 }
192  
193  
194 2 ▶sub ar_get_delete_parameters {
Subroutine ar_get_delete_parameters: called CC=1 SCAR=0.0
Pod documented
195 return [
196 2 [ 'id|i=i@' , 'delete particular preconditions', 'Can be given multiple times.', ],
197 [ 'force|f' , 'really execute the command', ],
198 [ 'verbose|v' , 'print all output, without only print ids', ],
199 [ 'help|?' , 'Print this help message and exit.', ],
200 ];
201 }
202  
203  
204 2 ▶sub b_delete {
Subroutine b_delete: called CC=7 SCAR=24.2
Pod documented
205  
206 2 my ( $or_app_rad ) = @_;
207  
208 2 my $ar_parameters = ar_get_delete_parameters();
209 2 $or_app_rad->getopt( map { $_->[0] } @{$ar_parameters} );
210 2 my $hr_options = $or_app_rad->options;
211  
212 2 ▶ if ( $hr_options->{help} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hr_options->{"help"}) 0 2 2
213 0 say {*STDERR} "Usage: $PROGRAM_NAME precondition-delete [options]";
214 0 require Tapper::CLI::Base;
215 0 Tapper::CLI::Base::b_print_help( $ar_parameters );
216 0 return;
217 }
218  
219 2 ▶ if (! $hr_options->{id} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hr_options->{"id"}) 0 2 2
220 0 die "error: required parameter 'id' is missing\n";
221 }
222  
223 2 ▶ if (! $hr_options->{force} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hr_options->{"force"}) 0 2 2
224 0 say {*STDERR} "info: Skip deleting preconditions unless --force is used.";
225 0 return;
226 }
227  
228 2 require Tapper::Cmd::Precondition;
229 2 my $or_cmd = Tapper::Cmd::Precondition->new();
230 2 foreach my $i_precondition_id ( @{$hr_options->{id}} ){
231 2 ▶ if ( my $s_error = $or_cmd->del( $i_precondition_id ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $s_error = $or_cmd->del($i_precondition_id)) { } 0 2 2
elsif ($hr_options->{'verbose'}) { } 0 2 2
232 0 die "$s_error\n";
233 }
234 elsif ( $hr_options->{verbose} ) {
235 0 say "info: precondition deleted $i_precondition_id";
236 }
237 }
238  
239 2 return;
240  
241 }
242  
243  
244 0 ▶sub s_read_condition_file {
Subroutine s_read_condition_file: not called CC=3 SCAR=24.8
Pod documented
245  
246 0 my ( $s_condition_file ) = @_;
247  
248 0 my $s_condition;
249 0 ▶ if ( $s_condition_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s_condition_file) { } 0 0 0
250 0 require File::Slurp;
251 0 ▶ $s_condition = File::Slurp::read_file(
Branch: 1 branch
BranchTrueFalseTotal
$s_condition_file eq '-' ? : 0 0 0
252 # read from file or STDIN if filename == '-'
253 $s_condition_file eq q#-# ? \*STDIN : $s_condition_file
254 );
255 }
256 else {
257 0 die "error: missing parameter 'condition file'\n";
258 }
259  
260 0 return $s_condition;
261  
262 }
263  
264  
265 5 ▶sub ar_get_precondition_parameters {
Subroutine ar_get_precondition_parameters: called CC=2 SCAR=6.9
Pod documented
266 5 my ( $s_command ) = @_;
267 return [
268 (
269 5 ▶ $s_command eq 'precondition-update'
Branch: 1 branch
BranchTrueFalseTotal
$s_command eq 'precondition-update' ? : 3 2 5
270 ? ( [ 'id|i=s', 'the precondition id to change', ] )
271 : ()
272 ),
273 [ 'shortname|s=s' , 'shortname', ],
274 [ 'timeout|t=s' , 'stop trying to fullfill this precondition after timeout second', ],
275 [ 'condition|c=s' , 'condition description in YAML format (see Spec)', ],
276 [ 'condition_file|f=s' , 'filename from where to read condition, use - to read from STDIN' ],
277 [ 'verbose|v' , 'some more informational output', ],
278 [ 'help|?' , 'print this help message and exit.', ],
279 ];
280 }
281  
282  
283 5 ▶sub hr_init_precondition_commands {
Subroutine hr_init_precondition_commands: called CC=10 SCAR=26.5
Pod undocumented
284  
285 5 my ( $or_app_rad, $s_command ) = @_;
286  
287 5 my $ar_parameters = ar_get_precondition_parameters( $s_command );
288 5 $or_app_rad->getopt( map { $_->[0] } @{$ar_parameters} );
289 5 my $hr_options = $or_app_rad->options;
290  
291 5 ▶ if ( $hr_options->{help} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hr_options->{"help"}) 0 5 5
292 0 say {*STDERR} "Usage: $PROGRAM_NAME $s_command [options]";
293 0 require Tapper::CLI::Base;
294 0 Tapper::CLI::Base::b_print_help( $ar_parameters );
295 0 return;
296 }
297  
298 5 ▶ if ( !$hr_options->{condition} && !$hr_options->{condition_file} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $hr_options->{'condition'} || $hr_options->{'condition_file'}) { } 0 5 5
elsif ($hr_options->{'condition'} and $hr_options->{'condition_file'}) { } 0 5 5
Condition33%1/3
$hr_options->{'condition'} || $hr_options->{'condition_file'}
l!l&&r!l&&!r
500
MC/DC0%0/2
$hr_options->{'condition'} || $hr_options->{'condition_file'}
$hr_options->{'condition'} $hr_options->{'condition_file'}
Truth table33%1/3
A || B
A= $hr_options->{'condition'}B= $hr_options->{'condition_file'}
ABresult
1X1
011
000
299 0 die "error: missing parameter --condition or --condition_file\n";
300 }
301 ▶ elsif ( $hr_options->{condition} && $hr_options->{condition_file} ) {
Condition33%1/3
$hr_options->{"condition"} and $hr_options->{"condition_file"}
!ll&&!rl&&r
050
MC/DC0%0/2
$hr_options->{"condition"} and $hr_options->{"condition_file"}
$hr_options->{"condition"} $hr_options->{"condition_file"}
Truth table33%1/3
A and B
A= $hr_options->{"condition"}B= $hr_options->{"condition_file"}
ABresult
0X0
100
111
302 0 die "error: only one of --condition or --condition_file allowed\n";
303 }
304  
305 5 my $s_condition = $hr_options->{condition};
306 5 my $s_condition_file = $hr_options->{condition_file};
307  
308 5 ▶ if (! $s_condition ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($s_condition) 0 5 5
309 0 $s_condition = s_read_condition_file( $s_condition_file );
310 }
311  
312 5 ▶ if ( $s_condition !~ /\n$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($s_condition =~ /\n$/) 5 0 5
313 5 $s_condition .= "\n";
314 }
315  
316 5 ▶ if ( my @a_elements = grep { $hr_options->{$_} } qw/ shortname timeout / ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@a_elements) = grep({$hr_options->{$_};} "shortname", "timeout")) 3 2 5
317  
318 3 require YAML::Syck;
319 3 my @a_yaml = YAML::Syck::Load( $s_condition );
320 3 for my $hr_yaml ( @a_yaml ) {
321 3 @{$hr_yaml}{@a_elements} = @{$hr_options}{@a_elements};
322 }
323 3 $s_condition = YAML::Syck::Dump( @a_yaml );
324  
325 }
326  
327 return {
328 5 condition => $s_condition,
329 options => $hr_options,
330 };
331  
332 }
333  
334  
335 2 ▶sub b_new {
Subroutine b_new: called CC=4 SCAR=14.6
Pod documented
336  
337 2 my ( $or_app_rad ) = @_;
338  
339 2 my $hr_precond_data = hr_init_precondition_commands(
340 $or_app_rad, 'precondition-new',
341 );
342  
343 2 require Tapper::Model;
344 2 require Tapper::Cmd::Precondition;
345 2 ▶ if ( my @a_precondition_ids = Tapper::Cmd::Precondition->new->add( $hr_precond_data->{condition} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@a_precondition_ids) = "Tapper::Cmd::Precondition"->new->add($hr_precond_data->{"condition"})) 2 0 2
346 2 ▶ if ( $hr_precond_data->{options}{verbose} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hr_precond_data->{'options'}{'verbose'}) { } 0 2 2
347 0 foreach my $i_precondition_id ( @a_precondition_ids ) {
348 0 b_print_single_precondition(
349 Tapper::Model::model('TestrunDB')
350 ->resultset('Precondition')
351 ->search({ id => $i_precondition_id }, { rows => 1 })
352 ->first
353 );
354 }
355 }
356 else {
357 2 print map { $_, "\n" } @a_precondition_ids;
358 }
359 }
360  
361 2 return;
362  
363 }
364  
365  
366 3 ▶sub b_update {
Subroutine b_update: called CC=4 SCAR=14.2
Pod documented
367  
368 3 my ( $or_app_rad ) = @_;
369  
370 3 my $hr_precond_data = hr_init_precondition_commands(
371 $or_app_rad, 'precondition-update',
372 );
373  
374 3 ▶ if (! $hr_precond_data->{options}{id} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hr_precond_data->{"options"}{"id"}) 1 2 3
375 1 die "error: Missing required parameter 'id'\n";
376 }
377  
378 2 require Tapper::Model;
379 2 require Tapper::Cmd::Precondition;
380 2 my $or_cmd = Tapper::Cmd::Precondition->new();
381  
382 2 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($or_cmd->update($hr_precond_data->{'options'}{'id'}, $hr_precond_data->{'condition'})) { } 2 0 2
383 $or_cmd->update(
384 $hr_precond_data->{options}{id},
385 $hr_precond_data->{condition},
386 )
387 ) {
388 2 ▶ if ( $hr_precond_data->{options}{verbose} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hr_precond_data->{'options'}{'verbose'}) { } 0 2 2
389 b_print_single_precondition(
390 Tapper::Model::model('TestrunDB')
391 ->resultset('Precondition')
392 ->find( $hr_precond_data->{options}{id} )
393 0 );
394 }
395 else {
396 2 say $hr_precond_data->{options}{id};
397 }
398 }
399 else {
400 0 die "error: cannot update precondition\n";
401 }
402  
403 2 return;
404  
405 }
406  
407  
408 71 ▶sub setup {
Subroutine setup: called CC=2 SCAR=7.1
Pod documented
409  
410 71 my ( $or_apprad ) = @_;
411  
412 71 $or_apprad->register( 'precondition-list' , \&b_list , 'List preconditions' , );
413 71 $or_apprad->register( 'precondition-new' , \&b_new , 'Create preconditions', );
414 71 $or_apprad->register( 'precondition-update', \&b_update, 'Update preconditions', );
415 71 $or_apprad->register( 'precondition-delete', \&b_delete, 'Delete preconditions', );
416  
417 71 ▶ if ( $or_apprad->can('group_commands') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($or_apprad->can("group_commands")) 0 71 71
418 0 $or_apprad->group_commands(
419 'Testrun commands',
420 'precondition-list',
421 'precondition-new',
422 'precondition-update',
423 'precondition-delete',
424 );
425 }
426  
427 71 return;
428  
429 }
430  
431 72 1; # End of Tapper::CLI
432  
433 __END__
« blib/lib/Tapper/CLI/Init.pm ↑ Summary blib/lib/Tapper/CLI/DbDeploy/Command/saveschema.pm »