lib/CPAN/Audit.pm

filter: Statementstmt 68.7% 130 / 189 Branchbran 47.2% 34 / 72 Conditioncond 61.7% 21 / 34 MC/DCmcdc 42.1% 8 / 19 Subroutinesub 88.0% 22 / 25 Podpod 0.0% 0 / 9 totaltotal 61.7% 215 / 348 CC 55 SCAR 25.3
CC
55
Cov
63%
command_show
42.0
_get_db
20.7
command_release
20.0
CRAP 12.5
« lib/CPAN/Audit/Discover/CpanfileSnapshot.pm ↑ Summary script/cpan-audit »
1 package CPAN::Audit;
2 13 ▶use v5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 13 ▶use Carp qw(carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use Module::CoreList;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 13 ▶use CPAN::Audit::Installed;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use CPAN::Audit::Discover;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use CPAN::Audit::Filter;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use CPAN::Audit::Version;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 ▶use CPAN::Audit::Query;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 13 ▶use CPANSA::DB;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 13 our $VERSION = '20261105.001';
18  
19 8 ▶sub new {
Subroutine new: called CC=3 SCAR=11.2
Pod undocumented
20 8 my( $class, %params ) = @_;
21  
22 8 my @allowed_keys = qw(ascii db exclude exclude_file include_perl interactive no_corelist quiet verbose version);
23  
24 8 my %args = map { $_, $params{$_} } @allowed_keys;
25 8 my $self = bless \%args, $class;
26  
27 8 ▶ $self->_handle_exclude_file if $self->{exclude_file};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"exclude_file"} 0 8 8
28  
29 8 ▶ $self->{db} //= $self->_get_db(%args);
Condition33%1/3
$self->{"db"} //= $self->_get_db(%args)
l!l&&r!l&&!r
080
MC/DC0%0/2
$self->{"db"} //= $self->_get_db(%args)
$self->{"db"} $self->_get_db(%args)
Truth table33%1/3
A //= B
A= $self->{"db"}B= $self->_get_db(%args)
ABresult
1X1
011
000
30  
31 8 $self->{filter} = CPAN::Audit::Filter->new( exclude => $args{exclude} );
32 8 $self->{query} = CPAN::Audit::Query->new( db => $self->{db} );
33 8 $self->{discover} = CPAN::Audit::Discover->new( db => $self->{db} );
34  
35 8 return $self;
36 }
37  
38 8 ▶sub _get_db {
Subroutine _get_db: called CC=6 SCAR=30.3
39 8 my( $self, %params ) = @_;
40  
41 8 ▶ if ( $params{'json_db'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"json_db"}) 0 8 8
42 0 my $data = do {
43 0 local $/;
44 0 ▶ open my($fh), '<:raw', $params{'json_db'}
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $params{"json_db"} or die qq[could not read file <$params{"json_db"}>\n] 0 0 0
45 or die "could not read file <$params{json_db}>\n";
46 0 <$fh>;
47 };
48 0 state $rc = require JSON;
49  
50 0 my $decoded = eval { JSON::decode_json($data) };
51 0 ▶ die "could not decode JSON from <$params{json_db}>: @_\n" unless defined $decoded;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $decoded 0 0 0
52 0 return $decoded;
53 }
54  
55 8 my $rc = eval { require CPANSA::DB };
56 8 ▶ if ( $rc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc) 8 0 8
57 8 return CPANSA::DB->db;
58 }
59  
60 0 $rc = eval {
61 0 warn "CPAN::Audit::DB is deprecated. Use CPANSA::DB instead. CPAN::Audit::DB will be removed in January 2027.\n";
62 require CPAN::Audit::DB
63 0 };
64 0 ▶ if ( $rc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc) 0 0 0
65 0 return CPAN::Audit::DB->db;
66 }
67  
68 0 die "could not find a CPANSA database in CPANSA::DB or CPAN::Audit::DB\n";
69 }
70  
71 0 ▶sub _handle_exclude_file {
Subroutine _handle_exclude_file: not called CC=3 SCAR=24.8
72 0 my( $self ) = @_;
73  
74 0 foreach my $file (@{$self->{exclude_file}}) {
75 0 my $fh;
76 0 ▶ unless( open $fh, "<", $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, "<", $file) 0 0 0
77 0 carp "unable to open exclude_file [$file]: $!\n";
78 0 return;
79 }
80 my @excludes =
81 0 grep { !/^\s*$/ } # no blank lines
82 0 map { s{^\s+|\s+$}{}g; $_ } # strip leading/trailing whitespace
83 0 map { s{#.*}{}; $_ } # strip comments
84 <$fh>;
85 0 push @{$self->{exclude}}, @excludes;
86 }
87 }
88  
89 5 ▶sub command_module {
Subroutine command_module: called CC=4 SCAR=14.2
Pod undocumented
90 5 my ( $self, $dists, $queried, $module, $version_range ) = @_;
91 5 ▶ return "Usage: module <module> [version-range]" unless $module;
Branch: 1 branch
BranchTrueFalseTotal
unless $module 0 5 5
92  
93 5 my $distname = $self->{db}->{module2dist}->{$module};
94  
95 5 ▶ if ( !$distname ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($distname) 0 5 5
96 0 return "Module '$module' is not in database";
97 }
98  
99 5 push @{ $queried->{$distname} }, $module;
100 5 ▶ $dists->{$distname} = $version_range // '';
Condition100%2/2
$version_range // ""
l!l
32
MC/DC100%1/1
$version_range // ""
$version_range
Truth table100%2/2
A
A= $version_range
Aresult
11
00
101  
102 5 return;
103 }
104  
105 0 ▶sub command_release {
Subroutine command_release: not called CC=4 SCAR=30.0
Pod undocumented
106 0 my ( $self, $dists, $queried, $distname, $version_range ) = @_;
107 0 ▶ return "Usage: dist|release <module> [version-range]"
Branch: 1 branch
BranchTrueFalseTotal
unless $distname 0 0 0
108 unless $distname;
109  
110 0 ▶ if ( !$self->{db}->{dists}->{$distname} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"db"}{"dists"}{$distname}) 0 0 0
111 0 return "Distribution '$distname' is not in database";
112 }
113  
114 0 ▶ $dists->{$distname} = $version_range // '';
Condition0%0/2
$version_range // ""
l!l
00
MC/DC0%0/1
$version_range // ""
$version_range
Truth table0%0/2
A
A= $version_range
Aresult
11
00
115  
116 0 return;
117 }
118  
119 0 ▶sub command_show {
Subroutine command_show: not called CC=6 SCAR=37.4
Pod undocumented
120 0 my ( $self, $dists, $queried, $advisory_id ) = @_;
121 0 ▶ return "Usage: show <advisory-id>" unless $advisory_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $advisory_id 0 0 0
122  
123 0 my ($release) = $advisory_id =~ m/^CPANSA-(.*?)-(\d+)-(\d+)$/;
124 0 ▶ return "Invalid advisory id" unless $release;
Branch: 1 branch
BranchTrueFalseTotal
unless $release 0 0 0
125  
126 0 my $dist = $self->{db}->{dists}->{$release};
127 0 ▶ return "Unknown advisory id" unless $dist;
Branch: 1 branch
BranchTrueFalseTotal
unless $dist 0 0 0
128  
129 my ($advisory) =
130 0 grep { $_->{id} eq $advisory_id } @{ $dist->{advisories} };
131 0 ▶ return "Unknown advisory id" unless $advisory;
Branch: 1 branch
BranchTrueFalseTotal
unless $advisory 0 0 0
132  
133 0 ▶ my $distname = $advisory->{distribution} // 'Unknown distribution name';
Condition0%0/2
$advisory->{"distribution"} // "Unknown distribution name"
l!l
00
MC/DC0%0/1
$advisory->{"distribution"} // "Unknown distribution name"
$advisory->{"distribution"}
Truth table0%0/2
A
A= $advisory->{"distribution"}
Aresult
11
00
134 0 $dists->{$distname}{advisories} = [ $advisory ];
135 0 $dists->{$distname}{version} = 'Any';
136  
137 0 return;
138 }
139  
140 1 ▶sub command_modules {
Subroutine command_modules: called CC=5 SCAR=17.8
Pod undocumented
141 1 my ($self, $dists, $queried, @modules) = @_;
142 1 ▶ return "Usage: modules '<module>[;version-range]' '<module>[;version-range]'" unless @modules;
Branch: 1 branch
BranchTrueFalseTotal
unless @modules 0 1 1
143  
144 1 foreach my $module ( @modules ) {
145 3 my ($name, $version) = split /;/, $module;
146  
147 3 ▶ my $failed = $self->command_module( $dists, $queried, $name, $version // '' );
Condition50%1/2
$version // ""
l!l
03
MC/DC0%0/1
$version // ""
$version
Truth table50%1/2
A
A= $version
Aresult
11
00
148  
149 3 ▶ if ( $failed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($failed) 0 3 3
150 0 $self->verbose( $failed );
151 0 next;
152 }
153 }
154  
155 1 return;
156 }
157  
158 3 ▶sub command_deps {
Subroutine command_deps: called CC=7 SCAR=20.6
Pod undocumented
159 3 my ($self, $dists, $queried, $dir) = @_;
160 3 ▶ $dir = '.' unless defined $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dir 0 3 3
161  
162 3 ▶ return "Usage: deps <dir>" unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 3 3
163  
164 3 my @deps = $self->{discover}->discover($dir);
165  
166 3 $self->verbose( sprintf 'Discovered %d dependencies', scalar(@deps) );
167  
168 3 foreach my $dep (@deps) {
169 my $dist = $dep->{dist}
170 5 ▶ || $self->{db}->{module2dist}->{ $dep->{module} };
Condition33%1/3
$dep->{'dist'} || $self->{'db'}{'module2dist'}{$dep->{'module'}}
l!l&&r!l&&!r
050
MC/DC0%0/2
$dep->{'dist'} || $self->{'db'}{'module2dist'}{$dep->{'module'}}
$dep->{'dist'} $self->{'db'}{'module2dist'}{$dep->{'module'}}
Truth table33%1/3
A || B
A= $dep->{'dist'}B= $self->{'db'}{'module2dist'}{$dep->{'module'}}
ABresult
1X1
011
000
171 5 ▶ next unless $dist;
Branch: 1 branch
BranchTrueFalseTotal
unless $dist 0 5 5
172  
173 5 ▶ push @{ $queried->{$dist} }, $dep->{module} if !$dep->{dist};
Branch: 1 branch
BranchTrueFalseTotal
unless $dep->{"dist"} 5 0 5
174  
175 5 $dists->{$dist} = $dep->{version};
176 }
177  
178 3 return;
179 }
180  
181 2 ▶sub command_installed {
Subroutine command_installed: called CC=5 SCAR=16.3
Pod undocumented
182 2 my ($self, $dists, $queried, @args) = @_;
183  
184 2 $self->verbose('Collecting all installed modules. This can take a while...');
185  
186 52 ▶ my $verbose_callback = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
187 52 my ($info) = @_;
188 52 $self->verbose( sprintf '%s: %s-%s', $info->{path}, $info->{distname}, $info->{version} );
189 2 };
190  
191 my @deps = CPAN::Audit::Installed->new(
192 db => $self->{db},
193 include_perl => $self->{include_perl},
194 2 ▶ ( $self->{verbose} ? ( cb => $verbose_callback ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
$self->{'verbose'} ? : 1 1 2
195 )->find(@args);
196  
197 2 foreach my $dep (@deps) {
198 my $dist = $dep->{dist}
199 104 ▶ || $self->{db}->{module2dist}->{ $dep->{module} };
Condition33%1/3
$dep->{'dist'} || $self->{'db'}{'module2dist'}{$dep->{'module'}}
l!l&&r!l&&!r
10400
MC/DC0%0/2
$dep->{'dist'} || $self->{'db'}{'module2dist'}{$dep->{'module'}}
$dep->{'dist'} $self->{'db'}{'module2dist'}{$dep->{'module'}}
Truth table33%1/3
A || B
A= $dep->{'dist'}B= $self->{'db'}{'module2dist'}{$dep->{'module'}}
ABresult
1X1
011
000
200 104 ▶ next unless $dist;
Branch: 1 branch
BranchTrueFalseTotal
unless $dist 0 104 104
201  
202 104 $dists->{ $dep->{dist} } = '==' . $dep->{version};
203 }
204  
205 2 return;
206 }
207  
208 8 ▶sub command {
Subroutine command: called CC=18 SCAR=29.2
Pod undocumented
209 8 state $command_table = {
210 dependencies => 'command_deps',
211 deps => 'command_deps',
212 installed => 'command_installed',
213 module => 'command_module',
214 modules => 'command_modules',
215 release => 'command_release',
216 dist => 'command_release',
217 show => 'command_show',
218 };
219  
220 8 my( $self, $command, @args ) = @_;
221  
222 8 my %report = (
223 meta => {
224 command => $command,
225 args => [ @args ],
226 cpan_audit => {
227 version => $VERSION,
228 db => $CPANSA::DB::VERSION,
229 },
230 total_advisories => 0,
231 },
232 errors => [],
233 dists => {},
234 );
235 8 my $dists = $report{dists};
236 8 my $queried = {};
237  
238 8 ▶ if (!$self->{no_corelist}
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"no_corelist"} and $command eq "dependencies" || $command eq "deps" || $command eq "installed") 2 6 8
Condition77%7/9
not $self->{"no_corelist"} and $command eq "dependencies" || $command eq "deps" || $command eq "installed"
!ll&&!rl&&r
332
$command eq "dependencies" or $command eq "deps" or $command eq "installed"
l!l&&r!l&&!r
203
$command eq "dependencies" or $command eq "deps"
l!l&&r!l&&!r
023
MC/DC50%2/4
not $self->{"no_corelist"} and $command eq "dependencies" || $command eq "deps" || $command eq "installed"
not $self->{"no_corelist"} $command eq "dependencies" $command eq "deps" $command eq "installed"
Truth table60%3/5
not(A) and B or C or D
A= not $self->{"no_corelist"}B= $command eq "dependencies"C= $command eq "deps"D= $command eq "installed"
ABCDresult
0XXX0
10000
11XX1
101X1
10011
239 && ( $command eq 'dependencies'
240 || $command eq 'deps'
241 || $command eq 'installed' )
242 )
243 {
244 # Find core modules for this perl version first.
245 # This way explictly installed versions will overwrite.
246 2 ▶ if ( my $core = $Module::CoreList::version{$]} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $core = $Module::CoreList::version{$]}) 2 0 2
247 2 ▶ while ( my ( $mod, $ver ) = each %$core ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$core) 1574 2 1576
248 1574 ▶ my $dist = $self->{db}{module2dist}{$mod} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $dist = $self->{"db"}{"module2dist"}{$mod} or next 618 956 1574
249 618 ▶ $dists->{$dist} = $ver if( ! defined $dists->{$dist} or version->parse($ver) > $dists->{$dist} );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $dists->{$dist} or "version"->parse($ver) > $dists->{$dist} 81 537 618
Condition100%3/3
not defined $dists->{$dist} or "version"->parse($ver) > $dists->{$dist}
l!l&&r!l&&!r
6021537
MC/DC100%2/2
not defined $dists->{$dist} or "version"->parse($ver) > $dists->{$dist}
not defined $dists->{$dist} "version"->parse($ver) > $dists->{$dist}
Truth table100%3/3
not(A) or B
A= not defined $dists->{$dist}B= "version"->parse($ver) > $dists->{$dist}
ABresult
1X1
011
000
250 }
251 }
252 }
253  
254 8 ▶ if ( exists $command_table->{$command} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $command_table->{$command}) { } 8 0 8
255 8 my $method = $command_table->{$command};
256 8 push @{ $report{errors} }, $self->$method( $dists, $queried, @args );
257 8 ▶ return \%report if $command eq 'show';
Branch: 1 branch
BranchTrueFalseTotal
if $command eq "show" 0 8 8
258 }
259 else {
260 0 push @{ $report{errors} }, "unknown command: $command. See -h";
261 }
262  
263 8 ▶ if (%$dists) {
Branch: 1 branch
BranchTrueFalseTotal
if (%$dists) 8 0 8
264 8 my $query = $self->{query};
265  
266 8 foreach my $distname ( keys %$dists ) {
267 170 my $version_range = $dists->{$distname};
268 my @advisories =
269 170 grep { ! $self->{filter}->excludes($_) }
270 $query->advisories_for( $distname, $version_range );
271  
272 170 ▶ $version_range = 'Any'
Branch: 1 branch
BranchTrueFalseTotal
if $version_range eq "" or $version_range eq 0 6 164 170
Condition100%3/3
$version_range eq "" or $version_range eq 0
l!l&&r!l&&!r
42164
MC/DC100%2/2
$version_range eq "" or $version_range eq 0
$version_range eq "" $version_range eq 0
Truth table100%3/3
A or B
A= $version_range eq ""B= $version_range eq 0
ABresult
1X1
011
000
273 if $version_range eq '' || $version_range eq '0';
274  
275 170 $report{meta}{total_advisories} += @advisories;
276  
277 170 ▶ if ( @advisories ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@advisories) { } 22 148 170
278 $dists->{$distname} = {
279 advisories => \@advisories,
280 version => $version_range,
281 22 ▶ queried_modules => $queried->{$distname} || [],
Condition100%2/2
$queried->{$distname} || []
l!l
814
MC/DC100%1/1
$queried->{$distname} || []
$queried->{$distname}
Truth table100%2/2
A
A= $queried->{$distname}
Aresult
11
00
282 };
283 }
284 else {
285 148 delete $dists->{$distname}
286 }
287 }
288 }
289  
290 8 return \%report;
291 }
292  
293 57 ▶ sub verbose {
Subroutine verbose: called CC=2 SCAR=6.9
Pod undocumented
294 57 my ( $self, $message ) = @_;
295 57 ▶ return if $self->{quiet};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"quiet"} 3 54 57
296 54 $self->_print( *STDERR, $message );
297 }
298  
299  
300 54 ▶ sub _print {
Subroutine _print: called CC=3 SCAR=12.5
301 54 my ( $self, $fh, $message ) = @_;
302  
303 54 ▶ if ( $self->{no_color} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'no_color'}) { } 0 54 54
304 0 $message =~ s{__BOLD__}{}g;
305 0 $message =~ s{__GREEN__}{}g;
306 0 $message =~ s{__RED__}{}g;
307 0 $message =~ s{__RESET__}{}g;
308 }
309 else {
310 54 $message =~ s{__BOLD__}{\e[39;1m}g;
311 54 $message =~ s{__GREEN__}{\e[32m}g;
312 54 $message =~ s{__RED__}{\e[31m}g;
313 54 $message =~ s{__RESET__}{\e[0m}g;
314  
315 54 ▶ $message .= "\e[0m" if length $message;
Branch: 1 branch
BranchTrueFalseTotal
if length $message 54 0 54
316 }
317  
318 54 print $fh "$message\n";
319 }
320  
321 13 1;
322 __END__
« lib/CPAN/Audit/Discover/CpanfileSnapshot.pm ↑ Summary script/cpan-audit »