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