blib/lib/Test/CPAN/Health/Check.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Test/CPAN/Health/Cache.pm ↑ Summary blib/lib/Test/CPAN/Health/Check/AbandonedDeps.pm »
1 package Test::CPAN::Health::Check;
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 # Sub::Protected enforces that _result/_skip/_error may only be called from
8 # within Test::CPAN::Health::Check or its subclasses -- not from the outside.
9 use Sub::Protected;
10  
11 use Carp qw(croak carp);
12 use Readonly;
13 use Params::Validate::Strict qw(validate_strict);
14  
15 our $VERSION = '0.1.0';
16  
17 # Valid categories for grouping checks in the report.
18 Readonly::Array my @VALID_CATEGORIES => qw(packaging quality security ci);
19 Readonly::Hash my %CATEGORY_SET => map { $_ => 1 } @VALID_CATEGORIES;
20  
21 =head1 NAME
22  
23 Test::CPAN::Health::Check - Abstract base class for all health checks
24  
25 =head1 SYNOPSIS
26  
27 package Test::CPAN::Health::Check::MyCheck;
28  
29 use parent 'Test::CPAN::Health::Check';
30  
31 sub id { return 'my_check' }
32 sub name { return 'My Check' }
33 sub description { return 'Checks something useful' }
34 sub weight { return 4 }
35 sub category { return 'quality' }
36  
37 sub run {
38 my ($self, $dist) = @_;
39  
40 # ... perform analysis on $dist ...
41  
42 return $self->_result(
43 status => 'pass',
44 score => 100,
45 summary => 'Everything looks good',
46 );
47 }
48  
49 1;
50  
51 =head1 DESCRIPTION
52  
53 Every health check in C<Test::CPAN::Health> is a subclass of this base class.
54 Subclasses B<must> override C<id>, C<name>, and C<run>. Overriding
55 C<description>, C<weight>, and C<category> is recommended.
56  
57 The base class provides three B<protected> helpers (callable only from
58 subclasses): C<_result>, C<_skip>, and C<_error>. These wrap
59 L<Test::CPAN::Health::Result> construction so subclasses never need to
60 C<use> Result directly. Access is enforced at runtime by L<Sub::Protected>;
61 calling them from outside the class hierarchy throws an exception.
62  
63 =head1 LIMITATIONS
64  
65 =over 4
66  
67 =item * This is not a Moo/Moose role -- inheritance is via C<use parent>.
68  
69 =item * The C<run> method receives a fully-constructed
70 L<Test::CPAN::Health::Distribution> object; checks that need network access
71 should honour C<no_network>.
72  
73 =item * C<_result>, C<_skip>, and C<_error> are protected (subclass-only)
74 via L<Sub::Protected>. White-box test packages that inherit from this class
75 may call them freely; non-inheriting test code cannot.
76  
77 =item * L<Sub::Protected> uses a CHECK block to install access control.
78 When this module is first loaded at runtime (e.g. via C<use_ok> in a test
79 harness rather than at compile-time via C<use parent>), Perl emits a
80 "Too late to run CHECK block" warning. The runtime protection still
81 operates correctly despite this warning; it is cosmetic only.
82  
83 =back
84  
85 =cut
86  
87 =head2 new
88  
89 =head3 PURPOSE
90  
91 Construct a check instance. Subclasses do not normally need to override
92 C<new>; all check-level configuration (severity, network flag, cover flag)
93 is managed here.
94  
95 =head3 API SPECIFICATION
96  
97 =head4 INPUT
98  
99 severity integer 1..5 optional default 3
100 no_network scalar bool optional default 0
101 no_cover scalar bool optional default 0
102  
103 =head4 OUTPUT
104  
105 Blessed hashref of the concrete subclass.
106  
107 =head3 MESSAGES
108  
109 Code | Severity | Message | Resolution
110 ------+----------+---------------------------------------+----------------------------
111 CHK00 | FATAL | Unknown parameter '<key>' | Remove unrecognised argument
112 CHK00 | FATAL | severity must be integer 1..5 | Pass integer in range
113  
114 =head3 FORMAL SPECIFICATION
115  
116 Pre: class isa Test::CPAN::Health::Check
117 0 < severity <= 5 (if supplied)
118 Post: self._severity = severity // 3
119 self._no_network = no_network // 0
120 self._no_cover = no_cover // 0
121  
122 =head3 USAGE EXAMPLE
123  
124 my $check = Test::CPAN::Health::Check::SemVer->new(no_network => 1);
125  
126 =cut
127  
128 sub new {
129 my ($class, %args) = @_;
130  
131 %args = %{ validate_strict(
132 schema => {
133 severity => { type => 'integer', min => 1, max => 5, optional => 1, default => 3 },
134 no_network => { type => 'scalar', optional => 1, default => 0 },
135 no_cover => { type => 'scalar', optional => 1, default => 0 },
136 },
137 input => \%args,
138 ) };
139  
140 my $self = bless {
141 _severity => $args{severity},
142 _no_network => $args{no_network},
143 _no_cover => $args{no_cover},
144 }, $class;
145  
146 return $self;
147 }
148  
149 =head2 id
150  
151 =head3 PURPOSE
152  
153 Returns a stable, lowercase_underscore string that uniquely identifies
154 this check. Used as the hash key in Report results and in C<--skip>/C<--check>
155 CLI flags.
156  
157 =head3 API SPECIFICATION
158  
159 =head4 INPUT
160  
161 None.
162  
163 =head4 OUTPUT
164  
165 Non-empty scalar string; lowercase alphanumeric and underscores only.
166  
167 =head3 MESSAGES
168  
169 Code | Severity | Message | Resolution
170 ------+----------+-------------------------------------------+----------------------
171 CHK01 | FATAL | <Class> must implement id() | Override id() in subclass
172  
173 =head3 FORMAL SPECIFICATION
174  
175 Pre: self isa concrete subclass that overrides id()
176 Post: result /= ""
177 result =~ /^[a-z][a-z0-9_]*$/
178  
179 =head3 USAGE EXAMPLE
180  
181 print $check->id; # 'sem_ver'
182  
183 =cut
184  
185 sub id { my ($self) = @_; croak ref($self) . ' must implement id()' }
186  
187 =head2 name
188  
189 =head3 PURPOSE
190  
191 Returns a short human-readable display name for use in report headers and
192 terminal output.
193  
194 =head3 API SPECIFICATION
195  
196 =head4 INPUT
197  
198 None.
199  
200 =head4 OUTPUT
201  
202 Non-empty scalar string.
203  
204 =head3 MESSAGES
205  
206 Code | Severity | Message | Resolution
207 ------+----------+-------------------------------------------+----------------------
208 CHK02 | FATAL | <Class> must implement name() | Override name() in subclass
209  
210 =head3 FORMAL SPECIFICATION
211  
212 Pre: self isa concrete subclass that overrides name()
213 Post: result /= ""
214  
215 =head3 USAGE EXAMPLE
216  
217 print $check->name; # 'Semantic Versioning'
218  
219 =cut
220  
221 sub name { my ($self) = @_; croak ref($self) . ' must implement name()' }
222  
223 =head2 description
224  
225 =head3 PURPOSE
226  
227 Returns a one-sentence description of what the check measures.
228 The default implementation returns an empty string; subclasses are
229 encouraged to override this for tooling that exposes check metadata.
230  
231 =head3 API SPECIFICATION
232  
233 =head4 INPUT
234  
235 None.
236  
237 =head4 OUTPUT
238  
239 Scalar string. Empty string is acceptable but discouraged.
240  
241 =head3 MESSAGES
242  
243 (none -- default returns empty string; no exception path)
244  
245 =head3 FORMAL SPECIFICATION
246  
247 Post: is_string(result) -- empty string is valid
248  
249 =head3 USAGE EXAMPLE
250  
251 print $check->description;
252  
253 =cut
254  
255 sub description { return '' }
256  
257 =head2 weight
258  
259 =head3 PURPOSE
260  
261 Returns the numeric weight applied to this check's score in the weighted
262 average that produces the overall Report score. Higher weights make a
263 check more influential. The default is 1.
264  
265 =head3 API SPECIFICATION
266  
267 =head4 INPUT
268  
269 None.
270  
271 =head4 OUTPUT
272  
273 Positive integer or float.
274  
275 =head3 MESSAGES
276  
277 (none -- default returns 1; no exception path)
278  
279 =head3 FORMAL SPECIFICATION
280  
281 Post: result > 0
282  
283 =head3 USAGE EXAMPLE
284  
285 print $check->weight; # 8
286  
287 =cut
288  
289 sub weight { return 1 }
290  
291 =head2 category
292  
293 =head3 PURPOSE
294  
295 Returns the category string used to group checks in the report.
296 Must be one of: C<packaging>, C<quality>, C<security>, C<ci>.
297 The default is C<'quality'>.
298  
299 =head3 API SPECIFICATION
300  
301 =head4 INPUT
302  
303 None.
304  
305 =head4 OUTPUT
306  
307 One of: packaging, quality, security, ci.
308  
309 =head3 MESSAGES
310  
311 (none -- default returns 'quality'; subclass overrides must use a valid value)
312  
313 =head3 FORMAL SPECIFICATION
314  
315 Post: result IN {packaging, quality, security, ci}
316  
317 =head3 USAGE EXAMPLE
318  
319 print $check->category; # 'quality'
320  
321 =cut
322  
323 sub category { return 'quality' }
324  
325 =head2 run
326  
327 =head3 PURPOSE
328  
329 Executes the check against a distribution and returns a Result. Returns
330 C<undef> when the check is not applicable (e.g. CPANTesters for a dist
331 that has never been released to CPAN).
332  
333 B<Subclasses must override this method.> The base-class implementation
334 always croaks.
335  
336 =head3 API SPECIFICATION
337  
338 =head4 INPUT
339  
340 dist Test::CPAN::Health::Distribution required
341 context Hashref of check_id => Result optional
342  
343 =head4 OUTPUT
344  
345 L<Test::CPAN::Health::Result> object, or C<undef> if not applicable.
346  
347 =head3 MESSAGES
348  
349 Code | Severity | Message | Resolution
350 ------+----------+--------------------------------------------+---------------------
351 CHK03 | FATAL | <Class> must implement run() | Override run() in subclass
352  
353 =head3 FORMAL SPECIFICATION
354  
355 Pre: dist isa Test::CPAN::Health::Distribution
356 context is hashref (may be empty)
357 Post: result = undef
358 OR (result isa Result AND result.check_id = self.id)
359  
360 =head3 SIDE EFFECTS
361  
362 May perform network I/O, filesystem reads, and subprocess invocations
363 depending on the concrete check. Honours C<no_network> and C<no_cover>
364 flags to suppress optional side effects.
365  
366 =head3 USAGE EXAMPLE
367  
368 my $result = $check->run($dist, \%context);
369 print $result->summary if defined $result;
370  
371 =cut
372  
373 sub run { my ($self) = @_; croak ref($self) . ' must implement run()' }
374  
375 # ---------------------------------------------------------------------------
376 # Protected helpers -- callable from this class and all subclasses only.
377 # Sub::Protected enforces this at runtime; any call from outside the
378 # inheritance chain throws "Can't locate object method".
379 # ---------------------------------------------------------------------------
380  
381 # Purpose: Build a Result object stamped with this check's id.
382 # Convenience so subclasses don't need to 'use' Result directly.
383 # Entry: %args are passed verbatim to Result->new (with check_id prepended).
384 # Exit: Returns a Test::CPAN::Health::Result.
385 # Side effects: Requires Test::CPAN::Health::Result lazily.
386 ## no critic (ProhibitUnusedPrivateSubroutines)
387 sub _result : Protected {
388 my ($self, %args) = @_;
389  
390 require Test::CPAN::Health::Result;
391  
392 return Test::CPAN::Health::Result->new(
393 check_id => $self->id,
394 %args,
395 );
396 }
397  
398 # Purpose: Return a skip Result conveying why the check was not applicable.
399 # Entry: $reason is a human-readable explanation.
400 # Exit: Test::CPAN::Health::Result with status='skip'.
401 # Side effects: None beyond _result.
402 ## no critic (ProhibitUnusedPrivateSubroutines)
403 sub _skip : Protected {
404 my ($self, $reason) = @_;
405  
406 return $self->_result(
407 status => 'skip',
408 summary => $reason // 'Not applicable',
409 data => { name => $self->name },
410 );
411 }
412  
413 # Purpose: Return an error Result when the check itself malfunctions.
414 # (Distinct from a 'fail' result, which means the dist is defective.)
415 # Entry: $message describes the check's internal failure.
416 # Exit: Test::CPAN::Health::Result with status='error'.
417 # Side effects: None beyond _result.
418 ## no critic (ProhibitUnusedPrivateSubroutines)
419 sub _error : Protected {
420 my ($self, $message) = @_;
421  
422 return $self->_result(
423 status => 'error',
424 summary => $message // 'Unknown error',
425 data => { name => $self->name },
426 );
427 }
428  
429 # ---------------------------------------------------------------------------
430 # Public read-only accessors
431 # ---------------------------------------------------------------------------
432  
433 sub severity { my ($self) = @_; return $self->{_severity} }
434 sub no_network { my ($self) = @_; return $self->{_no_network} }
435 sub no_cover { my ($self) = @_; return $self->{_no_cover} }
436  
437 =head1 AUTHOR
438  
439 Nigel Horne, C<< <njh at nigelhorne.com> >>
440  
441 =head1 LICENSE AND COPYRIGHT
442  
443 Copyright (C) 2025-2026 Nigel Horne.
444  
445 This program is free software; you can redistribute it and/or modify it
446 under the terms of the GNU General Public License as published by the Free
447 Software Foundation; either version 2 of the License, or (at your option)
448 any later version.
449  
450 =cut
451  
452 1;
« blib/lib/Test/CPAN/Health/Cache.pm ↑ Summary blib/lib/Test/CPAN/Health/Check/AbandonedDeps.pm »