File Coverage

blib/lib/CPAN/Meta/Check.pm
Criterion Covered Total %
statement 45 45 100.0
branch 18 22 81.8
condition 2 2 100.0
subroutine 11 11 100.0
pod 3 3 100.0
total 79 83 95.1


line stmt bran cond sub pod time code
1             package CPAN::Meta::Check;
2             # vi:noet:sts=2:sw=2:ts=2
3             $CPAN::Meta::Check::VERSION = '0.015';
4 2     2   274918 use strict;
  2         15  
  2         63  
5 2     2   11 use warnings;
  2         13  
  2         80  
6              
7 2     2   11 use base 'Exporter';
  2         4  
  2         371  
8             our @EXPORT = qw//;
9             our @EXPORT_OK = qw/check_requirements requirements_for verify_dependencies/;
10             our %EXPORT_TAGS = (all => [ @EXPORT, @EXPORT_OK ] );
11              
12 2     2   25 use CPAN::Meta::Prereqs '2.132830';
  2         5  
  2         68  
13 2     2   11 use CPAN::Meta::Requirements 2.121;
  2         33  
  2         63  
14 2     2   1255 use Module::Metadata 1.000023;
  2         12780  
  2         839  
15              
16             sub _check_dep {
17 13     13   27 my ($reqs, $module, $dirs) = @_;
18              
19 13 50       38 $module eq 'perl' and return ($reqs->accepts_module($module, $]) ? () : sprintf "Your Perl (%s) is not in the range '%s'", $], $reqs->requirements_for_module($module));
    100          
20              
21 11         55 my $metadata = Module::Metadata->new_from_module($module, inc => $dirs);
22 11 100       37546 return "Module '$module' is not installed" if not defined $metadata;
23              
24 10         35 my $version = eval { $metadata->version };
  10         37  
25 10 100 100     225 return sprintf 'Installed version (%s) of %s is not in range \'%s\'',
    100          
26             (defined $version ? $version : 'undef'), $module, $reqs->requirements_for_module($module)
27             if not $reqs->accepts_module($module, $version || 0);
28 8         382 return;
29             }
30              
31             sub _check_conflict {
32 5     5   29 my ($reqs, $module, $dirs) = @_;
33 5         48 my $metadata = Module::Metadata->new_from_module($module, inc => $dirs);
34 5 50       40742 return if not defined $metadata;
35              
36 5         13 my $version = eval { $metadata->version };
  5         50  
37 5 100       76 return sprintf 'Installed version (%s) of %s is in range \'%s\'',
    100          
38             (defined $version ? $version : 'undef'), $module, $reqs->requirements_for_module($module)
39             if $reqs->accepts_module($module, $version);
40 2         130 return;
41             }
42              
43             sub requirements_for {
44 1     1 1 3 my ($meta, $phases, $type) = @_;
45 1 50       8 my $prereqs = ref($meta) eq 'CPAN::Meta' ? $meta->effective_prereqs : $meta;
46 1 50       1291 return $prereqs->merged_requirements(ref($phases) ? $phases : [ $phases ], [ $type ]);
47             }
48              
49             sub check_requirements {
50 8     8 1 14253 my ($reqs, $type, $dirs) = @_;
51              
52             return +{
53             map {
54 8 100       27 $_ => $type ne 'conflicts'
  18         451  
55             ? scalar _check_dep($reqs, $_, $dirs)
56             : scalar _check_conflict($reqs, $_, $dirs)
57             } $reqs->required_modules
58             };
59             }
60              
61             sub verify_dependencies {
62 1     1 1 3993 my ($meta, $phases, $type, $dirs) = @_;
63 1         4 my $reqs = requirements_for($meta, $phases, $type);
64 1         333 my $issues = check_requirements($reqs, $type, $dirs);
65 1         3 return grep { defined } values %{ $issues };
  4         17  
  1         5  
66             }
67              
68             1;
69              
70             #ABSTRACT: Verify requirements in a CPAN::Meta object
71              
72             __END__
73              
74             =pod
75              
76             =encoding UTF-8
77              
78             =head1 NAME
79              
80             CPAN::Meta::Check - Verify requirements in a CPAN::Meta object
81              
82             =head1 VERSION
83              
84             version 0.015
85              
86             =head1 SYNOPSIS
87              
88             warn "$_\n" for verify_dependencies($meta, [qw/runtime build test/], 'requires');
89              
90             =head1 DESCRIPTION
91              
92             This module verifies if requirements described in a CPAN::Meta object are present.
93              
94             =head1 FUNCTIONS
95              
96             =head2 check_requirements($reqs, $type, $incdirs)
97              
98             This function checks if all dependencies in C<$reqs> (a L<CPAN::Meta::Requirements|CPAN::Meta::Requirements> object) are met, taking into account that 'conflicts' dependencies have to be checked in reverse. It returns a hash with the modules as keys and any problems as values; the value for a successfully found module will be undef. Modules are searched for in C<@$incdirs>, defaulting to C<@INC>.
99              
100             =head2 verify_dependencies($meta, $phases, $types, $incdirs)
101              
102             Check all requirements in C<$meta> for phases C<$phases> and type C<$type>. Modules are searched for in C<@$incdirs>, defaulting to C<@INC>. C<$meta> should be a L<CPAN::Meta::Prereqs> or L<CPAN::Meta> object.
103              
104             =head2 requirements_for($meta, $phases, $types)
105              
106             B<< This function is deprecated and may be removed at some point in the future, please use CPAN::Meta::Prereqs->merged_requirements instead. >>
107              
108             This function returns a unified L<CPAN::Meta::Requirements|CPAN::Meta::Requirements> object for all C<$type> requirements for C<$phases>. C<$phases> may be either one (scalar) value or an arrayref of valid values as defined by the L<CPAN::Meta spec|CPAN::Meta::Spec>. C<$type> must be a relationship as defined by the same spec. C<$meta> should be a L<CPAN::Meta::Prereqs> or L<CPAN::Meta> object.
109              
110             =head1 SEE ALSO
111              
112             =over 4
113              
114             =item * L<Test::CheckDeps|Test::CheckDeps>
115              
116             =item * L<CPAN::Meta|CPAN::Meta>
117              
118             =back
119              
120             =head1 AUTHOR
121              
122             Leon Timmermans <leont@cpan.org>
123              
124             =head1 COPYRIGHT AND LICENSE
125              
126             This software is copyright (c) 2012 by Leon Timmermans.
127              
128             This is free software; you can redistribute it and/or modify it under
129             the same terms as the Perl 5 programming language system itself.
130              
131             =cut