blib/lib/Test/CPAN/Health/Check/Changelog.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/Check/CPANTesters.pm ↑ Summary blib/lib/Test/CPAN/Health/Check/Complexity.pm »
1 package Test::CPAN::Health::Check::Changelog;
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 carp);
8 use File::Spec;
9 use Readonly;
10 use Params::Validate::Strict qw(validate_strict);
11  
12 use parent 'Test::CPAN::Health::Check';
13  
14 our $VERSION = '0.1.0';
15  
16 Readonly::Scalar my $SCORE_PASS => 100;
17 Readonly::Scalar my $SCORE_NO_VERSION => 50;
18 Readonly::Scalar my $SCORE_EMPTY => 10;
19 Readonly::Scalar my $SCORE_NONE => 0;
20  
21 # Recognised changelog filenames, checked in preference order.
22 # 'Changes' is the CPAN convention; others are common alternatives.
23 Readonly::Array my @CHANGELOG_NAMES => qw(
24 Changes Changelog CHANGES ChangeLog CHANGELOG CHANGELOG.md NEWS NEWS.md
25 );
26  
27 =head1 NAME
28  
29 Test::CPAN::Health::Check::Changelog - Check that a changelog file is present and has an entry for the current version
30  
31 =head1 SYNOPSIS
32  
33 use Test::CPAN::Health::Check::Changelog;
34  
35 my $check = Test::CPAN::Health::Check::Changelog->new;
36 my $result = $check->run($dist);
37  
38 printf "%s: %s\n", $result->status, $result->summary;
39  
40 =head1 DESCRIPTION
41  
42 Verifies that the distribution root contains a changelog file
43 (C<Changes>, C<Changelog>, C<CHANGES>, C<ChangeLog>, C<CHANGELOG>,
44 C<CHANGELOG.md>, C<NEWS>, or C<NEWS.md>) and, when the distribution version
45 is determinable from META, that the file contains a release entry for that
46 version.
47  
48 Score matrix:
49  
50 =over 4
51  
52 =item * 100 -- Changelog found with an entry for the current version (or version is unknown).
53  
54 =item * 50 -- Changelog file found with content, but no entry for the current version.
55  
56 =item * 10 -- Changelog file exists but is empty.
57  
58 =item * 0 -- No changelog file found.
59  
60 =back
61  
62 =head1 LIMITATIONS
63  
64 =over 4
65  
66 =item * Version-entry detection is heuristic. It matches the most common
67 CPAN (C<0.01 YYYY-MM-DD>) and Keep a Changelog (C<[0.01] - YYYY-MM-DD>)
68 formats but may miss exotic styles.
69  
70 =item * Only the distribution root directory is searched; changelogs in
71 subdirectories are not recognised.
72  
73 =back
74  
75 =cut
76  
77 sub id { return 'changelog' }
78 sub name { return 'Changelog' }
79 sub description { return 'Checks that a changelog file exists with a version entry' }
80 sub weight { return 3 }
81 sub category { return 'packaging' }
82  
83 =head2 run
84  
85 =head3 PURPOSE
86  
87 Locate a changelog file in the distribution root and verify it contains a
88 release entry for the current version.
89  
90 =head3 API SPECIFICATION
91  
92 =head4 INPUT
93  
94 dist Test::CPAN::Health::Distribution required
95 context Hashref optional
96  
97 =head4 OUTPUT
98  
99 L<Test::CPAN::Health::Result> with check_id C<'changelog'>.
100  
101 =head3 MESSAGES
102  
103 Code | Severity | Message | Resolution
104 ------+----------+--------------------------------------------------+-----------
105 CL001 | FAIL | No changelog file found | Create a Changes file
106 CL002 | FAIL | Changes exists but is empty | Add release notes
107 CL003 | WARN | Changes found but no entry for version N.NN | Add a version entry
108 CL004 | PASS | Changes found with entry for version N.NN |
109 CL005 | PASS | Changes found (version not determinable) |
110  
111 =head3 FORMAL SPECIFICATION
112  
113 Pre: dist isa Test::CPAN::Health::Distribution
114 Post: (no changelog) => status = fail /\ score = 0
115 (empty file) => status = fail /\ score = 10
116 (no ver entry) => status = warn /\ score = 50
117 (ver entry ok) => status = pass /\ score = 100
118  
119 =head3 SIDE EFFECTS
120  
121 Reads at most one file from disk.
122  
123 =head3 USAGE EXAMPLE
124  
125 my $result = Test::CPAN::Health::Check::Changelog->new->run($dist);
126 print $result->summary;
127  
128 =cut
129  
130 sub run {
131 my ($self, $dist, $context) = @_;
132  
133 croak 'dist must be a Test::CPAN::Health::Distribution'
134 unless ref($dist) && $dist->isa('Test::CPAN::Health::Distribution');
135  
136 # Locate the first changelog file that exists in the distribution root.
137 my $changelog_path;
138 my $changelog_name;
139 for my $name (@CHANGELOG_NAMES) {
140 my $p = $dist->file_path($name);
141 if (defined $p) {
142 $changelog_path = $p;
143 $changelog_name = $name;
144 last;
145 }
146 }
147  
148 unless (defined $changelog_path) {
149 my $list = join ', ', @CHANGELOG_NAMES;
150 return $self->_result(
151 status => 'fail',
152 score => $SCORE_NONE,
153 summary => 'No changelog file found',
154 details => [
155 "Create a 'Changes' file in the distribution root",
156 "Recognised names: $list",
157 "Recommended format: \"0.01 YYYY-MM-DD\" followed by indented bullet points",
158 ],
159 data => { name => $self->name },
160 );
161 }
162  
163 # Read the changelog content. file_path verified existence so open
164 # failures are unexpected (permissions race); autodie will propagate them.
165 open my $fh, '<', $changelog_path;
166 local $/ = undef;
167 my $content = <$fh>;
168 close $fh;
169  
170 if (!defined $content || $content !~ /\S/x) {
171 return $self->_result(
172 status => 'fail',
173 score => $SCORE_EMPTY,
174 summary => "$changelog_name exists but is empty",
175 details => [ "Add release notes for the current version to $changelog_name" ],
176 data => { name => $self->name, file => $changelog_name },
177 );
178 }
179  
180 # Try to locate an entry for the current distribution version.
181 my $dist_version = $dist->version;
182 unless (defined $dist_version && length $dist_version) {
183 return $self->_result(
184 status => 'pass',
185 score => $SCORE_PASS,
186 summary => "$changelog_name found with content (distribution version not determinable)",
187 data => { name => $self->name, file => $changelog_name },
188 );
189 }
190  
191 # Match common release-entry formats (split into qr// per PBP):
192 # 0.1.0 2026-07-03 (CPAN standard)
193 # v0.1.0 2026-07-03 (with v prefix)
194 # ## [0.1.0] - 2026-07-03 (Keep a Changelog)
195 # version 0.1.0 / release 0.1.0
196 # Strip any leading 'v' before quotemeta so that the v? in each regex
197 # handles both "v0.1.0" and "0.1.0" in the file regardless of whether
198 # CPAN::Meta normalized the version string to include a v prefix.
199 (my $bare_version = $dist_version) =~ s/ ^ v //ix;
200 my $v = quotemeta($bare_version);
201 my $re_standard = qr/ (?: ^ | \n ) [^\n]* v? $v \s /xi;
202 my $re_keepal = qr/ \[ \s* v? $v \s* \] /xi;
203 my $re_prose = qr/ (?: version | release ) \s+ v? $v \b /xi;
204 my $has_entry = $content =~ $re_standard
205 || $content =~ $re_keepal
206 || $content =~ $re_prose;
207  
208 unless ($has_entry) {
209 return $self->_result(
210 status => 'warn',
211 score => $SCORE_NO_VERSION,
212 summary => "$changelog_name found but contains no entry for version $dist_version",
213 details => [
214 "Add a release entry for version $dist_version to $changelog_name",
215 "Example: \"$dist_version " . _today() . "\"",
216 ],
217 data => {
218 name => $self->name,
219 file => $changelog_name,
220 version => $dist_version,
221 },
222 );
223 }
224  
225 return $self->_result(
226 status => 'pass',
227 score => $SCORE_PASS,
228 summary => "$changelog_name found with entry for version $dist_version",
229 data => {
230 name => $self->name,
231 file => $changelog_name,
232 version => $dist_version,
233 },
234 );
235 }
236  
237 # ---------------------------------------------------------------------------
238 # Private helpers
239 # ---------------------------------------------------------------------------
240  
241 sub _today {
242 my @t = localtime;
243 return sprintf '%04d-%02d-%02d', $t[5] + 1900, $t[4] + 1, $t[3];
244 }
245  
246 =head1 AUTHOR
247  
248 Nigel Horne, C<< <njh at nigelhorne.com> >>
249  
250 =head1 LICENSE AND COPYRIGHT
251  
252 Copyright (C) 2026 Nigel Horne.
253  
254 This program is free software; you can redistribute it and/or modify it
255 under the terms of the GNU General Public License as published by the Free
256 Software Foundation; either version 2 of the License, or (at your option)
257 any later version.
258  
259 =cut
260  
261 1;
« blib/lib/Test/CPAN/Health/Check/CPANTesters.pm ↑ Summary blib/lib/Test/CPAN/Health/Check/Complexity.pm »