blib/lib/Test/CPAN/Health/Reporter/TAP.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/Markdown.pm ↑ Summary blib/lib/Test/CPAN/Health/Reporter/Terminal.pm »
1 package Test::CPAN::Health::Reporter::TAP;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use autodie qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 use Carp qw(croak);
8 use Params::Validate::Strict qw(validate_strict);
9 use Scalar::Util qw(blessed);
10  
11 our $VERSION = '0.1.0';
12  
13 =head1 NAME
14  
15 Test::CPAN::Health::Reporter::TAP - Render a health report as TAP output
16  
17 =head1 SYNOPSIS
18  
19 use Test::CPAN::Health::Reporter::TAP;
20  
21 my $reporter = Test::CPAN::Health::Reporter::TAP->new;
22 print $reporter->render($report);
23  
24 # Pipe into a harness:
25 # cpan-health --format=tap My-Dist | prove --stdin
26  
27 =head1 DESCRIPTION
28  
29 Produces TAP (Test Anything Protocol) output for a
30 L<Test::CPAN::Health::Report>. Each check becomes one TAP test line.
31  
32 pass -> ok N - name: summary
33 warn -> ok N - name: summary # WARN
34 fail -> not ok N - name: summary
35 error -> not ok N - name: summary
36 skip -> ok N # SKIP name: reason
37  
38 The TAP plan (C<1..N>) is printed first. Detail strings are emitted as
39 TAP diagnostic lines (C<# detail>). Overall score and status counts
40 appear as diagnostics at the end.
41  
42 =head1 LIMITATIONS
43  
44 =over 4
45  
46 =item * The C<warn> status maps to C<ok ... # WARN>. C<# WARN> is not a
47 standard TAP directive but is readable and does not break harnesses.
48  
49 =item * Hash characters (C<#>) in check summaries are replaced with C<[#]>
50 to avoid confusing TAP parsers that treat C<#> as starting a directive.
51  
52 =back
53  
54 =cut
55  
56 sub new {
57 my ($class, %args) = @_;
58  
59 %args = %{ validate_strict(
60 schema => {},
61 input => \%args,
62 ) };
63  
64 return bless {}, $class;
65 }
66  
67 =head2 render
68  
69 =head3 PURPOSE
70  
71 Render a Report as a valid TAP document.
72  
73 =head3 API SPECIFICATION
74  
75 =head4 INPUT
76  
77 report Test::CPAN::Health::Report required
78  
79 =head4 OUTPUT
80  
81 Scalar string: TAP output ending with a newline.
82  
83 =head3 MESSAGES
84  
85 Code | Severity | Message | Resolution
86 ------+----------+------------------------------------+---------------------
87 TAP01 | FATAL | report must be a Report object | Pass a Report instance
88  
89 =head3 FORMAL SPECIFICATION
90  
91 -- Z schema (placeholder) --
92 RenderOp
93 report : Report
94 tap! : String
95 -------------------------------------------------------
96 tap! starts with "1.." ++ str(#results)
97 valid_tap(tap!)
98  
99 =head3 SIDE EFFECTS
100  
101 None.
102  
103 =head3 USAGE EXAMPLE
104  
105 print $reporter->render($report);
106  
107 =cut
108  
109 sub render {
110 my ($self, $report) = @_;
111  
112 croak 'report must be a Test::CPAN::Health::Report'
113 unless blessed($report) && $report->isa('Test::CPAN::Health::Report');
114  
115 my @results = sort { $a->check_id cmp $b->check_id } @{$report->results};
116 my $n = scalar @results;
117  
118 my @lines;
119 push @lines, "1..$n";
120  
121 my $i = 0;
122 for my $result (@results) {
123 $i++;
124  
125 my $label = ($result->data->{name} // $result->check_id)
126 . ': ' . $result->summary;
127 $label =~ s/ [#] /[#]/gx; # '#' is TAP-reserved; replace to avoid parse confusion
128  
129 my $line;
130 if ($result->is_skip) {
131 $line = "ok $i # SKIP $label";
132 } elsif ($result->is_pass) {
133 $line = "ok $i - $label";
134 } elsif ($result->is_warn) {
135 $line = "ok $i - $label # WARN";
136 } else {
137 $line = "not ok $i - $label";
138 }
139  
140 push @lines, $line;
141 push @lines, "# $_" for @{$result->details};
142 }
143  
144 push @lines, '';
145 push @lines, sprintf('# Overall score: %d/100', $report->overall_score);
146 push @lines, sprintf('# Passed: %d Warned: %d Failed: %d Skipped: %d',
147 $report->pass_count,
148 $report->warn_count,
149 $report->fail_count,
150 $report->skip_count,
151 );
152  
153 return join("\n", @lines) . "\n";
154 }
155  
156 =head1 AUTHOR
157  
158 Nigel Horne, C<< <njh at nigelhorne.com> >>
159  
160 =head1 LICENSE AND COPYRIGHT
161  
162 Copyright (C) 2025-2026 Nigel Horne.
163  
164 This program is free software; you can redistribute it and/or modify it
165 under the terms of the GNU General Public License as published by the Free
166 Software Foundation; either version 2 of the License, or (at your option)
167 any later version.
168  
169 =cut
170  
171 1;
« blib/lib/Test/CPAN/Health/Reporter/Markdown.pm ↑ Summary blib/lib/Test/CPAN/Health/Reporter/Terminal.pm »