lib/App/Perlbrew/Util.pm

filter: Statementstmt 92.7% 77 / 83 Branchbran 80.7% 21 / 26 Conditioncond 64.7% 11 / 17 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 9 totaltotal 79.3% 127 / 160 CC 26 SCAR 15.7
CC
26
Cov
87%
make_skaji_relocatable_perl_url
10.3
perl_version_to_integer
7.1
editdist
6.0
CRAP 4.8
« lib/App/Perlbrew/HTTP.pm ↑ Summary lib/App/Perlbrew/Path.pm »
1 package App::Perlbrew::Util;
2 73 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 73 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 73 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 73 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 73 our @EXPORT = qw( uniq min editdist files_are_the_same perl_version_to_integer );
8 73 our @EXPORT_OK = qw(
9 find_similar_tokens
10 looks_like_url_of_skaji_relocatable_perl
11 looks_like_sys_would_be_compatible_with_skaji_relocatable_perl
12 make_skaji_relocatable_perl_url
13 );
14  
15 38 ▶sub uniq {
Subroutine uniq: called CC=1 SCAR=0.4
Pod undocumented
16 38 my %seen;
17 0 grep { !$seen{$_}++ } @_;
18 }
19  
20 25179 ▶sub min(@) {
Subroutine min: called CC=3 SCAR=11.0
Pod undocumented
21 25179 my $m = $_[0];
22 25179 for(@_) {
23 75537 ▶ $m = $_ if $_ < $m;
Branch: 1 branch
BranchTrueFalseTotal
if $_ < $m 12160 63377 75537
24 }
25 25179 return $m;
26 }
27  
28 # straight copy of Wikipedia's "Levenshtein Distance"
29 234 ▶sub editdist {
Subroutine editdist: called CC=6 SCAR=17.9
Pod undocumented
30 234 my @a = split //, shift;
31 234 my @b = split //, shift;
32  
33 # There is an extra row and column in the matrix. This is the
34 # distance from the empty string to a substring of the target.
35 234 my @d;
36 234 $d[$_][0] = $_ for (0 .. @a);
37 234 $d[0][$_] = $_ for (0 .. @b);
38  
39 234 for my $i (1 .. @a) {
40 2458 for my $j (1 .. @b) {
41 26764 ▶ $d[$i][$j] = ($a[$i-1] eq $b[$j-1] ? $d[$i-1][$j-1]
Branch: 1 branch
BranchTrueFalseTotal
$a[$i - 1] eq $b[$j - 1] ? : 1585 25179 26764
42 : 1 + min($d[$i-1][$j], $d[$i][$j-1], $d[$i-1][$j-1]));
43 }
44 }
45  
46 234 return $d[@a][@b];
47 }
48  
49 26 ▶sub files_are_the_same {
Subroutine files_are_the_same: called CC=5 SCAR=16.1
Pod undocumented
50 ## Check dev and inode num. Not useful on Win32.
51 ## The for loop should always return false on Win32, as a result.
52  
53 26 my @files = @_;
54 26 my @stats = map {[ stat($_) ]} @files;
55  
56 26 my $stats0 = join " ", @{$stats[0]}[0,1];
57 26 for (@stats) {
58 52 ▶ return 0 if ((! defined($_->[1])) || $_->[1] == 0);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_->[1] or $_->[1] == 0 2 50 52
Condition66%2/3
not defined $_->[1] or $_->[1] == 0
l!l&&r!l&&!r
2050
MC/DC50%1/2
not defined $_->[1] or $_->[1] == 0
not defined $_->[1] $_->[1] == 0
Truth table66%2/3
not(A) or B
A= not defined $_->[1]B= $_->[1] == 0
ABresult
1X1
011
000
59 50 ▶ unless ($stats0 eq join(" ", $_->[0], $_->[1])) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($stats0 eq join(" ", $_->[0], $_->[1])) 14 36 50
60 14 return 0;
61 }
62 }
63 10 return 1
64 }
65  
66 209 ▶sub perl_version_to_integer {
Subroutine perl_version_to_integer: called CC=7 SCAR=19.6
Pod undocumented
67 209 my $version = shift;
68  
69 209 my @v;
70 209 ▶ if ($version eq 'blead') {
Branch: 1 branch
BranchTrueFalseTotal
if ($version eq 'blead') { } 1 208 209
71 1 @v = (999,999,999);
72 } else {
73 208 @v = split(/[\.\-_]/, $version);
74 }
75 209 ▶ return undef if @v < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @v < 2 0 209 209
76  
77 209 ▶ if ($v[1] <= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v[1] <= 5) { } 24 185 209
78 24 ▶ $v[2] ||= 0;
Condition100%2/2
$v[2] ||= 0
l!l
204
MC/DC100%1/1
$v[2] ||= 0
$v[2]
Truth table100%2/2
A
A= $v[2]
Aresult
11
00
79 24 $v[3] = 0;
80 }
81 else {
82 185 ▶ $v[3] ||= $v[1] >= 6 ? 9 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$v[1] >= 6 ? : 141 0 141
Condition66%2/3
$v[3] ||= $v[1] >= 6 ? 9 : 0
l!l&&r!l&&!r
441410
MC/DC0%0/2
$v[3] ||= $v[1] >= 6 ? 9 : 0
$v[3] $v[1] >= 6 ? 9 : 0
Truth table66%2/3
A ||= B
A= $v[3]B= $v[1] >= 6 ? 9 : 0
ABresult
1X1
011
000
83 185 $v[3] =~ s/[^0-9]//g;
84 }
85  
86 209 return $v[1]*1000000 + $v[2]*1000 + $v[3];
87 }
88  
89 8 ▶sub find_similar_tokens {
Subroutine find_similar_tokens: called CC=3 SCAR=11.0
Pod undocumented
90 8 my ($token, $tokens) = @_;
91 8 my $SIMILAR_DISTANCE = 6;
92  
93 51 my @similar_tokens = sort { $a->[1] <=> $b->[1] } map {
94 8 my $d = editdist( $_, $token );
95 228 ▶ ( ( $d < $SIMILAR_DISTANCE ) ? [$_, $d] : () )
Branch: 1 branch
BranchTrueFalseTotal
$d < $SIMILAR_DISTANCE ? : 32 196 228
96 } @$tokens;
97  
98 8 ▶ if (@similar_tokens) {
Branch: 1 branch
BranchTrueFalseTotal
if (@similar_tokens) 6 2 8
99 6 my $best_score = $similar_tokens[0][1];
100 6 @similar_tokens = map { $_->[0] } grep { $_->[1] == $best_score } @similar_tokens;
101 }
102  
103 8 return \@similar_tokens;
104 }
105  
106 68 ▶sub looks_like_url_of_skaji_relocatable_perl {
Subroutine looks_like_url_of_skaji_relocatable_perl: called CC=2 SCAR=6.9
Pod undocumented
107 68 my ($str) = @_;
108 # https://github.com/skaji/relocatable-perl/releases/download/5.40.0.0/perl-linux-amd64.tar.gz
109 68 my $prefix = "https://github.com/skaji/relocatable-perl/releases/download";
110 68 my $version_re = qr/(5\.[0-9][0-9]\.[0-9][0-9]?.[0-9])/;
111 68 my $name_re = qr/perl-(linux|darwin)-(amd64|arm64)\.tar\.gz/;
112 68 ▶ return undef unless $str =~ m{ \Q$prefix\E / $version_re / $name_re }x;
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ m[ \Q$prefix\E / $version_re / $name_re ]x 65 3 68
113 return {
114 3 url => $str,
115 version => $1,
116 os => $2,
117 arch => $3,
118 original_filename => "perl-$2-$3.tar.gz",
119 };
120 }
121  
122  
123 6 ▶sub _arch_compat {
Subroutine _arch_compat: called CC=2 SCAR=7.0
124 6 my ($arch) = @_;
125 6 my $compat = {
126 x86_64 => "amd64",
127 i386 => "amd64",
128 };
129 6 ▶ return $compat->{$arch} || $arch;
Condition66%2/3
$compat->{$arch} || $arch
l!l&&r!l&&!r
150
MC/DC0%0/2
$compat->{$arch} || $arch
$compat->{$arch} $arch
Truth table66%2/3
A || B
A= $compat->{$arch}B= $arch
ABresult
1X1
011
000
130 }
131  
132 5 ▶sub looks_like_sys_would_be_compatible_with_skaji_relocatable_perl {
Subroutine looks_like_sys_would_be_compatible_with_skaji_relocatable_perl: called CC=2 SCAR=6.9
Pod undocumented
133 5 my ($detail, $sys) = @_;
134  
135 return (
136 ($detail->{os} eq $sys->os)
137 5 ▶ && (_arch_compat($detail->{arch}) eq _arch_compat($sys->arch))
Condition100%3/3
$detail->{'os'} eq $sys->os && _arch_compat($detail->{'arch'}) eq _arch_compat($sys->arch)
!ll&&!rl&&r
212
MC/DC100%2/2
$detail->{'os'} eq $sys->os && _arch_compat($detail->{'arch'}) eq _arch_compat($sys->arch)
$detail->{'os'} eq $sys->os _arch_compat($detail->{'arch'}) eq _arch_compat($sys->arch)
Truth table100%3/3
A && B
A= $detail->{'os'} eq $sys->osB= _arch_compat($detail->{'arch'}) eq _arch_compat($sys->arch)
ABresult
0X0
100
111
138 );
139 }
140  
141 62 ▶sub make_skaji_relocatable_perl_url {
Subroutine make_skaji_relocatable_perl_url: called CC=4 SCAR=23.3
Pod undocumented
142 62 my ($str, $sys) = @_;
143 62 ▶ if ($str =~ m/\Askaji-relocatable-perl-(5\.[0-9][0-9]\.[0-9][0-9]?.[0-9])\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /\Askaji-relocatable-perl-(5\.[0-9][0-9]\.[0-9][0-9]?.[0-9])\z/) 0 62 62
144 0 my $version = $1;
145 0 my $os = $sys->os;
146 0 my $arch = $sys->arch;
147 0 ▶ $arch = "amd64" if $arch eq 'x86_64' || $arch eq 'i386';
Branch: 1 branch
BranchTrueFalseTotal
if $arch eq "x86_64" or $arch eq "i386" 0 0 0
Condition0%0/3
$arch eq "x86_64" or $arch eq "i386"
l!l&&r!l&&!r
000
MC/DC0%0/2
$arch eq "x86_64" or $arch eq "i386"
$arch eq "x86_64" $arch eq "i386"
Truth table0%0/3
A or B
A= $arch eq "x86_64"B= $arch eq "i386"
ABresult
1X1
011
000
148  
149 0 return "https://github.com/skaji/relocatable-perl/releases/download/$version/perl-$os-$arch.tar.gz";
150 }
151 62 return undef;
152 }
153  
154 73 1;
« lib/App/Perlbrew/HTTP.pm ↑ Summary lib/App/Perlbrew/Path.pm »