blib/lib/Test/CPAN/Health/Check/License.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/Kwalitee.pm ↑ Summary blib/lib/Test/CPAN/Health/Check/MetaJSON.pm »
1 package Test::CPAN::Health::Check::License;
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 # License identifiers that are technically valid per CPAN::Meta::Spec but are
16 # too vague to be meaningful -- they give downstream consumers no actionable
17 # information about how the software may be used.
18 Readonly::Array my @VAGUE_LICENSES => qw(unknown open_source restricted unrestricted);
19 Readonly::Hash my %VAGUE_LICENSE_SET => map { $_ => 1 } @VAGUE_LICENSES;
20  
21 # File names to check for a licence text, in preference order.
22 Readonly::Array my @LICENSE_FILES => qw(
23 LICENSE
24 LICENCE
25 LICENSE.txt
26 LICENCE.txt
27 LICENSE.md
28 LICENCE.md
29 COPYING
30 );
31  
32 =head1 NAME
33  
34 Test::CPAN::Health::Check::License - Check that a valid licence is declared and a licence file is present
35  
36 =head1 SYNOPSIS
37  
38 use Test::CPAN::Health::Check::License;
39  
40 my $check = Test::CPAN::Health::Check::License->new;
41 my $result = $check->run($dist);
42  
43 printf "%s: %s\n", $result->status, $result->summary;
44  
45 =head1 DESCRIPTION
46  
47 Verifies two things:
48  
49 =over 4
50  
51 =item 1.
52  
53 The distribution declares a specific, non-vague licence in META (i.e. not
54 C<unknown>, C<open_source>, C<restricted>, or C<unrestricted>).
55  
56 =item 2.
57  
58 A corresponding licence text file exists in the distribution root
59 (LICENSE, LICENCE, LICENSE.txt, LICENCE.txt, LICENSE.md, LICENCE.md,
60 or COPYING).
61  
62 =back
63  
64 Score matrix:
65  
66 =over 4
67  
68 =item * 100 -- Specific licence declared and licence file present.
69  
70 =item * 50 -- Specific licence declared but no licence file found.
71  
72 =item * 0 -- No licence declared, or licence is vague/unknown.
73  
74 =item * skip -- No META file found.
75  
76 =back
77  
78 =head1 LIMITATIONS
79  
80 =over 4
81  
82 =item * This check does not verify that the licence text in the file matches
83 the identifier in META.
84  
85 =item * Licence identifiers are compared case-sensitively against the CPAN::Meta
86 spec identifiers (lowercase, underscore-separated).
87  
88 =back
89  
90 =cut
91  
92 sub id { return 'license' }
93 sub name { return 'License' }
94 sub description { return 'Checks that a valid licence is declared and a file present' }
95 sub weight { return 4 }
96 sub category { return 'packaging' }
97  
98 =head2 run
99  
100 =head3 PURPOSE
101  
102 Inspect the distribution's META licence declaration and confirm a licence text
103 file is present in the distribution root.
104  
105 =head3 API SPECIFICATION
106  
107 =head4 INPUT
108  
109 dist Test::CPAN::Health::Distribution required
110 context Hashref optional prior check results
111  
112 =head4 OUTPUT
113  
114 L<Test::CPAN::Health::Result> with check_id C<'license'>.
115  
116 =head3 MESSAGES
117  
118 Code | Severity | Message | Resolution
119 ------+----------+---------------------------------------------+-----------
120 LI001 | SKIP | No META file -- cannot determine licence | Add META file
121 LI002 | FAIL | No licence declared in META | Add license field
122 LI003 | FAIL | Licence declared as "{id}" -- too vague | Use specific identifier
123 LI004 | WARN | Licence declared but no licence file found | Add LICENSE file
124 LI005 | PASS | Licence declared and licence file present |
125  
126 =head3 FORMAL SPECIFICATION
127  
128 -- Z schema (placeholder) --
129 LicenseOp
130 dist : Distribution
131 licenses : seq String
132 license_file : String | undefined
133 -------------------------------------------------------
134 meta = undefined => status = skip
135 #licenses = 0 => status = fail /\ score = 0
136 exists vague(licenses) => status = fail /\ score = 0
137 license_file = undef => status = warn /\ score = 50
138 license_file /= undef => status = pass /\ score = 100
139  
140 =head3 SIDE EFFECTS
141  
142 None. Uses the already-parsed META object and filesystem stat calls only.
143  
144 =head3 USAGE EXAMPLE
145  
146 my $result = Test::CPAN::Health::Check::License->new->run($dist);
147 print $result->summary;
148  
149 =cut
150  
151 sub run {
152 my ($self, $dist, $context) = @_;
153  
154 croak 'dist must be a Test::CPAN::Health::Distribution'
155 unless ref($dist) && $dist->isa('Test::CPAN::Health::Distribution');
156  
157 my $meta = $dist->meta;
158  
159 unless ($meta) {
160 return $self->_skip('No META file found -- cannot determine licence');
161 }
162  
163 my @licenses = $meta->license;
164  
165 unless (@licenses) {
166 return $self->_result(
167 status => 'fail',
168 score => 0,
169 summary => 'No licence declared in META',
170 details => [
171 'Add a "license" field to META.json',
172 'Common CPAN values: perl_5, artistic_2, mit, gpl_2, lgpl_3_0',
173 ],
174 data => { name => $self->name },
175 );
176 }
177  
178 my @vague = grep { $VAGUE_LICENSE_SET{$_} } @licenses;
179 if (@vague) {
180 return $self->_result(
181 status => 'fail',
182 score => 0,
183 summary => sprintf(
184 'Licence declared as "%s" which is too vague for downstream consumers',
185 join(', ', @vague),
186 ),
187 details => [
188 'Replace with a specific SPDX-compatible identifier',
189 'Common CPAN values: perl_5, artistic_2, mit, gpl_2, lgpl_3_0',
190 ],
191 data => { name => $self->name, licenses => \@licenses },
192 );
193 }
194  
195 my $license_file;
196 for my $filename (@LICENSE_FILES) {
197 if (defined $dist->file_path($filename)) {
198 $license_file = $filename;
199 last;
200 }
201 }
202  
203 unless ($license_file) {
204 return $self->_result(
205 status => 'warn',
206 score => 50,
207 summary => sprintf(
208 'Licence "%s" declared in META but no licence file found in distribution root',
209 join(', ', @licenses),
210 ),
211 details => ['Add a LICENSE or LICENCE file to the distribution root'],
212 data => { name => $self->name, licenses => \@licenses },
213 );
214 }
215  
216 return $self->_result(
217 status => 'pass',
218 score => 100,
219 summary => sprintf(
220 'Licence "%s" declared in META and %s file present',
221 join(', ', @licenses), $license_file,
222 ),
223 data => {
224 name => $self->name,
225 licenses => \@licenses,
226 file => $license_file,
227 },
228 );
229 }
230  
231 =head1 AUTHOR
232  
233 Nigel Horne, C<< <njh at nigelhorne.com> >>
234  
235 =head1 LICENSE AND COPYRIGHT
236  
237 Copyright (C) 2025-2026 Nigel Horne.
238  
239 This program is free software; you can redistribute it and/or modify it
240 under the terms of the GNU General Public License as published by the Free
241 Software Foundation; either version 2 of the License, or (at your option)
242 any later version.
243  
244 =cut
245  
246 1;
« blib/lib/Test/CPAN/Health/Check/Kwalitee.pm ↑ Summary blib/lib/Test/CPAN/Health/Check/MetaJSON.pm »