blib/lib/Lang/Go/Mod.pm

filter: Statementstmt 100% 73 / 73 Branchbran 90.3% 47 / 52 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 88.6% 133 / 150 CC 31 SCAR 31.3
CC
31
Cov
92%
parse_go_mod
29.3
read_go_mod
3.6
BEGIN
1.0
CRAP 22.8
↑ 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, 2021 -- brad.clawsie@gmail.com
5  
6 package Lang::Go::Mod;
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Path::Tiny qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # ABSTRACT: parse and model go.mod files
15  
16 2 our $VERSION = '0.001';
17 2 our $AUTHORITY = 'cpan:bclawsie';
18  
19 2 our @EXPORT_OK = qw(read_go_mod parse_go_mod);
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=29 SCAR=33.8
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 $m->{exclude} = {};
35 13 $m->{replace} = {};
36 13 $m->{'require'} = {};
37 13 my ( $excludes, $replaces, $requires ) = ( 0, 0, 0 );
38  
39 13 LINE: for my $line ( split /\n/msx, $go_mod_content ) {
40 67 ▶ next LINE if ( $line =~ /^\s*$/msx );
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/msx 14 53 67
41 53 ▶ if ($excludes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($excludes) 3 50 53
42 3 ▶ if ( $line =~ /^\s*[)]\s*$/msx ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^\s*[)]\s*$/msx) { } 1 2 3
elsif ($line =~ /\s*(\S+)\s+(\S+)/msx) { } 1 1 2
43 1 $excludes = 0;
44 }
45 elsif ( $line =~ /\s*(\S+)\s+(\S+)/msx ) {
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 50 ▶ if ($replaces) {
Branch: 1 branch
BranchTrueFalseTotal
if ($replaces) 4 46 50
55 4 ▶ if ( $line =~ /^\s*[)]\s*$/msx ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^\s*[)]\s*$/msx) { } 1 3 4
elsif ($line =~ /^\s*(\S+)\s+=>\s+(\S+)\s*$/msx) { } 2 1 3
56 1 $replaces = 0;
57 }
58 elsif ( $line =~ /^\s*(\S+)\s+=>\s+(\S+)\s*$/msx ) {
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 46 ▶ if ($requires) {
Branch: 1 branch
BranchTrueFalseTotal
if ($requires) 5 41 46
69 5 ▶ if ( $line =~ /^\s*[)]\s*$/msx ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^\s*[)]\s*$/msx) { } 1 4 5
elsif ($line =~ /^\s*(\S+)\s+(\S+).*$/msx) { } 3 1 4
70 1 $requires = 0;
71 }
72 elsif ( $line =~ /^\s*(\S+)\s+(\S+).*$/msx ) {
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  
83 41 ▶ if ( $line =~ /^module\s+(\S+)$/msx ) {
Branch: 8 branches
BranchTrueFalseTotal
if ($line =~ /^module\s+(\S+)$/msx) { } 12 29 41
elsif ($line =~ /^go\s+(\S+)$/msx) { } 12 17 29
elsif ($line =~ /^exclude\s+[(]\s*$/msx) { } 2 15 17
elsif ($line =~ /^replace\s+[(]\s*$/msx) { } 2 13 15
elsif ($line =~ /^require\s+[(]\s*$/msx) { } 2 11 13
elsif ($line =~ /^exclude\s+(\S+)\s+(\S+)\s*$/msx) { } 3 8 11
elsif ($line =~ /^replace\s+(\S+)\s+=>\s+(\S+)\s*$/msx) { } 1 7 8
elsif ($line =~ /^require\s+(\S+)+\s+(\S+).*$/msx) { } 1 6 7
84 12 $m->{module} = $1;
85 }
86 elsif ( $line =~ /^go\s+(\S+)$/msx ) {
87 12 $m->{go} = $1;
88 }
89 elsif ( $line =~ /^exclude\s+[(]\s*$/msx ) {
90  
91 # beginning of exclude block
92 2 $excludes = 1;
93 }
94 elsif ( $line =~ /^replace\s+[(]\s*$/msx ) {
95  
96 # beginning of replace block
97 2 $replaces = 1;
98 }
99 elsif ( $line =~ /^require\s+[(]\s*$/msx ) {
100  
101 # beginning of require block
102 2 $requires = 1;
103 }
104 elsif ( $line =~ /^exclude\s+(\S+)\s+(\S+)\s*$/msx ) {
105  
106 # single exclude
107 3 ▶ $m->{$1} = [] unless ( defined $m->{exclude}->{$1} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $m->{"exclude"}{$1} 2 1 3
108 3 push @{ $m->{exclude}->{$1} }, $2;
109 }
110 elsif ( $line =~ /^replace\s+(\S+)\s+=>\s+(\S+)\s*$/msx ) {
111  
112 # single replace
113 croak "duplicate replace for $1"
114 1 ▶ if ( defined $m->{replace}->{$1} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $m->{"replace"}{$1} 0 1 1
115 1 $m->{replace}->{$1} = $2;
116 }
117 elsif ( $line =~ /^require\s+(\S+)+\s+(\S+).*$/msx ) {
118  
119 # single require
120 croak "duplicate require for $1"
121 1 ▶ if ( defined $m->{'require'}->{$1} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $m->{"require"}{$1} 0 1 1
122 1 $m->{'require'}->{$1} = $2;
123 }
124 else {
125 6 croak "unknown line content: $line";
126 }
127 }
128  
129 4 ▶ croak 'missing module line' unless ( defined $m->{module} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $m->{"module"} 1 3 4
130 3 ▶ croak 'missing go line' unless ( defined $m->{go} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $m->{"go"} 1 2 3
131  
132 2 return $m;
133 }
134  
135 2 1;
136  
137 __END__
↑ Summary