blib/lib/App/Prove/Elasticsearch/Parser.pm

filter: Statementstmt 85.1% 109 / 128 Branchbran 53.3% 16 / 30 Conditioncond 37.5% 9 / 24 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 93.7% 15 / 16 Podpod 100% 8 / 8 totaltotal 71.6% 157 / 219 CC 25 SCAR 23.4
CC
25
Cov
74%
EOFCallback
22.2
testCallback
9.6
new
5.0
CRAP 10.3
« blib/lib/App/Prove/Elasticsearch/Indexer.pm ↑ Summary blib/lib/App/Prove/Elasticsearch/Searcher/ByName.pm »
1 # PODNAME: App::Prove::Elasticsearch::Parser
2 # ABSTRACT: Capture the output of prove, and upload the results of the test to elasticsearch
3  
4 package App::Prove::Elasticsearch::Parser;
5 2 $App::Prove::Elasticsearch::Parser::VERSION = '0.001';
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use parent qw/TAP::Parser/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use Clone qw{clone};
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use File::Basename qw{basename dirname};
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use POSIX qw{strftime};
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use App::Prove::Elasticsearch::Utils();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶sub new {
Subroutine new: called CC=5 SCAR=16.2
Pod documented
17 2 my ($class, $opts) = @_;
18 2 $opts = clone $opts; #Convenience, if we are passing over and over again...
19  
20 #Load our callbacks
21 2 $opts->{'callbacks'} = {
22 'test' => \&testCallback,
23 'comment' => \&commentCallback,
24 'unknown' => \&unknownCallback,
25 'bailout' => \&bailoutCallback,
26 'EOF' => \&EOFCallback,
27 'plan' => \&planCallback,
28 };
29  
30 my $esopts = {
31 'server.host' => delete $opts->{'server.host'},
32 'server.port' => delete $opts->{'server.port'},
33 'client.indexer' => delete $opts->{'client.indexer'},
34 'client.versioner' => delete $opts->{'client.versioner'} // 'Default',
35 'client.blamer' => delete $opts->{'client.blamer'} // 'Default',
36 'client.platformer' => delete $opts->{'client.platformer'} // 'Default',
37 2 ▶ 'client.autodiscover' => delete $opts->{'client.autodiscover'},
Condition50%3/6
delete $opts->{"client.versioner"} // "Default"
l!l
20
delete $opts->{"client.blamer"} // "Default"
l!l
20
delete $opts->{"client.platformer"} // "Default"
l!l
20
MC/DC0%0/1
delete $opts->{"client.versioner"} // "Default"
delete $opts->{"client.versioner"}
MC/DC0%0/1
delete $opts->{"client.blamer"} // "Default"
delete $opts->{"client.blamer"}
MC/DC0%0/1
delete $opts->{"client.platformer"} // "Default"
delete $opts->{"client.platformer"}
Truth table50%1/2
A
A= delete $opts->{"client.versioner"}
Aresult
11
00
Truth table50%1/2
A
A= delete $opts->{"client.blamer"}
Aresult
11
00
Truth table50%1/2
A
A= delete $opts->{"client.platformer"}
Aresult
11
00
38 };
39  
40 2 my $self = $class->SUPER::new($opts);
41 2 ▶ if (ref($self->{'_iterator'}->{'command'}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"_iterator"}{"command"} eq "ARRAY") 2 0 2
42 2 $self->{'file'} = $self->{'_iterator'}->{'command'}->[-1];
43 2 print "# PROCESSING RESULTS FROM TEST FILE: $self->{'file'}\n";
44 }
45  
46 2 my $indexer = $esopts->{'client.indexer'};
47 2 _require_indexer($indexer);
48 2 my $versioner =
49 App::Prove::Elasticsearch::Utils::require_versioner($esopts);
50 2 my $platformer =
51 App::Prove::Elasticsearch::Utils::require_platformer($esopts);
52 2 my $blamer = App::Prove::Elasticsearch::Utils::require_blamer($esopts);
53  
54 $self->{executor} =
55 2 &{\&{$blamer . "::get_responsible_party"}}($self->{file});
56 2 $self->{sut_version} = &{\&{$versioner . "::get_version"}}($self->{file});
57 2 $self->{platform} = &{\&{$platformer . "::get_platforms"}}();
58 2 $self->{indexer} = $indexer;
59  
60 $self->{test_version} =
61 2 &{\&{$versioner . "::get_file_version"}}($self->{file});
62 2 $self->{steps} = [];
63 2 $self->{starttime} = [ Time::HiRes::gettimeofday() ];
64 2 $self->{es_opts} = $esopts;
65 2 return $self;
66 }
67  
68 1 ▶sub _require_indexer {
Subroutine _require_indexer: called CC=2 SCAR=7.2
69 1 my $indexer = shift;
70 1 ▶ eval "require $indexer" or die "cannot find needed indexer $indexer";
Branch: 1 branch
BranchTrueFalseTotal
eval "require $indexer" or die "cannot find needed indexer $indexer" 1 0 1
71 }
72  
73 # Look for file boundaries, etc.
74 1 ▶sub unknownCallback {
Subroutine unknownCallback: called CC=2 SCAR=7.0
Pod documented
75 1 my ($test) = @_;
76 1 my $self = $test->{'parser'};
77 1 my $line = $test->as_string;
78 1 $self->{'raw_output'} .= "$line\n";
79  
80 #Unofficial "Extensions" to TAP
81 1 my ($status_override) = $line =~ m/^% mark_status=([A-Z|_]*)/;
82 1 ▶ $self->{global_status} = $status_override if $status_override;
Branch: 1 branch
BranchTrueFalseTotal
if $status_override 1 0 1
83  
84 1 return;
85 }
86  
87 # Register the current suite or test desc for use by test callback, if the line begins with the special magic words
88 2 ▶sub commentCallback {
Subroutine commentCallback: called CC=1 SCAR=0.0
Pod documented
89 2 my ($test) = @_;
90 2 my $self = $test->{'parser'};
91 2 my $line = $test->as_string;
92 2 $self->{'raw_output'} .= "$line\n";
93  
94 2 return;
95 }
96  
97 2 ▶sub testCallback {
Subroutine testCallback: called CC=6 SCAR=22.6
Pod documented
98 2 my ($test) = @_;
99 2 my $self = $test->{'parser'};
100  
101 2 my $line = $test->as_string;
102 2 $self->{'raw_output'} .= "$line\n";
103  
104 2 $line =~ s/^(ok|not ok)\s[0-9]*\s-\s//g;
105 2 my $test_name = $line;
106  
107 #Setup args to pass to function
108 2 my $status_name = 'NOT OK';
109 2 ▶ if ($test->is_actual_ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($test->is_actual_ok) { } 2 0 2
110 2 $status_name = 'OK';
111 2 ▶ if ($test->has_skip()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($test->has_skip) 0 2 2
112 0 $status_name = 'SKIP';
113 0 $test_name =~ s/^(ok|not ok)\s[0-9]*\s//g;
114 0 $test_name =~ s/^# skip //gi;
115 }
116 2 ▶ if ($test->has_todo()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($test->has_todo) 0 2 2
117 0 $status_name = 'TODO PASS';
118 0 $test_name =~ s/^(ok|not ok)\s[0-9]*\s//g;
119 0 $test_name =~ s/^# todo & skip //gi; #handle todo_skip
120 0 $test_name =~ s/# todo\s(.*)$//gi;
121 }
122 } else {
123 0 ▶ if ($test->has_todo()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($test->has_todo) 0 0 0
124 0 $status_name = 'TODO FAIL';
125 0 $test_name =~ s/^(ok|not ok)\s[0-9]*\s//g;
126 0 $test_name =~ s/^# todo & skip //gi; #handle todo_skip
127 0 $test_name =~ s/# todo\s(.*)$//gi;
128 }
129 }
130  
131 #XXX much of the above code would be unneeded if $test->description wasn't garbage
132 2 $test_name =~ s/\s+$//g;
133  
134 #Test done. Record elapsed time.
135 2 my $tm = [ Time::HiRes::gettimeofday() ];
136 2 ▶ $self->{lasttime} //= $self->{starttime};
Condition33%1/3
$self->{"lasttime"} //= $self->{"starttime"}
l!l&&r!l&&!r
020
MC/DC0%0/2
$self->{"lasttime"} //= $self->{"starttime"}
$self->{"lasttime"} $self->{"starttime"}
Truth table33%1/3
A //= B
A= $self->{"lasttime"}B= $self->{"starttime"}
ABresult
1X1
011
000
137 push(
138 2 @{$self->{steps}},
139 {
140 2 elapsed => Time::HiRes::tv_interval($self->{'lasttime'}, $tm),
141 step => $test->number, #XXX TODO maybe this isn't right
142 name => $test_name,
143 status => $status_name,
144 }
145 );
146 2 $self->{lasttime} = $tm;
147  
148 2 return 1;
149 }
150  
151 0 ▶sub bailoutCallback {
Subroutine bailoutCallback: not called CC=1 SCAR=6.9
Pod documented
152 0 my ($test) = @_;
153 0 my $self = $test->{'parser'};
154 0 my $line = $test->as_string;
155 0 $self->{'raw_output'} .= "$line\n";
156 0 $self->{'is_bailout'} = 1;
157 0 return;
158 }
159  
160 2 ▶sub EOFCallback {
Subroutine EOFCallback: called CC=14 SCAR=31.0
Pod documented
161 2 my ($self) = @_;
162  
163 #Test done. Record elapsed time.
164 2 $self->{'elapsed'} = Time::HiRes::tv_interval($self->{'starttime'});
165  
166 2 my $todo_failed = $self->todo() - $self->todo_passed();
167  
168 2 my $status = 'OK';
169  
170 2 ▶ $status = 'NOT OK' if $self->has_problems();
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_problems 0 2 2
171  
172 2 ▶ $status = 'TODO PASSED'
Branch: 1 branch
BranchTrueFalseTotal
if $self->todo_passed and not $self->failed and $self->is_good_plan 0 2 2
Condition33%2/6
$self->todo_passed and not $self->failed and $self->is_good_plan
!ll&&!rl&&r
200
$self->todo_passed and not $self->failed
!ll&&!rl&&r
200
MC/DC0%0/3
$self->todo_passed and not $self->failed and $self->is_good_plan
$self->todo_passed not $self->failed $self->is_good_plan
Truth table25%1/4
A and not(B) and C
A= $self->todo_passedB= not $self->failedC= $self->is_good_plan
ABCresult
0XX0
10X0
1100
1111
173 if $self->todo_passed()
174 && !$self->failed()
175 && $self->is_good_plan(); #If no fails, but a TODO pass, mark as TODOP
176  
177 2 ▶ $status = 'TODO FAILED'
Branch: 1 branch
BranchTrueFalseTotal
if $todo_failed and not $self->failed and $self->is_good_plan 0 2 2
Condition33%2/6
$todo_failed and not $self->failed and $self->is_good_plan
!ll&&!rl&&r
200
$todo_failed and not $self->failed
!ll&&!rl&&r
200
MC/DC0%0/3
$todo_failed and not $self->failed and $self->is_good_plan
$todo_failed not $self->failed $self->is_good_plan
Truth table25%1/4
A and not(B) and C
A= $todo_failedB= not $self->failedC= $self->is_good_plan
ABCresult
0XX0
10X0
1100
1111
178 if $todo_failed
179 && !$self->failed()
180 && $self->is_good_plan()
181 ; #If no fails, but a TODO fail, prefer TODOF to TODOP
182  
183 2 ▶ $status = "SKIPPED" if $self->skip_all(); #Skip all, whee
Branch: 1 branch
BranchTrueFalseTotal
if $self->skip_all 0 2 2
184  
185 2 ▶ $status = "BAIL OUT" if $self->{is_bailout};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"is_bailout"} 0 2 2
186  
187 #Global status override
188 2 ▶ $status = $self->{'global_status'} if $self->{'global_status'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"global_status"} 1 1 2
189 2 ▶ return if $status eq 'DISCARD';
Branch: 1 branch
BranchTrueFalseTotal
if $status eq "DISCARD" 1 1 2
190  
191 #Notify user about bad plan a bit better, supposing we haven't bailed
192 1 ▶ if (!$self->is_good_plan() && !$self->{'is_bailout'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_good_plan or $self->{"is_bailout"}) 0 1 1
Condition33%1/3
$self->is_good_plan or $self->{"is_bailout"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->is_good_plan or $self->{"is_bailout"}
$self->is_good_plan $self->{"is_bailout"}
Truth table33%1/3
A or B
A= $self->is_good_planB= $self->{"is_bailout"}
ABresult
1X1
011
000
193 0 $self->{'raw_output'} .=
194 "\n# ERROR: Bad plan. You ran "
195 . $self->tests_run
196 . " tests, but planned "
197 . $self->tests_planned . ".";
198 }
199  
200 $self->{upload} = {
201 body => $self->{raw_output},
202 elapsed => $self->{elapsed},
203 occurred =>
204 strftime("%Y-%m-%d %H:%M:%S", localtime($self->{starttime}->[0])),
205 status => $status,
206 platform => $self->{platform},
207 executor => $self->{executor},
208 version => $self->{sut_version},
209 test_version => $self->{test_version},
210 name => basename($self->{file}),
211 path => dirname($self->{file}),
212 steps => $self->{steps},
213 1 steps_planned => $self->tests_planned
214 };
215  
216 1 &{\&{$self->{indexer} . "::index_results"}}($self->{upload});
217 1 return $status;
218 }
219  
220 2 ▶sub planCallback {
Subroutine planCallback: called CC=1 SCAR=0.0
Pod documented
221 2 my ($plan) = @_;
222 2 my $self = $plan->{'parser'};
223 2 $self->{raw_output} .= $plan->as_string . "\n";
224 }
225  
226 7 ▶sub make_result {
Subroutine make_result: called CC=1 SCAR=0.0
Pod documented
227 7 my ($self, @args) = @_;
228 7 my $res = $self->SUPER::make_result(@args);
229 7 $res->{'parser'} = $self;
230 7 return $res;
231 }
232  
233 2 1;
234  
235 __END__
« blib/lib/App/Prove/Elasticsearch/Indexer.pm ↑ Summary blib/lib/App/Prove/Elasticsearch/Searcher/ByName.pm »