« blib/lib/Test/CPAN/Health/Check/SemVer.pm
↑ Summary
blib/lib/Test/CPAN/Health/Check/TestCoverage.pm »
| 1 | package Test::CPAN::Health::Check::StaleDeps; | ||
| 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 HTTP::Tiny (); | ||
| 9 | use JSON::MaybeXS (); | ||
| 10 | use Readonly; | ||
| 11 | use Params::Validate::Strict qw(validate_strict); | ||
| 12 | |||
| 13 | use parent 'Test::CPAN::Health::Check'; | ||
| 14 | |||
| 15 | our $VERSION = '0.1.0'; | ||
| 16 | |||
| 17 | Readonly::Scalar my $METACPAN_API => 'https://fastapi.metacpan.org/v1'; | ||
| 18 | Readonly::Scalar my $HTTP_TIMEOUT => 30; | ||
| 19 | Readonly::Scalar my $SCORE_PASS => 80; | ||
| 20 | Readonly::Scalar my $SCORE_WARN => 60; | ||
| 21 | |||
| 22 | =head1 NAME | ||
| 23 | |||
| 24 | Test::CPAN::Health::Check::StaleDeps - Check for dependencies pinned to old major versions | ||
| 25 | |||
| 26 | =head1 SYNOPSIS | ||
| 27 | |||
| 28 | use Test::CPAN::Health::Check::StaleDeps; | ||
| 29 | |||
| 30 | my $check = Test::CPAN::Health::Check::StaleDeps->new; | ||
| 31 | my $result = $check->run($dist); | ||
| 32 | |||
| 33 | =head1 DESCRIPTION | ||
| 34 | |||
| 35 | Compares the minimum declared version for each runtime dependency against the | ||
| 36 | latest release on MetaCPAN. A dependency is flagged as I<stale> when: | ||
| 37 | |||
| 38 | =over 4 | ||
| 39 | |||
| 40 | =item * It has a declared minimum version greater than zero, AND | ||
| 41 | |||
| 42 | =item * The latest release's leading integer (the component before the first | ||
| 43 | decimal point) is strictly greater than the declared minimum's leading integer. | ||
| 44 | |||
| 45 | =back | ||
| 46 | |||
| 47 | The score is the fraction of non-stale dependencies expressed as a 0-100 | ||
| 48 | integer. Perl built-in pragmas (lowercase names) and core modules (identified | ||
| 49 | via L<Module::CoreList> when available, or a hardcoded fallback set) are | ||
| 50 | excluded from analysis. | ||
| 51 | |||
| 52 | Status thresholds: pass E<ge> 80, warn E<ge> 60, fail otherwise. | ||
| 53 | |||
| 54 | =head1 LIMITATIONS | ||
| 55 | |||
| 56 | =over 4 | ||
| 57 | |||
| 58 | =item * Heuristic leading-integer comparison may produce false positives for | ||
| 59 | modules that use date-based (C<20250101>-style) versioning where each year is a | ||
| 60 | new "major". | ||
| 61 | |||
| 62 | =item * Dependencies declared with version C<0> (any version acceptable) are | ||
| 63 | not flagged even if the module has advanced significantly. | ||
| 64 | |||
| 65 | =item * MetaCPAN is queried serially for each dependency. | ||
| 66 | |||
| 67 | =back | ||
| 68 | |||
| 69 | =cut | ||
| 70 | |||
| 71 | sub id { return 'stale_deps' } | ||
| 72 | sub name { return 'Stale Dependencies' } | ||
| 73 | sub description { return 'Checks for dependencies pinned to significantly old versions' } | ||
| 74 | sub weight { return 5 } | ||
| 75 | sub category { return 'security' } | ||
| 76 | |||
| 77 | =head2 run | ||
| 78 | |||
| 79 | =head3 PURPOSE | ||
| 80 | |||
| 81 | Compare declared dependency versions against the latest MetaCPAN releases and | ||
| 82 | return a scored result listing stale dependencies. | ||
| 83 | |||
| 84 | =head3 API SPECIFICATION | ||
| 85 | |||
| 86 | =head4 INPUT | ||
| 87 | |||
| 88 | dist Test::CPAN::Health::Distribution required | ||
| 89 | context Hashref optional | ||
| 90 | |||
| 91 | =head4 OUTPUT | ||
| 92 | |||
| 93 | L<Test::CPAN::Health::Result> with check_id C<'stale_deps'>. | ||
| 94 | |||
| 95 | =head3 MESSAGES | ||
| 96 | |||
| 97 | Code | Severity | Message | Resolution | ||
| 98 | ------+----------+---------------------------------------+----------- | ||
| 99 | SD001 | SKIP | Network checks disabled | Remove --no-network | ||
| 100 | SD002 | SKIP | No META file found | Add META.yml / META.json | ||
| 101 | SD003 | SKIP | No checkable runtime dependencies | n/a | ||
| 102 | SD004 | PASS | All N dependencies are current | | ||
| 103 | SD005 | WARN | N of M dependencies may be stale | Update declared versions | ||
| 104 | SD006 | FAIL | N of M dependencies may be stale | Update declared versions | ||
| 105 | |||
| 106 | =head3 FORMAL SPECIFICATION | ||
| 107 | |||
| 108 | -- Z schema (placeholder) -- | ||
| 109 | StaleDepsOp | ||
| 110 | stale : N -- count of stale deps | ||
| 111 | total : N -- count of checked deps | ||
| 112 | score : 0..100 | ||
| 113 | ------------------------------------------------------- | ||
| 114 | no_network => status = skip | ||
| 115 | meta = undef => status = skip | ||
| 116 | total = 0 => status = skip | ||
| 117 | score >= 80 => status = pass | ||
| 118 | score >= 60 => status = warn | ||
| 119 | score < 60 => status = fail | ||
| 120 | |||
| 121 | =head3 SIDE EFFECTS | ||
| 122 | |||
| 123 | Makes one HTTPS GET request to C<fastapi.metacpan.org> per dependency. | ||
| 124 | |||
| 125 | =head3 USAGE EXAMPLE | ||
| 126 | |||
| 127 | my $result = Test::CPAN::Health::Check::StaleDeps->new->run($dist); | ||
| 128 | printf "Stale: %s\n", $result->summary; | ||
| 129 | |||
| 130 | =cut | ||
| 131 | |||
| 132 | sub run { | ||
| 133 | my ($self, $dist, $context) = @_; | ||
| 134 | |||
| 135 | croak 'dist must be a Test::CPAN::Health::Distribution' | ||
| 136 | unless ref($dist) && $dist->isa('Test::CPAN::Health::Distribution'); | ||
| 137 | |||
| 138 | return $self->_skip('Network checks disabled (--no-network)') | ||
| 139 | if $self->no_network; | ||
| 140 | |||
| 141 | my $meta = $dist->meta; | ||
| 142 | return $self->_skip('No META file found') unless $meta; | ||
| 143 | |||
| 144 | my %checkable = _collect_checkable($meta); | ||
| 145 | return $self->_skip('No checkable runtime dependencies found') | ||
| 146 | unless %checkable; | ||
| 147 | |||
| 148 | my (@stale_mods, @current_mods); | ||
| 149 | for my $mod (sort keys %checkable) { | ||
| 150 | my ($is_stale, $detail) = _check_dep_freshness($mod, $checkable{$mod}); | ||
| 151 | next unless defined $is_stale; | ||
| 152 | if ($is_stale) { push @stale_mods, $detail } | ||
| 153 | else { push @current_mods, $mod } | ||
| 154 | } | ||
| 155 | |||
| 156 | my $total = @stale_mods + @current_mods; | ||
| 157 | return $self->_skip('No versioned runtime dependencies found') unless $total; | ||
| 158 | |||
| 159 | my $n_stale = scalar @stale_mods; | ||
| 160 | my $score = int(($total - $n_stale) / $total * 100); | ||
| 161 | my $status = $score >= $SCORE_PASS ? 'pass' | ||
| 162 | : $score >= $SCORE_WARN ? 'warn' | ||
| 163 | : 'fail'; | ||
| 164 | |||
| 165 | my $summary = $n_stale | ||
| 166 | ? "$n_stale of $total runtime dependencies may be stale (major version behind)" | ||
| 167 | : ($total == 2 ? 'Both' : "All $total") . ' checked runtime dependencies are current'; | ||
| 168 | |||
| 169 | return $self->_result( | ||
| 170 | status => $status, | ||
| 171 | score => $score, | ||
| 172 | summary => $summary, | ||
| 173 | details => [ map { "Stale: $_" } sort @stale_mods ], | ||
| 174 | data => { | ||
| 175 | name => $self->name, | ||
| 176 | total => $total, | ||
| 177 | stale => $n_stale, | ||
| 178 | stale_mods => \@stale_mods, | ||
| 179 | current_mods => \@current_mods, | ||
| 180 | }, | ||
| 181 | ); | ||
| 182 | } | ||
| 183 | |||
| 184 | sub _collect_checkable { | ||
| 185 | my ($meta) = @_; | ||
| 186 | |||
| 187 | my $prereqs = $meta->effective_prereqs; | ||
| 188 | my $runtime = $prereqs->requirements_for('runtime', 'requires'); | ||
| 189 | my %deps = %{ $runtime->as_string_hash }; | ||
| 190 | |||
| 191 | my $use_corelist = eval { require Module::CoreList; 1 }; | ||
| 192 | |||
| 193 | my %checkable; | ||
| 194 | for my $mod (sort keys %deps) { | ||
| 195 | next if $mod eq 'perl'; | ||
| 196 | next if $mod =~ /^ [a-z] /x; # lowercase = pragma (strict, warnings, etc.) | ||
| 197 | next if $use_corelist && Module::CoreList->first_release($mod); | ||
| 198 | $checkable{$mod} = $deps{$mod}; | ||
| 199 | } | ||
| 200 | return %checkable; | ||
| 201 | } | ||
| 202 | |||
| 203 | sub _check_dep_freshness { | ||
| 204 | my ($mod, $declared) = @_; | ||
| 205 | |||
| 206 | $declared //= '0'; | ||
| 207 | return if $declared == 0; # no meaningful version constraint | ||
| 208 | |||
| 209 | my ($data, $err) = _http_get("$METACPAN_API/module/$mod"); | ||
| 210 | return if $err || !defined $data->{version}; | ||
| 211 | |||
| 212 | my $dec_major = _major($declared); | ||
| 213 | my $latest_major = _major($data->{version}); | ||
| 214 | |||
| 215 | if ($latest_major > $dec_major) { | ||
| 216 | my $detail = sprintf '%s (declared >= %s, latest %s)', | ||
| 217 | $mod, $declared, $data->{version}; | ||
| 218 | return (1, $detail); | ||
| 219 | } | ||
| 220 | return (0, undef); | ||
| 221 | } | ||
| 222 | |||
| 223 | # --------------------------------------------------------------------------- | ||
| 224 | # Private helpers | ||
| 225 | # --------------------------------------------------------------------------- | ||
| 226 | |||
| 227 | # Extract the leading integer component ("major") from a version string. | ||
| 228 | # Examples: "1.23" -> 1, "v2.0.0" -> 2, "0.99" -> 0, "10.00" -> 10. | ||
| 229 | sub _major { | ||
| 230 | my ($v) = @_; | ||
| 231 | return 0 unless defined $v && length $v; | ||
| 232 | my ($n) = $v =~ /^ [v]? (\d+) /x; | ||
| 233 | return $n // 0; | ||
| 234 | } | ||
| 235 | |||
| 236 | sub _http_get { | ||
| 237 | my ($url) = @_; | ||
| 238 | |||
| 239 | my $ua = HTTP::Tiny->new(timeout => $HTTP_TIMEOUT); | ||
| 240 | my $res = $ua->get($url, { headers => { 'Accept' => 'application/json' } }); | ||
| 241 | |||
| 242 | return (undef, "HTTP $res->{status} $res->{reason}") unless $res->{success}; | ||
| 243 | |||
| 244 | my $data = eval { JSON::MaybeXS::decode_json($res->{content}) }; | ||
| 245 | return (undef, "JSON parse error: $@") if $@; | ||
| 246 | |||
| 247 | return ($data, undef); | ||
| 248 | } | ||
| 249 | |||
| 250 | =head1 AUTHOR | ||
| 251 | |||
| 252 | Nigel Horne, C<< <njh at nigelhorne.com> >> | ||
| 253 | |||
| 254 | =head1 LICENSE AND COPYRIGHT | ||
| 255 | |||
| 256 | Copyright (C) 2025-2026 Nigel Horne. | ||
| 257 | |||
| 258 | This program is free software; you can redistribute it and/or modify it | ||
| 259 | under the terms of the GNU General Public License as published by the Free | ||
| 260 | Software Foundation; either version 2 of the License, or (at your option) | ||
| 261 | any later version. | ||
| 262 | |||
| 263 | =cut | ||
| 264 | |||
| 265 | 1; | ||