File Coverage

lib/Test/CPANfile.pm
Criterion Covered Total %
statement 55 56 98.2
branch 12 20 60.0
condition 10 21 47.6
subroutine 8 8 100.0
pod 1 1 100.0
total 86 106 81.1


line stmt bran cond sub pod time code
1             package Test::CPANfile;
2              
3 2     2   82833 use strict;
  2         11  
  2         59  
4 2     2   9 use warnings;
  2         4  
  2         66  
5 2     2   10 use Exporter 5.57 'import';
  2         48  
  2         74  
6 2     2   1013 use Module::CPANfile;
  2         33381  
  2         74  
7 2     2   1140 use Perl::PrereqScanner::NotQuiteLite::App;
  2         65941  
  2         87  
8 2     2   654 use Test::More;
  2         64827  
  2         20  
9              
10             our $VERSION = '0.06';
11             our @EXPORT = qw/cpanfile_has_all_used_modules/;
12              
13             my %Phases = (
14             runtime => [qw/runtime/],
15             build => [qw/runtime build/],
16             test => [qw/runtime build test/],
17             configure => [qw/configure/],
18             );
19             my %Types = (
20             requires => [qw/requires/],
21             recommends => [qw/requires recommends/],
22             suggests => [qw/requires recommends suggests/],
23             );
24              
25             sub cpanfile_has_all_used_modules {
26 1     1 1 303 my %args = @_;
27              
28 1   50     9 my $cpanfile = delete $args{cpanfile} || 'cpanfile';
29 1 50       55 plan skip_all => "$cpanfile is not found" if !-f $cpanfile;
30              
31 1         9 my $declared = _load_cpanfile($cpanfile);
32              
33 1         14 my $scanner = Perl::PrereqScanner::NotQuiteLite::App->new(
34             parsers => [':installed'],
35             exclude_core => 1,
36             %args,
37             print => 0,
38             );
39              
40 1         85 my $prereqs = $scanner->run;
41              
42 1         270045 my $index = $args{index};
43 1         3 my %uri_cache;
44 1         3 for my $phase (sort $prereqs->phases) {
45 3         55 for my $type (sort $prereqs->types_in($phase)) {
46 9         1386 my $req = $prereqs->requirements_for($phase, $type);
47 9         402 my $declared_req = $declared->merged_requirements($Phases{$phase}, $Types{$type});
48              
49 9         4738 my %uri_map;
50 9 50       34 if ($index) {
51 9         27 for my $module ($declared_req->required_modules) {
52 36 50       129 next if $module eq 'perl';
53 36   66     101 my $uri = $uri_cache{$module} ||= do {
54 8         113 my $res = $index->search_packages({ package => $module });
55 8 50       578393 $res ? $res->{uri} : undef;
56             };
57 36 50       160 $uri_map{$uri} = $module if $uri;
58             }
59             }
60              
61 9         47 for my $module (sort $req->required_modules) {
62 7         1883 my $required_version = $req->requirements_for_module($module);
63 7         401 my $declared_version = $declared_req->requirements_for_module($module);
64 7 100 66     245 if ($required_version and $required_version =~ /^[0-9._]+$/) {
    100          
65 2   33     19 ok((defined $declared_version and $req->accepts_module($module, $required_version)),
      50        
      50        
66             sprintf "$module %s ($phase $type) is declared and satisfies minimum version (%s)",
67             $required_version || 0, $declared_version || 0
68             );
69             } elsif (defined $declared_version) {
70 3         26 pass "$module ($phase $type) is declared";
71             } else {
72 2         7 my $uri;
73 2 50       10 if ($index) {
74 2   33     13 $uri = $uri_cache{$module} ||= do {
75 2         14 my $res = $index->search_packages({ package => $module });
76 2 50       39468 $res ? $res->{uri} : undef;
77             };
78             }
79 2 50 33     46 ok($uri && $uri_map{$uri}, "$module ($phase $type) is declared" . ($uri_map{$uri} ? " (as $uri_map{$uri})" : ""));
80             }
81             }
82             }
83             }
84             }
85              
86             sub _load_cpanfile {
87 1     1   3 my $file = shift;
88 1         10 my $data = Module::CPANfile->load($file);
89 1         70 my $prereqs = $data->prereqs;
90 1         1348 for my $feature ($data->features) {
91 0         0 $prereqs = $prereqs->with_merged_prereqs($feature->prereqs);
92             }
93 1         20 $prereqs;
94             }
95              
96             1;
97              
98             __END__