« blib/lib/Test/CPAN/Health/Check/Perlcritic.pm
↑ Summary
blib/lib/Test/CPAN/Health/Check/ReverseDeps.pm »
| 1 | package Test::CPAN::Health::Check::ReadmeSync; | ||
| 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 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_HAS_README => 60; | ||
| 17 | Readonly::Scalar my $SCORE_NAME_MATCH => 80; | ||
| 18 | Readonly::Scalar my $SCORE_FULL => 100; | ||
| 19 | Readonly::Scalar my $MIN_README_BYTES => 100; | ||
| 20 | |||
| 21 | # Recognised README filenames, in preference order. | ||
| 22 | Readonly::Array my @README_NAMES => qw( | ||
| 23 | README.md | ||
| 24 | README.markdown | ||
| 25 | README.pod | ||
| 26 | README.txt | ||
| 27 | README | ||
| 28 | ); | ||
| 29 | |||
| 30 | =head1 NAME | ||
| 31 | |||
| 32 | Test::CPAN::Health::Check::ReadmeSync - Check that a README exists and reflects the main module | ||
| 33 | |||
| 34 | =head1 SYNOPSIS | ||
| 35 | |||
| 36 | use Test::CPAN::Health::Check::ReadmeSync; | ||
| 37 | |||
| 38 | my $check = Test::CPAN::Health::Check::ReadmeSync->new; | ||
| 39 | my $result = $check->run($dist); | ||
| 40 | |||
| 41 | printf "%s: %s\n", $result->status, $result->summary; | ||
| 42 | |||
| 43 | =head1 DESCRIPTION | ||
| 44 | |||
| 45 | Verifies that the distribution includes a non-trivial README file and that | ||
| 46 | it mentions the distribution name, indicating it has not been left as a | ||
| 47 | boilerplate stub. | ||
| 48 | |||
| 49 | Score breakdown: | ||
| 50 | |||
| 51 | 0 No README file found at all | ||
| 52 | 60 README found but very short (< 100 bytes) -- likely a placeholder | ||
| 53 | 80 README found and non-trivial, but distribution name not mentioned | ||
| 54 | 100 README found, non-trivial, and mentions the distribution name | ||
| 55 | |||
| 56 | =head1 LIMITATIONS | ||
| 57 | |||
| 58 | =over 4 | ||
| 59 | |||
| 60 | =item * Only the content of the README is checked, not whether it is | ||
| 61 | up-to-date with the current C<DESCRIPTION> or C<SYNOPSIS> in the POD. | ||
| 62 | |||
| 63 | =item * The distribution name match is case-insensitive and converts | ||
| 64 | C<::> to C<-> before searching. | ||
| 65 | |||
| 66 | =back | ||
| 67 | |||
| 68 | =cut | ||
| 69 | |||
| 70 | sub id { return 'readme_sync' } | ||
| 71 | sub name { return 'README Sync' } | ||
| 72 | sub description { return 'Checks that a non-trivial README mentioning the dist exists' } | ||
| 73 | sub weight { return 2 } | ||
| 74 | sub category { return 'packaging' } | ||
| 75 | |||
| 76 | =head2 run | ||
| 77 | |||
| 78 | =head3 PURPOSE | ||
| 79 | |||
| 80 | Locate a README file and verify it is populated and references the | ||
| 81 | distribution name. | ||
| 82 | |||
| 83 | =head3 API SPECIFICATION | ||
| 84 | |||
| 85 | =head4 INPUT | ||
| 86 | |||
| 87 | dist Test::CPAN::Health::Distribution required | ||
| 88 | context Hashref optional | ||
| 89 | |||
| 90 | =head4 OUTPUT | ||
| 91 | |||
| 92 | L<Test::CPAN::Health::Result> with check_id C<'readme_sync'>. | ||
| 93 | |||
| 94 | =head3 MESSAGES | ||
| 95 | |||
| 96 | Code | Severity | Message | Resolution | ||
| 97 | ------+----------+-----------------------------------------------------+----------- | ||
| 98 | RS001 | FAIL | No README file found | Add README.md | ||
| 99 | RS002 | WARN | README is very short (< 100 bytes) | Expand README | ||
| 100 | RS003 | WARN | README does not mention the distribution name | Update README | ||
| 101 | RS004 | PASS | README found and mentions the distribution name | | ||
| 102 | |||
| 103 | =head3 FORMAL SPECIFICATION | ||
| 104 | |||
| 105 | -- Z schema (placeholder) -- | ||
| 106 | ReadmeSyncOp | ||
| 107 | dist_name : String | undefined | ||
| 108 | readme : FileName | undefined | ||
| 109 | content : String | ||
| 110 | ------------------------------------------------------- | ||
| 111 | readme = undefined => status = fail /\ score = 0 | ||
| 112 | #content < 100 => status = warn /\ score = 60 | ||
| 113 | dist_name not in content => status = warn /\ score = 80 | ||
| 114 | dist_name in content => status = pass /\ score = 100 | ||
| 115 | |||
| 116 | =head3 SIDE EFFECTS | ||
| 117 | |||
| 118 | Reads at most one file from disk; no network I/O. | ||
| 119 | |||
| 120 | =head3 USAGE EXAMPLE | ||
| 121 | |||
| 122 | my $result = Test::CPAN::Health::Check::ReadmeSync->new->run($dist); | ||
| 123 | print $result->summary; | ||
| 124 | |||
| 125 | =cut | ||
| 126 | |||
| 127 | sub run { | ||
| 128 | my ($self, $dist, $context) = @_; | ||
| 129 | |||
| 130 | croak 'dist must be a Test::CPAN::Health::Distribution' | ||
| 131 | unless ref($dist) && $dist->isa('Test::CPAN::Health::Distribution'); | ||
| 132 | |||
| 133 | my ($readme_path, $readme_name) = _find_readme($dist->path); | ||
| 134 | |||
| 135 | unless (defined $readme_path) { | ||
| 136 | return $self->_result( | ||
| 137 | status => 'fail', | ||
| 138 | score => 0, | ||
| 139 | summary => 'No README file found', | ||
| 140 | details => [ | ||
| 141 | 'Add README.md (or README.pod / README.txt)', | ||
| 142 | 'Tools: Pod::Markdown, pod2text, or Dist::Zilla::Plugin::ReadmeAnyFromPod', | ||
| 143 | ], | ||
| 144 | data => { name => $self->name, readme => undef }, | ||
| 145 | ); | ||
| 146 | } | ||
| 147 | |||
| 148 | my $content = _read_file($readme_path); | ||
| 149 | |||
| 150 | if (length($content) < $MIN_README_BYTES) { | ||
| 151 | return $self->_result( | ||
| 152 | status => 'warn', | ||
| 153 | score => $SCORE_HAS_README, | ||
| 154 | summary => "README ($readme_name) is very short -- likely a placeholder", | ||
| 155 | details => [ "Expand $readme_name with at least a NAME, SYNOPSIS, and DESCRIPTION" ], | ||
| 156 | data => { name => $self->name, readme => $readme_name }, | ||
| 157 | ); | ||
| 158 | } | ||
| 159 | |||
| 160 | my $dist_name = $dist->name // ''; | ||
| 161 | (my $hyphen_form = $dist_name) =~ s/ :: /-/gx; | ||
| 162 | (my $module_form = $dist_name) =~ s/ - /::/gx; | ||
| 163 | |||
| 164 | my $mentions = ($hyphen_form && $content =~ / \Q$hyphen_form\E /xi) | ||
| 165 | || ($module_form && $content =~ / \Q$module_form\E /xi); | ||
| 166 | |||
| 167 | unless ($mentions) { | ||
| 168 | return $self->_result( | ||
| 169 | status => 'warn', | ||
| 170 | score => $SCORE_NAME_MATCH, | ||
| 171 | summary => "README ($readme_name) does not mention the distribution name", | ||
| 172 | details => [ | ||
| 173 | "Update $readme_name to mention '$hyphen_form' or '$module_form'", | ||
| 174 | ], | ||
| 175 | data => { name => $self->name, readme => $readme_name, dist_name => $dist_name }, | ||
| 176 | ); | ||
| 177 | } | ||
| 178 | |||
| 179 | return $self->_result( | ||
| 180 | status => 'pass', | ||
| 181 | score => $SCORE_FULL, | ||
| 182 | summary => "README ($readme_name) found and mentions the distribution name", | ||
| 183 | data => { name => $self->name, readme => $readme_name, dist_name => $dist_name }, | ||
| 184 | ); | ||
| 185 | } | ||
| 186 | |||
| 187 | # --------------------------------------------------------------------------- | ||
| 188 | # Private helpers | ||
| 189 | # --------------------------------------------------------------------------- | ||
| 190 | |||
| 191 | # Locate the first recognised README file under $root. | ||
| 192 | # Returns (absolute_path, basename) or (undef, undef). | ||
| 193 | sub _find_readme { | ||
| 194 | my ($root) = @_; | ||
| 195 | for my $name (@README_NAMES) { | ||
| 196 | my $path = File::Spec->catfile($root, $name); | ||
| 197 | return ($path, $name) if -f $path; | ||
| 198 | } | ||
| 199 | return (undef, undef); | ||
| 200 | } | ||
| 201 | |||
| 202 | # Read the full contents of $file; return empty string on failure. | ||
| 203 | sub _read_file { | ||
| 204 | my ($file) = @_; | ||
| 205 | open my $fh, '<', $file or return ''; | ||
| 206 | local $/ = undef; | ||
| 207 | my $content = <$fh>; | ||
| 208 | close $fh; | ||
| 209 | return $content // ''; | ||
| 210 | } | ||
| 211 | |||
| 212 | =head1 AUTHOR | ||
| 213 | |||
| 214 | Nigel Horne, C<< <njh at nigelhorne.com> >> | ||
| 215 | |||
| 216 | =head1 LICENSE AND COPYRIGHT | ||
| 217 | |||
| 218 | Copyright (C) 2026 Nigel Horne. | ||
| 219 | |||
| 220 | This program is free software; you can redistribute it and/or modify it | ||
| 221 | under the terms of the GNU General Public License as published by the Free | ||
| 222 | Software Foundation; either version 2 of the License, or (at your option) | ||
| 223 | any later version. | ||
| 224 | |||
| 225 | =cut | ||
| 226 | |||
| 227 | 1; | ||