File Coverage

blib/lib/Module/CPANTS/Kwalitee/MetaYML.pm
Criterion Covered Total %
statement 84 144 58.3
branch 28 84 33.3
condition 12 31 38.7
subroutine 20 30 66.6
pod 3 3 100.0
total 147 292 50.3


line stmt bran cond sub pod time code
1             package Module::CPANTS::Kwalitee::MetaYML;
2 7     7   3323 use warnings;
  7         17  
  7         198  
3 7     7   36 use strict;
  7         20  
  7         236  
4 7     7   37 use File::Spec::Functions qw(catfile);
  7         18  
  7         312  
5 7     7   1389 use CPAN::Meta::YAML;
  7         13953  
  7         445  
6 7     7   3233 use CPAN::Meta::Validator;
  7         31416  
  7         268  
7 7     7   3721 use CPAN::Meta::Converter;
  7         100626  
  7         427  
8 7     7   57 use List::Util qw/first/;
  7         16  
  7         12169  
9              
10             our $VERSION = '1.01';
11             $VERSION =~ s/_//; ## no critic
12              
13 28     28 1 67 sub order { 10 }
14              
15             my $JSON_DECODER = _load_json_decoder() || do { require JSON::PP; JSON::PP->can('decode_json') };
16              
17             ##################################################################
18             # Analyse
19             ##################################################################
20              
21             sub analyse {
22 11     11 1 42 my $class = shift;
23 11         24 my $me = shift;
24 11         223 my $distdir = $me->distdir;
25 11         121 my $meta_yml = catfile($distdir, 'META.yml');
26 11         55 my $meta_json = catfile($distdir, 'META.json');
27 11         80 my $mymeta_yml = catfile($distdir, 'MYMETA.yml');
28              
29             # META.yml is not always the most preferred meta file,
30             # but test it anyway because it may be broken sometimes.
31 11 100 66     288 if (-f $meta_yml && -r _) {
32 3         34 _analyse_yml($me, $meta_yml);
33             }
34              
35             # check also META.json (if exists).
36 11 50 33     233 if (-f $meta_json && -r _) {
37 0         0 _analyse_json($me, $meta_json);
38             }
39              
40             # If, and only if META.yml and META.json don't exist,
41             # try MYMETA.yml
42 11 50 66     287 if (!$me->d->{meta_yml} && -f $mymeta_yml && -r _) {
      33        
43 0         0 _analyse_yml($me, $mymeta_yml);
44             }
45              
46 11 100       427 if (!$me->d->{meta_yml}) {
47 8         63 return;
48             }
49              
50             # Theoretically it might be better to convert 1.* to 2.0.
51             # However, converting 2.0 to 1.4 is much cheaper for CPANTS
52             # website as it's much rarer as of this writing.
53 3 50 50     64 if (($me->d->{meta_yml_spec_version} || '1.0') gt '1.4') {
54 0         0 my $cmc = CPAN::Meta::Converter->new($me->d->{meta_yml});
55 0         0 my $meta_14 = eval { $cmc->convert(version => '1.4') };
  0         0  
56 0 0 0     0 if (!$@ && $meta_14) {
57 0         0 $me->d->{meta_yml} = $meta_14;
58             }
59             }
60              
61 3 50 33     71 $me->d->{dynamic_config} = (!exists $me->d->{meta_yml}{dynamic_config} or $me->d->{meta_yml}{dynamic_config}) ? 1 : 0;
62             }
63              
64             sub _analyse_yml {
65 3     3   11 my ($me, $file) = @_;
66 3         7 my @warnings;
67 3         8 eval {
68             # CPAN::Meta::YAML warns if it finds a duplicate key
69 3     0   78 local $SIG{__WARN__} = sub { push @warnings, @_ };
  0         0  
70 3 50       38 my $meta = CPAN::Meta::YAML->read($file) or die CPAN::Meta::YAML->errstr;
71             # Broken META.yml may return a "YAML 1.0" string first.
72             # eg. M/MH/MHASCH/Date-Gregorian-0.07.tar.gz
73 3 50 33     4488 if (@$meta > 1 or ref $meta->[0] ne ref {}) {
74 0     0   0 $me->d->{meta_yml} = first { ref $_ eq ref {} } @$meta;
  0         0  
75 0         0 $me->d->{error}{meta_yml_is_parsable} = "multiple parts found in META.yml";
76             } else {
77 3         87 $me->d->{meta_yml} = $meta->[0];
78 3         67 $me->d->{meta_yml_is_parsable} = 1;
79             }
80             };
81 3 50       57 if (my $error = $@) {
82 0         0 $error =~ s/ at \S+ line \d+.+$//s;
83 0         0 $me->d->{error}{meta_yml_is_parsable} = $error;
84             }
85 3 50       88 if ($me->d->{meta_yml}) {
86 3         78 my ($spec, $error) = _validate_meta($me->d->{meta_yml});
87 3 50       11 $me->d->{error}{meta_yml_conforms_to_known_spec} = $error if $error;
88 3         74 $me->d->{meta_yml_spec_version} = $spec->{spec};
89             }
90 3 50       52 if (@warnings) {
91 0         0 $me->d->{error}{meta_yml_has_duplicate_keys} = join ',', @warnings;
92             }
93             }
94              
95             sub _analyse_json {
96 0     0   0 my ($me, $file) = @_;
97              
98 0         0 my $meta;
99 0         0 eval {
100 0 0       0 my $json = do { open my $fh, '<', $file or die "$file: $!"; local $/; <$fh> };
  0         0  
  0         0  
  0         0  
101 0         0 $meta = $JSON_DECODER->($json);
102 0         0 $me->d->{meta_json_is_parsable} = 1;
103             };
104 0 0       0 if (my $error = $@) {
105 0         0 $error =~ s/ at \S+ line \d+.+$//s;
106 0         0 $me->d->{error}{meta_json_is_parsable} = $error;
107             }
108 0 0       0 if ($meta) {
109 0         0 my ($spec, $error) = _validate_meta($meta);
110 0 0       0 $me->d->{error}{meta_json_conforms_to_known_spec} = $error if $error;
111 0         0 $me->d->{meta_json_spec_version} = $spec->{spec};
112             }
113 0 0       0 if (!$me->d->{meta_yml}) {
114 0         0 $me->d->{meta_yml} = $meta;
115 0         0 $me->d->{meta_yml_spec_version} = $me->d->{meta_json_spec_version};
116 0         0 $me->d->{meta_yml_is_meta_json} = 1;
117             }
118             }
119              
120             sub _load_json_decoder {
121 7   50 7   73 my $json_class = $ENV{CPAN_META_JSON_BACKEND} || $ENV{PERL_JSON_BACKEND} || 'JSON::PP';
122 7 50       381 eval "require $json_class; 1" or return;
123 7         91 $json_class->can('decode_json');
124             }
125              
126             sub _validate_meta {
127 3     3   32 my $meta = shift;
128 3         7 my $error;
129 3         6 my $spec = eval { CPAN::Meta::Validator->new($meta) };
  3         44  
130 3 50       94 if ($error = $@) {
    50          
131 0         0 $error =~ s/ at \S+ line \d+.+$//s;
132             } elsif (!$spec->is_valid) {
133 0         0 $error = join ';', sort $spec->errors;
134             }
135 3         1034 $error =~ s/(SCALAR|ARRAY|HASH|GLOB|REF)\(0x[0-9a-f]+\)/$1(...)/g;
136 3         23 return ($spec, $error);
137             }
138              
139             ##################################################################
140             # Kwalitee Indicators
141             ##################################################################
142              
143             sub kwalitee_indicators{
144             return [
145             {
146             name => 'meta_yml_is_parsable',
147             error => q{The META.yml file of this distribution could not be parsed by the version of CPAN::Meta::YAML.pm CPANTS is using.},
148             remedy => q{Upgrade your YAML generator so it produces valid YAML.},
149             code => sub {
150 11     11   75 my $d = shift;
151 11 50       48 !$d->{error}{meta_yml_is_parsable} ? 1 : 0
152             },
153             details => sub {
154 0     0   0 my $d = shift;
155 0         0 $d->{error}{meta_yml_is_parsable};
156             },
157             },
158             {
159             name => 'meta_json_is_parsable',
160             error => q{The META.json file of this distribution could not be parsed by the version of JSON parser CPANTS is using.},
161             remedy => q{Upgrade your META.json generator so it produces valid JSON.},
162             code => sub {
163 11     11   82 my $d = shift;
164 11 50       45 !$d->{error}{meta_json_is_parsable} ? 1 : 0
165             },
166             details => sub {
167 0     0   0 my $d = shift;
168 0         0 $d->{error}{meta_json_is_parsable};
169             },
170             },
171             {
172             name => 'meta_yml_has_provides',
173             is_experimental => 1,
174             error => q{This distribution does not have a list of provided modules defined in META.yml.},
175             remedy => q{Add all modules contained in this distribution to the META.yml field 'provides'. Module::Build or Dist::Zilla::Plugin::MetaProvides do this automatically for you.},
176             code => sub {
177 11     11   60 my $d = shift;
178 11 50       33 return 1 if !$d->{meta_yml};
179 11 50       38 return 1 if $d->{meta_yml}{provides};
180 11         22 return 0;
181             },
182             details => sub {
183 0     0   0 my $d = shift;
184 0 0       0 return "No META.yml." unless $d->{meta_yml};
185 0         0 return q{No "provides" was found in META.yml.};
186             },
187             },
188             {
189             name => 'meta_yml_conforms_to_known_spec',
190             error => q{META.yml does not conform to any recognised META.yml Spec.},
191             remedy => q{Take a look at the META.yml Spec at https://metacpan.org/pod/CPAN::Meta::History::Meta_1_4 (for version 1.4) or https://metacpan.org/pod/CPAN::Meta::Spec (for version 2), and change your META.yml accordingly.},
192             code => sub {
193 11     11   82 my $d = shift;
194 11 50       39 return 0 if $d->{error}{meta_yml_conforms_to_known_spec};
195 11         30 return 1;
196             },
197             details => sub {
198 0     0   0 my $d = shift;
199 0 0       0 return "No META.yml." unless $d->{meta_yml};
200 0 0       0 return "META.yml is broken." unless $d->{meta_yml_is_parsable};
201 0         0 return $d->{error}{meta_yml_conforms_to_known_spec};
202             },
203             },
204             {
205             name => 'meta_json_conforms_to_known_spec',
206             error => q{META.json does not conform to any recognised META Spec.},
207             remedy => q{Take a look at the META.json Spec at https://metacpan.org/pod/CPAN::Meta::History::Meta_1_4 (for version 1.4) or https://metacpan.org/pod/CPAN::Meta::Spec (for version 2), and change your META.json accordingly.},
208             code => sub {
209 11     11   84 my $d = shift;
210 11 50       52 return 0 if $d->{error}{meta_json_is_parsable};
211 11 50       35 return 0 if $d->{error}{meta_json_conforms_to_known_spec};
212 11         29 return 1;
213             },
214             details => sub {
215 0     0   0 my $d = shift;
216 0 0       0 return "META.json is broken." unless $d->{meta_json_is_parsable};
217 0         0 return $d->{error}{meta_json_conforms_to_known_spec};
218             },
219             },
220             {
221             name => 'meta_yml_declares_perl_version',
222             error => q{This distribution does not declare the minimum perl version in META.yml.},
223             is_extra => 1,
224             remedy => q{If you are using Build.PL define the {requires}{perl} = VERSION field. If you are using MakeMaker (Makefile.PL) you should upgrade ExtUtils::MakeMaker to 6.48 and use MIN_PERL_VERSION parameter. Perl::MinimumVersion can help you determine which version of Perl your module needs.},
225             code => sub {
226 11     11   77 my $d = shift;
227 11         24 my $yaml = $d->{meta_yml};
228 11 50       31 return 1 unless $yaml;
229 11 50 33     81 return ref $yaml->{requires} eq ref {} && $yaml->{requires}{perl} ? 1 : 0;
230             },
231             details => sub {
232 0     0   0 my $d = shift;
233 0         0 my $yaml = $d->{meta_yml};
234 0 0       0 return "No META.yml." unless $yaml;
235 0 0       0 return q{No "requires" was found in META.yml.} unless ref $yaml->{requires} eq ref {};
236 0 0       0 return q{No "perl" subkey was found in META.yml.} unless $yaml->{requires}{perl};
237             },
238             },
239             {
240             name => 'meta_yml_has_repository_resource',
241             is_experimental => 1,
242             error => q{This distribution does not have a link to a repository in META.yml.},
243             remedy => q{Add a 'repository' resource to the META.yml via 'meta_add' accessor (for Module::Build) or META_ADD parameter (for ExtUtils::MakeMaker).},
244             code => sub {
245 11     11   61 my $d = shift;
246 11         22 my $yaml = $d->{meta_yml};
247 11 50       31 return 1 unless $yaml;
248 11 50 33     59 return ref $yaml->{resources} eq ref {} && $yaml->{resources}{repository} ? 1 : 0;
249             },
250             details => sub {
251 0     0   0 my $d = shift;
252 0         0 my $yaml = $d->{meta_yml};
253 0 0       0 return "No META.yml." unless $yaml;
254 0 0       0 return q{No "resources" was found in META.yml.} unless ref $yaml->{resources} eq ref {};
255 0 0       0 return q{No "repository" subkey was found in META.yml.} unless $yaml->{resources}{repository};
256             },
257             },
258 8     8 1 486 ];
259             }
260              
261             q{Barbies Favourite record of the moment:
262             Nine Inch Nails: Year Zero};
263              
264             __END__