blib/lib/Devel/Cover/Report/Codecovbash.pm untested

filter: Statementstmt 0.0% 0 / 69 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 13 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 121 SCAR 50.5
CC
12
Cov
0%
_get_line_coverage
72.0
_get_file_coverage
12.0
_get_file_realpath
12.0
CRAP 156.0
1 package Devel::Cover::Report::Codecovbash;
2  
3 # Nearly all of the code in this package is copied from Pine Mizuna's
4 # Devel::Cover::Report::Codecov distro. This package simply omits the part of
5 # the code that sent the coverage to codecov directly. Instead, we write it to
6 # a file so that we can use codecov's bash script to do the upload instead.
7  
8 use strict;
9 use warnings;
10 use namespace::autoclean;
11  
12 use File::Spec;
13 use JSON::MaybeXS qw( encode_json );
14  
15 our $VERSION = 'v0.43.0'; # VERSION
16  
17 sub report {
18 shift;
19 my $db = shift;
20 my $options = shift;
21  
22 my $json = _get_codecov_json( $options->{file}, $db );
23 my $file = File::Spec->catfile( $options->{outputdir}, 'codecov.json' );
24 open my $fh, '>', $file
25 or die "Cannot write to $file: $!";
26 print {$fh} $json
27 or die "Cannot write to $file: $!";
28 close $fh
29 or die "Cannot write to $file: $!";
30 }
31  
32 sub _get_codecov_json {
33 my ( $files, $db ) = @_;
34  
35 my %coverages = map { _get_file_coverage( $_, $db ) } @$files;
36 return encode_json( { coverage => \%coverages, messages => {} } );
37 }
38  
39 sub _get_file_coverage {
40 my ( $filepath, $db ) = @_;
41  
42 my $realpath = _get_file_realpath($filepath);
43 my $lines = _get_file_lines($realpath);
44 my $file = $db->cover->file($filepath);
45 my $statements = $file->statement;
46 my $branches = $file->branch;
47 my @coverage = (undef);
48  
49 for my $i ( 1 .. $lines ) {
50 unless (defined $statements) {
51 push @coverage, undef;
52 next;
53 }
54 my $statement = $statements->location($i);
55 my $branch = defined $branches ? $branches->location($i) : undef;
56 push @coverage, _get_line_coverage( $statement, $branch );
57 }
58  
59 return $realpath => \@coverage;
60 }
61  
62 sub _get_file_realpath {
63 my $file = shift;
64  
65 if ( -d 'blib' ) {
66 my $realpath = $file;
67 $realpath =~ s/blib\/lib/lib/;
68  
69 return $realpath if -f $realpath;
70 }
71  
72 return $file;
73 }
74  
75 sub _get_file_lines {
76 my ($file) = @_;
77  
78 my $lines = 0;
79  
80 open my $fp, '<', $file
81 or die "Cannot read $file: $!";
82 $lines++ while <$fp>;
83 close $fp
84 or die "Cannot read $file: $!";
85  
86 return $lines;
87 }
88  
89 sub _get_line_coverage {
90 my ( $statement, $branch ) = @_;
91  
92 # If all branches covered or uncoverable, report as all covered
93 return $branch->[0]->total . '/' . $branch->[0]->total
94 if $branch && !$branch->[0]->error;
95 return $branch->[0]->covered . '/' . $branch->[0]->total if $branch;
96 return $statement unless $statement;
97 return undef if $statement->[0]->uncoverable;
98 # In some cases it can happen that the first statement on the line is
99 # reported as uncovered and the second as covered. In this case we want
100 # to report the line as covered as well.
101 my @covered = grep { $_->covered } @$statement;
102 return 0 unless @covered;
103 if ($ENV{DEVEL_COVER_CODECOVBASH_COVER_ALL_STATEMENTS}) {
104 return @covered == @$statement ? 1 : 0;
105 }
106 return $covered[0]->covered;
107 }
108  
109 1;
110  
111 # ABSTRACT: Generate a JSON file to be uploaded with the codecov bash script.
112  
113 __END__
114  
115 =encoding utf-8
116  
117 =head1 NAME
118  
119 Devel::Cover::Report::Codecovbash
120  
121 =head1 ABSTRACT
122  
123 Generate a JSON file to be uploaded with the codecov bash script.
124  
125 =head1 DESCRIPTION
126  
127 This is a coverage reporter for Codecov. It generates a JSON file that can be
128 uploaded with the bash script provided by codecov. See
129 L<https://docs.codecov.io/docs/about-the-codecov-bash-uploader> for details.
130  
131 The generated file will be named F<codecov.json> and will be in the
132 F<cover_db> directory by default.
133  
134 Nearly all of the code in this distribution was simply copied from Pine
135 Mizune's
136 L<Devel-Cover-Report-Codecov|https://metacpan.org/release/Devel-Cover-Report-Codecov>
137 distribution.
138  
139 =head1 UPLOADING RESULTS
140  
141 Use the codecov bash script:
142  
143 cover -report codecovbash
144 bash <(curl -s https://codecov.io/bash) -t token -f cover_db/codecov.json
145  
146 =head1 CONFIGURATION
147  
148 =over
149  
150 =item C<DEVEL_COVER_CODECOVBASH_COVER_ALL_STATEMENTS>
151  
152 Since version v0.43
153  
154 Codecov measures line coverage, while L<Devel::Cover> returns statement
155 coverage. So we have to decide if we want to count a line as covered if
156 at least B<one> statement or B<all> statements in a line are covered.
157  
158 By default a line is reported as covered if at least one statement is covered.
159  
160 You can set C<DEVEL_COVER_CODECOVBASH_COVER_ALL_STATEMENTS> to true. Then
161 all statements in a line must be covered.
162  
163 DEVEL_COVER_CODECOVBASH_COVER_ALL_STATEMENTS=1 cover -report codecovbash
164  
165 =back
166  
167 =head1 SOURCE
168  
169 The source code repository for Devel-Cover-Report-Codecovbash can be found at
170 L<https://github.com/perlpunk/Devel-Cover-Report-Codecovbash>.
171  
172 =head1 AUTHOR
173  
174 Tina Müller <tinita@cpan.org>
175  
176 =head1 CONTRIBUTORS
177  
178 =for stopwords Dave Rolsky Tina Müller
179  
180 =over 4
181  
182 =item *
183  
184 Dave Rolsky <autarch@urth.org>
185  
186 =item *
187  
188 Tina Müller <cpan2@tinita.de>
189  
190 =back
191  
192 =head1 COPYRIGHT AND LICENSE
193  
194 This software is Copyright (c) 2019 - 2021 by Pine Mizune.
195  
196 This is free software, licensed under:
197  
198 The MIT (X11) License
199  
200 The full text of the license can be found in the
201 F<LICENSE> file included with this distribution.
202  
203 =cut