blib/lib/App/ape/plan.pm

filter: Statementstmt 68.5% 161 / 235 Branchbran 53.8% 42 / 78 Conditioncond 50.0% 17 / 34 MC/DCmcdc 28.5% 6 / 21 Subroutinesub 93.3% 14 / 15 Podpod 100% 2 / 2 totaltotal 62.8% 242 / 385 CC 61 SCAR 46.7
CC
61
Cov
63%
_build_plans
380.0
_print_plan
21.1
run
14.5
CRAP 106.2
↑ Summary blib/lib/App/ape/test.pm »
1 # PODNAME: App::ape::plan
2 # ABSTRACT: plan testing using elasticsearch
3  
4 package App::ape::plan;
5 6 $App::ape::plan::VERSION = '0.001';
6 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 ▶use Getopt::Long qw{GetOptionsFromArray};
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use App::Prove::Elasticsearch::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use App::Prove::State;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use IO::Prompter [ -yesno, -single, -stdio, -style => 'bold' ];
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use List::Util qw{shuffle};
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use File::Basename qw{basename};
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use POSIX qw{strftime};
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 4 ▶sub new {
Subroutine new: called CC=10 SCAR=23.6
Pod documented
19 4 my ($class, @args) = @_;
20  
21 4 my (%options, @conf, $help);
22 GetOptionsFromArray(
23 \@args,
24 'platform=s@' => \$options{platforms},
25 'version=s' => \$options{version},
26 'show' => \$options{show},
27 'prompt' => \$options{prompt},
28 'pairwise' => \$options{pairwise},
29 'all-platforms' => \$options{allplatforms},
30 'recurse' => \$options{recurse},
31 'extension=s@' => \$options{exts},
32 'name' => \$options{name},
33 'requeue' => \$options{requeue},
34 'replay' => \$options{replay},
35 4 'help' => \$help,
36 );
37 4 ▶ $options{platforms} //= [];
Condition100%2/2
$options{"platforms"} //= []
l!l
13
MC/DC100%1/1
$options{"platforms"} //= []
$options{"platforms"}
Truth table100%2/2
A
A= $options{"platforms"}
Aresult
11
00
38  
39 #Deliberately exiting here, as I "unit" test this as the binary
40 4 ▶ pod2usage(0) if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 0 4 4
41  
42 4 ▶ if (!$options{version}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"version"}) 1 3 4
43 1 pod2usage(
44 -exitval => "NOEXIT",
45 -msg => "Insufficient arguments. You must pass --version.",
46 );
47 1 return 2;
48 }
49  
50 3 ▶ if ($options{prompt} && $options{show}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"prompt"} and $options{"show"}) 1 2 3
Condition66%2/3
$options{"prompt"} and $options{"show"}
!ll&&!rl&&r
201
MC/DC50%1/2
$options{"prompt"} and $options{"show"}
$options{"prompt"} $options{"show"}
Truth table66%2/3
A and B
A= $options{"prompt"}B= $options{"show"}
ABresult
0X0
100
111
51 1 pod2usage(
52 -exitval => "NOEXIT",
53 -msg =>
54 "--prompt and --show are mutually exclusive options. You must pass one or the other.",
55 );
56 1 return 3;
57 }
58  
59 #Store platform groups in the configuration to differentiate further plans
60 2 my $conf = App::Prove::Elasticsearch::Utils::process_configuration(@conf);
61  
62 2 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (scalar grep({my $subj = $_; grep {$subj eq $_;} "server.host", "server.port";} keys %$conf) != 2) 1 1 2
63 scalar(
64 grep {
65 2 my $subj = $_;
66 2 grep { $subj eq $_ } qw{server.host server.port}
67 } keys(%$conf)
68 ) != 2
69 ) {
70 1 pod2usage(
71 -exitval => "NOEXIT",
72 -msg =>
73 "Insufficient information provided to associate defect with test results to elasticsearch",
74 );
75 1 return 4;
76 }
77  
78 1 my $self = {};
79  
80 #default platforms to whatever platformer can figure out
81 1 ▶ if (!scalar(@{$options{platforms}}) && !$options{allplatforms}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @{$options{"platforms"};} or $options{"allplatforms"}) 0 1 1
Condition33%1/3
scalar @{$options{"platforms"};} or $options{"allplatforms"}
l!l&&r!l&&!r
100
MC/DC0%0/2
scalar @{$options{"platforms"};} or $options{"allplatforms"}
scalar @{$options{"platforms"};} $options{"allplatforms"}
Truth table33%1/3
A or B
A= scalar @{$options{"platforms"};}B= $options{"allplatforms"}
ABresult
1X1
011
000
82 0 my $platformer =
83 App::Prove::Elasticsearch::Utils::require_platformer($conf);
84 0 $options{platforms} = &{\&{$platformer . "::get_platforms"}}();
85 }
86  
87 1 $self->{planner} = App::Prove::Elasticsearch::Utils::require_planner($conf);
88 1 &{\&{$self->{planner} . "::check_index"}}($conf);
89  
90 1 my $queue = App::Prove::Elasticsearch::Utils::require_queue($conf);
91 1 $self->{queue} = &{\&{$queue . "::new"}}($queue, \@conf);
92 1 $self->{queue}->{requeue} = $options{requeue};
93  
94 1 $self->{searcher} = $self->{queue}->_get_searcher();
95  
96 #Use Prove's arg parser to grab tests & globs correctly
97 1 my $proveState = App::Prove::State->new();
98 1 ▶ $proveState->extensions($options{exts}) if $options{exts};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"exts"} 1 0 1
99 1 my @tests_filtered = $proveState->get_tests($options{'recurse'}, @args);
100 0 @args = map { basename $_ } grep { -f $_ } @tests_filtered;
101 1 $self->{cases} = \@args;
102  
103 1 $self->{conf} = $conf;
104 1 $self->{options} = \%options;
105  
106 1 return bless($self, $class);
107 }
108  
109 5 ▶sub run {
Subroutine run: called CC=14 SCAR=26.7
Pod documented
110 5 my $self = shift;
111  
112 my @plans = _build_plans(
113 $self->{planner}, $self->{conf}, $self->{cases},
114 5 %{$self->{options}}
115 );
116  
117 5 my $global_result = 0;
118 5 my $queue_result = 0;
119 5 foreach my $plan (@plans) {
120  
121 5 ▶ if ($self->{options}{show}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"show"}) 1 4 5
122 1 ▶ $plan->{replay} = $self->{cases} if $self->{options}{replay};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"options"}{"replay"} 0 1 1
123  
124 #Get the state of the plan
125 1 $plan->{state} = [];
126 1 @{$plan->{state}} = &{\&{$self->{planner} . "::get_plan_status"}}
127 1 ($plan, $self->{searcher});
128  
129 1 _print_plan($plan, 1);
130 1 next;
131 }
132 4 ▶ if ($self->{options}{prompt}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"prompt"}) 3 1 4
133 3 _print_plan($plan);
134 3 ▶ if (!$plan->{noop}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $plan->{'noop'}) { } 2 1 3
135 2 ▶ IO::Prompter::prompt("Do you want to enact the above changes?")
Branch: 1 branch
BranchTrueFalseTotal
IO::Prompter::prompt("Do you want to enact the above changes?") or next 2 0 2
136 or next;
137 } else {
138 (
139 IO::Prompter::prompt("Do you want to re-queue the plan?")
140 or next
141 1 ▶ ) unless $self->{options}{requeue};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"options"}{"requeue"} 1 0 1
Condition40%2/5
$self->{'options'}{'requeue'} || (IO::Prompter::prompt('Do you want to re-queue the plan?') || (next))
l!l&&r!l&&!r
010
IO::Prompter::prompt("Do you want to re-queue the plan?") or next
l!l
10
MC/DC0%0/2
$self->{'options'}{'requeue'} || (IO::Prompter::prompt('Do you want to re-queue the plan?') || (next))
$self->{'options'}{'requeue'} IO::Prompter::prompt("Do you want to re-queue the plan?")
Truth table33%1/3
A || B
A= $self->{'options'}{'requeue'}B= IO::Prompter::prompt("Do you want to re-queue the plan?")
ABresult
1X1
011
000
142 1 $self->{queue}->{requeue} = 1;
143 1 $queue_result += $self->{queue}->queue_jobs($plan);
144 1 next;
145 }
146 }
147  
148 #Ensure bogus data doesn't get into ES
149 3 delete $plan->{replay};
150 3 delete $plan->{requeue};
151  
152 $global_result +=
153 3 &{\&{$self->{planner} . "::add_plan_to_index"}}($plan);
154 $queue_result += $self->{queue}->queue_jobs($plan)
155 3 ▶ if !$plan->{noop} || $self->{options}{requeue};
Branch: 1 branch
BranchTrueFalseTotal
if not $plan->{"noop"} or $self->{"options"}{"requeue"} 3 0 3
Condition66%2/3
not $plan->{"noop"} or $self->{"options"}{"requeue"}
l!l&&r!l&&!r
210
MC/DC0%0/2
not $plan->{"noop"} or $self->{"options"}{"requeue"}
not $plan->{"noop"} $self->{"options"}{"requeue"}
Truth table66%2/3
not(A) or B
A= not $plan->{"noop"}B= $self->{"options"}{"requeue"}
ABresult
1X1
011
000
156 }
157 5 ▶ print "$global_result plans failed to be created, examine above output\n"
Branch: 1 branch
BranchTrueFalseTotal
if $global_result 1 4 5
158 if $global_result;
159 5 ▶ print "$queue_result plans failed to be queued, examine above output\n"
Branch: 1 branch
BranchTrueFalseTotal
if $queue_result 1 4 5
160 if $queue_result;
161 5 ▶ return $global_result ? 2 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$global_result ? : 1 4 5
162 }
163  
164 0 ▶sub _build_plans {
Subroutine _build_plans: not called CC=19 SCAR=59.4
165 0 my ($planner, $conf, $tests, %options) = @_;
166  
167 0 my @plans;
168 0 my @pgroups = grep { $_ =~ m/PlatformGroups/ } keys(%$conf);
169  
170 #filter groups by what we actually passed, if we have any
171 0 ▶ if (scalar(@{$options{platforms}}) && !$options{allplatforms}) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$options{"platforms"};} and not $options{"allplatforms"}) 0 0 0
Condition0%0/3
scalar @{$options{"platforms"};} and not $options{"allplatforms"}
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @{$options{"platforms"};} and not $options{"allplatforms"}
scalar @{$options{"platforms"};} not $options{"allplatforms"}
Truth table0%0/3
A and not(B)
A= scalar @{$options{"platforms"};}B= not $options{"allplatforms"}
ABresult
0X0
100
111
172  
173 0 foreach my $grp (@pgroups) {
174 0 @{$conf->{$grp}} = grep {
175 0 my $grp = $_;
176 0 grep { $grp eq $_ } @{$options{platforms}};
177 0 } @{$conf->{$grp}};
178 0 ▶ delete $conf->{$grp} unless scalar(@{$conf->{$grp}});
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$conf->{$grp};} 0 0 0
179 }
180 0 @pgroups = grep { $_ =~ m/PlatformGroups/ } keys(%$conf);
181 }
182  
183 0 ▶ if (scalar(@pgroups)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @pgroups) { } 0 0 0
184  
185 #break out the groups depending if we are pairwise or not
186 0 ▶ if ($options{pairwise}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{'pairwise'}) { } 0 0 0
187  
188 #Randomize execution order
189 0 @$tests = shuffle(@$tests);
190  
191 # The idea here is to have at least one pigeon in each hole.
192 # This is accomplished by finding the longest list of groups, and then iterating over everything we have modulo their size.
193 0 my $longest;
194 0 foreach my $pgroup (@pgroups) {
195 0 ▶ $longest ||= $pgroup;
Condition0%0/3
$longest ||= $pgroup
l!l&&r!l&&!r
000
MC/DC0%0/2
$longest ||= $pgroup
$longest $pgroup
Truth table0%0/3
A ||= B
A= $longestB= $pgroup
ABresult
1X1
011
000
196 $longest = $pgroup
197 0 ▶ if scalar(@{$conf->{$pgroup}}) > scalar(@{$conf->{$longest}});
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$conf->{$pgroup};} > scalar @{$conf->{$longest};} 0 0 0
198 }
199  
200 0 my @last_tests_apportioned;
201 0 ▶ for (my $i = 0; $i < scalar(@{$conf->{$longest}}); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$conf->{$longest};}) 0 0 0
202 0 my %cloned = %options;
203 0 my @newplats;
204 0 foreach my $pgroup (@pgroups) {
205 0 my $idx = $i % scalar(@{$conf->{$pgroup}});
206 0 push(@newplats, $conf->{$pgroup}->[$idx]);
207 }
208 0 $cloned{platforms} = \@newplats;
209  
210 #Figure out how many tests to dole out to the run
211 0 my @tests_apportioned;
212 my $tests_picked =
213 0 int(scalar(@$tests) / scalar(@{$conf->{$longest}}));
214 0 for (0 .. $tests_picked) {
215 0 my $picked = shift @$tests;
216 0 ▶ push(@tests_apportioned, $picked) if $picked;
Branch: 1 branch
BranchTrueFalseTotal
if $picked 0 0 0
217 }
218  
219 #Handle the corner case where we are passed less tests than we have platforms
220 @tests_apportioned = @last_tests_apportioned
221 0 ▶ if !scalar(@tests_apportioned);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @tests_apportioned 0 0 0
222 0 @last_tests_apportioned = @tests_apportioned;
223  
224 0 push(
225 @plans,
226 _build_plan($planner, \@tests_apportioned, %cloned)
227 );
228 }
229 } else {
230  
231 #construct iterator
232 0 my @pigeonholes = map { $conf->{$_} } @pgroups;
233  
234 0 my @iterator = @{$pigeonholes[0]};
235 0 ▶ while (scalar(@iterator)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @iterator) 0 0 0
236 0 my $subj = shift @iterator;
237  
238 #Handle initial elements
239 0 ▶ $subj = [$subj] if ref $subj ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $subj ne "ARRAY" 0 0 0
240  
241 #Break out of the loop if we have no more possibilities to exploit
242 0 ▶ if (scalar(@$subj) == scalar(@pigeonholes)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$subj == scalar @pigeonholes) 0 0 0
243 0 my %cloned = %options;
244 0 $cloned{platforms} = $subj;
245 0 push(@plans, _build_plan($planner, $tests, %cloned));
246 0 next;
247 }
248  
249 #Keep pushing partials on to the end of the iterator, until we run out of categories to add
250 0 foreach my $element (@{$pigeonholes[ scalar(@$subj) ]}) {
251 0 my @partial = @$subj;
252 0 push(@partial, $element);
253 0 push(@iterator, \@partial);
254 }
255 }
256  
257 }
258 } else {
259 0 push(@plans, _build_plan($planner, $tests, %options));
260 }
261  
262 #TODO inject creator & created time into plans
263 @plans =
264 0 map { $_->{created} = strftime("%Y-%m-%d %H:%M:%S", localtime()); $_ }
265 @plans;
266  
267 0 return @plans;
268 }
269  
270 2 ▶sub _build_plan {
Subroutine _build_plan: called CC=2 SCAR=6.9
271 2 my ($planner, $tests, %options) = @_;
272 2 $options{tests} = $tests;
273  
274 #First, see if we already have a plan like this.
275 2 my $existing = &{\&{$planner . "::get_plan"}}(%options);
276  
277 #If not, make the plan. Otherwise, construct the update statements needed to 'make it so'.
278 2 ▶ if (!$existing) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $existing) { } 1 1 2
279 1 $existing = &{\&{$planner . "::make_plan"}}(%options);
280 } else {
281 1 $existing = &{\&{$planner . "::make_plan_update"}}($existing, %options);
282 }
283  
284 2 return $existing;
285 }
286  
287 7 ▶sub _print_plan {
Subroutine _print_plan: called CC=20 SCAR=30.5
288 7 my ($plan, $force) = @_;
289 7 ▶ if (!$plan->{noop} || $force) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $plan->{'noop'} or $force) { } 6 1 7
Condition100%3/3
not $plan->{"noop"} or $force
l!l&&r!l&&!r
421
MC/DC100%2/2
not $plan->{"noop"} or $force
not $plan->{"noop"} $force
Truth table100%3/3
not(A) or B
A= not $plan->{"noop"}B= $force
ABresult
1X1
011
000
290 6 ▶ print "Name: $plan->{name}\n" if $plan->{name};
Branch: 1 branch
BranchTrueFalseTotal
if $plan->{"name"} 6 0 6
291 6 print "SUT version: $plan->{version}\n";
292 6 print "Platforms: " . join(', ', @{$plan->{platforms}}) . "\n";
293 print "Pairwise? "
294 6 ▶ . ($plan->{pairwise} ne 'false' ? 'yes' : 'no') . "\n";
Branch: 1 branch
BranchTrueFalseTotal
$plan->{'pairwise'} ne 'false' ? : 6 0 6
295 6 print "Created at $plan->{created}\n";
296 6 print "=========================\n";
297 6 ▶ if ($plan->{state}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plan->{'state'}) { } 4 2 6
298 4 foreach my $t (@{$plan->{state}}) {
299 8 ▶ if ($plan->{replay} && $t->{body}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plan->{"replay"} and $t->{"body"}) 2 6 8
Condition66%2/3
$plan->{"replay"} and $t->{"body"}
!ll&&!rl&&r
602
MC/DC50%1/2
$plan->{"replay"} and $t->{"body"}
$plan->{"replay"} $t->{"body"}
Truth table66%2/3
A and B
A= $plan->{"replay"}B= $t->{"body"}
ABresult
0X0
100
111
300 next
301 2 if (scalar(@{$plan->{replay}})
302 2 ▶ && !grep { $_ eq $t->{name} } @{$plan->{replay}});
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$plan->{"replay"};} and not grep({$_ eq $t->{"name"};} @{$$plan{"replay"};}) 0 2 2
Condition33%1/3
scalar @{$plan->{"replay"};} and not grep({$_ eq $t->{"name"};} @{$$plan{"replay"};})
!ll&&!rl&&r
020
MC/DC0%0/2
scalar @{$plan->{"replay"};} and not grep({$_ eq $t->{"name"};} @{$$plan{"replay"};})
scalar @{$plan->{"replay"};} not grep({$_ eq $t->{"name"};} @{$$plan{"replay"};})
Truth table33%1/3
A and not(B)
A= scalar @{$plan->{"replay"};}B= not grep({$_ eq $t->{"name"};} @{$$plan{"replay"};})
ABresult
0X0
100
111
303 2 print "\n$t->{name}..\n";
304 print "Test Version: $t->{test_version}\n"
305 2 ▶ if $t->{test_version};
Branch: 1 branch
BranchTrueFalseTotal
if $t->{"test_version"} 2 0 2
306 2 print "=========================\n";
307 2 print "$t->{body}";
308 }
309  
310 8 my $pln = '';
311 8 ▶ if ( ($t->{status} ne 'UNTESTED')
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{"status"} ne "UNTESTED" and ref $t->{"steps"} eq "ARRAY") 4 4 8
Condition66%2/3
$t->{"status"} ne "UNTESTED" and ref $t->{"steps"} eq "ARRAY"
!ll&&!rl&&r
404
MC/DC50%1/2
$t->{"status"} ne "UNTESTED" and ref $t->{"steps"} eq "ARRAY"
$t->{"status"} ne "UNTESTED" ref $t->{"steps"} eq "ARRAY"
Truth table66%2/3
A and B
A= $t->{"status"} ne "UNTESTED"B= ref $t->{"steps"} eq "ARRAY"
ABresult
0X0
100
111
312 && (ref($t->{steps}) eq 'ARRAY')) {
313 4 my $executed = scalar(@{$t->{steps}});
314 4 my $planned = $t->{steps_planned};
315 4 $pln = "$executed/$planned ";
316 }
317 8 printf "%-60s %-10s %s\n", $t->{name}, $pln, $t->{status};
318 }
319 } else {
320 2 foreach my $t (@{$plan->{tests}}) {
321 4 print "$t\n";
322 }
323 2 ▶ if ($plan->{update}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plan->{"update"}) 2 0 2
324 2 ▶ if (ref $plan->{update}->{subtraction}->{tests} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $plan->{"update"}{"subtraction"}{"tests"} eq "ARRAY") 2 0 2
325 2 print "\nRemove the following from the plan:\n";
326 2 print "=========================\n";
327 2 foreach my $t (@{$plan->{update}->{subtraction}->{tests}}) {
328 2 print "$t\n";
329 }
330 }
331 2 ▶ if (ref $plan->{update}->{addition}->{tests} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $plan->{"update"}{"addition"}{"tests"} eq "ARRAY") 2 0 2
332 2 print "\nAdd the following to the plan:\n";
333 2 print "=========================\n";
334 2 foreach my $t (@{$plan->{update}->{addition}->{tests}}) {
335 2 print "$t\n";
336 }
337 }
338 }
339 }
340 } else {
341 1 print "Plan already exists, and no updates will be made.\n";
342 }
343 }
344  
345 6 1;
346  
347 __END__
↑ Summary blib/lib/App/ape/test.pm »