lib/App/Perlbrew/Util.pm

filter: Statementstmt 98.9% 91 / 92 Branchbran 87.5% 28 / 32 Conditioncond 70.0% 14 / 20 MC/DCmcdc 38.4% 5 / 13 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 10 totaltotal 84.0% 153 / 182 CC 30 SCAR 14.5
CC
30
Cov
92%
perl_version_to_integer
7.1
editdist
6.0
prompt
5.0
CRAP 4.3
« lib/App/Perlbrew/HTTP.pm ↑ Summary lib/App/Perlbrew/Path.pm »
1 package App::Perlbrew::Util;
2 76 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 76 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 76 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 76 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 76 our @EXPORT = qw( uniq min editdist files_are_the_same perl_version_to_integer );
8 76 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 prompt
14 );
15  
16 38 ▶sub uniq {
Subroutine uniq: called CC=1 SCAR=0.4
Pod undocumented
17 38 my %seen;
18 0 grep { !$seen{$_}++ } @_;
19 }
20  
21 25179 ▶sub min(@) {
Subroutine min: called CC=3 SCAR=11.0
Pod undocumented
22 25179 my $m = $_[0];
23 25179 for(@_) {
24 75537 ▶ $m = $_ if $_ < $m;
Branch: 1 branch
BranchTrueFalseTotal
if $_ < $m 12160 63377 75537
25 }
26 25179 return $m;
27 }
28  
29 # straight copy of Wikipedia's "Levenshtein Distance"
30 234 ▶sub editdist {
Subroutine editdist: called CC=6 SCAR=17.9
Pod undocumented
31 234 my @a = split //, shift;
32 234 my @b = split //, shift;
33  
34 # There is an extra row and column in the matrix. This is the
35 # distance from the empty string to a substring of the target.
36 234 my @d;
37 234 $d[$_][0] = $_ for (0 .. @a);
38 234 $d[0][$_] = $_ for (0 .. @b);
39  
40 234 for my $i (1 .. @a) {
41 2458 for my $j (1 .. @b) {
42 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
43 : 1 + min($d[$i-1][$j], $d[$i][$j-1], $d[$i-1][$j-1]));
44 }
45 }
46  
47 234 return $d[@a][@b];
48 }
49  
50 26 ▶sub files_are_the_same {
Subroutine files_are_the_same: called CC=5 SCAR=16.1
Pod undocumented
51 ## Check dev and inode num. Not useful on Win32.
52 ## The for loop should always return false on Win32, as a result.
53  
54 26 my @files = @_;
55 26 my @stats = map {[ stat($_) ]} @files;
56  
57 26 my $stats0 = join " ", @{$stats[0]}[0,1];
58 26 for (@stats) {
59 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
60 50 ▶ unless ($stats0 eq join(" ", $_->[0], $_->[1])) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($stats0 eq join(" ", $_->[0], $_->[1])) 14 36 50
61 14 return 0;
62 }
63 }
64 10 return 1
65 }
66  
67 209 ▶sub perl_version_to_integer {
Subroutine perl_version_to_integer: called CC=7 SCAR=19.6
Pod undocumented
68 209 my $version = shift;
69  
70 209 my @v;
71 209 ▶ if ($version eq 'blead') {
Branch: 1 branch
BranchTrueFalseTotal
if ($version eq 'blead') { } 1 208 209
72 1 @v = (999,999,999);
73 } else {
74 208 @v = split(/[\.\-_]/, $version);
75 }
76 209 ▶ return undef if @v < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @v < 2 0 209 209
77  
78 209 ▶ if ($v[1] <= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v[1] <= 5) { } 24 185 209
79 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
80 24 $v[3] = 0;
81 }
82 else {
83 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
84 185 $v[3] =~ s/[^0-9]//g;
85 }
86  
87 209 return $v[1]*1000000 + $v[2]*1000 + $v[3];
88 }
89  
90 8 ▶sub find_similar_tokens {
Subroutine find_similar_tokens: called CC=3 SCAR=11.0
Pod undocumented
91 8 my ($token, $tokens) = @_;
92 8 my $SIMILAR_DISTANCE = 6;
93  
94 51 my @similar_tokens = sort { $a->[1] <=> $b->[1] } map {
95 8 my $d = editdist( $_, $token );
96 228 ▶ ( ( $d < $SIMILAR_DISTANCE ) ? [$_, $d] : () )
Branch: 1 branch
BranchTrueFalseTotal
$d < $SIMILAR_DISTANCE ? : 32 196 228
97 } @$tokens;
98  
99 8 ▶ if (@similar_tokens) {
Branch: 1 branch
BranchTrueFalseTotal
if (@similar_tokens) 6 2 8
100 6 my $best_score = $similar_tokens[0][1];
101 6 @similar_tokens = map { $_->[0] } grep { $_->[1] == $best_score } @similar_tokens;
102 }
103  
104 8 return \@similar_tokens;
105 }
106  
107 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
108 68 my ($str) = @_;
109 # https://github.com/skaji/relocatable-perl/releases/download/5.40.0.0/perl-linux-amd64.tar.gz
110 68 my $prefix = "https://github.com/skaji/relocatable-perl/releases/download";
111 68 my $version_re = qr/(5\.[0-9][0-9]\.[0-9][0-9]?.[0-9])/;
112 68 my $name_re = qr/perl-(linux|darwin)-(amd64|arm64)\.tar\.gz/;
113 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
114 return {
115 3 url => $str,
116 version => $1,
117 os => $2,
118 arch => $3,
119 original_filename => "perl-$2-$3.tar.gz",
120 };
121 }
122  
123  
124 6 ▶sub _arch_compat {
Subroutine _arch_compat: called CC=2 SCAR=7.0
125 6 my ($arch) = @_;
126 6 my $compat = {
127 x86_64 => "amd64",
128 i386 => "amd64",
129 };
130 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
131 }
132  
133 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
134 5 my ($detail, $sys) = @_;
135  
136 return (
137 ($detail->{os} eq $sys->os)
138 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
139 );
140 }
141  
142 63 ▶sub make_skaji_relocatable_perl_url {
Subroutine make_skaji_relocatable_perl_url: called CC=4 SCAR=14.2
Pod undocumented
143 63 my ($str, $sys) = @_;
144 # The downloadable distributions can be explored at the page of Release:
145 # https://github.com/skaji/relocatable-perl/releases
146  
147 63 ▶ 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/) 1 62 63
148 1 my $version = $1;
149  
150 # Eath of these 2 on-URL terms has 2 variants.
151 #
152 # OS is either 'darwin' or 'linux'
153 # Arch is either 'arm64' or 'amd64'
154 #
155 # In here we map all our detected value to those 4 values. If
156 # we encounter anything we do not recognize, then we return
157 # undef, meaning that there are no corresponding distribution
158 # of skaji-relocatable-perl for this system.
159  
160 my $os = {
161 'darwin' => 'darwin',
162 'linux' => 'linux',
163 'cygwin' => undef,
164 1 }->{$sys->os};
165  
166 my $arch = {
167 'i386' => 'amd64',
168 'x86_64' => 'amd64',
169 'arm64' => 'arm64',
170 1 }->{$sys->arch};
171  
172 1 ▶ return undef unless defined($os) && defined($arch);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $os and defined $arch 0 1 1
Condition33%1/3
defined $os and defined $arch
!ll&&!rl&&r
001
MC/DC0%0/2
defined $os and defined $arch
defined $os defined $arch
Truth table33%1/3
A and B
A= defined $osB= defined $arch
ABresult
0X0
100
111
173  
174 1 return "https://github.com/skaji/relocatable-perl/releases/download/$version/perl-$os-$arch.tar.gz";
175 }
176 62 return undef;
177 }
178  
179 6 ▶sub prompt {
Subroutine prompt: called CC=5 SCAR=16.2
Pod undocumented
180 6 my ($message, $default) = @_;
181  
182 6 print $message;
183  
184 # perlbrew has been using ExtUtils::MakeMaker::prompt()
185 # and it makes sense to keep the support of PERL_MM_USE_DEFAULT for a few more versions.
186 6 ▶ if ($ENV{PERL_MM_USE_DEFAULT}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"PERL_MM_USE_DEFAULT"}) 1 5 6
187 1 return $default;
188 }
189  
190 5 my $ans = <STDIN>;
191  
192 5 ▶ if (defined($ans)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ans) 5 0 5
193 5 $ans =~ s{\015?\012$}{};
194 }
195  
196 5 ▶ $ans = $default
Branch: 1 branch
BranchTrueFalseTotal
if not defined $ans or $ans eq "" 2 3 5
Condition66%2/3
not defined $ans or $ans eq ""
l!l&&r!l&&!r
023
MC/DC50%1/2
not defined $ans or $ans eq ""
not defined $ans $ans eq ""
Truth table66%2/3
not(A) or B
A= not defined $ansB= $ans eq ""
ABresult
1X1
011
000
197 if (!defined($ans) || ($ans eq ''));
198  
199 5 return $ans;
200 }
201  
202 76 1;
« lib/App/Perlbrew/HTTP.pm ↑ Summary lib/App/Perlbrew/Path.pm »