File Coverage

blib/lib/Dist/Zilla/App/Command/coverh.pm
Criterion Covered Total %
statement 11 65 16.9
branch 0 12 0.0
condition 0 3 0.0
subroutine 4 9 44.4
pod 3 5 60.0
total 18 94 19.1


line stmt bran cond sub pod time code
1 1     1   101305 use 5.10.1;
  1         14  
2 1     1   5 use strict;
  1         7  
  1         21  
3 1     1   5 use warnings;
  1         2  
  1         80  
4              
5             package Dist::Zilla::App::Command::coverh;
6              
7             our $VERSION = '0.0101';
8             # ABSTRACT: Code coverage metrics, with history
9             our $AUTHORITY = 'cpan:CSSON'; # AUTHORITY
10              
11 1     1   469 use Dist::Zilla::App -command;
  1         43925  
  1         14  
12              
13 0     0 1   sub abstract { 'code coverage metrics, with history' }
14              
15             sub opt_spec {
16 0     0 1   ['history' => 'Show history'],
17             }
18              
19             sub execute {
20 0     0 1   my $self = shift;
21 0           my $opt = shift;
22 0           my $args = shift;
23              
24 0           require File::chdir;
25 0           require Path::Tiny;
26 0           Path::Tiny->import;
27 0           require File::Temp;
28 0           require DateTime;
29 0           require JSON::MaybeXS;
30              
31 0 0         if($opt->{'history'}) {
32 0           $self->show_history;
33             }
34             else {
35 0           $self->cover;
36             }
37             }
38              
39             sub show_history {
40 0     0 0   my $self = shift;
41 0           my $json = JSON::MaybeXS->new(pretty => 1);
42              
43 0           my $history_file = path('.coverhistory.json');
44 0 0         exit if !$history_file->exists;
45              
46 0           my $history = $json->decode($history_file->slurp);
47 0 0         exit if !scalar @$history;
48              
49 0           printf "%20.20s %-10s %6.6s %6.6s %6.6s %6.6s %6.6s %6.6s\n" =>
50             'version',
51             'date......',
52             '...tot',
53             '..stmt',
54             '..bran',
55             '..cond',
56             '...sub',
57             '...pod';
58              
59 0           for my $hist (@$history) {
60             printf "%20s %10s %5.1f%% %5.1f%% %5.1f%% %5.1f%% %5.1f%% %5.1f%%\n" =>
61             $hist->{'version'},
62             substr($hist->{'created_at'}, 0, 10),
63             $hist->{'total'},
64             $hist->{'statement'},
65             $hist->{'branch'},
66             $hist->{'condition'},
67             $hist->{'subroutine'},
68 0           $hist->{'pod'};
69             }
70              
71             }
72              
73             sub cover {
74 0     0 0   my $self = shift;
75              
76 0           my $json = JSON::MaybeXS->new(pretty => 1);
77 0           local $ENV{'HARNESS_PERL_SWITCHES'} = '-MDevel::Cover';
78              
79 0           my @cover_command = qw/cover -report json/;
80 0           my $zilla = $self->zilla;
81 0           my $dist_dir = path('.');
82 0           my $build_dir = $dist_dir->child('.build');
83 0 0         $build_dir->mkpath if !$build_dir->is_dir;
84              
85 0           my $cover_dir = path(File::Temp::tempdir(DIR => $build_dir));
86 0           $self->log("building distribution for coverage testing in @{[ $cover_dir->stringify ]}");
  0            
87              
88 0           $zilla->ensure_built_in($cover_dir);
89 0           $self->zilla->run_tests_in($cover_dir);
90              
91 0           $self->log(join ' ' => @cover_command);
92              
93             {
94 0           local $File::chdir::CWD = $cover_dir;
  0            
95 0           system @cover_command;
96             }
97 0           $self->log("leaving $cover_dir intact");
98              
99 0           my $full_summary = $json->decode($cover_dir->child(qw/cover_db cover.json/)->slurp)->{'summary'}{'Total'};
100 0           my $summary = { map { ($_ => $full_summary->{ $_ }{'percentage'}) } keys %$full_summary };
  0            
101 0           $summary->{'created_at'} = DateTime->now->strftime('%Y-%m-%dT%H:%M:%SZ');
102 0           $summary->{'version'} = $zilla->distmeta->{'version'};
103              
104 0           my $log_file = $dist_dir->child('.coverhistory.json');
105 0 0         if(!$log_file->exists) {
106 0           $log_file->spew($json->encode([]));
107             }
108              
109 0           my $log = $json->decode($log_file->slurp);
110 0 0 0       if(scalar @$log && $log->[-1]{'version'} eq $summary->{'version'}) {
111 0           $log->[-1] = $summary;
112             }
113             else {
114 0           push @$log => $summary;
115             }
116 0           $log_file->spew($json->encode($log));
117              
118             }
119              
120             1;
121              
122             __END__
123              
124             =pod
125              
126             =encoding UTF-8
127              
128             =head1 NAME
129              
130             Dist::Zilla::App::Command::coverh - Code coverage metrics, with history
131              
132              
133              
134             =begin html
135              
136             <p>
137             <img src="https://img.shields.io/badge/perl-5.10.1+-blue.svg" alt="Requires Perl 5.10.1+" />
138             <a href="https://travis-ci.org/Csson/p5-Dist-Zilla-App-Command-coverh"><img src="https://api.travis-ci.org/Csson/p5-Dist-Zilla-App-Command-coverh.svg?branch=master" alt="Travis status" /></a>
139             <a href="http://cpants.cpanauthors.org/release/CSSON/Dist-Zilla-App-Command-coverh-0.0101"><img src="http://badgedepot.code301.com/badge/kwalitee/CSSON/Dist-Zilla-App-Command-coverh/0.0101" alt="Distribution kwalitee" /></a>
140             <a href="http://matrix.cpantesters.org/?dist=Dist-Zilla-App-Command-coverh%200.0101"><img src="http://badgedepot.code301.com/badge/cpantesters/Dist-Zilla-App-Command-coverh/0.0101" alt="CPAN Testers result" /></a>
141             <img src="https://img.shields.io/badge/coverage-19.1%-red.svg" alt="coverage 19.1%" />
142             </p>
143              
144             =end html
145              
146             =head1 VERSION
147              
148             Version 0.0101, released 2020-02-29.
149              
150             =head1 SYNOPSIS
151              
152             $ dzil coverh
153             $ dzil coverh --history
154              
155             =head1 DESCRIPTION
156              
157             Dist::Zilla::App::Command::coverh is a command extension for L<Dist::Zilla>. It provides the C<coverh> command, which generates code coverage metrics (in json format)
158             for the current distribution using L<Devel::Cover>. It appends the summary to a C<.coverhistory.json> file in the distribution root.
159              
160             Tests must pass for this to work. Author and release tests are not run. No command-line arguments are passed on to
161             the C<cover> command from L<Devel::Cover>.
162              
163             =head2 Options
164              
165             =head3 --history
166              
167             Prints the contents of the log file:
168              
169             version date...... ...tot ..stmt ..bran ..cond ...sub ...pod
170             0.0001 2016-01-16 25.8% 22.4% 0.0% 0.0% 66.7% 100.0%
171             0.0002 2016-01-17 68.0% 79.8% 23.4% 19.4% 83.0% 100.0%
172              
173             =head1 NOTE
174              
175             Depending on which VersionProvider is in use, the version number logged in the log file may or may not correspond to the version number
176             on cpan (for the same I<version> of the distribution).
177              
178             Only the latest run of C<coverh> for a certain version number is kept in the log file.
179              
180             =head1 SEE ALSO
181              
182             =over 4
183              
184             =item *
185              
186             L<Dist::Zilla::App::Command::cover>
187              
188             =item *
189              
190             L<Devel::Cover>
191              
192             =back
193              
194             =head1 ACKNOWLEDGEMENTS
195              
196             Some parts were borrowed from L<Dist::Zilla::App::Command::cover>.
197              
198             =head1 SOURCE
199              
200             L<https://github.com/Csson/p5-Dist-Zilla-App-Command-coverh>
201              
202             =head1 HOMEPAGE
203              
204             L<https://metacpan.org/release/Dist-Zilla-App-Command-coverh>
205              
206             =head1 AUTHOR
207              
208             Erik Carlsson <info@code301.com>
209              
210             =head1 COPYRIGHT AND LICENSE
211              
212             This software is copyright (c) 2016 by Erik Carlsson.
213              
214             This is free software; you can redistribute it and/or modify it under
215             the same terms as the Perl 5 programming language system itself.
216              
217             =cut