blib/lib/App/Project/Doctor/Report.pm

filter: Statementstmt 100% 107 / 107 Branchbran 100% 28 / 28 Conditioncond 100% 8 / 8 MC/DCmcdc 100% 5 / 5 Subroutinesub 100% 21 / 21 Podpod 100% 13 / 13 totaltotal 100% 182 / 182 CC 24 SCAR 19.5
CC
24
Cov
100%
render_text
16.0
add_findings
4.0
render_tap
3.0
CRAP 7.0
« blib/lib/App/Project/Doctor/Check/License.pm ↑ Summary blib/lib/App/Project/Doctor/Check/CpanReadiness.pm »
1 package App::Project::Doctor::Report;
2  
3 # A Report aggregates all Finding objects produced by check plugins and
4 # renders them as human-readable text, machine-readable JSON, or TAP output
5 # for CI pipelines. It also tracks the overall pass/fail exit code.
6  
7 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use autodie qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # croak dies at the caller's location; carp warns there.
12 5 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 # Readonly prevents accidental mutation of constants after they are defined.
14 5 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 # blessed() lets us confirm that add_findings() received real Finding objects.
16 5 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 # validate_strict enforces parameter schemas; not used by new() (takes no args).
18 5 ▶use Params::Validate::Strict qw(validate_strict);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 5 our $VERSION = '0.02';
21  
22 # ---------------------------------------------------------------------------
23 # Constants
24 # ---------------------------------------------------------------------------
25  
26 # The icons shown at the start of each text-report line, keyed by severity.
27 5 Readonly::Hash my %ICON => (
28 pass => '[v]', # Healthy -- no action needed
29 error => '[X]', # Broken -- must be fixed
30 warning => '[!]', # Suspicious -- should be reviewed
31 info => '[i]', # Informational -- no action needed
32 );
33  
34 # Numeric rank used to pick the "worst" severity in a group of findings.
35 # Higher number = more severe; error is always the worst.
36 5 Readonly::Hash my %SEV_RANK => (error => 3, warning => 2, info => 1, pass => 0);
37  
38 # The column width reserved for the check name in the text report.
39 # Chosen to accommodate the longest default check name ('CpanReadiness' = 13 chars).
40 5 Readonly::Scalar my $LABEL_WIDTH => 18;
41  
42 # ---------------------------------------------------------------------------
43 # Constructor
44 # ---------------------------------------------------------------------------
45  
46 126 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
47 # Report takes no constructor arguments; start with an empty findings list.
48 126 my ($class, %args) = @_;
49 126 return bless { _findings => [] }, $class;
50 }
51  
52 # ---------------------------------------------------------------------------
53 # Mutator
54 # ---------------------------------------------------------------------------
55  
56 =head2 add_findings( @findings )
57  
58 Appends one or more L<App::Project::Doctor::Finding> objects.
59 Croaks on non-Finding arguments.
60  
61 =cut
62  
63 140 ▶sub add_findings {
Subroutine add_findings: called CC=4 SCAR=13.9
Pod documented
64 140 my ($self, @findings) = @_;
65 140 for my $f (@findings) {
66 # Validate each element to catch bugs where a check returns a string
67 # or undef instead of a real Finding object.
68 197 ▶ croak 'Expected an App::Project::Doctor::Finding'
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($f) and $f->isa("App::Project::Doctor::Finding") 11 186 197
Condition100%3/3
builtin::blessed($f) and $f->isa("App::Project::Doctor::Finding")
!ll&&!rl&&r
92186
MC/DC100%2/2
builtin::blessed($f) and $f->isa("App::Project::Doctor::Finding")
builtin::blessed($f) $f->isa("App::Project::Doctor::Finding")
Truth table100%3/3
A and B
A= builtin::blessed($f)B= $f->isa("App::Project::Doctor::Finding")
ABresult
0X0
100
111
69 unless blessed($f) && $f->isa('App::Project::Doctor::Finding');
70 186 push @{ $self->{_findings} }, $f;
71 }
72 # Return $self so callers can chain: $report->add_findings(...)->render_text.
73 129 return $self;
74 }
75  
76 # ---------------------------------------------------------------------------
77 # Accessors / filters
78 # ---------------------------------------------------------------------------
79  
80 # Return every finding in insertion order (used by render_* methods).
81 50 ▶sub all_findings { @{ $_[0]->{_findings} } }
Subroutine all_findings: called CC=1 SCAR=0.0
Pod documented
82 # Return only the findings with severity 'error'.
83 64 ▶sub errors { grep { $_->severity eq 'error' } @{ $_[0]->{_findings} } }
Subroutine errors: called CC=1 SCAR=0.0
Pod documented
84 # Return only the findings with severity 'warning'.
85 43 ▶sub warnings { grep { $_->severity eq 'warning' } @{ $_[0]->{_findings} } }
Subroutine warnings: called CC=1 SCAR=0.0
Pod documented
86 # Return only the findings with severity 'pass'.
87 3 ▶sub passes { grep { $_->severity eq 'pass' } @{ $_[0]->{_findings} } }
Subroutine passes: called CC=1 SCAR=0.0
Pod documented
88 # Return only the findings that carry an automated fix coderef.
89 77 ▶sub fixable { grep { $_->is_fixable } @{ $_[0]->{_findings} } }
Subroutine fixable: called CC=1 SCAR=0.0
Pod documented
90  
91 # has_errors returns 1/0 (not just truthy) for type-safe callers.
92 29 ▶sub has_errors { (scalar($_[0]->errors) > 0) ? 1 : 0 }
Subroutine has_errors: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
scalar $_[0]->errors > 0 ? : 13 16 29
93 10 ▶sub has_warnings { (scalar($_[0]->warnings) > 0) ? 1 : 0 }
Subroutine has_warnings: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
scalar $_[0]->warnings > 0 ? : 5 5 10
94  
95 =head2 exit_code
96  
97 Returns 0 (clean) or 1 (errors present).
98  
99 =cut
100  
101 # The process should exit 1 if any finding is an error, 0 otherwise.
102 16 ▶sub exit_code { $_[0]->has_errors ? 1 : 0 }
Subroutine exit_code: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->has_errors ? : 7 9 16
103  
104 # ---------------------------------------------------------------------------
105 # Rendering
106 # ---------------------------------------------------------------------------
107  
108 =head2 render_text( %opts )
109  
110 Returns the full text report. Accepted options: C<verbose> (bool).
111  
112 =cut
113  
114 28 ▶sub render_text {
Subroutine render_text: called CC=16 SCAR=27.7
Pod documented
115 28 my ($self, %opts) = @_;
116 # verbose mode adds per-finding detail lines under each check summary.
117 28 ▶ my $verbose = $opts{verbose} // 0;
Condition100%2/2
$opts{"verbose"} // 0
l!l
919
MC/DC100%1/1
$opts{"verbose"} // 0
$opts{"verbose"}
Truth table100%2/2
A
A= $opts{"verbose"}
Aresult
11
00
118  
119 # Group findings by check name while preserving the insertion order of
120 # the first finding seen for each check. This keeps the output stable.
121 28 my (%by_check, @order);
122 28 for my $f ($self->all_findings) {
123 34 my $name = $f->check_name;
124 34 ▶ unless (exists $by_check{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $by_check{$name}) 31 3 34
125 # First time we see this check name -- record its position.
126 31 push @order, $name;
127 31 $by_check{$name} = [];
128 }
129 34 push @{ $by_check{$name} }, $f;
130 }
131  
132 # Build the output one line at a time and join at the end.
133 28 my @lines;
134 28 for my $name (@order) {
135 31 my @group = @{ $by_check{$name} };
136 # Choose the worst severity in this group to pick the icon.
137 31 my $sev = _worst_severity(\@group);
138 31 my $icon = $ICON{$sev}; # Severity is always valid; no fallback needed.
139  
140 # Use the first non-pass finding as the summary line for mixed groups.
141 31 my ($lead) = grep { $_->severity ne 'pass' } @group;
142 31 ▶ my $summary = $lead ? $lead->message : $group[0]->message;
Branch: 1 branch
BranchTrueFalseTotal
$lead ? : 26 5 31
143  
144 # Format: icon check-name (padded) summary message
145 31 push @lines, sprintf(' %-4s %-*s %s', $icon, $LABEL_WIDTH, $name, $summary);
146  
147 31 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 6 25 31
148 # In verbose mode, print each non-pass finding with its detail.
149 6 for my $f (@group) {
150 # Skip pass findings in verbose mode -- they have no useful detail.
151 7 ▶ next if $f->severity eq 'pass';
Branch: 1 branch
BranchTrueFalseTotal
if $f->severity eq "pass" 1 6 7
152 6 push @lines, sprintf(' -> %s', $f->message);
153 # Only print the detail line when there is something to show.
154 6 ▶ push @lines, sprintf(' %s', $f->detail) if $f->detail;
Branch: 1 branch
BranchTrueFalseTotal
if $f->detail 5 1 6
155 }
156 }
157 }
158  
159 # Print a one-line summary of error/warning counts below the check table.
160 28 my $ec = scalar($self->errors);
161 28 my $wc = scalar($self->warnings);
162 28 push @lines, ''; # Blank line before the summary.
163 28 ▶ push @lines, $ec || $wc
Branch: 3 branches
BranchTrueFalseTotal
$ec || $wc ? : 23 5 28
$ec ? : 21 2 23
$wc ? : 4 19 23
Condition100%3/3
$ec or $wc
l!l&&r!l&&!r
2125
MC/DC100%2/2
$ec or $wc
$ec $wc
Truth table100%3/3
A or B
A= $ecB= $wc
ABresult
1X1
011
000
164 ? join(' - ', ($ec ? "$ec error(s)" : ()), ($wc ? "$wc warning(s)" : ()))
165 : 'No errors or warnings.';
166  
167 # If there are fixable findings, show them and hint that the user can apply them.
168 28 my @fixable = $self->fixable;
169 28 ▶ if (@fixable) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fixable) 4 24 28
170 4 push @lines, '';
171 4 push @lines, 'Suggested fixes:';
172 4 my $i = 0;
173 # Number each fix starting at 1 for the interactive prompt that follows.
174 4 push @lines, sprintf(' [%d] %s', ++$i, $_->message) for @fixable;
175 4 push @lines, '';
176 4 push @lines, 'Would you like me to apply them? [Y/n]';
177 }
178  
179 # Join with newlines and add a trailing newline for clean shell output.
180 28 return join("\n", @lines) . "\n";
181 }
182  
183 =head2 render_json
184  
185 Returns findings as a pretty-printed JSON string (requires L<JSON::MaybeXS>).
186  
187 =cut
188  
189 4 ▶sub render_json {
Subroutine render_json: called CC=1 SCAR=0.0
Pod documented
190 4 my $self = shift;
191 # JSON::MaybeXS is loaded lazily so it is only required when --format=json.
192 4 require JSON::MaybeXS;
193 # canonical => 1 sorts keys so the output is diff-friendly.
194 return JSON::MaybeXS->new(utf8 => 1, pretty => 1, canonical => 1)
195 4 ->encode([ map { $_->to_hash } $self->all_findings ]);
196 }
197  
198 =head2 render_tap
199  
200 Returns a TAP-format string for CI pipeline consumption.
201  
202 =cut
203  
204 4 ▶sub render_tap {
Subroutine render_tap: called CC=3 SCAR=11.0
Pod documented
205 4 my $self = shift;
206 4 my @findings = $self->all_findings;
207 # TAP header declares how many tests will follow.
208 4 my @lines = ('1..' . scalar @findings);
209 4 my $n = 0;
210 4 for my $f (@findings) {
211 10 $n++;
212 # pass and info severities are "ok"; error and warning are "not ok".
213 10 ▶ my $ok = $f->severity =~ /^(?:pass|info)$/ ? 'ok' : 'not ok';
Branch: 1 branch
BranchTrueFalseTotal
$f->severity =~ /^(?:pass|info)$/ ? : 5 5 10
214 10 push @lines, sprintf('%s %d - [%s] %s', $ok, $n, $f->check_name, $f->message);
215 }
216 4 return join("\n", @lines) . "\n";
217 }
218  
219 # ---------------------------------------------------------------------------
220 # Private helpers
221 # ---------------------------------------------------------------------------
222  
223 # Purpose: Find the most severe severity string in a group of findings.
224 # Entry: $group is a non-empty arrayref of Finding objects.
225 # Exit: String -- one of 'error', 'warning', 'info', 'pass'.
226 # Side effects: None.
227 35 ▶sub _worst_severity {
Subroutine _worst_severity: called CC=1 SCAR=0.0
228 35 my $group = shift;
229 # Sort by numeric rank (descending) and take the first (= highest) value.
230 35 return (sort { $SEV_RANK{$b} <=> $SEV_RANK{$a} } map { $_->severity } @{$group})[0];
231 }
232  
233 5 1;
234  
235 __END__
« blib/lib/App/Project/Doctor/Check/License.pm ↑ Summary blib/lib/App/Project/Doctor/Check/CpanReadiness.pm »