| 1 | package Test::CPAN::Health::Check::MetaJSON; | ||
| 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 | Readonly::Scalar my $SCORE_JSON_COMPLETE => 100; | ||
| 16 | Readonly::Scalar my $SCORE_YML_COMPLETE => 70; | ||
| 17 | Readonly::Scalar my $SCORE_MYMETA_COMPLETE => 50; | ||
| 18 | Readonly::Scalar my $SCORE_JSON_INCOMPLETE => 40; | ||
| 19 | Readonly::Scalar my $SCORE_YML_INCOMPLETE => 25; | ||
| 20 | Readonly::Scalar my $SCORE_MYMETA_INCOMPLETE => 15; | ||
| 21 | |||
| 22 | =head1 NAME | ||
| 23 | |||
| 24 | Test::CPAN::Health::Check::MetaJSON - Check that META.json is present, valid, and complete | ||
| 25 | |||
| 26 | =head1 SYNOPSIS | ||
| 27 | |||
| 28 | use Test::CPAN::Health::Check::MetaJSON; | ||
| 29 | |||
| 30 | my $check = Test::CPAN::Health::Check::MetaJSON->new; | ||
| 31 | my $result = $check->run($dist); | ||
| 32 | |||
| 33 | printf "%s: %s\n", $result->status, $result->summary; | ||
| 34 | |||
| 35 | =head1 DESCRIPTION | ||
| 36 | |||
| 37 | Verifies that the distribution ships a well-formed META.json (preferred) or | ||
| 38 | META.yml (fallback) and that the required CPAN::Meta v2 fields -- name, | ||
| 39 | version, abstract, author, license -- are present and non-empty. | ||
| 40 | |||
| 41 | C<Distribution-E<gt>meta> searches in order: META.json, META.yml, | ||
| 42 | MYMETA.json, MYMETA.yml. MYMETA files are generated by C<perl Makefile.PL> | ||
| 43 | and serve as a fallback for local checkouts. Canonical META files are | ||
| 44 | produced by C<make distdir> (MakeMaker) or C<dzil build> (Dist::Zilla) and | ||
| 45 | must be committed to the repository separately. | ||
| 46 | |||
| 47 | Score matrix: | ||
| 48 | |||
| 49 | =over 4 | ||
| 50 | |||
| 51 | =item * 100 -- META.json present; all required fields populated. | ||
| 52 | |||
| 53 | =item * 70 -- Only META.yml present; all required fields populated. | ||
| 54 | |||
| 55 | =item * 50 -- Only MYMETA.json or MYMETA.yml present; all required fields populated. | ||
| 56 | |||
| 57 | =item * 40 -- META.json present; one or more required fields missing/vague. | ||
| 58 | |||
| 59 | =item * 25 -- Only META.yml present; one or more required fields missing/vague. | ||
| 60 | |||
| 61 | =item * 15 -- Only MYMETA present; one or more required fields missing/vague. | ||
| 62 | |||
| 63 | =item * 0 -- No META or MYMETA file found. | ||
| 64 | |||
| 65 | =back | ||
| 66 | |||
| 67 | =head1 LIMITATIONS | ||
| 68 | |||
| 69 | =over 4 | ||
| 70 | |||
| 71 | =item * The check reads whichever META file C<Distribution-E<gt>meta> resolves. | ||
| 72 | It does not independently re-parse the file. | ||
| 73 | |||
| 74 | =item * C<abstract> is considered missing when it is the literal string | ||
| 75 | C<'unknown'>, which Dist::Zilla and MakeMaker sometimes use as a placeholder. | ||
| 76 | |||
| 77 | =back | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | sub id { return 'meta_json' } | ||
| 82 | sub name { return 'META.json' } | ||
| 83 | sub description { return 'Checks that META.json is present, valid, and complete' } | ||
| 84 | sub weight { return 5 } | ||
| 85 | sub category { return 'packaging' } | ||
| 86 | |||
| 87 | =head2 run | ||
| 88 | |||
| 89 | =head3 PURPOSE | ||
| 90 | |||
| 91 | Determine whether a META.json (or META.yml) exists and contains the minimum | ||
| 92 | required metadata fields. | ||
| 93 | |||
| 94 | =head3 API SPECIFICATION | ||
| 95 | |||
| 96 | =head4 INPUT | ||
| 97 | |||
| 98 | dist Test::CPAN::Health::Distribution required | ||
| 99 | context Hashref optional prior check results | ||
| 100 | |||
| 101 | =head4 OUTPUT | ||
| 102 | |||
| 103 | L<Test::CPAN::Health::Result> with check_id C<'meta_json'>. | ||
| 104 | |||
| 105 | =head3 MESSAGES | ||
| 106 | |||
| 107 | Code | Severity | Message | Resolution | ||
| 108 | ------+----------+-------------------------------------------+----------- | ||
| 109 | MJ001 | FAIL | No META.json or META.yml found | Generate META files | ||
| 110 | MJ002 | WARN | META missing required fields: {list} | Add missing fields | ||
| 111 | MJ003 | WARN | META.yml present but META.json is missing | Generate META.json | ||
| 112 | MJ004 | PASS | META.json is present and complete | | ||
| 113 | |||
| 114 | =head3 FORMAL SPECIFICATION | ||
| 115 | |||
| 116 | -- Z schema (placeholder) -- | ||
| 117 | MetaJSONOp | ||
| 118 | dist : Distribution | ||
| 119 | has_json : Boolean | ||
| 120 | meta : CPAN::Meta | undefined | ||
| 121 | missing : seq String | ||
| 122 | ------------------------------------------------------- | ||
| 123 | meta = undefined => status = fail /\ score = 0 | ||
| 124 | #missing > 0 /\ has_json => status = warn /\ score = 50 | ||
| 125 | #missing > 0 /\ ~has_json => status = warn /\ score = 30 | ||
| 126 | #missing = 0 /\ ~has_json => status = warn /\ score = 70 | ||
| 127 | #missing = 0 /\ has_json => status = pass /\ score = 100 | ||
| 128 | |||
| 129 | =head3 SIDE EFFECTS | ||
| 130 | |||
| 131 | None. Uses the already-parsed META object from C<$dist->meta>. | ||
| 132 | |||
| 133 | =head3 USAGE EXAMPLE | ||
| 134 | |||
| 135 | my $result = Test::CPAN::Health::Check::MetaJSON->new->run($dist); | ||
| 136 | print $result->summary; | ||
| 137 | |||
| 138 | =cut | ||
| 139 | |||
| 140 | sub run { | ||
| 141 | my ($self, $dist, $context) = @_; | ||
| 142 | |||
| 143 | croak 'dist must be a Test::CPAN::Health::Distribution' | ||
| 144 | unless ref($dist) && $dist->isa('Test::CPAN::Health::Distribution'); | ||
| 145 | |||
| 146 | my $flags = _meta_presence_flags($dist); | ||
| 147 | my $meta = $dist->meta; | ||
| 148 | |||
| 149 | unless ($meta) { | ||
| 150 | return $self->_result( | ||
| 151 | status => 'fail', | ||
| 152 | score => 0, | ||
| 153 | summary => 'No META.json, META.yml, MYMETA.json, or MYMETA.yml found', | ||
| 154 | details => [ | ||
| 155 | 'MakeMaker: run "perl Makefile.PL && make distdir" then copy META.json and META.yml from the generated dist directory', | ||
| 156 | 'Dist::Zilla: run "dzil build" which writes META.json to the working directory automatically', | ||
| 157 | ], | ||
| 158 | data => { name => $self->name }, | ||
| 159 | ); | ||
| 160 | } | ||
| 161 | |||
| 162 | my @missing = _missing_meta_fields($meta); | ||
| 163 | |||
| 164 | if (@missing) { | ||
| 165 | return $self->_result( | ||
| 166 | status => 'warn', | ||
| 167 | score => _incomplete_meta_score($flags), | ||
| 168 | summary => sprintf('META file is missing required fields: %s', join(', ', @missing)), | ||
| 169 | details => [ map { "Add '$_' to META.json" } @missing ], | ||
| 170 | data => { | ||
| 171 | name => $self->name, | ||
| 172 | has_json => $flags->{json} ? 1 : 0, | ||
| 173 | missing => \@missing, | ||
| 174 | }, | ||
| 175 | ); | ||
| 176 | } | ||
| 177 | |||
| 178 | if (!$flags->{meta} && $flags->{mymeta}) { | ||
| 179 | my $which = $flags->{myjson} ? 'MYMETA.json' : 'MYMETA.yml'; | ||
| 180 | return $self->_result( | ||
| 181 | status => 'warn', | ||
| 182 | score => $SCORE_MYMETA_COMPLETE, | ||
| 183 | summary => "$which found (generated); ship META.json for CPAN compatibility", | ||
| 184 | details => [ | ||
| 185 | 'MakeMaker: run "perl Makefile.PL && make distdir" then copy META.json from the generated dist directory', | ||
| 186 | 'Dist::Zilla: run "dzil build" which writes META.json to the working directory automatically', | ||
| 187 | ], | ||
| 188 | data => { name => $self->name, has_json => 0 }, | ||
| 189 | ); | ||
| 190 | } | ||
| 191 | |||
| 192 | unless ($flags->{json}) { | ||
| 193 | return $self->_result( | ||
| 194 | status => 'warn', | ||
| 195 | score => $SCORE_YML_COMPLETE, | ||
| 196 | summary => 'META.yml found but META.json is missing (JSON preferred for tooling compatibility)', | ||
| 197 | details => ['Generate META.json alongside META.yml'], | ||
| 198 | data => { name => $self->name, has_json => 0 }, | ||
| 199 | ); | ||
| 200 | } | ||
| 201 | |||
| 202 | return $self->_result( | ||
| 203 | status => 'pass', | ||
| 204 | score => $SCORE_JSON_COMPLETE, | ||
| 205 | summary => 'META.json is present and contains all required fields', | ||
| 206 | data => { name => $self->name, has_json => 1 }, | ||
| 207 | ); | ||
| 208 | } | ||
| 209 | |||
| 210 | # --------------------------------------------------------------------------- | ||
| 211 | # Private helpers | ||
| 212 | # --------------------------------------------------------------------------- | ||
| 213 | |||
| 214 | ## no critic (ProhibitUnusedPrivateSubroutines) | ||
| 215 | sub _meta_presence_flags { | ||
| 216 | my ($dist) = @_; | ||
| 217 | my $has_json = defined $dist->file_path('META.json'); | ||
| 218 | my $has_yml = defined $dist->file_path('META.yml'); | ||
| 219 | my $has_myjson = defined $dist->file_path('MYMETA.json'); | ||
| 220 | my $has_myyml = defined $dist->file_path('MYMETA.yml'); | ||
| 221 | return { | ||
| 222 | json => $has_json, | ||
| 223 | yml => $has_yml, | ||
| 224 | myjson => $has_myjson, | ||
| 225 | myyml => $has_myyml, | ||
| 226 | meta => $has_json || $has_yml, | ||
| 227 | mymeta => $has_myjson || $has_myyml, | ||
| 228 | }; | ||
| 229 | } | ||
| 230 | |||
| 231 | ## no critic (ProhibitUnusedPrivateSubroutines) | ||
| 232 | sub _missing_meta_fields { | ||
| 233 | my ($meta) = @_; | ||
| 234 | my @authors = $meta->author; | ||
| 235 | my @licenses = $meta->license; | ||
| 236 | my @missing; | ||
| 237 | push @missing, 'name' | ||
| 238 | unless defined $meta->name && length $meta->name; | ||
| 239 | push @missing, 'version' | ||
| 240 | unless defined $meta->version && length $meta->version; | ||
| 241 | push @missing, 'abstract' | ||
| 242 | if !defined $meta->abstract | ||
| 243 | || !length $meta->abstract | ||
| 244 | || $meta->abstract eq 'unknown'; | ||
| 245 | push @missing, 'author' unless @authors; | ||
| 246 | push @missing, 'license' unless @licenses; | ||
| 247 | return @missing; | ||
| 248 | } | ||
| 249 | |||
| 250 | ## no critic (ProhibitUnusedPrivateSubroutines) | ||
| 251 | sub _incomplete_meta_score { | ||
| 252 | my ($flags) = @_; | ||
| 253 | return $flags->{json} ? $SCORE_JSON_INCOMPLETE | ||
| 254 | : $flags->{yml} ? $SCORE_YML_INCOMPLETE | ||
| 255 | : $SCORE_MYMETA_INCOMPLETE; | ||
| 256 | } | ||
| 257 | |||
| 258 | =head1 AUTHOR | ||
| 259 | |||
| 260 | Nigel Horne, C<< <njh at nigelhorne.com> >> | ||
| 261 | |||
| 262 | =head1 LICENSE AND COPYRIGHT | ||
| 263 | |||
| 264 | Copyright (C) 2025-2026 Nigel Horne. | ||
| 265 | |||
| 266 | This program is free software; you can redistribute it and/or modify it | ||
| 267 | under the terms of the GNU General Public License as published by the Free | ||
| 268 | Software Foundation; either version 2 of the License, or (at your option) | ||
| 269 | any later version. | ||
| 270 | |||
| 271 | =cut | ||
| 272 | |||
| 273 | 1; | ||