« blib/lib/Test/CPAN/Health/Check/SecurityAdvisories.pm
↑ Summary
blib/lib/Test/CPAN/Health/Check/StaleDeps.pm »
| 1 | package Test::CPAN::Health::Check::SemVer; | ||
| 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 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 | # Semantic versioning regex per semver.org 2.0.0 spec (strictly ASCII). | ||
| 16 | # Accepts optional leading 'v'. Split into smaller qr// chunks for readability. | ||
| 17 | Readonly::Scalar my $SEMVER_NUM_RE => qr{ 0 | [1-9]\d* }x; | ||
| 18 | Readonly::Scalar my $SEMVER_PRE_RE => qr{ - [0-9a-zA-Z.-]+ }x; | ||
| 19 | Readonly::Scalar my $SEMVER_BLD_RE => qr{ \+ [0-9a-zA-Z.-]+ }x; | ||
| 20 | Readonly::Scalar my $SEMVER_RE => qr{ | ||
| 21 | ^ v? | ||
| 22 | ($SEMVER_NUM_RE) # major | ||
| 23 | \.($SEMVER_NUM_RE) # minor | ||
| 24 | \.($SEMVER_NUM_RE) # patch | ||
| 25 | (?: $SEMVER_PRE_RE )? # pre-release (optional) | ||
| 26 | (?: $SEMVER_BLD_RE )? # build metadata (optional) | ||
| 27 | $ | ||
| 28 | }x; | ||
| 29 | |||
| 30 | # The "decimal" convention common on CPAN: 1.000001, 0.27, etc. | ||
| 31 | # We accept it with a warning because it is pervasive but not semver. | ||
| 32 | Readonly::Scalar my $DECIMAL_RE => qr/ ^ v? [0-9]+ \. [0-9]+ $ /x; | ||
| 33 | |||
| 34 | =head1 NAME | ||
| 35 | |||
| 36 | Test::CPAN::Health::Check::SemVer - Check that a distribution version follows semantic versioning | ||
| 37 | |||
| 38 | =head1 SYNOPSIS | ||
| 39 | |||
| 40 | use Test::CPAN::Health::Check::SemVer; | ||
| 41 | |||
| 42 | my $check = Test::CPAN::Health::Check::SemVer->new; | ||
| 43 | my $result = $check->run($dist); | ||
| 44 | |||
| 45 | printf "%s: %s\n", $result->status, $result->summary; | ||
| 46 | |||
| 47 | =head1 DESCRIPTION | ||
| 48 | |||
| 49 | Examines the version string declared in the distribution's META.json (or | ||
| 50 | META.yml) and scores it against the Semantic Versioning 2.0.0 specification | ||
| 51 | (L<https://semver.org>). | ||
| 52 | |||
| 53 | Score matrix: | ||
| 54 | |||
| 55 | =over 4 | ||
| 56 | |||
| 57 | =item * 100 -- strict semver (X.Y.Z or vX.Y.Z, optionally with pre-release | ||
| 58 | and/or build metadata). | ||
| 59 | |||
| 60 | =item * 60 -- Perl-style decimal (X.YYY), accepted with a warning because | ||
| 61 | it is ubiquitous on CPAN but not interoperable with semver tooling. | ||
| 62 | |||
| 63 | =item * 0 -- anything else (missing version, empty string, epoch-only, etc.). | ||
| 64 | |||
| 65 | =back | ||
| 66 | |||
| 67 | =head1 LIMITATIONS | ||
| 68 | |||
| 69 | =over 4 | ||
| 70 | |||
| 71 | =item * Version 0.x.y is legal semver and scores 100; it is B<not> penalised | ||
| 72 | here because many mature distributions use 0.x to signal API instability. | ||
| 73 | |||
| 74 | =item * This check does not compare the declared version against the version | ||
| 75 | embedded in the main C<.pm> file. That comparison belongs to C<MinPerl> or | ||
| 76 | a dedicated C<VersionSync> check. | ||
| 77 | |||
| 78 | =back | ||
| 79 | |||
| 80 | =cut | ||
| 81 | |||
| 82 | sub id { return 'sem_ver' } | ||
| 83 | sub name { return 'Semantic Versioning' } | ||
| 84 | sub description { return 'Checks that the distribution version follows semver 2.0' } | ||
| 85 | sub weight { return 3 } | ||
| 86 | sub category { return 'packaging' } | ||
| 87 | |||
| 88 | =head2 run | ||
| 89 | |||
| 90 | =head3 PURPOSE | ||
| 91 | |||
| 92 | Extract the version string from the distribution's META file and score it. | ||
| 93 | |||
| 94 | =head3 API SPECIFICATION | ||
| 95 | |||
| 96 | =head4 INPUT | ||
| 97 | |||
| 98 | dist Test::CPAN::Health::Distribution required | ||
| 99 | context Hashref optional prior check results | ||
| 100 | |||
| 101 | =head4 OUTPUT | ||
| 102 | |||
| 103 | L<Test::CPAN::Health::Result> with: | ||
| 104 | |||
| 105 | check_id 'sem_ver' | ||
| 106 | status 'pass' | 'warn' | 'fail' | 'skip' | ||
| 107 | score 0 | 60 | 100 | ||
| 108 | summary human-readable verdict | ||
| 109 | details arrayref of diagnostic strings (non-empty on warn/fail) | ||
| 110 | |||
| 111 | =head3 MESSAGES | ||
| 112 | |||
| 113 | Code | Severity | Message | Resolution | ||
| 114 | ------+----------+------------------------------------+--------------------- | ||
| 115 | SV001 | SKIP | No META file found | Add META.json or META.yml | ||
| 116 | SV002 | FAIL | Version is missing or empty | Declare a version in META | ||
| 117 | SV003 | WARN | Decimal-style version {v} | Migrate to X.Y.Z semver | ||
| 118 | SV004 | FAIL | Version {v} does not match semver | Use X.Y.Z format | ||
| 119 | SV005 | PASS | Version {v} follows semver 2.0 | | ||
| 120 | |||
| 121 | =head3 FORMAL SPECIFICATION | ||
| 122 | |||
| 123 | -- Z schema (placeholder) -- | ||
| 124 | SemVerOp | ||
| 125 | dist : Distribution | ||
| 126 | version : String | undefined | ||
| 127 | score : {0, 60, 100} | ||
| 128 | status : {pass, warn, fail, skip} | ||
| 129 | ------------------------------------------------------- | ||
| 130 | version = undefined => status = skip | ||
| 131 | version = "" => status = fail /\ score = 0 | ||
| 132 | version matches SEMVER_RE => status = pass /\ score = 100 | ||
| 133 | version matches DECIMAL_RE => status = warn /\ score = 60 | ||
| 134 | otherwise => status = fail /\ score = 0 | ||
| 135 | |||
| 136 | =head3 SIDE EFFECTS | ||
| 137 | |||
| 138 | None. All information comes from the already-parsed META object. | ||
| 139 | |||
| 140 | =head3 USAGE EXAMPLE | ||
| 141 | |||
| 142 | my $result = Test::CPAN::Health::Check::SemVer->new->run($dist); | ||
| 143 | print $result->summary; | ||
| 144 | |||
| 145 | =cut | ||
| 146 | |||
| 147 | sub run { | ||
| 148 | my ($self, $dist, $context) = @_; | ||
| 149 | |||
| 150 | # Validate positional arg (context is optional; Params::Validate would | ||
| 151 | # require hash-style args here, so we validate manually for performance). | ||
| 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 (defined $meta) { | ||
| 158 | return $self->_skip('No META.json or META.yml found -- cannot determine version'); | ||
| 159 | } | ||
| 160 | |||
| 161 | my $version = $meta->version; | ||
| 162 | |||
| 163 | unless (defined $version && length $version) { | ||
| 164 | return $self->_result( | ||
| 165 | status => 'fail', | ||
| 166 | score => 0, | ||
| 167 | summary => 'Version is missing or empty in META', | ||
| 168 | details => ['Add a "version" key to META.json'], | ||
| 169 | data => { name => $self->name }, | ||
| 170 | ); | ||
| 171 | } | ||
| 172 | |||
| 173 | # Strict semver check | ||
| 174 | if ($version =~ $SEMVER_RE) { | ||
| 175 | return $self->_result( | ||
| 176 | status => 'pass', | ||
| 177 | score => 100, | ||
| 178 | summary => "Version $version follows Semantic Versioning 2.0", | ||
| 179 | data => { name => $self->name, version => $version }, | ||
| 180 | ); | ||
| 181 | } | ||
| 182 | |||
| 183 | # Decimal-style (CPAN convention) -- warn, not fail, because it is legal | ||
| 184 | # Perl and the ecosystem is saturated with it. Nudge towards X.Y.Z. | ||
| 185 | if ($version =~ $DECIMAL_RE) { | ||
| 186 | return $self->_result( | ||
| 187 | status => 'warn', | ||
| 188 | score => 60, | ||
| 189 | summary => "Version $version uses Perl-style decimals, not semver", | ||
| 190 | details => [ | ||
| 191 | 'Consider migrating to X.Y.Z format (e.g. "' . _decimal_to_semver($version) . '")', | ||
| 192 | 'See https://semver.org for the specification', | ||
| 193 | ], | ||
| 194 | data => { name => $self->name, version => $version }, | ||
| 195 | ); | ||
| 196 | } | ||
| 197 | |||
| 198 | # Anything else: epoch, git-describe hash, etc. | ||
| 199 | return $self->_result( | ||
| 200 | status => 'fail', | ||
| 201 | score => 0, | ||
| 202 | summary => "Version $version does not match any recognised versioning scheme", | ||
| 203 | details => [ | ||
| 204 | "Expected X.Y.Z (semver) or X.YY (Perl decimal); got: $version", | ||
| 205 | ], | ||
| 206 | data => { name => $self->name, version => $version }, | ||
| 207 | ); | ||
| 208 | } | ||
| 209 | |||
| 210 | # --------------------------------------------------------------------------- | ||
| 211 | # Private helpers | ||
| 212 | # --------------------------------------------------------------------------- | ||
| 213 | |||
| 214 | # Best-effort conversion of a decimal version to semver for display in hints. | ||
| 215 | # 0.27 -> 0.27.0, 1.002003 -> 1.2.3, etc. | ||
| 216 | # Not used for scoring; only for the human-readable suggestion string. | ||
| 217 | sub _decimal_to_semver { | ||
| 218 | my ($decimal) = @_; | ||
| 219 | |||
| 220 | $decimal =~ s/ ^ v //x; | ||
| 221 | |||
| 222 | # Already has two dots -- might be close to semver already | ||
| 223 | return $decimal if $decimal =~ tr/././ >= 2; | ||
| 224 | |||
| 225 | my ($major, $frac) = split / \. /x, $decimal, 2; | ||
| 226 | $frac //= '0'; | ||
| 227 | |||
| 228 | # Pad fractional part to 6 digits and split into minor+patch | ||
| 229 | $frac = sprintf('%-06s', $frac); | ||
| 230 | $frac =~ s/ \s /0/gx; | ||
| 231 | my $minor = int(substr($frac, 0, 3)); | ||
| 232 | my $patch = int(substr($frac, 3, 3)); | ||
| 233 | |||
| 234 | return "$major.$minor.$patch"; | ||
| 235 | } | ||
| 236 | |||
| 237 | =head1 AUTHOR | ||
| 238 | |||
| 239 | Nigel Horne, C<< <njh at nigelhorne.com> >> | ||
| 240 | |||
| 241 | =head1 LICENSE AND COPYRIGHT | ||
| 242 | |||
| 243 | Copyright (C) 2025-2026 Nigel Horne. | ||
| 244 | |||
| 245 | This program is free software; you can redistribute it and/or modify it | ||
| 246 | under the terms of the GNU General Public License as published by the Free | ||
| 247 | Software Foundation; either version 2 of the License, or (at your option) | ||
| 248 | any later version. | ||
| 249 | |||
| 250 | =cut | ||
| 251 | |||
| 252 | 1; | ||