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

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