lib/Serge/Engine/Plugin/parse_chrome_json.pm

filter: Statementstmt 24.6% 16 / 65 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 50.0% 5 / 10 Podpod 0.0% 0 / 5 totaltotal 20.1% 21 / 104 SCAR 46.5
CC
13
Cov
19%
report_errors
42.0
parse
42.0
validate_data
12.0
CRAP 104.1
« lib/Serge/Sync/Plugin/VCS/gerrit.pm ↑ Summary lib/Serge/Engine/Plugin/parse_xml.pm »
1 package Serge::Engine::Plugin::parse_chrome_json;
2 1 ▶use parent Serge::Engine::Plugin::Base::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use JSON -support_by_pp; # -support_by_pp is used to make Perl on Mac happy
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Serge::Mail;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Serge::Util qw(xml_escape_strref);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 sub name {
11 0 ▶ return 'Chrome extension messages.json parser plugin';
Subroutine name: not called CC=1 SCAR=6.9
Pod undocumented
12 }
13  
14 sub init {
15 0 ▶ my $self = shift;
Subroutine init: not called CC=1 SCAR=6.9
Pod undocumented
16  
17 0 $self->SUPER::init(@_);
18  
19 0 $self->{errors} = {};
20  
21 0 $self->merge_schema({
22 email_from => 'STRING',
23 email_to => 'ARRAY',
24 email_subject => 'STRING',
25 });
26  
27 0 $self->add('after_job', \&report_errors);
28 }
29  
30 sub validate_data {
31 0 ▶ my $self = shift;
Subroutine validate_data: not called CC=3 SCAR=24.8
Pod undocumented
32  
33 0 $self->SUPER::validate_data;
34  
35 0 ▶ if (!defined $self->{data}->{email_from}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"data"}{"email_from"}) 0 0 0
36 0 print "WARNING: 'email_from' is not defined. Will skip sending any reports.\n";
37 }
38  
39 0 ▶ if (!defined $self->{data}->{email_to}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"data"}{"email_to"}) 0 0 0
40 0 print "WARNING: 'email_to' is not defined. Will skip sending any reports.\n";
41 }
42 }
43  
44 sub report_errors {
45 0 ▶ my ($self, $phase) = @_;
Subroutine report_errors: not called CC=6 SCAR=37.4
Pod undocumented
46  
47 0 my $email_from = $self->{data}->{email_from};
48 0 ▶ if (!$email_from) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($email_from) 0 0 0
49 0 $self->{errors} = {};
50 0 return;
51 }
52  
53 0 my $email_to = $self->{data}->{email_to};
54 0 ▶ if (!$email_to) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($email_to) 0 0 0
55 0 $self->{errors} = {};
56 0 return;
57 }
58  
59 0 ▶ my $email_subject = $self->{data}->{email_subject} || ("[".$self->{parent}->{id}.']: Chrome Extension messages.json Parse Errors');
Condition0%0/2
$self->{'data'}{'email_subject'} || '[' . $self->{'parent'}{'id'} . ']: Chrome Extension messages.json Parse Errors'
l!l
00
MC/DC0%0/1
$self->{'data'}{'email_subject'} || '[' . $self->{'parent'}{'id'} . ']: Chrome Extension messages.json Parse Errors'
$self->{'data'}{'email_subject'}
Truth table0%0/2
A
A= $self->{'data'}{'email_subject'}
Aresult
11
00
60  
61 0 my $text;
62 0 foreach my $key (sort keys %{$self->{errors}}) {
63 0 my $pre_contents = $self->{errors}->{$key};
64 0 xml_escape_strref(\$pre_contents);
65 0 $text .= "<hr />\n<p><b style='color: red'>$key</b> <pre>".$pre_contents."</pre></p>\n";
66 }
67  
68 0 $self->{errors} = {};
69  
70 0 ▶ if ($text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text) 0 0 0
71 0 $text = qq|
72 <html>
73 <head>
74 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
75 </head>
76 <body style="font-family: sans-serif; font-size: 120%">
77  
78 <p>
79 # This is an automatically generated message.
80  
81 The following parsing errors were found when attempting to localize resource files.
82 </p>
83  
84 $text
85  
86 </body>
87 </html>
88 |;
89  
90 0 Serge::Mail::send_html_message(
91 $email_from, # from
92 $email_to, # to (list)
93 $email_subject, # subject
94 $text # message body
95 );
96 }
97 }
98  
99 sub parse {
100 0 ▶ my ($self, $textref, $callbackref, $lang) = @_;
Subroutine parse: not called CC=6 SCAR=37.4
Pod undocumented
101  
102 0 ▶ die 'callbackref not specified' unless $callbackref;
Branch: 1 branch
BranchTrueFalseTotal
unless $callbackref 0 0 0
103  
104 # Parse JSON
105  
106 0 my $messages;
107 0 eval {
108 0 ($messages) = from_json($$textref);
109 };
110 0 ▶ if ($@ || !$messages) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $messages) 0 0 0
Condition0%0/3
$@ or not $messages
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or not $messages
$@ not $messages
Truth table0%0/3
A or not(B)
A= $@B= not $messages
ABresult
1X1
011
000
111 0 my $error_text = $@;
112 0 ▶ if ($error_text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error_text) { } 0 0 0
113 0 $error_text =~ s/\t/ /g;
114 0 $error_text =~ s/^\s+//s;
115 } else {
116 0 $error_text = "from_json() returned empty data structure";
117 }
118  
119 0 $self->{errors}->{$self->{parent}->{engine}->{current_file_rel}} = $error_text;
120  
121 0 die $error_text;
122 }
123  
124 0 foreach my $key (sort keys %$messages) {
125 0 my $msg = $messages->{$key};
126 0 $msg->{message} = &$callbackref($msg->{message}, $key, $msg->{description}, undef, $lang);
127 }
128  
129 # need to force indent_length, otherwise it will be zero when PP extension 'escape_slash' is used
130 # (looks like an issue in some newer version of JSON:PP)
131 # also, force 'canonical' to sort keys alphabetically to ensure the structure won't be changed on subsequent script runs
132 0 return to_json($messages, {pretty => 1, indent_length => 3, canonical => 1, escape_slash => 1});
133 }
134  
135 1 1;
« lib/Serge/Sync/Plugin/VCS/gerrit.pm ↑ Summary lib/Serge/Engine/Plugin/parse_xml.pm »