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 8     8   75578 use warnings;
  8         10  
  8         225  
4 8     8   29 use strict;
  8         10  
  8         204  
5              
6 8     8   29 use vars qw($VERSION);
  8         10  
  8         462  
7             $VERSION = '0.25';
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 tests => 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 8     8   3153 use Parse::CPAN::Meta;
  8         6330  
  8         260  
68 8     8   556 use Test::Builder;
  8         7444  
  8         118  
69 8     8   2761 use Test::CPAN::Meta::Version;
  8         15  
  8         430  
70              
71             #----------------------------------------------------------------------------
72              
73             my $Test = Test::Builder->new();
74              
75             sub import {
76 8     8   81 my $self = shift;
77 8         10 my $caller = caller;
78 8     8   37 no strict 'refs';
  8         9  
  8         1875  
79 8         14 *{$caller.'::meta_yaml_ok'} = \&meta_yaml_ok;
  8         30  
80 8         293 *{$caller.'::meta_spec_ok'} = \&meta_spec_ok;
  8         21  
81              
82 8         27 $Test->exported_to($caller);
83 8         97 $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 16 $Test->plan( tests => 2 );
104 1         91 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 3188 my ($file, $vers, $msg) = @_;
121 10   100     23 $file ||= 'META.yml';
122              
123 10 100       21 unless($msg) {
124 8         9 $msg = "$file meets the designated specification";
125 8 100       31 $msg .= " ($vers)" if($vers);
126             }
127              
128 10         10 my ($data) = eval { Parse::CPAN::Meta::LoadFile($file) };
  10         61  
129              
130 10 50       55152 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         44 $Test->ok(1,"$file contains valid YAML");
137             }
138              
139 10         2547 my %hash;
140 10 100       29 $hash{spec} = $vers if($vers);
141 10         16 $hash{data} = $data;
142              
143 10         58 my $spec = Test::CPAN::Meta::Version->new(%hash);
144 10 100       25 if(my $result = $spec->parse()) {
145 2         5 $Test->ok(0,$msg);
146 2         668 $Test->diag(" ERR: $_") for($spec->errors);
147             } else {
148 8         20 $Test->ok(1,$msg);
149             }
150              
151 10         1653 return $data;
152             }
153              
154             q( "Before software can be reusable it first has to be usable." - Ralph Johnson );
155              
156             __END__