blib/lib/Lang/Go/Mod.pm

filter: Statementstmt 98.1% 104 / 106 Branchbran 87.5% 77 / 88 Conditioncond 41.6% 5 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 87.5% 197 / 225 CC 52 SCAR 34.0
CC
52
Cov
90%
parse_go_mod
41.4
_parse_retract
11.1
read_go_mod
3.6
CRAP 29.9
↑ Summary
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Brad Clawsie, 2023 -- brad.clawsie@gmail.com
5  
6 package Lang::Go::Mod;
7 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Path::Tiny qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # ABSTRACT: parse and model go.mod files
15  
16 3 our $VERSION = '0.006';
17 3 our $AUTHORITY = 'cpan:bclawsie';
18  
19 3 our @EXPORT_OK = qw(read_go_mod parse_go_mod _parse_retract);
20  
21 sub read_go_mod {
22 1 ▶ my $use_msg = 'use: read_go_mod(go_mod_path)';
Subroutine read_go_mod: called CC=3 SCAR=12.7
Pod documented
23 1 ▶ my $go_mod_path = shift || croak $use_msg;
Condition33%1/3
shift() || croak($use_msg)
l!l&&r!l&&!r
100
MC/DC0%0/2
shift() || croak($use_msg)
shift() croak($use_msg)
Truth table33%1/3
A || B
A= shift()B= croak($use_msg)
ABresult
1X1
011
000
24  
25 1 ▶ my $go_mod_content = path($go_mod_path)->slurp_utf8 || croak "$ERRNO";
Condition33%1/3
path($go_mod_path)->slurp_utf8 || croak("$Lang::Go::Mod::ERRNO")
l!l&&r!l&&!r
100
MC/DC0%0/2
path($go_mod_path)->slurp_utf8 || croak("$Lang::Go::Mod::ERRNO")
path($go_mod_path)->slurp_utf8 croak("$Lang::Go::Mod::ERRNO")
Truth table33%1/3
A || B
A= path($go_mod_path)->slurp_utf8B= croak("$Lang::Go::Mod::ERRNO")
ABresult
1X1
011
000
26  
27 1 return parse_go_mod($go_mod_content);
28 }
29  
30 sub parse_go_mod {
31 13 ▶ my $go_mod_content = shift || croak 'use: parse_go_mod(go_mod_content)';
Subroutine parse_go_mod: called CC=40 SCAR=37.2
Pod documented
Condition33%1/3
shift() || croak('use: parse_go_mod(go_mod_content)')
l!l&&r!l&&!r
1300
MC/DC0%0/2
shift() || croak('use: parse_go_mod(go_mod_content)')
shift() croak('use: parse_go_mod(go_mod_content)')
Truth table33%1/3
A || B
A= shift()B= croak('use: parse_go_mod(go_mod_content)')
ABresult
1X1
011
000
32  
33 13 my $m = {};
34 13 for my $k ( 'exclude', 'replace', 'require', 'retracts' ) {
35 52 $m->{$k} = {};
36 }
37 13 my ( $excludes, $replaces, $requires, $retracts ) = ( 0, 0, 0, 0 );
38  
39 13 LINE: for my $line ( split /\n/x, $go_mod_content ) {
40 77 ▶ next LINE if ( $line =~ /^\s*$/x );
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/x 17 60 77
41 60 ▶ if ($excludes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($excludes) 3 57 60
42 3 ▶ if ( $line =~ /^\s*[)]\s*$/x ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^\s*[)]\s*$/x) { } 1 2 3
elsif ($line =~ /\s*(\S+)\s+(\S+)/x) { } 1 1 2
43 1 $excludes = 0;
44 }
45 elsif ( $line =~ /\s*(\S+)\s+(\S+)/x ) {
46 1 ▶ $m->{exclude}->{$1} = [] unless ( defined $m->{exclude}->{$1} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $m->{"exclude"}{$1} 1 0 1
47 1 push @{ $m->{exclude}->{$1} }, $2;
48 }
49 else {
50 1 croak "malformed exclude line $line";
51 }
52 2 next LINE;
53 }
54 57 ▶ if ($replaces) {
Branch: 1 branch
BranchTrueFalseTotal
if ($replaces) 4 53 57
55 4 ▶ if ( $line =~ /^\s*[)]\s*$/x ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^\s*[)]\s*$/x) { } 1 3 4
elsif ($line =~ /^\s*(\S+)\s+=>\s+(\S+)\s*$/x) { } 2 1 3
56 1 $replaces = 0;
57 }
58 elsif ( $line =~ /^\s*(\S+)\s+=>\s+(\S+)\s*$/x ) {
59 croak "duplicate replace for $1"
60 2 ▶ if ( defined $m->{replace}->{$1} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $m->{"replace"}{$1} 0 2 2
61 2 $m->{replace}->{$1} = $2;
62 }
63 else {
64 1 croak "malformed replace line $line";
65 }
66 3 next LINE;
67 }
68 53 ▶ if ($requires) {
Branch: 1 branch
BranchTrueFalseTotal
if ($requires) 5 48 53
69 5 ▶ if ( $line =~ /^\s*[)]\s*$/x ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^\s*[)]\s*$/x) { } 1 4 5
elsif ($line =~ /^\s*(\S+)\s+(\S+).*$/x) { } 3 1 4
70 1 $requires = 0;
71 }
72 elsif ( $line =~ /^\s*(\S+)\s+(\S+).*$/x ) {
73 croak "duplicate require for $1"
74 3 ▶ if ( defined $m->{'require'}->{$1} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $m->{"require"}{$1} 0 3 3
75 3 $m->{'require'}->{$1} = $2;
76 }
77 else {
78 1 croak "malformed require line $line";
79 }
80 4 next LINE;
81 }
82 48 ▶ if ($retracts) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retracts) 3 45 48
83 3 ▶ if ( $line =~ /^\s*[)]\s*$/x ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^\s*[)]\s*$/x) { } 1 2 3
elsif ($line =~ /^\s*(\S+)(.*)$/x) { } 2 0 2
84 1 $retracts = 0;
85 }
86 elsif ( $line =~ /^\s*(\S+)(.*)$/x ) {
87 2 my $retract = _parse_retract( $1 . $2 );
88 2 ▶ croak "unparseable retract content: $line"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $retract 0 2 2
89 unless ( defined($retract) );
90 croak "duplicate retract for $retract"
91 2 ▶ if ( defined $m->{retract}->{$retract} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $m->{"retract"}{$retract} 0 2 2
92 2 $m->{retract}->{$retract} = 1;
93 }
94 else {
95 0 croak "malformed retract line $line";
96 }
97 3 next LINE;
98 }
99  
100 # single-line directives
101 45 ▶ if ( $line =~ /^module\s+(\S+)$/x ) {
Branch: 11 branches
BranchTrueFalseTotal
if ($line =~ /^module\s+(\S+)$/x) { } 12 33 45
elsif ($line =~ /^go\s+(\S+)$/x) { } 12 21 33
elsif ($line =~ /^exclude\s+[(]\s*$/x) { } 2 19 21
elsif ($line =~ /^replace\s+[(]\s*$/x) { } 2 17 19
elsif ($line =~ /^require\s+[(]\s*$/x) { } 2 15 17
elsif ($line =~ /^retract\s+[(]\s*$/x) { } 1 14 15
elsif ($line =~ /^exclude\s+(\S+)\s+(\S+)\s*$/x) { } 3 11 14
elsif ($line =~ /^replace\s+(\S+)\s+=>\s+(\S+)\s*$/x) { } 1 10 11
elsif ($line =~ /^require\s+(\S+)+\s+(\S+).*$/x) { } 1 9 10
elsif ($line =~ /^retract\s+(.+)/x) { } 2 7 9
elsif ($line =~ m[^\s*//.*$]mx) { } 1 6 7
102 12 $m->{module} = $1;
103 }
104 elsif ( $line =~ /^go\s+(\S+)$/x ) {
105 12 $m->{go} = $1;
106 }
107  
108 # multi-line directive
109 elsif ( $line =~ /^exclude\s+[(]\s*$/x ) {
110  
111 # toggle beginning of exclude block (and negate the other block checks)
112 2 ( $excludes, $replaces, $requires, $retracts ) = ( 1, 0, 0, 0 );
113 }
114 elsif ( $line =~ /^replace\s+[(]\s*$/x ) {
115  
116 # toggle beginning of replace block (and negate the other block checks)
117 2 ( $excludes, $replaces, $requires, $retracts ) = ( 0, 1, 0, 0 );
118 }
119 elsif ( $line =~ /^require\s+[(]\s*$/x ) {
120  
121 # toggle beginning of require block (and negate the other block checks)
122 2 ( $excludes, $replaces, $requires, $retracts ) = ( 0, 0, 1, 0 );
123 }
124 elsif ( $line =~ /^retract\s+[(]\s*$/x ) {
125  
126 # toggle beginning of retract block (and negate the other block checks)
127 1 ( $excludes, $replaces, $requires, $retracts ) = ( 0, 0, 0, 1 );
128 }
129  
130 # single-line forms of multi-line directives
131 elsif ( $line =~ /^exclude\s+(\S+)\s+(\S+)\s*$/x ) {
132  
133 # single exclude
134 3 ▶ $m->{$1} = [] unless ( defined $m->{exclude}->{$1} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $m->{"exclude"}{$1} 2 1 3
135 3 push @{ $m->{exclude}->{$1} }, $2;
136 }
137 elsif ( $line =~ /^replace\s+(\S+)\s+=>\s+(\S+)\s*$/x ) {
138  
139 # single replace
140 croak "duplicate replace for $1"
141 1 ▶ if ( defined $m->{replace}->{$1} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $m->{"replace"}{$1} 0 1 1
142 1 $m->{replace}->{$1} = $2;
143 }
144 elsif ( $line =~ /^require\s+(\S+)+\s+(\S+).*$/x ) {
145  
146 # single require
147 croak "duplicate require for $1"
148 1 ▶ if ( defined $m->{'require'}->{$1} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $m->{"require"}{$1} 0 1 1
149 1 $m->{'require'}->{$1} = $2;
150 }
151 elsif ( $line =~ /^retract\s+(.+)/x ) {
152  
153 # single retract
154 2 my $retract = _parse_retract($1);
155 2 ▶ croak "unparseable retract content: $line"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $retract 0 2 2
156 unless ( defined($retract) );
157 croak "duplicate retract for $retract"
158 2 ▶ if ( defined $m->{retract}->{$retract} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $m->{"retract"}{$retract} 0 2 2
159 2 $m->{retract}->{$retract} = 1;
160 }
161 elsif ( $line =~ m{^\s*//.*$}mx ) {
162  
163 # comment - strip
164 # (can also be part of a multi-line retract rationale)
165  
166 }
167 else {
168 6 croak "unparseable line content: $line";
169 }
170 }
171  
172 4 ▶ croak 'missing module line' unless ( defined $m->{module} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $m->{"module"} 1 3 4
173 3 ▶ croak 'missing go line' unless ( defined $m->{go} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $m->{"go"} 1 2 3
174  
175 2 return $m;
176 }
177  
178 # 'private' sub to extract individual retract lines and strip off the rationale comments
179 # see: https://go.dev/ref/mod#go-mod-file-retract
180 #
181 # rationale comments are stripped out
182 #
183 # this sub should only see one line; if a retract rational had multiple lines, like:
184 # retract v1.0.0 // why
185 # // oh why
186 #
187 # then the second comment line is caught by the comment match in the loop of parse_go_mod
188 sub _parse_retract {
189 23 ▶ my $retract = shift || croak 'missing retract string';
Subroutine _parse_retract: called CC=11 SCAR=24.0
Condition66%2/3
shift() || croak('missing retract string')
l!l&&r!l&&!r
2210
MC/DC0%0/2
shift() || croak('missing retract string')
shift() croak('missing retract string')
Truth table33%1/3
A || B
A= shift()B= croak('missing retract string')
ABresult
1X1
011
000
190  
191 22 ▶ if ( $retract =~ /^\s*\[(.+?)\](.*)$/x ) { # version-range
Branch: 2 branches
BranchTrueFalseTotal
if ($retract =~ /^\s*\[(.+?)\](.*)$/x) { } 11 11 22
elsif ($retract =~ /^\s*(\S+)(.*)$/x) { } 11 0 11
192 11 my $range = $1;
193 11 my $rest = $2;
194  
195 # trim whitespace from range
196 11 $range =~ s/\s+//gx;
197 11 my @versions = split( /,/x, $range );
198 11 my $count = 0;
199 11 for my $version (@versions) {
200 20 ▶ return undef unless ( $version =~ /\S+/x );
Branch: 1 branch
BranchTrueFalseTotal
unless $version =~ /\S+/x 1 19 20
201 19 $count++;
202 }
203 10 ▶ return undef if ( $count != 2 );
Branch: 1 branch
BranchTrueFalseTotal
if $count != 2 3 7 10
204  
205 # if there is a comment, it must be properly formatted
206 7 ▶ if ( $rest =~ /\S/x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /\S/x) 5 2 7
207 5 ▶ return undef unless ( $rest =~ m{^\s+//}ox );
Branch: 1 branch
BranchTrueFalseTotal
unless $rest =~ m[^\s+//]ox 2 3 5
208 }
209 5 return '[' . $range . ']';
210 }
211 elsif ( $retract =~ /^\s*(\S+)(.*)$/x ) { # single version
212 11 my $version = $1;
213 11 my $rest = $2;
214  
215 # if there is a comment, it must be properly formatted
216 11 ▶ if ( $rest =~ /\S/x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /\S/x) 9 2 11
217 9 ▶ return undef unless ( $rest =~ m{^\s+//}ox );
Branch: 1 branch
BranchTrueFalseTotal
unless $rest =~ m[^\s+//]ox 6 3 9
218 }
219 5 return $version;
220 }
221  
222 # unparseable retract string
223 0 return undef;
224 }
225  
226 3 1;
227  
228 __END__
↑ Summary