blib/lib/Dist/Zilla/Plugin/SyncCPANfile.pm

filter: Statementstmt 91.3% 74 / 81 Branchbran 79.1% 19 / 24 Conditioncond 63.6% 14 / 22 MC/DCmcdc 30.7% 4 / 13 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 2 totaltotal 79.8% 123 / 154 CC 26 SCAR 26.2
CC
26
Cov
84%
_get_cpanfile
22.7
_audit
6.6
__ANON__
2.1
CRAP 13.7
↑ Summary
1 package Dist::Zilla::Plugin::SyncCPANfile;
2  
3 # ABSTRACT: Sync a cpanfile with the prereqs listed in dist.ini
4  
5 #use v5.10;
6  
7 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 6 our $VERSION = '0.04'; # VERSION
11  
12 6 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use CPAN::Audit;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 6 with qw(
20 Dist::Zilla::Role::AfterBuild
21 );
22  
23 6 has cpan_audit => (
24 is => 'ro',
25 isa => 'Bool',
26 default => 0,
27 );
28  
29 6 has filename => (
30 is => 'ro',
31 isa => 'Str',
32 default => 'cpanfile',
33 );
34
35 has comment => (
36 is => 'ro',
37 isa => 'ArrayRef[Str]',
38 15 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
39 [
40 15 ▶ ( sprintf 'This file is generated by %s v%s', __PACKAGE__, __PACKAGE__->VERSION // '<internal>' ),
Condition50%1/2
__PACKAGE__->VERSION // "<internal>"
l!l
150
MC/DC0%0/1
__PACKAGE__->VERSION // "<internal>"
__PACKAGE__->VERSION
Truth table50%1/2
A
A= __PACKAGE__->VERSION
Aresult
11
00
41 'Do not edit this file directly. To change prereqs, edit the `dist.ini` file.',
42 ]
43 }
44 6 );
45  
46 17 ▶sub mvp_multivalue_args { qw( comment ) }
Subroutine mvp_multivalue_args: called CC=1 SCAR=0.0
Pod undocumented
47
48 17 ▶sub after_build {
Subroutine after_build: called CC=1 SCAR=0.0
Pod undocumented
49 17 my ($self) = @_;
50  
51 17 my $content = $self->_get_cpanfile();
52  
53 # need to write it to disk if we're in a
54 # phase that is not filemunge
55 17 path( $self->filename )->spew_raw( $content );
56 }
57  
58 17 ▶sub _get_cpanfile {
Subroutine _get_cpanfile: called CC=20 SCAR=31.2
59 17 my ($self) = @_;
60  
61 17 my $audit = CPAN::Audit->new;
62  
63 17 my $zilla = $self->zilla;
64 17 my $prereqs = $zilla->prereqs;
65
66 17 my @types = qw(requires recommends suggests conflicts);
67 17 my @phases = qw(runtime build test configure develop);
68
69 17 my $str = join "\n", ( map { "# $_" } @{ $self->comment } ), '', '';
70 17 for my $phase (@phases) {
71 85 ▶ my $prefix = $phase eq 'runtime' ? '' : (sprintf "\non '%s' => sub {\n", $phase );
Branch: 1 branch
BranchTrueFalseTotal
$phase eq 'runtime' ? : 17 68 85
72 85 ▶ my $postfix = $phase eq 'runtime' ? '' : "};\n";
Branch: 1 branch
BranchTrueFalseTotal
$phase eq 'runtime' ? : 17 68 85
73 85 ▶ my $indent = $phase eq 'runtime' ? '' : ' ';
Branch: 1 branch
BranchTrueFalseTotal
$phase eq 'runtime' ? : 17 68 85
74  
75 85 for my $type (@types) {
76 340 my $req = $prereqs->requirements_for($phase, $type);
77  
78 340 ▶ next unless $req->required_modules;
Branch: 1 branch
BranchTrueFalseTotal
unless $req->required_modules 323 17 340
79  
80 17 $str .= $prefix;
81
82 17 for my $module ( sort $req->required_modules ) {
83 19 ▶ my $version = $req->requirements_for_module( $module ) || 0;
Condition100%2/2
$req->requirements_for_module($module) || 0
l!l
172
MC/DC100%1/1
$req->requirements_for_module($module) || 0
$req->requirements_for_module($module)
Truth table100%2/2
A
A= $req->requirements_for_module($module)
Aresult
11
00
84  
85 19 my ($min_version, $advisories);
86  
87 19 ▶ if ( $self->cpan_audit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cpan_audit) 9 10 19
88 9 ($min_version, $advisories) = _audit( $audit, $module, $version );
89 }
90  
91 19 ▶ if ( $advisories && $version =~ m{(>|<|>=|<=|!=|==)} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($advisories and $version =~ /(>|<|>=|<=|!=|==)/) { } 1 18 19
elsif ($advisories) { } 3 15 18
Condition100%3/3
$advisories and $version =~ /(>|<|>=|<=|!=|==)/
!ll&&!rl&&r
1531
MC/DC100%2/2
$advisories and $version =~ /(>|<|>=|<=|!=|==)/
$advisories $version =~ /(>|<|>=|<=|!=|==)/
Truth table100%3/3
A and B
A= $advisoriesB= $version =~ /(>|<|>=|<=|!=|==)/
ABresult
0X0
100
111
92  
93 # this seems to be a version range, so check if the latest fixed version would be accepted
94 1 ▶ if ( defined $min_version && !$req->accepts_module( $module, $min_version ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $min_version and not $req->accepts_module($module, $min_version)) { } 0 1 1
elsif (defined $min_version) { } 0 1 1
Condition33%1/3
defined $min_version and not $req->accepts_module($module, $min_version)
!ll&&!rl&&r
100
MC/DC0%0/2
defined $min_version and not $req->accepts_module($module, $min_version)
defined $min_version not $req->accepts_module($module, $min_version)
Truth table33%1/3
A and not(B)
A= defined $min_versionB= not $req->accepts_module($module, $min_version)
ABresult
0X0
100
111
95 0 $self->log( "Range '$version' for $module does not include latest fixed version ($min_version)!" );
96 }
97 elsif ( defined $min_version ) {
98 0 $self->log( "Current version range includes vulnerable versions. Consider updating the minimum to $min_version" ) #if $affected_version_allowed;
99 }
100 }
101 elsif ( $advisories ) {
102  
103 # this branch is used when no version range is given but a version number
104 3 ▶ my $vuln_version_requested = $min_version && (
Condition33%1/3
$min_version && 'version'->new($version) < 'version'->new($min_version)
!ll&&!rl&&r
300
MC/DC0%0/2
$min_version && 'version'->new($version) < 'version'->new($min_version)
$min_version 'version'->new($version) < 'version'->new($min_version)
Truth table33%1/3
A && B
A= $min_versionB= 'version'->new($version) < 'version'->new($min_version)
ABresult
0X0
100
111
105 version->new( $version ) < version->new( $min_version )
106 );
107  
108 3 ▶ if ( $version == 0 && $vuln_version_requested ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($version == 0 and $vuln_version_requested) { } 0 3 3
elsif ($vuln_version_requested) { } 0 3 3
Condition66%2/3
$version == 0 and $vuln_version_requested
!ll&&!rl&&r
120
MC/DC0%0/2
$version == 0 and $vuln_version_requested
$version == 0 $vuln_version_requested
Truth table66%2/3
A and B
A= $version == 0B= $vuln_version_requested
ABresult
0X0
100
111
109 0 $version = $min_version;
110 }
111 elsif ( $vuln_version_requested ) {
112 0 $self->log( "Prereq $module $version is vulnerable" );
113 }
114 }
115  
116 19 $str .= sprintf qq~%s%s "%s" => "%s";\n~,
117 $indent,
118 $type,
119 $module,
120 $version;
121 }
122  
123 17 $str .= $postfix;
124 }
125 }
126  
127 17 return $str;
128 }
129  
130 9 ▶sub _audit {
Subroutine _audit: called CC=6 SCAR=18.9
131 9 my ($audit, $module, $version) = @_;
132  
133 9 my $result = $audit->command( 'module', $module, $version );
134 9 ▶ my ($module_data) = values %{ $result->{dists} || {} };
Condition50%1/2
$result->{'dists'} || {}
l!l
90
MC/DC0%0/1
$result->{'dists'} || {}
$result->{'dists'}
Truth table50%1/2
A
A= $result->{'dists'}
Aresult
11
00
135 9 ▶ my @advisories = @{ $module_data->{advisories} || [] };
Condition100%2/2
$module_data->{'advisories'} || []
l!l
45
MC/DC100%1/1
$module_data->{'advisories'} || []
$module_data->{'advisories'}
Truth table100%2/2
A
A= $module_data->{'advisories'}
Aresult
11
00
136  
137 9 my @versions;
138 9 for my $advisory ( @advisories ) {
139 22 ▶ my ($fixed_version) = ( $advisory->{fixed_versions} // '' ) =~ m{(v?[0-9]+(?:\.[0-9]+){0,2})};
Condition50%1/2
$advisory->{"fixed_versions"} // ""
l!l
220
MC/DC0%0/1
$advisory->{"fixed_versions"} // ""
$advisory->{"fixed_versions"}
Truth table50%1/2
A
A= $advisory->{"fixed_versions"}
Aresult
11
00
140 22 ▶ next if !$fixed_version;
Branch: 1 branch
BranchTrueFalseTotal
unless $fixed_version 22 0 22
141  
142 0 my $version_object = version->new( $fixed_version );
143 0 push @versions, $version_object;
144 }
145  
146 0 my ($min_version) = sort { $b <=> $a } @versions;
147 9 return ( $min_version, scalar @advisories );
148 }
149  
150 6 __PACKAGE__->meta->make_immutable;
151  
152 6 1;
153  
154 __END__
↑ Summary