« blib/lib/Test/CPAN/Health/Check/PODCoverage.pm
↑ Summary
blib/lib/Test/CPAN/Health/Check/ReadmeSync.pm »
| 1 | package Test::CPAN::Health::Check::Perlcritic; | ||
| 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 | # Run Perl::Critic at severity 3 (the "important" band: avoids nitpicky | ||
| 17 | # style warnings while still catching meaningful policy violations). | ||
| 18 | Readonly::Scalar my $CRITIC_SEVERITY => 3; | ||
| 19 | |||
| 20 | # Status thresholds on the "fraction of clean files * 100" scale. | ||
| 21 | Readonly::Scalar my $SCORE_PASS => 90; | ||
| 22 | Readonly::Scalar my $SCORE_WARN => 50; | ||
| 23 | |||
| 24 | =head1 NAME | ||
| 25 | |||
| 26 | Test::CPAN::Health::Check::Perlcritic - Run Perl::Critic and report policy violations | ||
| 27 | |||
| 28 | =head1 SYNOPSIS | ||
| 29 | |||
| 30 | use Test::CPAN::Health::Check::Perlcritic; | ||
| 31 | |||
| 32 | my $check = Test::CPAN::Health::Check::Perlcritic->new; | ||
| 33 | my $result = $check->run($dist); | ||
| 34 | |||
| 35 | printf "%s: %s\n", $result->status, $result->summary; | ||
| 36 | |||
| 37 | =head1 DESCRIPTION | ||
| 38 | |||
| 39 | Runs L<Perl::Critic> at severity 3 against every C<.pm> and C<.pl> source | ||
| 40 | file in the distribution. Score = round((clean_files / total_files) * 100). | ||
| 41 | |||
| 42 | Status thresholds: pass E<ge> 90 %, warn E<ge> 50 %, fail below 50 %. | ||
| 43 | |||
| 44 | =head1 LIMITATIONS | ||
| 45 | |||
| 46 | =over 4 | ||
| 47 | |||
| 48 | =item * No C<.perlcriticrc> from the distribution under analysis is honoured; | ||
| 49 | the check always runs at the fixed severity level to ensure comparable scores | ||
| 50 | across distributions. | ||
| 51 | |||
| 52 | =item * Files that cannot be parsed by PPI (Perl::Critic's parser) are skipped | ||
| 53 | with a warning and excluded from the score calculation. | ||
| 54 | |||
| 55 | =back | ||
| 56 | |||
| 57 | =cut | ||
| 58 | |||
| 59 | sub id { return 'perlcritic' } | ||
| 60 | sub name { return 'Perl::Critic' } | ||
| 61 | sub description { return 'Runs Perl::Critic and reports policy violations' } | ||
| 62 | sub weight { return 6 } | ||
| 63 | sub category { return 'quality' } | ||
| 64 | |||
| 65 | =head2 run | ||
| 66 | |||
| 67 | =head3 PURPOSE | ||
| 68 | |||
| 69 | Critique all source files at severity 3 and return a scored Result reflecting | ||
| 70 | the fraction that pass without violations. | ||
| 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<'perlcritic'>. | ||
| 82 | |||
| 83 | =head3 MESSAGES | ||
| 84 | |||
| 85 | Code | Severity | Message | Resolution | ||
| 86 | ------+----------+---------------------------------------------+----------- | ||
| 87 | CR001 | SKIP | No source files found | Add source files | ||
| 88 | CR002 | PASS | All N files pass Perl::Critic at sev 3 | | ||
| 89 | CR003 | WARN | N of M files have policy violations | Fix violations | ||
| 90 | CR004 | FAIL | Most files have Perl::Critic violations | Fix violations | ||
| 91 | |||
| 92 | =head3 FORMAL SPECIFICATION | ||
| 93 | |||
| 94 | -- Z schema (placeholder) -- | ||
| 95 | PerlcriticOp | ||
| 96 | total_files : N | ||
| 97 | clean_files : N | ||
| 98 | score : 0..100 | ||
| 99 | ------------------------------------------------------- | ||
| 100 | total_files = 0 => status = skip | ||
| 101 | score >= 90 => status = pass | ||
| 102 | score >= 50 => status = warn | ||
| 103 | score < 50 => status = fail | ||
| 104 | |||
| 105 | =head3 SIDE EFFECTS | ||
| 106 | |||
| 107 | Invokes Perl::Critic which performs in-process PPI parsing of source files. | ||
| 108 | No network or subprocess I/O. | ||
| 109 | |||
| 110 | =head3 USAGE EXAMPLE | ||
| 111 | |||
| 112 | my $result = Test::CPAN::Health::Check::Perlcritic->new->run($dist); | ||
| 113 | print $result->summary; | ||
| 114 | |||
| 115 | =cut | ||
| 116 | |||
| 117 | sub run { | ||
| 118 | my ($self, $dist, $context) = @_; | ||
| 119 | |||
| 120 | croak 'dist must be a Test::CPAN::Health::Distribution' | ||
| 121 | unless ref($dist) && $dist->isa('Test::CPAN::Health::Distribution'); | ||
| 122 | |||
| 123 | my @files = @{ $dist->all_source_files }; | ||
| 124 | |||
| 125 | unless (@files) { | ||
| 126 | return $self->_skip('No source files found'); | ||
| 127 | } | ||
| 128 | |||
| 129 | require Perl::Critic; | ||
| 130 | my $critic = Perl::Critic->new('-severity' => $CRITIC_SEVERITY); | ||
| 131 | |||
| 132 | my ($clean, $total) = (0, 0); | ||
| 133 | my @details; | ||
| 134 | |||
| 135 | for my $file (@files) { | ||
| 136 | my @violations; | ||
| 137 | my $ok = eval { | ||
| 138 | local $SIG{__WARN__} = sub { }; # suppress PPI noise | ||
| 139 | @violations = $critic->critique($file); | ||
| 140 | 1; | ||
| 141 | }; | ||
| 142 | if (!$ok) { | ||
| 143 | carp "Perl::Critic could not parse $file: $@"; | ||
| 144 | next; | ||
| 145 | } | ||
| 146 | $total++; | ||
| 147 | if (@violations) { | ||
| 148 | my $rel = File::Spec->abs2rel($file, $dist->path); | ||
| 149 | push @details, sprintf('%s: %d violation(s)', $rel, scalar @violations); | ||
| 150 | } else { | ||
| 151 | $clean++; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | unless ($total) { | ||
| 156 | return $self->_skip('No files could be parsed by Perl::Critic'); | ||
| 157 | } | ||
| 158 | |||
| 159 | my $score = int($clean / $total * 100); | ||
| 160 | my $status = $score >= $SCORE_PASS ? 'pass' | ||
| 161 | : $score >= $SCORE_WARN ? 'warn' | ||
| 162 | : 'fail'; | ||
| 163 | |||
| 164 | return $self->_result( | ||
| 165 | status => $status, | ||
| 166 | score => $score, | ||
| 167 | summary => sprintf( | ||
| 168 | '%d of %d file(s) pass Perl::Critic at severity %d', | ||
| 169 | $clean, $total, $CRITIC_SEVERITY, | ||
| 170 | ), | ||
| 171 | details => \@details, | ||
| 172 | data => { | ||
| 173 | name => $self->name, | ||
| 174 | total => $total, | ||
| 175 | clean => $clean, | ||
| 176 | severity => $CRITIC_SEVERITY, | ||
| 177 | }, | ||
| 178 | ); | ||
| 179 | } | ||
| 180 | |||
| 181 | =head1 AUTHOR | ||
| 182 | |||
| 183 | Nigel Horne, C<< <njh at nigelhorne.com> >> | ||
| 184 | |||
| 185 | =head1 LICENSE AND COPYRIGHT | ||
| 186 | |||
| 187 | Copyright (C) 2025-2026 Nigel Horne. | ||
| 188 | |||
| 189 | This program is free software; you can redistribute it and/or modify it | ||
| 190 | under the terms of the GNU General Public License as published by the Free | ||
| 191 | Software Foundation; either version 2 of the License, or (at your option) | ||
| 192 | any later version. | ||
| 193 | |||
| 194 | =cut | ||
| 195 | |||
| 196 | 1; | ||