« blib/lib/Test/CPAN/Health/Check/MetaJSON.pm
↑ Summary
blib/lib/Test/CPAN/Health/Check/PODCoverage.pm »
| 1 | package Test::CPAN::Health::Check::MinPerl; | ||
| 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 Readonly; | ||
| 9 | use Params::Validate::Strict qw(validate_strict); | ||
| 10 | |||
| 11 | use parent 'Test::CPAN::Health::Check'; | ||
| 12 | |||
| 13 | our $VERSION = '0.1.0'; | ||
| 14 | |||
| 15 | # Score awarded when the declared version is verified to match or exceed what | ||
| 16 | # Perl::MinimumVersion detects in the source code. | ||
| 17 | Readonly::Scalar my $SCORE_VERIFIED => 100; | ||
| 18 | |||
| 19 | # Score when a version is declared but Perl::MinimumVersion is unavailable or | ||
| 20 | # could not scan any source files -- partial credit. | ||
| 21 | Readonly::Scalar my $SCORE_UNVERIFIED => 80; | ||
| 22 | |||
| 23 | # Score when the declared minimum is lower than what source code actually | ||
| 24 | # requires -- underdeclaring is misleading to CPAN installers. | ||
| 25 | Readonly::Scalar my $SCORE_UNDER => 40; | ||
| 26 | |||
| 27 | =head1 NAME | ||
| 28 | |||
| 29 | Test::CPAN::Health::Check::MinPerl - Check that a minimum Perl version is declared and accurate | ||
| 30 | |||
| 31 | =head1 SYNOPSIS | ||
| 32 | |||
| 33 | use Test::CPAN::Health::Check::MinPerl; | ||
| 34 | |||
| 35 | my $check = Test::CPAN::Health::Check::MinPerl->new; | ||
| 36 | my $result = $check->run($dist); | ||
| 37 | |||
| 38 | printf "%s: %s\n", $result->status, $result->summary; | ||
| 39 | |||
| 40 | =head1 DESCRIPTION | ||
| 41 | |||
| 42 | Checks two things: | ||
| 43 | |||
| 44 | =over 4 | ||
| 45 | |||
| 46 | =item 1. | ||
| 47 | |||
| 48 | The distribution declares C<requires perl =E<gt> 'X.Y'> in its runtime | ||
| 49 | prerequisites (readable via META). | ||
| 50 | |||
| 51 | =item 2. | ||
| 52 | |||
| 53 | If L<Perl::MinimumVersion> is available, it scans all C<.pm> and C<.pl> | ||
| 54 | source files and confirms the declared minimum is not lower than what the | ||
| 55 | source code actually requires. | ||
| 56 | |||
| 57 | =back | ||
| 58 | |||
| 59 | Score matrix: | ||
| 60 | |||
| 61 | =over 4 | ||
| 62 | |||
| 63 | =item * 100 -- Declared, and verified to match or exceed detected minimum. | ||
| 64 | |||
| 65 | =item * 80 -- Declared, but C<Perl::MinimumVersion> unavailable to verify. | ||
| 66 | |||
| 67 | =item * 40 -- Declared minimum is lower than detected source minimum (underdeclared). | ||
| 68 | |||
| 69 | =item * 0 -- No minimum Perl version declared. | ||
| 70 | |||
| 71 | =item * skip -- No META file found. | ||
| 72 | |||
| 73 | =back | ||
| 74 | |||
| 75 | =head1 LIMITATIONS | ||
| 76 | |||
| 77 | =over 4 | ||
| 78 | |||
| 79 | =item * C<Perl::MinimumVersion> is an optional dependency; the check degrades | ||
| 80 | gracefully to score 80 when it is absent. | ||
| 81 | |||
| 82 | =item * Only C<runtime.requires.perl> is inspected. A constraint in | ||
| 83 | C<configure.requires> or C<build.requires> is ignored. | ||
| 84 | |||
| 85 | =back | ||
| 86 | |||
| 87 | =cut | ||
| 88 | |||
| 89 | sub id { return 'min_perl' } | ||
| 90 | sub name { return 'Minimum Perl Version' } | ||
| 91 | sub description { return 'Checks that a minimum Perl version is declared in META' } | ||
| 92 | sub weight { return 3 } | ||
| 93 | sub category { return 'packaging' } | ||
| 94 | |||
| 95 | =head2 run | ||
| 96 | |||
| 97 | =head3 PURPOSE | ||
| 98 | |||
| 99 | Inspect C<prereqs.runtime.requires.perl> in META and, when | ||
| 100 | L<Perl::MinimumVersion> is available, verify it against the actual source code. | ||
| 101 | |||
| 102 | =head3 API SPECIFICATION | ||
| 103 | |||
| 104 | =head4 INPUT | ||
| 105 | |||
| 106 | dist Test::CPAN::Health::Distribution required | ||
| 107 | context Hashref optional prior check results | ||
| 108 | |||
| 109 | =head4 OUTPUT | ||
| 110 | |||
| 111 | L<Test::CPAN::Health::Result> with check_id C<'min_perl'>. | ||
| 112 | |||
| 113 | =head3 MESSAGES | ||
| 114 | |||
| 115 | Code | Severity | Message | Resolution | ||
| 116 | ------+----------+------------------------------------------------+----------- | ||
| 117 | MP001 | SKIP | No META file | Add META file | ||
| 118 | MP002 | FAIL | No min Perl version declared in prereqs | Add requires perl | ||
| 119 | MP003 | PASS | Declared; Perl::MinimumVersion unavailable | | ||
| 120 | MP004 | WARN | Declared minimum lower than detected minimum | Raise declared minimum | ||
| 121 | MP005 | PASS | Declared minimum verified against source code | | ||
| 122 | |||
| 123 | =head3 FORMAL SPECIFICATION | ||
| 124 | |||
| 125 | -- Z schema (placeholder) -- | ||
| 126 | MinPerlOp | ||
| 127 | dist : Distribution | ||
| 128 | declared : String | undefined | ||
| 129 | detected : version | undefined | ||
| 130 | ------------------------------------------------------- | ||
| 131 | meta = undefined => status = skip | ||
| 132 | declared = undefined => status = fail /\ score = 0 | ||
| 133 | detected = undefined => status = pass /\ score = 80 | ||
| 134 | declared_v < detected => status = warn /\ score = 40 | ||
| 135 | declared_v >= detected => status = pass /\ score = 100 | ||
| 136 | |||
| 137 | =head3 SIDE EFFECTS | ||
| 138 | |||
| 139 | May invoke L<Perl::MinimumVersion> on all source files, which performs PPI | ||
| 140 | parsing (CPU-intensive for large distributions). | ||
| 141 | |||
| 142 | =head3 USAGE EXAMPLE | ||
| 143 | |||
| 144 | my $result = Test::CPAN::Health::Check::MinPerl->new->run($dist); | ||
| 145 | print $result->summary; | ||
| 146 | |||
| 147 | =cut | ||
| 148 | |||
| 149 | sub run { | ||
| 150 | my ($self, $dist, $context) = @_; | ||
| 151 | |||
| 152 | croak 'dist must be a Test::CPAN::Health::Distribution' | ||
| 153 | unless ref($dist) && $dist->isa('Test::CPAN::Health::Distribution'); | ||
| 154 | |||
| 155 | my $meta = $dist->meta; | ||
| 156 | |||
| 157 | unless ($meta) { | ||
| 158 | return $self->_skip('No META file found -- cannot check minimum Perl version'); | ||
| 159 | } | ||
| 160 | |||
| 161 | # Extract declared minimum Perl from the v2-normalised structure. | ||
| 162 | my $struct = $meta->as_struct; | ||
| 163 | my $declared = $struct->{prereqs}{runtime}{requires}{perl}; | ||
| 164 | |||
| 165 | unless (defined $declared && length $declared) { | ||
| 166 | return $self->_result( | ||
| 167 | status => 'fail', | ||
| 168 | score => 0, | ||
| 169 | summary => 'No minimum Perl version declared in META runtime prereqs', | ||
| 170 | details => [ | ||
| 171 | 'Add: requires "perl" => "5.x.y" to your dist prerequisites', | ||
| 172 | 'Common choices: 5.010, 5.014, 5.020, 5.032', | ||
| 173 | ], | ||
| 174 | data => { name => $self->name }, | ||
| 175 | ); | ||
| 176 | } | ||
| 177 | |||
| 178 | my $pmv_ok = eval { require Perl::MinimumVersion; 1 }; | ||
| 179 | |||
| 180 | unless ($pmv_ok) { | ||
| 181 | return $self->_result( | ||
| 182 | status => 'pass', | ||
| 183 | score => $SCORE_UNVERIFIED, | ||
| 184 | summary => "Minimum Perl $declared declared " | ||
| 185 | . '(install Perl::MinimumVersion to verify against source)', | ||
| 186 | data => { name => $self->name, declared => $declared }, | ||
| 187 | ); | ||
| 188 | } | ||
| 189 | |||
| 190 | my $detected = _detect_minimum($dist->all_source_files); | ||
| 191 | |||
| 192 | unless (defined $detected) { | ||
| 193 | return $self->_result( | ||
| 194 | status => 'pass', | ||
| 195 | score => $SCORE_UNVERIFIED, | ||
| 196 | summary => "Minimum Perl $declared declared " | ||
| 197 | . '(no source files found to verify against)', | ||
| 198 | data => { name => $self->name, declared => $declared }, | ||
| 199 | ); | ||
| 200 | } | ||
| 201 | |||
| 202 | return _compare_versions($self, $declared, $detected); | ||
| 203 | } | ||
| 204 | |||
| 205 | ## no critic (ProhibitUnusedPrivateSubroutines) | ||
| 206 | sub _detect_minimum { | ||
| 207 | my ($files_ref) = @_; | ||
| 208 | |||
| 209 | my $detected; | ||
| 210 | for my $file (@{$files_ref}) { | ||
| 211 | my $min; | ||
| 212 | my $ok = eval { | ||
| 213 | my $pmv = Perl::MinimumVersion->new($file); | ||
| 214 | $min = $pmv->minimum_version; | ||
| 215 | 1; | ||
| 216 | }; | ||
| 217 | if (!$ok) { | ||
| 218 | carp "Perl::MinimumVersion could not parse $file: $@"; | ||
| 219 | next; | ||
| 220 | } | ||
| 221 | next unless defined $min; | ||
| 222 | $detected = $min if !defined $detected || $min > $detected; | ||
| 223 | } | ||
| 224 | return $detected; | ||
| 225 | } | ||
| 226 | |||
| 227 | ## no critic (ProhibitUnusedPrivateSubroutines) | ||
| 228 | sub _compare_versions { | ||
| 229 | my ($self, $declared, $detected) = @_; | ||
| 230 | |||
| 231 | (my $clean = $declared) =~ s/ ^ [><=!] =? \s* //x; | ||
| 232 | require version; | ||
| 233 | my $declared_v = eval { version->parse($clean) }; | ||
| 234 | |||
| 235 | if (defined $declared_v && $declared_v < $detected) { | ||
| 236 | return $self->_result( | ||
| 237 | status => 'warn', | ||
| 238 | score => $SCORE_UNDER, | ||
| 239 | summary => "Declared minimum Perl $declared is lower than detected minimum " | ||
| 240 | . $detected->stringify, | ||
| 241 | details => [ | ||
| 242 | 'Update META prereqs to require perl ' . $detected->stringify, | ||
| 243 | 'Underdeclaring the minimum Perl version can cause installation failures', | ||
| 244 | ], | ||
| 245 | data => { | ||
| 246 | name => $self->name, | ||
| 247 | declared => $declared, | ||
| 248 | detected => $detected->stringify, | ||
| 249 | }, | ||
| 250 | ); | ||
| 251 | } | ||
| 252 | |||
| 253 | return $self->_result( | ||
| 254 | status => 'pass', | ||
| 255 | score => $SCORE_VERIFIED, | ||
| 256 | summary => "Minimum Perl $declared declared; detected minimum from source is " | ||
| 257 | . $detected->stringify, | ||
| 258 | data => { | ||
| 259 | name => $self->name, | ||
| 260 | declared => $declared, | ||
| 261 | detected => $detected->stringify, | ||
| 262 | }, | ||
| 263 | ); | ||
| 264 | } | ||
| 265 | |||
| 266 | =head1 AUTHOR | ||
| 267 | |||
| 268 | Nigel Horne, C<< <njh at nigelhorne.com> >> | ||
| 269 | |||
| 270 | =head1 LICENSE AND COPYRIGHT | ||
| 271 | |||
| 272 | Copyright (C) 2025-2026 Nigel Horne. | ||
| 273 | |||
| 274 | This program is free software; you can redistribute it and/or modify it | ||
| 275 | under the terms of the GNU General Public License as published by the Free | ||
| 276 | Software Foundation; either version 2 of the License, or (at your option) | ||
| 277 | any later version. | ||
| 278 | |||
| 279 | =cut | ||
| 280 | |||
| 281 | 1; | ||