blib/lib/App/Test/Generator/LCSAJ/Coverage.pm

filter: Statementstmt 100% 44 / 44 Branchbran 100% 12 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 100% 66 / 66 CC 9 SCAR 14.8
CC
9
Cov
100%
merge
8.0
_slurp
2.0
BEGIN
1.0
CRAP 4.4
« blib/lib/App/Test/Generator/LCSAJ.pm ↑ Summary bin/extract-schemas »
1 package App::Test::Generator::LCSAJ::Coverage;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use autodie qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 our $VERSION = '0.50';
11  
12 =head1 NAME
13  
14 App::Test::Generator::LCSAJ::Coverage - Merge LCSAJ path data with runtime hits
15  
16 =head1 VERSION
17  
18 Version 0.50
19  
20 =head1 DESCRIPTION
21  
22 Merges static LCSAJ path data produced by L<App::Test::Generator::LCSAJ>
23 with runtime line hit data to determine which LCSAJ paths were covered
24 during test execution. The merged result is written as JSON for
25 consumption by C<bin/test-generator-index>.
26  
27 =head2 merge
28  
29 Merge a static LCSAJ path JSON file with a runtime line hits JSON file
30 and write the annotated result to an output file.
31  
32 App::Test::Generator::LCSAJ::Coverage::merge(
33 'cover_db/lcsaj/MyModule.pm.lcsaj.json',
34 'cover_db/lcsaj/MyModule.pm.hits.json',
35 'cover_db/lcsaj/MyModule.pm.covered.json',
36 );
37  
38 =head3 Arguments
39  
40 =over 4
41  
42 =item * C<$lcsaj_file>
43  
44 Path to the C<.lcsaj.json> file produced by
45 L<App::Test::Generator::LCSAJ>. Required.
46  
47 =item * C<$hits_file>
48  
49 Path to a JSON file mapping line numbers (as strings) to hit counts,
50 as produced by L<Devel::App::Test::Generator::LCSAJ::Runtime>.
51 Required.
52  
53 =item * C<$out_file>
54  
55 Path to write the merged output JSON file. Required.
56  
57 =back
58  
59 =head3 Returns
60  
61 Nothing. Writes the annotated LCSAJ path data to C<$out_file>, with
62 a C<covered> key added to each path record.
63  
64 =head3 Side effects
65  
66 Writes to C<$out_file>. Croaks if any file cannot be read or written.
67  
68 =head3 Notes
69  
70 A path is considered covered if any line in the range C<start..end>
71 was executed at least once. This is a conservative approximation -
72 it does not verify that the jump target was actually reached. As a
73 result, coverage may be slightly overstated for paths where only the
74 beginning of the sequence was executed.
75  
76 =head3 API specification
77  
78 =head4 input
79  
80 {
81 lcsaj_file => { type => SCALAR },
82 hits_file => { type => SCALAR },
83 out_file => { type => SCALAR },
84 }
85  
86 =head4 output
87  
88 { type => UNDEF }
89  
90 =cut
91  
92 16 ▶sub merge {
Subroutine merge: called CC=8 SCAR=20.8
Pod documented
93 16 my ($lcsaj_file, $hits_file, $out_file) = @_;
94  
95 # Validate all three file arguments before attempting any IO
96 16 ▶ croak 'lcsaj_file required' unless defined $lcsaj_file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lcsaj_file 2 14 16
97 14 ▶ croak 'hits_file required' unless defined $hits_file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hits_file 2 12 14
98 12 ▶ croak 'out_file required' unless defined $out_file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $out_file 2 10 12
99  
100 # Load static LCSAJ path data extracted by App::Test::Generator::LCSAJ
101 10 my $paths = decode_json(_slurp($lcsaj_file));
102  
103 # Load runtime line hit counts from Devel::App::Test::Generator::LCSAJ::Runtime
104 9 my $hits = decode_json(_slurp($hits_file));
105  
106 # Annotate each path with a covered flag — a path is considered
107 # covered if any line in the start..end range was executed
108 9 for my $path (@{$paths}) {
109 9 my $covered = 0;
110  
111 9 for my $line ($path->{start} .. $path->{end}) {
112 27 ▶ if($hits->{$line}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hits->{$line}) 4 23 27
113 4 $covered = 1;
114 4 last;
115 }
116 }
117  
118 9 $path->{covered} = $covered;
119 }
120  
121 # Write the annotated paths to the output file. autodie is disabled
122 # for this open so the custom error message below is actually
123 # reachable -- under "use autodie qw(:all)" open() never returns
124 # false on failure, it throws its own exception instead, which
125 # would silently make the "or croak" dead code.
126 4 ▶ no autodie qw(open);
Subroutine BEGIN: called CC=1 SCAR=0.0
127 9 ▶ open my $fh, '>', $out_file or croak "Cannot write coverage output to $out_file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $out_file or croak("Cannot write coverage output to ${out_file}: $!") 8 1 9
128 8 print $fh encode_json($paths);
129 8 close $fh;
130 }
131  
132 # --------------------------------------------------
133 # _slurp
134 #
135 # Purpose: Read the entire contents of a file and
136 # return it as a string.
137 #
138 # Entry: $file - path to the file to read.
139 #
140 # Exit: Returns the file contents as a scalar
141 # string. Croaks if the file cannot be
142 # opened.
143 #
144 # Side effects: None beyond opening and closing the
145 # file handle.
146 #
147 # Notes: Uses three-argument open for safety with
148 # filenames containing special characters.
149 # Sets $/ to undef to slurp the whole file
150 # in one read, localised to avoid affecting
151 # other code.
152 # --------------------------------------------------
153 21 ▶sub _slurp {
Subroutine _slurp: called CC=2 SCAR=6.9
154 21 my $file = $_[0];
155  
156 # autodie is disabled for this open for the same reason as in
157 # merge() above -- it would otherwise make the custom "Cannot
158 # read" message unreachable dead code.
159 4 ▶ no autodie qw(open);
Subroutine BEGIN: called CC=1 SCAR=0.0
160 21 ▶ open my $fh, '<', $file or croak "Cannot read $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or croak("Cannot read ${file}: $!") 19 2 21
161  
162 # Localise $/ to undef to slurp entire file in one read
163 19 local $/;
164 19 return <$fh>;
165 }
166  
167 4 1;
« blib/lib/App/Test/Generator/LCSAJ.pm ↑ Summary bin/extract-schemas »