« blib/lib/Test/CPAN/Health/Check/Complexity.pm
↑ Summary
blib/lib/Test/CPAN/Health/Check/Deprecations.pm »
| 1 | package Test::CPAN::Health::Check::DeclaredDeps; | ||
| 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_WARN => 80; | ||
| 17 | |||
| 18 | # Perl pragma modules that are always part of the interpreter and need not be | ||
| 19 | # declared as dependencies. All-lowercase names (strict, warnings, autodie, | ||
| 20 | # utf8 ...) are caught by the /^[a-z]/ guard; this list handles mixed-case | ||
| 21 | # pragma-like modules that are always available. | ||
| 22 | Readonly::Array my @ALWAYS_AVAILABLE => qw( | ||
| 23 | POSIX Carp Config Cwd Exporter Fcntl Storable Symbol | ||
| 24 | Data::Dumper Scalar::Util List::Util | ||
| 25 | File::Basename File::Copy File::Path File::Spec File::Temp | ||
| 26 | IO::File IO::Handle | ||
| 27 | Module::CoreList Time::HiRes Time::Local | ||
| 28 | MIME::Base64 Encode Encode::Detect | ||
| 29 | ); | ||
| 30 | Readonly::Hash my %ALWAYS_AVAILABLE_SET => map { $_ => 1 } @ALWAYS_AVAILABLE; | ||
| 31 | |||
| 32 | =head1 NAME | ||
| 33 | |||
| 34 | Test::CPAN::Health::Check::DeclaredDeps - Verify all runtime module dependencies are declared in META | ||
| 35 | |||
| 36 | =head1 SYNOPSIS | ||
| 37 | |||
| 38 | use Test::CPAN::Health::Check::DeclaredDeps; | ||
| 39 | |||
| 40 | my $check = Test::CPAN::Health::Check::DeclaredDeps->new; | ||
| 41 | my $result = $check->run($dist); | ||
| 42 | |||
| 43 | printf "%s: %s\n", $result->status, $result->summary; | ||
| 44 | |||
| 45 | =head1 DESCRIPTION | ||
| 46 | |||
| 47 | Scans all C<.pm> and script files in the distribution for C<use MODULE> | ||
| 48 | and C<require MODULE> statements, then cross-references the extracted | ||
| 49 | module names against the runtime prerequisites declared in the distribution's | ||
| 50 | META (or MYMETA) file. | ||
| 51 | |||
| 52 | A module is B<not> flagged as undeclared when it is: | ||
| 53 | |||
| 54 | =over 4 | ||
| 55 | |||
| 56 | =item * An all-lowercase Perl pragma (C<strict>, C<warnings>, C<autodie>, | ||
| 57 | C<utf8>, C<feature>, C<constant>, etc.). | ||
| 58 | |||
| 59 | =item * An internal module within this distribution (same namespace prefix). | ||
| 60 | |||
| 61 | =item * A version string (C<require 5.014>) -- starts with a digit. | ||
| 62 | |||
| 63 | =item * A module known to be in Perl core for the declared minimum Perl | ||
| 64 | version, detected via L<Module::CoreList> when available. | ||
| 65 | |||
| 66 | =item * A member of the hardcoded C<@ALWAYS_AVAILABLE> list for cases where | ||
| 67 | L<Module::CoreList> is unavailable. | ||
| 68 | |||
| 69 | =back | ||
| 70 | |||
| 71 | Score = 100 * (used_and_declared / total_used). | ||
| 72 | Status: C<pass> when all dependencies are declared; C<warn> when score E<ge> | ||
| 73 | 80; C<fail> otherwise. | ||
| 74 | |||
| 75 | =head1 LIMITATIONS | ||
| 76 | |||
| 77 | =over 4 | ||
| 78 | |||
| 79 | =item * Requires a META or MYMETA file to provide the declared prerequisite | ||
| 80 | list. If no META exists the check is skipped. | ||
| 81 | |||
| 82 | =item * Dynamic C<require> expressions (e.g. C<require $class>) are not | ||
| 83 | detected -- only static string forms. | ||
| 84 | |||
| 85 | =item * POD code examples that contain C<use MODULE> are excluded via a | ||
| 86 | simple line-by-line POD state machine. | ||
| 87 | |||
| 88 | =back | ||
| 89 | |||
| 90 | =cut | ||
| 91 | |||
| 92 | sub id { return 'declared_deps' } | ||
| 93 | sub name { return 'Declared Dependencies' } | ||
| 94 | sub description { return 'Checks that all runtime module uses are declared in META prerequisites' } | ||
| 95 | sub weight { return 5 } | ||
| 96 | sub category { return 'packaging' } | ||
| 97 | |||
| 98 | =head2 run | ||
| 99 | |||
| 100 | =head3 PURPOSE | ||
| 101 | |||
| 102 | Scan source files for C<use>/C<require> module statements and compare against | ||
| 103 | the runtime prerequisites declared in META. | ||
| 104 | |||
| 105 | =head3 API SPECIFICATION | ||
| 106 | |||
| 107 | =head4 INPUT | ||
| 108 | |||
| 109 | dist Test::CPAN::Health::Distribution required | ||
| 110 | context Hashref optional | ||
| 111 | |||
| 112 | =head4 OUTPUT | ||
| 113 | |||
| 114 | L<Test::CPAN::Health::Result> with check_id C<'declared_deps'>. | ||
| 115 | |||
| 116 | =head3 MESSAGES | ||
| 117 | |||
| 118 | Code | Severity | Message | Resolution | ||
| 119 | ------+----------+----------------------------------------------+----------- | ||
| 120 | DD001 | SKIP | No META file found | Run perl Makefile.PL first | ||
| 121 | DD002 | SKIP | No source files to scan | Add .pm files under lib/ | ||
| 122 | DD003 | PASS | All N runtime dependencies are declared | | ||
| 123 | DD004 | WARN | N undeclared runtime dependencies found | Add to PREREQ_PM / cpanfile | ||
| 124 | DD005 | FAIL | N undeclared runtime dependencies found | Add to PREREQ_PM / cpanfile | ||
| 125 | |||
| 126 | =head3 FORMAL SPECIFICATION | ||
| 127 | |||
| 128 | Pre: dist isa Test::CPAN::Health::Distribution | ||
| 129 | META or MYMETA file present (else skip) | ||
| 130 | Post: n_bad = 0 => status = pass /\ score = 100 | ||
| 131 | n_bad > 0 | ||
| 132 | /\ score >= 80 => status = warn | ||
| 133 | /\ score < 80 => status = fail | ||
| 134 | |||
| 135 | =head3 SIDE EFFECTS | ||
| 136 | |||
| 137 | Reads source files from disk. No network I/O. | ||
| 138 | |||
| 139 | =head3 USAGE EXAMPLE | ||
| 140 | |||
| 141 | my $result = Test::CPAN::Health::Check::DeclaredDeps->new->run($dist); | ||
| 142 | if ($result->status ne 'pass') { | ||
| 143 | print "Undeclared: ", join(', ', @{ $result->details }), "\n"; | ||
| 144 | } | ||
| 145 | |||
| 146 | =cut | ||
| 147 | |||
| 148 | sub run { | ||
| 149 | my ($self, $dist, $context) = @_; | ||
| 150 | |||
| 151 | croak 'dist must be a Test::CPAN::Health::Distribution' | ||
| 152 | unless ref($dist) && $dist->isa('Test::CPAN::Health::Distribution'); | ||
| 153 | |||
| 154 | my $meta = $dist->meta; | ||
| 155 | return $self->_skip('No META or MYMETA file found -- run perl Makefile.PL first') | ||
| 156 | unless $meta; | ||
| 157 | |||
| 158 | my %declared = _collect_declared($meta); | ||
| 159 | my @files = (@{ $dist->pm_files }, @{ $dist->pl_files }); | ||
| 160 | return $self->_skip('No source files to scan') unless @files; | ||
| 161 | |||
| 162 | # Determine the distribution's own top-level namespace so self-referential | ||
| 163 | # internal uses (e.g. the dist using its own sub-modules) are not flagged. | ||
| 164 | my $dist_ns = do { | ||
| 165 | my $n = $dist->name // ''; | ||
| 166 | $n =~ s/-/::/gx; | ||
| 167 | $n; | ||
| 168 | }; | ||
| 169 | |||
| 170 | my $use_corelist = eval { require Module::CoreList; 1 }; | ||
| 171 | |||
| 172 | my %used; | ||
| 173 | for my $file (@files) { | ||
| 174 | my %file_used = _scan_file($file, $dist_ns, $use_corelist); | ||
| 175 | $used{$_}++ for keys %file_used; | ||
| 176 | } | ||
| 177 | |||
| 178 | return $self->_result( | ||
| 179 | status => 'pass', | ||
| 180 | score => 100, | ||
| 181 | summary => 'No external runtime dependencies detected in source', | ||
| 182 | data => { name => $self->name, used => 0, undeclared => 0 }, | ||
| 183 | ) unless %used; | ||
| 184 | |||
| 185 | my @undeclared = sort grep { | ||
| 186 | !$declared{$_} && !_covered_by_namespace(\%declared, $_) | ||
| 187 | } keys %used; | ||
| 188 | |||
| 189 | return $self->_build_result(\%used, \@undeclared); | ||
| 190 | } | ||
| 191 | |||
| 192 | ## no critic (ProhibitUnusedPrivateSubroutines) | ||
| 193 | sub _build_result { | ||
| 194 | my ($self, $used_ref, $undeclared_ref) = @_; | ||
| 195 | |||
| 196 | my $n_bad = scalar @{$undeclared_ref}; | ||
| 197 | my $n_total = scalar keys %{$used_ref}; | ||
| 198 | |||
| 199 | if ($n_bad == 0) { | ||
| 200 | my $dep_word = $n_total == 1 ? 'dependency is' : 'dependencies are'; | ||
| 201 | return $self->_result( | ||
| 202 | status => 'pass', | ||
| 203 | score => 100, | ||
| 204 | summary => "All $n_total runtime $dep_word declared", | ||
| 205 | data => { name => $self->name, used => $n_total, undeclared => 0 }, | ||
| 206 | ); | ||
| 207 | } | ||
| 208 | |||
| 209 | my $score = int(100 * ($n_total - $n_bad) / $n_total); | ||
| 210 | my $status = $score >= $SCORE_WARN ? 'warn' : 'fail'; | ||
| 211 | my $noun = $n_bad == 1 ? 'dependency' : 'dependencies'; | ||
| 212 | |||
| 213 | return $self->_result( | ||
| 214 | status => $status, | ||
| 215 | score => $score, | ||
| 216 | summary => "$n_bad undeclared runtime $noun found", | ||
| 217 | details => [ map { "Undeclared: $_" } @{$undeclared_ref} ], | ||
| 218 | data => { | ||
| 219 | name => $self->name, | ||
| 220 | used => $n_total, | ||
| 221 | undeclared => $n_bad, | ||
| 222 | modules => $undeclared_ref, | ||
| 223 | }, | ||
| 224 | ); | ||
| 225 | } | ||
| 226 | |||
| 227 | # --------------------------------------------------------------------------- | ||
| 228 | # Private helpers | ||
| 229 | # --------------------------------------------------------------------------- | ||
| 230 | |||
| 231 | ## no critic (ProhibitUnusedPrivateSubroutines) | ||
| 232 | sub _collect_declared { | ||
| 233 | my ($meta) = @_; | ||
| 234 | my $prereqs = $meta->effective_prereqs; | ||
| 235 | my %declared; | ||
| 236 | for my $phase (qw(runtime configure build develop)) { | ||
| 237 | my $req = $prereqs->requirements_for($phase, 'requires'); | ||
| 238 | $declared{$_} = 1 for $req->required_modules; | ||
| 239 | } | ||
| 240 | return %declared; | ||
| 241 | } | ||
| 242 | |||
| 243 | ## no critic (ProhibitUnusedPrivateSubroutines) | ||
| 244 | sub _scan_file { | ||
| 245 | my ($file, $dist_ns, $use_corelist) = @_; | ||
| 246 | my %found; | ||
| 247 | my $in_pod = 0; | ||
| 248 | |||
| 249 | open my $fh, '<', $file; | ||
| 250 | my @lines = <$fh>; | ||
| 251 | close $fh; | ||
| 252 | |||
| 253 | for my $line (@lines) { | ||
| 254 | if ($line =~ /^ = (\w+) /x) { | ||
| 255 | $in_pod = ($1 ne 'cut'); | ||
| 256 | next; | ||
| 257 | } | ||
| 258 | next if $in_pod; | ||
| 259 | next if $line =~ /^\s*[#]/x; # [#] so /x does not eat the literal # | ||
| 260 | |||
| 261 | # Remove eval { ... } blocks so that optional 'require' inside eval | ||
| 262 | # (e.g. eval { require Module::Foo; 1 }) are not treated as hard deps. | ||
| 263 | my $scan = $line; | ||
| 264 | $scan =~ s/ \b eval \s* [{] [^}]* [}] //gx; | ||
| 265 | |||
| 266 | while ($scan =~ / \b (?:use|require) \s+ ([\w:]+) /gx) { | ||
| 267 | my $mod = $1; | ||
| 268 | next if $mod =~ /^\d/x; | ||
| 269 | next unless _is_external_dep($mod, $dist_ns, $use_corelist); | ||
| 270 | $found{$mod}++; | ||
| 271 | } | ||
| 272 | } | ||
| 273 | |||
| 274 | return %found; | ||
| 275 | } | ||
| 276 | |||
| 277 | # Returns true when $mod is covered by a parent-namespace or two-part-prefix | ||
| 278 | # match in %declared. This handles: | ||
| 279 | # - CPAN::Audit::Query covered by declared CPAN::Audit | ||
| 280 | # - Module::CPANTS::Kwalitee covered by declared Module::CPANTS::Analyse | ||
| 281 | ## no critic (ProhibitUnusedPrivateSubroutines) | ||
| 282 | sub _covered_by_namespace { | ||
| 283 | my ($declared_ref, $mod) = @_; | ||
| 284 | |||
| 285 | for my $decl (keys %{$declared_ref}) { | ||
| 286 | # Strict sub-namespace: Foo::Bar::Baz covered by declared Foo::Bar | ||
| 287 | return 1 if index($mod, "${decl}::") == 0; | ||
| 288 | |||
| 289 | # Two-part-prefix sibling: Module::CPANTS::Kwalitee covered by Module::CPANTS::Analyse | ||
| 290 | my @mod_p = split / ::/x, $mod; | ||
| 291 | my @dec_p = split / ::/x, $decl; | ||
| 292 | next if @mod_p < 3 || @dec_p < 3; | ||
| 293 | return 1 if $mod_p[0] eq $dec_p[0] && $mod_p[1] eq $dec_p[1]; | ||
| 294 | } | ||
| 295 | |||
| 296 | return 0; | ||
| 297 | } | ||
| 298 | |||
| 299 | # Returns true when $mod is an external CPAN dependency that must be declared. | ||
| 300 | ## no critic (ProhibitUnusedPrivateSubroutines) | ||
| 301 | sub _is_external_dep { | ||
| 302 | my ($mod, $dist_ns, $use_corelist) = @_; | ||
| 303 | |||
| 304 | # All-lowercase names are Perl pragmas (strict, warnings, autodie, etc.) | ||
| 305 | return 0 if $mod =~ /^ [a-z] [a-z0-9_]* (?: :: [a-z] [a-z0-9_]* )* $/x; | ||
| 306 | |||
| 307 | # Internal -- same distribution namespace | ||
| 308 | if (length $dist_ns) { | ||
| 309 | return 0 if index($mod, $dist_ns) == 0; | ||
| 310 | } | ||
| 311 | |||
| 312 | # Hardcoded always-available set (core modules we know to always be present) | ||
| 313 | return 0 if $ALWAYS_AVAILABLE_SET{$mod}; | ||
| 314 | |||
| 315 | # Module::CoreList: if the module has ever been in Perl core, skip it. | ||
| 316 | if ($use_corelist) { | ||
| 317 | return 0 if defined Module::CoreList->first_release($mod); | ||
| 318 | } | ||
| 319 | |||
| 320 | return 1; | ||
| 321 | } | ||
| 322 | |||
| 323 | =head1 AUTHOR | ||
| 324 | |||
| 325 | Nigel Horne, C<< <njh at nigelhorne.com> >> | ||
| 326 | |||
| 327 | =head1 LICENSE AND COPYRIGHT | ||
| 328 | |||
| 329 | Copyright (C) 2026 Nigel Horne. | ||
| 330 | |||
| 331 | This program is free software; you can redistribute it and/or modify it | ||
| 332 | under the terms of the GNU General Public License as published by the Free | ||
| 333 | Software Foundation; either version 2 of the License, or (at your option) | ||
| 334 | any later version. | ||
| 335 | |||
| 336 | =cut | ||
| 337 | |||
| 338 | 1; | ||