blib/lib/Test/CPAN/Health/Config.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/VersionSync.pm ↑ Summary blib/lib/Test/CPAN/Health/Distribution.pm »
1 package Test::CPAN::Health::Config;
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 our $VERSION = '0.1.0';
13  
14 # Recognised configuration file names, in preference order.
15 Readonly::Array my @CONFIG_NAMES => qw(
16 .cpan-health.ini
17 .cpan-health.conf
18 cpan-health.ini
19 cpan-health.conf
20 );
21  
22 # Valid scalar boolean / numeric option keys.
23 Readonly::Hash my %SCALAR_KEYS => map { $_ => 1 } qw(
24 no_network no_cover min_score severity
25 );
26  
27 # Valid list option keys (comma-separated in the file).
28 Readonly::Hash my %LIST_KEYS => map { $_ => 1 } qw(
29 skip ignore_abandoned
30 );
31  
32 =head1 NAME
33  
34 Test::CPAN::Health::Config - Read per-project configuration for cpan-health
35  
36 =head1 SYNOPSIS
37  
38 use Test::CPAN::Health::Config;
39  
40 my $cfg = Test::CPAN::Health::Config->new(path => '/path/to/dist');
41  
42 # Returns the merged configuration (file values override defaults)
43 my %opts = $cfg->as_hash;
44  
45 # Individual accessors
46 print $cfg->min_score; # integer or undef
47 print join(', ', @{ $cfg->skip }); # arrayref
48 print join(', ', @{ $cfg->ignore_abandoned }); # arrayref
49  
50 =head1 DESCRIPTION
51  
52 Reads an optional C<.cpan-health.ini> (or C<cpan-health.conf>) file from a
53 distribution's root directory. The file uses a simple C<key = value> format;
54 comma-separated values are supported for list options.
55  
56 If no configuration file is found, all accessors return C<undef> (scalars)
57 or C<[]> (lists), and the caller falls back to its own defaults.
58  
59 Supported options:
60  
61 no_network = 0|1
62 no_cover = 0|1
63 min_score = 0..100
64 severity = 1..5
65 skip = check_id1, check_id2, ...
66 ignore_abandoned = Module::Name1, Module::Name2, ...
67  
68 =head1 LIMITATIONS
69  
70 =over 4
71  
72 =item * Sections (C<[section]>) are not supported; all keys live at the top level.
73  
74 =item * Unknown keys produce a C<carp> warning and are ignored.
75  
76 =item * Only the first matching config file in the preference order is read.
77  
78 =back
79  
80 =cut
81  
82 =head2 new
83  
84 =head3 PURPOSE
85  
86 Construct a Config object, locating and parsing any config file found under
87 the given distribution path.
88  
89 =head3 API SPECIFICATION
90  
91 =head4 INPUT
92  
93 path string required Absolute path to the distribution root
94  
95 =head4 OUTPUT
96  
97 Blessed Config object.
98  
99 =head3 MESSAGES
100  
101 Code | Severity | Message | Resolution
102 ------+----------+---------------------------------+--------------
103 CFG01 | WARNING | Unknown config key '<key>' | Remove the key
104 CFG02 | WARNING | Config parse error in <file> | Fix the file syntax
105  
106 =head3 FORMAL SPECIFICATION
107  
108 Pre: path is a readable directory
109 Post: self._data contains merged key/value from file (or empty if no file)
110  
111 =head3 SIDE EFFECTS
112  
113 Reads at most one file from disk.
114  
115 =head3 USAGE EXAMPLE
116  
117 my $cfg = Test::CPAN::Health::Config->new(path => $dist->path);
118 my $min = $cfg->min_score // 0;
119  
120 =cut
121  
122 sub new {
123 my ($class, %args) = @_;
124  
125 %args = %{ validate_strict(
126 schema => {
127 path => { type => 'string', optional => 1, default => '.' },
128 },
129 input => \%args,
130 ) };
131  
132 my $self = bless {
133 _path => $args{path},
134 _data => {},
135 _file => undef,
136 }, $class;
137  
138 $self->_load;
139  
140 return $self;
141 }
142  
143 =head2 as_hash
144  
145 =head3 PURPOSE
146  
147 Return all parsed configuration options as a flat hash. Suitable for
148 merging with CLI-supplied options.
149  
150 =head3 API SPECIFICATION
151  
152 =head4 INPUT
153  
154 None.
155  
156 =head4 OUTPUT
157  
158 Hash with keys: C<no_network>, C<no_cover>, C<min_score>, C<severity>,
159 C<skip> (arrayref), C<ignore_abandoned> (arrayref). Only keys that were
160 explicitly set in the config file are present.
161  
162 =head3 MESSAGES
163  
164 (none)
165  
166 =head3 FORMAL SPECIFICATION
167  
168 Post: result is a hashref subset of (SCALAR_KEYS union LIST_KEYS)
169  
170 =head3 SIDE EFFECTS
171  
172 None.
173  
174 =head3 USAGE EXAMPLE
175  
176 my %cfg = $config->as_hash;
177 my $effective_min = $cfg{min_score} // $cli_min_score // 0;
178  
179 =cut
180  
181 sub as_hash { my ($self) = @_; return %{ $self->{_data} } }
182  
183 =head2 file
184  
185 Returns the path of the config file that was loaded, or C<undef> if none was found.
186  
187 =cut
188  
189 sub file { my ($self) = @_; return $self->{_file} }
190  
191 =head2 no_network
192  
193 Returns the C<no_network> setting or C<undef>.
194  
195 =cut
196  
197 sub no_network { my ($self) = @_; return $self->{_data}{no_network} }
198  
199 =head2 no_cover
200  
201 Returns the C<no_cover> setting or C<undef>.
202  
203 =cut
204  
205 sub no_cover { my ($self) = @_; return $self->{_data}{no_cover} }
206  
207 =head2 min_score
208  
209 Returns the C<min_score> setting or C<undef>.
210  
211 =cut
212  
213 sub min_score { my ($self) = @_; return $self->{_data}{min_score} }
214  
215 =head2 severity
216  
217 Returns the C<severity> setting or C<undef>.
218  
219 =cut
220  
221 sub severity { my ($self) = @_; return $self->{_data}{severity} }
222  
223 =head2 skip
224  
225 Returns the C<skip> arrayref or C<[]>.
226  
227 =cut
228  
229 sub skip { my ($self) = @_; return $self->{_data}{skip} // [] }
230  
231 =head2 ignore_abandoned
232  
233 Returns the C<ignore_abandoned> arrayref or C<[]>.
234  
235 =cut
236  
237 sub ignore_abandoned { my ($self) = @_; return $self->{_data}{ignore_abandoned} // [] }
238  
239 # ---------------------------------------------------------------------------
240 # Private helpers
241 # ---------------------------------------------------------------------------
242  
243 sub _split_list {
244 my ($val) = @_;
245 my @raw = split / , /x, $val;
246 my @items;
247 for my $item (@raw) {
248 $item =~ s/ ^ \s+ | \s+ $ //gx;
249 push @items, $item if length $item;
250 }
251 return \@items;
252 }
253  
254 sub _load {
255 my ($self) = @_;
256  
257 my $cfg_file;
258 for my $name (@CONFIG_NAMES) {
259 my $candidate = File::Spec->catfile($self->{_path}, $name);
260 if (-f $candidate) {
261 $cfg_file = $candidate;
262 last;
263 }
264 }
265  
266 return unless defined $cfg_file;
267  
268 $self->{_file} = $cfg_file;
269  
270 open my $fh, '<', $cfg_file;
271 my @lines = <$fh>;
272 close $fh;
273  
274 for my $line (@lines) {
275 chomp $line;
276 next if _skip_line($line);
277 $self->_parse_line($line, $cfg_file);
278 }
279  
280 return;
281 }
282  
283 sub _skip_line {
284 my ($line) = @_;
285 return 1 if $line =~ / ^ \s* [#;] /x;
286 return 1 if $line =~ / ^ \s* $ /x;
287 return 1 if $line =~ / ^ \s* \[ /x;
288 return 0;
289 }
290  
291 sub _parse_line {
292 my ($self, $line, $cfg_file) = @_;
293  
294 if ($line =~ / ^ \s* ( [\w]+ ) \s* = \s* (.*?) \s* $ /x) {
295 my ($key, $val) = ($1, $2);
296 if ($SCALAR_KEYS{$key}) {
297 $self->{_data}{$key} = $val + 0;
298 } elsif ($LIST_KEYS{$key}) {
299 $self->{_data}{$key} = _split_list($val);
300 } else {
301 carp "Unknown config key '$key' in $cfg_file";
302 }
303 } else {
304 carp "Config parse error in $cfg_file: unrecognised line: $line";
305 }
306  
307 return;
308 }
309  
310 =head1 AUTHOR
311  
312 Nigel Horne, C<< <njh at nigelhorne.com> >>
313  
314 =head1 LICENSE AND COPYRIGHT
315  
316 Copyright (C) 2026 Nigel Horne.
317  
318 This program is free software; you can redistribute it and/or modify it
319 under the terms of the GNU General Public License as published by the Free
320 Software Foundation; either version 2 of the License, or (at your option)
321 any later version.
322  
323 =cut
324  
325 1;
« blib/lib/Test/CPAN/Health/Check/VersionSync.pm ↑ Summary blib/lib/Test/CPAN/Health/Distribution.pm »