blib/lib/Test/CPAN/Health/Check/VersionSync.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/Check/TestCoverage.pm ↑ Summary blib/lib/Test/CPAN/Health/Config.pm »
1 package Test::CPAN::Health::Check::VersionSync;
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 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_PASS => 90;
17 Readonly::Scalar my $SCORE_WARN => 50;
18  
19 =head1 NAME
20  
21 Test::CPAN::Health::Check::VersionSync - Check that all .pm files declare the same $VERSION
22  
23 =head1 SYNOPSIS
24  
25 use Test::CPAN::Health::Check::VersionSync;
26  
27 my $check = Test::CPAN::Health::Check::VersionSync->new;
28 my $result = $check->run($dist);
29  
30 printf "%s: %s\n", $result->status, $result->summary;
31  
32 =head1 DESCRIPTION
33  
34 Scans all C<.pm> files under C<lib/> for C<$VERSION> declarations and
35 verifies that every file that declares a version uses the same value as
36 the distribution version reported in the META file.
37  
38 Files that do not declare C<$VERSION> at all are not penalised (they may
39 intentionally inherit a version from a parent namespace).
40  
41 Score = (in-sync files / files-with-version) * 100.
42 Status: pass E<ge> 90, warn E<ge> 50, fail otherwise.
43  
44 =head1 LIMITATIONS
45  
46 =over 4
47  
48 =item * Only the first C<our $VERSION = '...';> line in each file is
49 checked; dynamic version assignment (e.g. via C<version::->new>) is
50 not supported.
51  
52 =item * The META version is used as the canonical source; if no META
53 file is present the check is skipped.
54  
55 =back
56  
57 =cut
58  
59 sub id { return 'version_sync' }
60 sub name { return 'Version Sync' }
61 sub description { return 'Checks that all .pm files declare the same $VERSION' }
62 sub weight { return 3 }
63 sub category { return 'packaging' }
64  
65 =head2 run
66  
67 =head3 PURPOSE
68  
69 Compare the C<$VERSION> declared in each C<.pm> file against the
70 distribution version from META, reporting any files that are out of sync.
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<'version_sync'>.
82  
83 =head3 MESSAGES
84  
85 Code | Severity | Message | Resolution
86 ------+----------+------------------------------------------------+-----------
87 VS001 | SKIP | No META file found | Add META file
88 VS002 | SKIP | No .pm files found | Add lib/ modules
89 VS003 | SKIP | META version not available | Add version to META
90 VS004 | PASS | All N file(s) with $VERSION in sync |
91 VS005 | PASS | No .pm files declare $VERSION |
92 VS006 | WARN | N file(s) have mismatched $VERSION | Update $VERSION
93 VS007 | FAIL | Most files have mismatched $VERSION | Update $VERSION
94  
95 =head3 FORMAL SPECIFICATION
96  
97 -- Z schema (placeholder) --
98 VersionSyncOp
99 meta_ver : String | undefined
100 files : seq FileName
101 mismatched : seq FileName
102 -------------------------------------------------------
103 meta = undefined => status = skip
104 meta_ver = undefined => status = skip
105 #files = 0 => status = skip
106 #mismatched = 0 => status = pass /\ score = 100
107 score >= 90 => status = pass
108 score >= 50 => status = warn
109 score < 50 => status = fail
110  
111 =head3 SIDE EFFECTS
112  
113 Reads each C<.pm> file once; no network I/O.
114  
115 =head3 USAGE EXAMPLE
116  
117 my $result = Test::CPAN::Health::Check::VersionSync->new->run($dist);
118 print $result->summary;
119  
120 =cut
121  
122 sub run {
123 my ($self, $dist, $context) = @_;
124  
125 croak 'dist must be a Test::CPAN::Health::Distribution'
126 unless ref($dist) && $dist->isa('Test::CPAN::Health::Distribution');
127  
128 my $meta = $dist->meta;
129 return $self->_skip('No META file found -- cannot determine canonical version')
130 unless $meta;
131  
132 my $meta_ver = $dist->version;
133 return $self->_skip('META version not available')
134 unless defined $meta_ver && length $meta_ver;
135  
136 my @pm_files = @{ $dist->pm_files };
137 return $self->_skip('No .pm files found under lib/')
138 unless @pm_files;
139  
140 my @mismatched;
141 my $n_with_version = 0;
142  
143 (my $norm_meta = $meta_ver) =~ s/ ^ v //x;
144  
145 for my $file (@pm_files) {
146 my $file_ver = _extract_version($file);
147 next unless defined $file_ver;
148 $n_with_version++;
149 (my $norm_file = $file_ver) =~ s/ ^ v //x;
150 if ($norm_file ne $norm_meta) {
151 my $rel = File::Spec->abs2rel($file, $dist->path);
152 push @mismatched, "$rel declares $file_ver (expected $norm_meta)";
153 }
154 }
155  
156 unless ($n_with_version) {
157 return $self->_result(
158 status => 'pass',
159 score => 100,
160 summary => 'No .pm files declare $VERSION',
161 data => { name => $self->name, meta_version => $norm_meta,
162 checked => 0, mismatched => 0 },
163 );
164 }
165  
166 my $n_bad = scalar @mismatched;
167 if ($n_bad == 0) {
168 return $self->_result(
169 status => 'pass',
170 score => 100,
171 summary => "All $n_with_version file(s) with \$VERSION are in sync ($norm_meta)",
172 data => { name => $self->name, meta_version => $norm_meta,
173 checked => $n_with_version, mismatched => 0 },
174 );
175 }
176  
177 my $score = int(100 * ($n_with_version - $n_bad) / $n_with_version);
178 my $status = $n_bad == 0 ? 'pass'
179 : $score >= $SCORE_PASS ? 'pass'
180 : $score >= $SCORE_WARN ? 'warn'
181 : 'fail';
182  
183 return $self->_result(
184 status => $status,
185 score => $score,
186 summary => "$n_bad of $n_with_version file(s) have \$VERSION out of sync with META ($norm_meta)",
187 details => \@mismatched,
188 data => {
189 name => $self->name,
190 meta_version => $norm_meta,
191 checked => $n_with_version,
192 mismatched => $n_bad,
193 },
194 );
195 }
196  
197 # ---------------------------------------------------------------------------
198 # Private helpers
199 # ---------------------------------------------------------------------------
200  
201 # Extract the first $VERSION value from a .pm file.
202 # Returns undef if no declaration is found.
203 sub _extract_version {
204 my ($file) = @_;
205  
206 open my $fh, '<', $file or return;
207 while (my $line = <$fh>) {
208 if ($line =~ / our \s+ \$VERSION \s* = \s* ['"] ( [^'"]+ ) ['"] /x) {
209 close $fh;
210 return $1;
211 }
212 }
213 close $fh;
214 return;
215 }
216  
217 =head1 AUTHOR
218  
219 Nigel Horne, C<< <njh at nigelhorne.com> >>
220  
221 =head1 LICENSE AND COPYRIGHT
222  
223 Copyright (C) 2026 Nigel Horne.
224  
225 This program is free software; you can redistribute it and/or modify it
226 under the terms of the GNU General Public License as published by the Free
227 Software Foundation; either version 2 of the License, or (at your option)
228 any later version.
229  
230 =cut
231  
232 1;
« blib/lib/Test/CPAN/Health/Check/TestCoverage.pm ↑ Summary blib/lib/Test/CPAN/Health/Config.pm »