blib/lib/Test/CPAN/Health/Runner.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/Result.pm ↑ Summary scripts/cpan-health »
1 package Test::CPAN::Health::Runner;
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 Readonly;
9 use Scalar::Util qw(blessed);
10 use Params::Validate::Strict qw(validate_strict);
11  
12 our $VERSION = '0.1.0';
13  
14 =head1 NAME
15  
16 Test::CPAN::Health::Runner - Orchestrate health checks against a distribution
17  
18 =head1 SYNOPSIS
19  
20 use Test::CPAN::Health::Runner;
21  
22 my $runner = Test::CPAN::Health::Runner->new(
23 checks => \@check_objects,
24 cache => $cache,
25 );
26  
27 my $report = $runner->run($distribution);
28  
29 =head1 DESCRIPTION
30  
31 The Runner iterates over an ordered list of L<Test::CPAN::Health::Check>
32 objects, invokes each against a L<Test::CPAN::Health::Distribution>, wraps
33 any exceptions so a single failing check cannot abort the run, and collects
34 the L<Test::CPAN::Health::Result> objects into a L<Test::CPAN::Health::Report>.
35  
36 Context propagation: after each check completes its result is stored, and
37 subsequent checks can inspect previously-completed results via the context
38 hashref passed to C<run>. This is how C<ReverseDeps> count reaches
39 C<SecurityAdvisories> to scale its weight.
40  
41 =head1 LIMITATIONS
42  
43 =over 4
44  
45 =item * Checks run sequentially. Parallel execution via C<Parallel::ForkManager>
46 is planned for a future release.
47  
48 =item * A check that calls C<exit> directly will terminate the entire run.
49  
50 =back
51  
52 =cut
53  
54 sub new {
55 my ($class, %args) = @_;
56  
57 %args = %{ validate_strict(
58 schema => {
59 checks => { type => 'arrayref', optional => 1, default => [] },
60 cache => { type => 'object', isa => 'Test::CPAN::Health::Cache', optional => 1 },
61 },
62 input => \%args,
63 ) };
64  
65 my $self = bless {
66 _checks => $args{checks},
67 _cache => $args{cache},
68 }, $class;
69  
70 return $self;
71 }
72  
73 =head2 run
74  
75 =head3 PURPOSE
76  
77 Execute all configured checks against the given distribution and return a
78 populated L<Test::CPAN::Health::Report>.
79  
80 Each check is wrapped in an eval block: exceptions produce an C<error>-status
81 Result rather than aborting the run. Checks may return C<undef> to indicate
82 they are not applicable; those are silently skipped.
83  
84 =head3 API SPECIFICATION
85  
86 =head4 INPUT
87  
88 dist Test::CPAN::Health::Distribution required
89  
90 =head4 OUTPUT
91  
92 L<Test::CPAN::Health::Report> object with all results attached.
93  
94 =head3 MESSAGES
95  
96 Code | Severity | Message | Resolution
97 ------+----------+--------------------------------------+------------------------
98 RUN01 | WARNING | Check {id} failed with exception {e} | Fix check or report bug
99  
100 =head3 FORMAL SPECIFICATION
101  
102 -- Z schema (placeholder) --
103 RunOp
104 checks : seq Check
105 dist : Distribution
106 report! : Report
107 -------------------------------------------------------
108 #report!.results <= #checks
109 forall c : checks @
110 (exists r : report!.results @ r.check_id = c.id)
111 \/ c returned undefined
112  
113 =head3 SIDE EFFECTS
114  
115 Runs each check, which may have network, filesystem, and subprocess side
116 effects. Writes check results to the cache if a cache is configured.
117  
118 =head3 USAGE EXAMPLE
119  
120 my $report = $runner->run($dist);
121 printf "%d checks run\n", scalar @{$report->results};
122  
123 =cut
124  
125 sub run {
126 my ($self, $dist) = @_;
127  
128 croak 'dist must be a Test::CPAN::Health::Distribution'
129 unless blessed($dist) && $dist->isa('Test::CPAN::Health::Distribution');
130  
131 require Test::CPAN::Health::Report;
132  
133 my $report = Test::CPAN::Health::Report->new(checks => $self->{_checks});
134 my %context; # shared state for inter-check communication
135  
136 for my $check (@{$self->{_checks}}) {
137 my $result = $self->_run_one($check, $dist, \%context);
138 next unless defined $result;
139  
140 # Stamp the check category onto the result's data hash so the
141 # Report can group by category without holding a reference to checks.
142 $result->data->{category} = $check->category;
143  
144 $report->add_result($result);
145  
146 # Publish to context so later checks can observe earlier outcomes.
147 $context{ $check->id } = $result;
148 }
149  
150 return $report;
151 }
152  
153 # ---------------------------------------------------------------------------
154 # Private helpers
155 # ---------------------------------------------------------------------------
156  
157 # Attempt to run one check, catching any exception and converting it to an
158 # error Result. Also handles the cache lookup/store cycle.
159 sub _run_one {
160 my ($self, $check, $dist, $context) = @_;
161  
162 my $cache_key = $self->_cache_key($check, $dist);
163  
164 # Cache hit: deserialise and return early -- avoids network/disk work.
165 if ($self->{_cache} && defined $cache_key) {
166 my $cached = $self->{_cache}->get($cache_key);
167 if (defined $cached) {
168 require Test::CPAN::Health::Result;
169 return Test::CPAN::Health::Result->new(%{$cached});
170 }
171 }
172  
173 my $result;
174 my $ok = eval {
175 local $SIG{__DIE__} = sub { }; # suppress autodie noise inside eval
176 $result = $check->run($dist, $context);
177 1;
178 };
179  
180 if (!$ok) {
181 carp sprintf("Check '%s' failed with exception: %s", $check->id, $@);
182 $result = $check->_error("Internal check error: $@");
183 } elsif (defined $result
184 && !(blessed($result) && $result->isa('Test::CPAN::Health::Result')))
185 {
186 # A check that returns a defined non-Result value is a programming error.
187 # Convert it to an error Result so the run can continue rather than dying
188 # when the outer loop tries to call $result->data->{category}.
189 carp sprintf("Check '%s' returned a non-Result value (type: %s); treating as error",
190 $check->id, ref($result) || 'SCALAR');
191 $result = $check->_error(
192 sprintf('Check returned non-Result: %s', ref($result) || 'SCALAR'),
193 );
194 }
195  
196 # Cache pass/warn/fail results so network API calls are not repeated.
197 # Skip results are NOT cached -- they reflect runtime flags (e.g. --no-network)
198 # or missing optional deps, not immutable distribution properties. Caching a
199 # skip would hide the real result on the next run once the flag is removed or
200 # the optional dep is installed.
201 if ($self->{_cache} && defined $result
202 && !$result->is_error && !$result->is_skip && defined $cache_key)
203 {
204 $self->{_cache}->store($cache_key, $result->as_hash);
205 }
206  
207 return $result;
208 }
209  
210 # Build a cache key from the dist name+version and check id.
211 # Returns undef for checks that should never be cached (future: per-check flag).
212 sub _cache_key {
213 my ($self, $check, $dist) = @_;
214  
215 my $name = $dist->name // return;
216 my $version = $dist->version // 'UNKNOWN';
217  
218 return join(':', $check->id, $name, $version);
219 }
220  
221 =head1 AUTHOR
222  
223 Nigel Horne, C<< <njh at nigelhorne.com> >>
224  
225 =head1 LICENSE AND COPYRIGHT
226  
227 Copyright (C) 2025-2026 Nigel Horne.
228  
229 This program is free software; you can redistribute it and/or modify it
230 under the terms of the GNU General Public License as published by the Free
231 Software Foundation; either version 2 of the License, or (at your option)
232 any later version.
233  
234 =cut
235  
236 1;
« blib/lib/Test/CPAN/Health/Result.pm ↑ Summary scripts/cpan-health »