blib/lib/Badge/Depot/Plugin/Coverage.pm

filter: Statementstmt 78.3% 65 / 83 Branchbran 27.7% 5 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 17 / 17 Podpod 0.0% 0 / 4 totaltotal 71.3% 87 / 122 CC 10 SCAR 19.2
CC
10
Cov
69%
determine_coverage
22.8
determine_color
9.1
BUILD
2.0
CRAP 6.8
↑ Summary
1 2 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use feature qw/say/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package Badge::Depot::Plugin::Coverage;
7  
8 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use MooseX::AttributeShortcuts;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Types::Standard qw/Str Bool Num/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Types::URI qw/Uri/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use JSON::MaybeXS 'decode_json';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use DateTime::Format::RFC3339;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 with 'Badge::Depot';
18  
19 # ABSTRACT: Code coverage plugin for Badge::Depot
20 2 our $AUTHORITY = 'cpan:CSSON'; # AUTHORITY
21 2 our $VERSION = '0.0101';
22  
23 2 has coverage => (
24 is => 'ro',
25 isa => Num,
26 required => 0,
27 predicate => 1,
28 );
29 2 has custom_image_url => (
30 is => 'ro',
31 isa => Uri,
32 coerce => 1,
33 default => 'https://img.shields.io/badge/%s-%s-%s.svg',
34 );
35 2 has text => (
36 is => 'ro',
37 isa => Str,
38 default => 'coverage',
39 );
40 2 has max_age => (
41 is => 'ro',
42 isa => 'Int',
43 default => '60',
44 documentation => q{Include coverage badge if the latest cover run was done less than 'max_age' minutes ago},
45 );
46  
47  
48 sub BUILD {
49 2 ▶ my $self = shift;
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
50  
51 2 my $coverage = $self->determine_coverage;
52 2 ▶ if($coverage == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($coverage == -1) 1 1 2
53 1 $self->log('! Could not determine coverage, skips Coverage badge');
54 1 return;
55 }
56  
57 1 $coverage = sprintf '%.1f', $coverage;
58 1 my $color = $self->determine_color($coverage);
59 1 $self->image_url(sprintf $self->custom_image_url, $self->text, $coverage.'%', $color);
60 1 $self->image_alt(sprintf '%s %s', $self->text, $coverage.'%');
61 1 $self->log("Adds coverage badge ($coverage%)");
62 }
63  
64  
65 sub determine_coverage {
66 2 ▶ my $self = shift;
Subroutine determine_coverage: called CC=5 SCAR=31.3
Pod undocumented
67 2 ▶ return $self->coverage if $self->has_coverage;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_coverage 2 0 2
68  
69 0 my $log_file = path(qw/. .coverhistory.json/);
70  
71 0 ▶ if(!$log_file->exists) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($log_file->exists) 0 0 0
72 0 $self->log('! Could not find cover history file (.coverhistory.json), no coverage to add');
73 0 return -1;
74 }
75  
76 0 my $history = decode_json($log_file->slurp);
77  
78 0 ▶ if(!scalar @$history) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @$history) 0 0 0
79 0 $self->log('! No coverage history in .coverhistory.json, nothing to add');
80 0 return -1;
81 }
82  
83 0 my $summary = $history->[-1];
84 0 my $created_at = DateTime::Format::RFC3339->parse_datetime($summary->{'created_at'});
85 0 my $since = DateTime->now - $created_at;
86  
87 0 my $minutes = $since->years * 1440 * 365
88 + $since->months * 1440 * 30
89 + $since->days * 1440
90 + $since->hours * 60
91 + $since->minutes
92 + $since->seconds / 60;
93  
94 0 ▶ if($minutes > $self->max_age) {
Branch: 1 branch
BranchTrueFalseTotal
if ($minutes > $self->max_age) 0 0 0
95 0 $self->log("! Latest coverage was run @{[ sprintf '%.1f', $minutes ]} minutes ago, maximum allowed age is @{[ $self->max_age ]} minutes, wont add coverage");
96 0 return -1;
97 }
98 0 return $summary->{'total'};
99 }
100 sub determine_color {
101 1 ▶ my $self = shift;
Subroutine determine_color: called CC=5 SCAR=22.0
Pod undocumented
102 1 my $coverage = shift;
103  
104 1 ▶ return $coverage < 75 ? 'red'
Branch: 4 branches
BranchTrueFalseTotal
$coverage < 75 ? : 0 1 1
$coverage < 90 ? : 1 0 1
$coverage < 100 ? : 0 0 0
$coverage == 100 ? : 0 0 0
105 : $coverage < 90 ? 'orange'
106 : $coverage < 100 ? 'yellow'
107 : $coverage == 100 ? 'brightgreen'
108 : 'blue'
109 ;
110 }
111 sub log {
112 2 ▶ my $self = shift;
Subroutine log: called CC=1 SCAR=0.0
Pod undocumented
113 2 my $text = shift;
114  
115 2 say "[Badge/Coverage] $text";
116  
117 };
118  
119 2 __PACKAGE__->meta->make_immutable;
120  
121 2 1;
122  
123 __END__
124  
125 =pod
126  
127 =encoding UTF-8
128  
129 =head1 NAME
130  
131 Badge::Depot::Plugin::Coverage - Code coverage plugin for Badge::Depot
132  
133 =head1 VERSION
134  
135 Version 0.0101, released 2016-02-17.
136  
137 =head1 SYNOPSIS
138  
139 Used standalone:
140  
141 use Badge::Depot::Plugin::Coverage;
142  
143 my $badge = Badge::Depot::Plugin::Coverage->new(coverage => 87);
144  
145 print $badge->to_html;
146  
147 Used together with L<Pod::Weaver::Section::Badges>, in weaver.ini:
148  
149 [Badges]
150 ; other settings
151 badge = Coverage
152  
153 =head1 DESCRIPTION
154  
155 This L<Badge::Depot> badge is meant to be used together with L<Dist::Zilla::App::Command::coverh> (or standalone, as per the synopsis) and creates a coverage badge:
156  
157 =for HTML <p><img src="https://img.shields.io/badge/coverage-87%-orange.svg" /></p>
158  
159 =for markdown ![Coverage 87%](https://img.shields.io/badge/coverage-87%-orange.svg)
160  
161 =head1 ATTRIBUTES
162  
163 =head2 coverage
164  
165 Set the code coverage percentage manually. Should only be used when L<Dist::Zilla::App::Command::coverh> is B<not> used.
166  
167 =head2 custom_image_url
168  
169 Default: C<https://img.shields.io/badge/%s-%s-%s.svg>
170  
171 Override the default image url. It is expected to have three C<sprintf> placeholders: Text, coverage percentage and color.
172  
173 =head2 max_age
174  
175 Default: C<60>
176  
177 When used together with L<Dist::Zilla::App::Command::coverh>, only include the badge if the latest coverage run was less than C<max_age> minutes ago.
178  
179 =head2 text
180  
181 Default: C<coverage>
182  
183 Set a different badge text.
184  
185 =head1 SEE ALSO
186  
187 =over 4
188  
189 =item *
190  
191 L<Badge::Depot>
192  
193 =item *
194  
195 L<Task::Badge::Depot>
196  
197 =back
198  
199 =head1 SOURCE
200  
201 L<https://github.com/Csson/p5-Badge-Depot-Plugin-Coverage>
202  
203 =head1 HOMEPAGE
204  
205 L<https://metacpan.org/release/Badge-Depot-Plugin-Coverage>
206  
207 =head1 AUTHOR
208  
209 Erik Carlsson <info@code301.com>
210  
211 =head1 COPYRIGHT AND LICENSE
212  
213 This software is copyright (c) 2016 by Erik Carlsson.
214  
215 This is free software; you can redistribute it and/or modify it under
216 the same terms as the Perl 5 programming language system itself.
217  
218 =cut
↑ Summary