File Coverage

blib/lib/Test/CPAN/Meta.pm
Criterion Covered Total %
statement 49 53 92.4
branch 9 10 90.0
condition 2 2 100.0
subroutine 10 10 100.0
pod 2 2 100.0
total 72 77 93.5


line stmt bran cond sub pod time code
1             package Test::CPAN::Meta;
2              
3 7     7   117889 use warnings;
  7         15  
  7         248  
4 7     7   39 use strict;
  7         13  
  7         255  
5              
6 7     7   37 use vars qw($VERSION);
  7         15  
  7         617  
7             $VERSION = '0.23';
8              
9             #----------------------------------------------------------------------------
10              
11             =head1 NAME
12              
13             Test::CPAN::Meta - Validate your CPAN META.yml files.
14              
15             =head1 SYNOPSIS
16              
17             There are two forms this module can be used.
18              
19             The first is a standalone test of your distribution's META.yml file:
20              
21             use Test::More;
22             eval "use Test::CPAN::Meta";
23             plan skip_all => "Test::CPAN::Meta required for testing META.yml" if $@;
24             meta_yaml_ok();
25              
26             Note that you may provide an optional label/comment/message/etc to the
27             function, or one will be created automatically.
28              
29             The second form allows you to test other META.yml files, or specify a specific
30             version you wish to test against:
31              
32             use Test::More test => 6;
33             use Test::CPAN::Meta;
34              
35             # specify a file and specification version
36             meta_spec_ok('META.yml','1.3',$msg);
37              
38             # specify the specification version to validate the local META.yml
39             meta_spec_ok(undef,'1.3',$msg);
40              
41             # specify a file, where the specification version is deduced
42             # from the file itself
43             meta_spec_ok('META.yml',undef,$msg);
44              
45             Note that this form requires you to specify the number of tests you will be
46             running in your test script. Also note that each 'meta_spec_ok' is actually 2
47             tests under the hood.
48              
49             =head1 DESCRIPTION
50              
51             This distribution was written to ensure that a META.yml file, provided with a
52             standard distribution uploaded to CPAN, meets the specifications that are
53             slowly being introduced to module uploads, via the use of package makers and
54             installers such as L, L and
55             L.
56              
57             See L for further details of the CPAN Meta Specification.
58              
59             =cut
60              
61             #----------------------------------------------------------------------------
62              
63             #############################################################################
64             #Library Modules #
65             #############################################################################
66              
67 7     7   6689 use Parse::CPAN::Meta;
  7         17715  
  7         365  
68 7     7   1499 use Test::Builder;
  7         13532  
  7         277  
69 7     7   4959 use Test::CPAN::Meta::Version;
  7         23  
  7         521  
70              
71             #----------------------------------------------------------------------------
72              
73             my $Test = Test::Builder->new();
74              
75             sub import {
76 7     7   161 my $self = shift;
77 7         20 my $caller = caller;
78 7     7   62 no strict 'refs';
  7         14  
  7         2724  
79 7         16 *{$caller.'::meta_yaml_ok'} = \&meta_yaml_ok;
  7         41  
80 7         14 *{$caller.'::meta_spec_ok'} = \&meta_spec_ok;
  7         28  
81              
82 7         558 $Test->exported_to($caller);
83 7         83 $Test->plan(@_);
84             }
85              
86             #############################################################################
87             #Interface Functions #
88             #############################################################################
89              
90             =head1 FUNCTIONS
91              
92             =over
93              
94             =item * meta_yaml_ok([$msg])
95              
96             Basic META.yml wrapper around meta_spec_ok.
97              
98             Returns a hash reference to the contents of the parsed META.yml
99              
100             =cut
101              
102             sub meta_yaml_ok {
103 1     1 1 18 $Test->plan( tests => 2 );
104 1         557 return meta_spec_ok(undef,undef,@_);
105             }
106              
107             =item * meta_spec_ok($file, $version [,$msg])
108              
109             Validates the named file against the given specification version. Both $file
110             and $version can be undefined.
111              
112             Returns a hash reference to the contents of the given file, after it has been
113             parsed.
114              
115             =back
116              
117             =cut
118              
119             sub meta_spec_ok {
120 10     10 1 6008 my ($file, $vers, $msg) = @_;
121 10   100     43 $file ||= 'META.yml';
122              
123 10 100       30 unless($msg) {
124 8         23 $msg = "$file meets the designated specification";
125 8 100       47 $msg .= " ($vers)" if($vers);
126             }
127              
128 10         20 my ($data) = eval { Parse::CPAN::Meta::LoadFile($file) };
  10         94  
129              
130 10 50       105315 if($@) {
131 0         0 $Test->ok(0,"$file contains valid YAML");
132 0         0 $Test->ok(0,$msg);
133 0         0 $Test->diag(" ERR: $@");
134 0         0 return;
135             } else {
136 10         76 $Test->ok(1,"$file contains valid YAML");
137             }
138              
139 10         4453 my %hash;
140 10 100       78 $hash{spec} = $vers if($vers);
141 10         25 $hash{data} = $data;
142              
143 10         111 my $spec = Test::CPAN::Meta::Version->new(%hash);
144 10 100       51 if(my $result = $spec->parse()) {
145 2         9 $Test->ok(0,$msg);
146 2         1161 $Test->diag(" ERR: $_") for($spec->errors);
147             } else {
148 8         31 $Test->ok(1,$msg);
149             }
150              
151 10         3104 return $data;
152             }
153              
154             q( "Before software can be reusable it first has to be usable." - Ralph Johnson );
155              
156             __END__