blib/lib/Test/CPAN/Health/Reporter/Terminal.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Test/CPAN/Health/Reporter/TAP.pm ↑ Summary blib/lib/Test/CPAN/Health/Result.pm »
1 package Test::CPAN::Health::Reporter::Terminal;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use autodie qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 use Carp qw(croak);
8 use Readonly;
9 use Term::ANSIColor qw(colored);
10 use Params::Validate::Strict qw(validate_strict);
11 use Scalar::Util qw(blessed);
12  
13 our $VERSION = '0.1.0';
14  
15 # Glyphs -- defined as named constants to keep them out of logic code.
16 # Using Unicode escape sequences keeps the source file strictly ASCII while
17 # producing the expected UTF-8 glyphs at runtime.
18 Readonly::Scalar my $GLYPH_PASS => "\x{2713}"; # U+2713 CHECK MARK
19 Readonly::Scalar my $GLYPH_WARN => "\x{26A0}"; # U+26A0 WARNING SIGN
20 Readonly::Scalar my $GLYPH_FAIL => "\x{2717}"; # U+2717 BALLOT X
21 Readonly::Scalar my $GLYPH_SKIP => "\x{2014}"; # U+2014 EM DASH
22 Readonly::Scalar my $GLYPH_ERROR => "\x{2049}"; # U+2049 EXCLAMATION QUESTION MARK
23  
24 # ANSI colour map per status
25 Readonly::Hash my %STATUS_COLOUR => (
26 pass => 'bold green',
27 warn => 'bold yellow',
28 fail => 'bold red',
29 skip => 'dark',
30 error => 'bold magenta',
31 );
32  
33 Readonly::Hash my %STATUS_GLYPH => (
34 pass => $GLYPH_PASS,
35 warn => $GLYPH_WARN,
36 fail => $GLYPH_FAIL,
37 skip => $GLYPH_SKIP,
38 error => $GLYPH_ERROR,
39 );
40  
41 =head1 NAME
42  
43 Test::CPAN::Health::Reporter::Terminal - Render a health report as coloured terminal output
44  
45 =head1 SYNOPSIS
46  
47 use Test::CPAN::Health::Reporter::Terminal;
48  
49 my $reporter = Test::CPAN::Health::Reporter::Terminal->new;
50 print $reporter->render($report);
51  
52 =head1 DESCRIPTION
53  
54 Produces a human-readable, ANSI-coloured summary of a
55 L<Test::CPAN::Health::Report> for display in a terminal. Each check result
56 is rendered on a single line with a status glyph, the check name, and the
57 per-check score. Detail lines are indented beneath failing and warning
58 results. An overall score and pass/warn/fail summary is printed at the end.
59  
60 Colour can be disabled by setting C<< colour => 0 >> in the constructor,
61 or automatically when the C<NO_COLOR> environment variable is set (see
62 https://no-color.org) or when STDOUT is not a terminal.
63  
64 =head1 LIMITATIONS
65  
66 =over 4
67  
68 =item * Long detail lines are not wrapped; the caller must ensure terminal
69 width is accounted for in test output if needed.
70  
71 =back
72  
73 =cut
74  
75 sub new {
76 my ($class, %args) = @_;
77  
78 %args = %{ validate_strict(
79 schema => {
80 colour => { type => 'scalar', optional => 1 },
81 verbose => { type => 'scalar', optional => 1, default => 0 },
82 },
83 input => \%args,
84 ) };
85  
86 # Honour NO_COLOR convention and isatty; explicit colour => 0 overrides.
87 my $colour = $args{colour}
88 // ($ENV{NO_COLOR} || !( -t STDOUT ) ? 0 : 1); ## no critic (ProhibitInteractiveTest)
89  
90 my $self = bless {
91 _colour => $colour,
92 _verbose => $args{verbose},
93 }, $class;
94  
95 return $self;
96 }
97  
98 =head2 render
99  
100 =head3 PURPOSE
101  
102 Render a Report to an ANSI-coloured string.
103  
104 =head3 API SPECIFICATION
105  
106 =head4 INPUT
107  
108 report Test::CPAN::Health::Report required
109  
110 =head4 OUTPUT
111  
112 Scalar string (UTF-8). Does not include a trailing newline on the last line.
113  
114 =head3 MESSAGES
115  
116 Code | Severity | Message | Resolution
117 ------+----------+------------------------------------+---------------------
118 TRM01 | FATAL | report must be a Report object | Pass a Report instance
119  
120 =head3 FORMAL SPECIFICATION
121  
122 -- Z schema (placeholder) --
123 RenderOp
124 report : Report
125 output : String
126 -------------------------------------------------------
127 #output > 0
128  
129 =head3 SIDE EFFECTS
130  
131 None.
132  
133 =head3 USAGE EXAMPLE
134  
135 print $reporter->render($report);
136  
137 =cut
138  
139 sub render {
140 my ($self, $report) = @_;
141  
142 croak 'report must be a Test::CPAN::Health::Report'
143 unless blessed($report) && $report->isa('Test::CPAN::Health::Report');
144  
145 my @lines;
146  
147 for my $result (sort { $a->check_id cmp $b->check_id } @{$report->results}) {
148 push @lines, $self->_render_result($result);
149 }
150  
151 push @lines, '';
152 push @lines, $self->_render_summary($report);
153  
154 return join("\n", @lines);
155 }
156  
157 # ---------------------------------------------------------------------------
158 # Private rendering helpers
159 # ---------------------------------------------------------------------------
160  
161 sub _render_result {
162 my ($self, $result) = @_;
163  
164 my $status = $result->status;
165 my $glyph = $STATUS_GLYPH{$status} // '?';
166 my $colour = $STATUS_COLOUR{$status} // 'reset';
167  
168 my $score_str = defined $result->score
169 ? sprintf(' (%d/100)', $result->score)
170 : '';
171  
172 my $line = sprintf('%s %-30s %s%s',
173 $glyph,
174 $result->data->{name} // $result->check_id,
175 $result->summary,
176 $score_str,
177 );
178  
179 $line = $self->_colour($colour, $line) if $self->{_colour};
180  
181 my @lines = ($line);
182  
183 # Only expand details for warn/fail/error, or always in verbose mode.
184 if ($self->{_verbose} || $status eq 'fail' || $status eq 'warn' || $status eq 'error') {
185 for my $detail (@{$result->details}) {
186 my $detail_line = " $detail";
187 $detail_line = $self->_colour('faint', $detail_line) if $self->{_colour};
188 push @lines, $detail_line;
189 }
190  
191 if ($result->url) {
192 my $url_line = " " . $result->url;
193 $url_line = $self->_colour('faint', $url_line) if $self->{_colour};
194 push @lines, $url_line;
195 }
196 }
197  
198 return @lines;
199 }
200  
201 sub _render_summary {
202 my ($self, $report) = @_;
203  
204 my $score = $report->overall_score;
205 my $score_colour = $score >= 90 ? 'bold green'
206 : $score >= 70 ? 'bold yellow'
207 : 'bold red';
208  
209 my $score_str = sprintf('Overall score: %d/100', $score);
210 $score_str = $self->_colour($score_colour, $score_str) if $self->{_colour};
211  
212 my $counts = sprintf(
213 'Passed: %d Warned: %d Failed: %d Skipped: %d',
214 $report->pass_count,
215 $report->warn_count,
216 $report->fail_count,
217 $report->skip_count,
218 );
219  
220 return ($score_str, $counts);
221 }
222  
223 # Wrapper that degrades gracefully when colour is disabled.
224 sub _colour {
225 my ($self, $attr, $text) = @_;
226  
227 return $text unless $self->{_colour};
228  
229 return colored($text, $attr);
230 }
231  
232 =head1 AUTHOR
233  
234 Nigel Horne, C<< <njh at nigelhorne.com> >>
235  
236 =head1 LICENSE AND COPYRIGHT
237  
238 Copyright (C) 2025-2026 Nigel Horne.
239  
240 This program is free software; you can redistribute it and/or modify it
241 under the terms of the GNU General Public License as published by the Free
242 Software Foundation; either version 2 of the License, or (at your option)
243 any later version.
244  
245 =cut
246  
247 1;
« blib/lib/Test/CPAN/Health/Reporter/TAP.pm ↑ Summary blib/lib/Test/CPAN/Health/Result.pm »