« blib/lib/Test/CPAN/Health/Check/Examples.pm
↑ Summary
blib/lib/Test/CPAN/Health/Check/License.pm »
| 1 | package Test::CPAN::Health::Check::Kwalitee; | ||
| 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 | Readonly::Scalar my $SCORE_PASS => 80; | ||
| 16 | Readonly::Scalar my $SCORE_WARN => 60; | ||
| 17 | |||
| 18 | =head1 NAME | ||
| 19 | |||
| 20 | Test::CPAN::Health::Check::Kwalitee - Check CPANTS kwalitee indicators | ||
| 21 | |||
| 22 | =head1 SYNOPSIS | ||
| 23 | |||
| 24 | use Test::CPAN::Health::Check::Kwalitee; | ||
| 25 | |||
| 26 | my $check = Test::CPAN::Health::Check::Kwalitee->new; | ||
| 27 | my $result = $check->run($dist); | ||
| 28 | |||
| 29 | printf "%s: %s\n", $result->status, $result->summary; | ||
| 30 | |||
| 31 | =head1 DESCRIPTION | ||
| 32 | |||
| 33 | Uses L<Module::CPANTS::Analyse> to evaluate the distribution against the | ||
| 34 | standard CPANTS kwalitee indicator set and converts the result into a | ||
| 35 | 0-100 check score. | ||
| 36 | |||
| 37 | Experimental indicators (C<is_experimental = 1>) are excluded from the | ||
| 38 | score because they may change without notice and should not gate a | ||
| 39 | distribution's health status. Core and extra indicators both count. | ||
| 40 | |||
| 41 | Score = (passed_non_experimental / total_non_experimental) * 100. | ||
| 42 | Status thresholds: pass E<ge> 80, warn E<ge> 60, fail otherwise. | ||
| 43 | |||
| 44 | =head1 LIMITATIONS | ||
| 45 | |||
| 46 | =over 4 | ||
| 47 | |||
| 48 | =item * L<Module::CPANTS::Analyse> expects a directory laid out like an | ||
| 49 | unpacked CPAN tarball. Fields like C<manifest_matches_dist> may return | ||
| 50 | false for a plain git working directory that lacks a C<MANIFEST> file. | ||
| 51 | |||
| 52 | =item * The C<_dangerous> option is required when analysing a local | ||
| 53 | directory rather than a downloaded distribution. | ||
| 54 | |||
| 55 | =back | ||
| 56 | |||
| 57 | =cut | ||
| 58 | |||
| 59 | sub id { return 'kwalitee' } | ||
| 60 | sub name { return 'CPANTS Kwalitee' } | ||
| 61 | sub description { return 'Checks CPANTS kwalitee indicators via Module::CPANTS::Analyse' } | ||
| 62 | sub weight { return 5 } | ||
| 63 | sub category { return 'quality' } | ||
| 64 | |||
| 65 | =head2 run | ||
| 66 | |||
| 67 | =head3 PURPOSE | ||
| 68 | |||
| 69 | Run the full CPANTS kwalitee analysis against the distribution and return | ||
| 70 | a scored result. | ||
| 71 | |||
| 72 | =head3 API SPECIFICATION | ||
| 73 | |||
| 74 | =head4 INPUT | ||
| 75 | |||
| 76 | dist Test::CPAN::Health::Distribution required | ||
| 77 | context Hashref optional | ||
| 78 | |||
| 79 | =head4 OUTPUT | ||
| 80 | |||
| 81 | L<Test::CPAN::Health::Result> with check_id C<'kwalitee'>. | ||
| 82 | |||
| 83 | =head3 MESSAGES | ||
| 84 | |||
| 85 | Code | Severity | Message | Resolution | ||
| 86 | ------+----------+-----------------------------------------------+----------- | ||
| 87 | KW001 | SKIP | Module::CPANTS::Analyse is not installed | cpanm Module::CPANTS::Analyse | ||
| 88 | KW002 | ERROR | Module::CPANTS::Analyse failed: ... | See error detail | ||
| 89 | KW003 | ERROR | No kwalitee indicators found | Upgrade Module::CPANTS::Analyse | ||
| 90 | KW004 | PASS | N of M kwalitee indicators passed (P%) | | ||
| 91 | KW005 | WARN | N of M kwalitee indicators passed (P%) | Fix listed indicators | ||
| 92 | KW006 | FAIL | N of M kwalitee indicators passed (P%) | Fix listed indicators | ||
| 93 | |||
| 94 | =head3 FORMAL SPECIFICATION | ||
| 95 | |||
| 96 | -- Z schema (placeholder) -- | ||
| 97 | KwaliteeOp | ||
| 98 | passed : N | ||
| 99 | total : N | ||
| 100 | score : 0..100 | ||
| 101 | ------------------------------------------------------- | ||
| 102 | Module::CPANTS::Analyse unavailable => status = skip | ||
| 103 | total = 0 => status = error | ||
| 104 | score >= 80 => status = pass | ||
| 105 | score >= 60 => status = warn | ||
| 106 | score < 60 => status = fail | ||
| 107 | |||
| 108 | =head3 SIDE EFFECTS | ||
| 109 | |||
| 110 | Reads all source files in the distribution. No network or subprocess I/O. | ||
| 111 | |||
| 112 | =head3 USAGE EXAMPLE | ||
| 113 | |||
| 114 | my $result = Test::CPAN::Health::Check::Kwalitee->new->run($dist); | ||
| 115 | printf "Kwalitee: %d/100\n", $result->score; | ||
| 116 | |||
| 117 | =cut | ||
| 118 | |||
| 119 | sub run { | ||
| 120 | my ($self, $dist, $context) = @_; | ||
| 121 | |||
| 122 | croak 'dist must be a Test::CPAN::Health::Distribution' | ||
| 123 | unless ref($dist) && $dist->isa('Test::CPAN::Health::Distribution'); | ||
| 124 | |||
| 125 | my $loaded = eval { require Module::CPANTS::Analyse; 1 }; | ||
| 126 | return $self->_skip('Module::CPANTS::Analyse is not installed') unless $loaded; | ||
| 127 | |||
| 128 | require Module::CPANTS::Kwalitee; | ||
| 129 | |||
| 130 | # Collect all indicators, scoring only the non-experimental ones. | ||
| 131 | my $kw_obj = Module::CPANTS::Kwalitee->new; | ||
| 132 | my @all_inds = @{ $kw_obj->get_indicators }; | ||
| 133 | my @scored_inds = grep { !$_->{is_experimental} } @all_inds; | ||
| 134 | my $total = scalar @scored_inds; | ||
| 135 | |||
| 136 | return $self->_error('No kwalitee indicators found') unless $total; | ||
| 137 | |||
| 138 | my $analyser = Module::CPANTS::Analyse->new({ | ||
| 139 | distdir => $dist->path, | ||
| 140 | dist => $dist->path, | ||
| 141 | _dangerous => 1, | ||
| 142 | }); | ||
| 143 | |||
| 144 | my $ok = eval { | ||
| 145 | local $SIG{__WARN__} = sub {}; # suppress numerous undef-value warnings | ||
| 146 | $analyser->analyse; | ||
| 147 | $analyser->calc_kwalitee; | ||
| 148 | 1; | ||
| 149 | }; | ||
| 150 | return $self->_error("Module::CPANTS::Analyse failed: $@") unless $ok; | ||
| 151 | |||
| 152 | my $d = $analyser->d; | ||
| 153 | my $kwhash = $d->{kwalitee} // {}; | ||
| 154 | |||
| 155 | my ($passed, @failed_core, @failed_extra) = (0); | ||
| 156 | for my $ind (@scored_inds) { | ||
| 157 | my $name = $ind->{name}; | ||
| 158 | if ($kwhash->{$name}) { | ||
| 159 | $passed++; | ||
| 160 | } elsif ($ind->{is_extra}) { | ||
| 161 | push @failed_extra, $name; | ||
| 162 | } else { | ||
| 163 | push @failed_core, $name; | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | my $score = int($passed / $total * 100); | ||
| 168 | my $status = $score >= $SCORE_PASS ? 'pass' | ||
| 169 | : $score >= $SCORE_WARN ? 'warn' | ||
| 170 | : 'fail'; | ||
| 171 | |||
| 172 | my @details = ( | ||
| 173 | (map { "Core indicator not met: $_" } sort @failed_core), | ||
| 174 | (map { "Extra indicator not met: $_" } sort @failed_extra), | ||
| 175 | ); | ||
| 176 | |||
| 177 | return $self->_result( | ||
| 178 | status => $status, | ||
| 179 | score => $score, | ||
| 180 | summary => sprintf( | ||
| 181 | '%d of %d kwalitee indicators passed (%d%%)', | ||
| 182 | $passed, $total, $score, | ||
| 183 | ), | ||
| 184 | details => \@details, | ||
| 185 | data => { | ||
| 186 | name => $self->name, | ||
| 187 | passed => $passed, | ||
| 188 | total => $total, | ||
| 189 | failed_core => \@failed_core, | ||
| 190 | failed_extra => \@failed_extra, | ||
| 191 | }, | ||
| 192 | ); | ||
| 193 | } | ||
| 194 | |||
| 195 | =head1 AUTHOR | ||
| 196 | |||
| 197 | Nigel Horne, C<< <njh at nigelhorne.com> >> | ||
| 198 | |||
| 199 | =head1 LICENSE AND COPYRIGHT | ||
| 200 | |||
| 201 | Copyright (C) 2025-2026 Nigel Horne. | ||
| 202 | |||
| 203 | This program is free software; you can redistribute it and/or modify it | ||
| 204 | under the terms of the GNU General Public License as published by the Free | ||
| 205 | Software Foundation; either version 2 of the License, or (at your option) | ||
| 206 | any later version. | ||
| 207 | |||
| 208 | =cut | ||
| 209 | |||
| 210 | 1; | ||