PTP/Util/Semver.pm

filter: Statementstmt 98.0% 49 / 50 Branchbran 80.0% 24 / 30 Conditioncond 61.1% 11 / 18 MC/DCmcdc 27.2% 3 / 11 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 3 totaltotal 79.3% 96 / 121 CC 26 SCAR 19.5
CC
26
Cov
86%
_compare_pre_release
9.5
compare_parsed
9.5
_compare_core
6.1
CRAP 7.0
« PTP/Util.pm ↑ Summary
1 # This module compares version strings following the precedence rules of the
2 # Semantic Versioning specification (https://semver.org, item 11). It is lenient
3 # about the structure of the input: an optional path-like prefix (one or more
4 # leading '/'-terminated segments, none of which contains a '.') and an optional
5 # leading 'v' are recognized, and the version core may have any number of
6 # dot-separated numeric components (missing components are treated as 0, so that
7 # "1.1" and "1.1.0" compare as equal). The prefix is compared alphabetically,
8 # which lets tags such as "refs/tags/projectname/v1.0.0" be grouped by their
9 # path and then ordered by their semver part.
10  
11 package App::PTP::Util::Semver;
12  
13 3 ▶use 5.022;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 our $VERSION = '0.01';
18  
19 # Parses a version string into a hash ref with the following keys:
20 # - prefix: the optional path-like prefix (the empty string when there is none);
21 # - core: an array ref of the dot-separated components of the version core;
22 # - pre: an array ref of the dot-separated pre-release identifiers, or undef
23 # when there is no pre-release part;
24 # - warnings: an array ref of messages describing the edge cases hit while
25 # parsing (currently, core components that are not numeric).
26 # The build metadata (anything after a '+') is dropped as it does not affect
27 # precedence.
28 24 ▶sub parse {
Subroutine parse: called CC=6 SCAR=17.9
Pod undocumented
29 24 my ($version) = @_;
30 24 my $original = $version;
31 24 my @warnings;
32 24 $version =~ s/\+.*//s;
33 # The prefix is any number of leading '/'-terminated segments, as long as none
34 # of them contains a '.' (a dot before a slash means we have already reached
35 # the version part, e.g. "1.2/3", and the slash is not a prefix separator).
36 # The trailing '/' is dropped so the prefix compares as a plain path string.
37 24 ▶ my $prefix = $version =~ s{^((?:[^./]+/)+)}{} ? $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$version =~ s[^((?:[^./]+/)+)][]u ? : 7 17 24
38 24 $prefix =~ s{/$}{};
39 24 $version =~ s/^v//i;
40 24 my ($core, $pre) = split /-/, $version, 2;
41 24 ▶ my @core = split /\./, $core // '';
Condition50%1/2
$core // ""
l!l
240
MC/DC0%0/1
$core // ""
$core
Truth table50%1/2
A
A= $core
Aresult
11
00
42  
43 24 for my $component (@core) {
44 60 ▶ push @warnings,
Branch: 1 branch
BranchTrueFalseTotal
unless $component =~ /^[0-9]+$/u 2 58 60
45 "version '${original}' has a non-numeric component '${component}' that is treated as 0"
46 if $component !~ /^[0-9]+$/;
47 }
48 return {
49 24 ▶ prefix => $prefix,
Branch: 1 branch
BranchTrueFalseTotal
defined $pre ? : 6 18 24
50 core => \@core,
51 pre => defined $pre ? [split /\./, $pre] : undef,
52 warnings => \@warnings,
53 };
54 }
55  
56 # Compares the numeric cores of two versions (two array refs), padding the
57 # shorter one with zeros. Returns a negative, zero or positive number.
58 9 ▶sub _compare_core {
Subroutine _compare_core: called CC=6 SCAR=18.0
59 9 my ($first, $other) = @_;
60 3 ▶ no warnings 'numeric'; ## no critic (ProhibitNoWarnings)
Subroutine BEGIN: called CC=1 SCAR=0.0
61 9 ▶ my $count = @$first > @$other ? @$first : @$other;
Branch: 1 branch
BranchTrueFalseTotal
@$first > @$other ? : 0 9 9
62 9 for my $i (0 .. $count - 1) {
63 21 ▶ my $result = ($first->[$i] // 0) <=> ($other->[$i] // 0);
Condition75%3/4
$first->[$i] // 0
l!l
201
$other->[$i] // 0
l!l
210
MC/DC100%1/1
$first->[$i] // 0
$first->[$i]
MC/DC0%0/1
$other->[$i] // 0
$other->[$i]
Truth table100%2/2
A
A= $first->[$i]
Aresult
11
00
Truth table50%1/2
A
A= $other->[$i]
Aresult
11
00
64 21 ▶ return $result if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 3 18 21
65 }
66 6 return 0;
67 }
68  
69 # Compares the pre-release parts of two versions (two array refs of
70 # identifiers), field by field. Numeric identifiers are compared numerically and
71 # have lower precedence than alphanumeric ones, which are compared as strings.
72 # When all the shared fields are equal, the longer list has higher precedence.
73 2 ▶sub _compare_pre_release {
Subroutine _compare_pre_release: called CC=8 SCAR=22.5
74 2 my ($first, $other) = @_;
75 2 ▶ my $count = @$first < @$other ? @$first : @$other;
Branch: 1 branch
BranchTrueFalseTotal
@$first < @$other ? : 0 2 2
76 2 for my $i (0 .. $count - 1) {
77 2 my ($x, $y) = ($first->[$i], $other->[$i]);
78 2 my ($x_numeric, $y_numeric) = ($x =~ /^[0-9]+$/, $y =~ /^[0-9]+$/);
79 2 ▶ my $result =
Branch: 3 branches
BranchTrueFalseTotal
$x_numeric && $y_numeric ? : 0 2 2
$x_numeric ? : 1 1 2
$y_numeric ? : 0 1 1
Condition66%2/3
$x_numeric and $y_numeric
!ll&&!rl&&r
110
MC/DC0%0/2
$x_numeric and $y_numeric
$x_numeric $y_numeric
Truth table66%2/3
A and B
A= $x_numericB= $y_numeric
ABresult
0X0
100
111
80 $x_numeric && $y_numeric ? $x <=> $y
81 : $x_numeric ? -1
82 : $y_numeric ? 1
83 : $x cmp $y;
84 2 ▶ return $result if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 2 0 2
85 }
86 0 return @$first <=> @$other;
87 }
88  
89 # Compares two parsed versions (as returned by parse), returning a negative,
90 # zero or positive number when the first one sorts respectively before, at the
91 # same place as, or after the second one. The prefixes are compared as strings
92 # first, then the numeric cores, and finally the pre-release parts: a version
93 # with a pre-release part sorts before the same version without one (e.g.
94 # "1.0.0-alpha" before "1.0.0").
95 10 ▶sub compare_parsed {
Subroutine compare_parsed: called CC=9 SCAR=22.5
Pod undocumented
96 10 my ($first, $other) = @_;
97 10 my $result = $first->{prefix} cmp $other->{prefix};
98 10 ▶ return $result if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 1 9 10
99 9 $result = _compare_core($first->{core}, $other->{core});
100 9 ▶ return $result if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 3 6 9
101 6 ▶ return 0 if !defined $first->{pre} && !defined $other->{pre};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $first->{"pre"} or defined $other->{"pre"} 2 4 6
Condition66%2/3
defined $first->{"pre"} or defined $other->{"pre"}
l!l&&r!l&&!r
402
MC/DC50%1/2
defined $first->{"pre"} or defined $other->{"pre"}
defined $first->{"pre"} defined $other->{"pre"}
Truth table66%2/3
A or B
A= defined $first->{"pre"}B= defined $other->{"pre"}
ABresult
1X1
011
000
102 4 ▶ return -1 if defined $first->{pre} && !defined $other->{pre};
Branch: 1 branch
BranchTrueFalseTotal
if defined $first->{"pre"} and not defined $other->{"pre"} 2 2 4
Condition66%2/3
defined $first->{"pre"} and not defined $other->{"pre"}
!ll&&!rl&&r
022
MC/DC50%1/2
defined $first->{"pre"} and not defined $other->{"pre"}
defined $first->{"pre"} not defined $other->{"pre"}
Truth table66%2/3
A and not(B)
A= defined $first->{"pre"}B= not defined $other->{"pre"}
ABresult
0X0
100
111
103 2 ▶ return 1 if !defined $first->{pre} && defined $other->{pre};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $first->{"pre"} and defined $other->{"pre"} 0 2 2
Condition33%1/3
not defined $first->{"pre"} and defined $other->{"pre"}
!ll&&!rl&&r
200
MC/DC0%0/2
not defined $first->{"pre"} and defined $other->{"pre"}
not defined $first->{"pre"} defined $other->{"pre"}
Truth table33%1/3
not(A) and B
A= not defined $first->{"pre"}B= defined $other->{"pre"}
ABresult
0X0
100
111
104 2 return _compare_pre_release($first->{pre}, $other->{pre});
105 }
106  
107 # Compares two version strings, parsing them with parse and comparing them with
108 # compare_parsed. Any parsing warning is silently ignored; callers that need to
109 # report them should use parse directly.
110 10 ▶sub compare {
Subroutine compare: called CC=1 SCAR=0.0
Pod undocumented
111 10 my ($first, $other) = @_;
112 10 return compare_parsed(parse($first), parse($other));
113 }
114  
115 3 1;
« PTP/Util.pm ↑ Summary