blib/arch/Panda/Config/Perl.pm

filter: Statementstmt 95.0% 114 / 120 Branchbran 75.9% 41 / 54 Conditioncond 57.1% 8 / 14 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 100% 13 / 13 Podpod 100% 1 / 1 totaltotal 85.6% 179 / 209 CC 37 SCAR 22.1
CC
37
Cov
87%
_get_config
18.1
_process_file
8.0
_process_directive
7.0
CRAP 9.1
↑ Summary Perl.xs »
1 package Panda::Config::Perl;
2 8 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use Data::Recursive(); # XS code needs xs::merge
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 8 our $VERSION = '1.1.8';
6  
7 8 XS::Loader::load();
8  
9 8 ▶sub process {
Subroutine process: called CC=3 SCAR=11.1
Pod documented
10 8 my ($class, $file, $initial_cfg) = @_;
11  
12 8 my ($mstash, $nsstash);
13 {
14 8 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 $mstash = \%{"::"};
16 8 delete $mstash->{'NS::'};
17 8 $nsstash = \%{"NS::"};
18 }
19  
20 8 ▶ _apply_initial_cfg('', Data::Recursive::clone($initial_cfg)) if $initial_cfg;
Branch: 1 branch
BranchTrueFalseTotal
if $initial_cfg 4 4 8
21 8 _process_file($file);
22  
23 6 my $ret = {};
24  
25 6 my $cfg = {};
26 6 ▶ if(defined $nsstash->{'__CONFIG__'}){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $nsstash->{"__CONFIG__"}) 0 6 6
27 0 $cfg = ${$nsstash->{'__CONFIG__'}};
28 0 delete $nsstash->{'__CONFIG__'};
29 }
30 6 _get_config($ret, $nsstash, $cfg, '');
31  
32 # remove garbage we've created
33 5 delete $mstash->{'NS::'};
34  
35 5 return $ret;
36 }
37  
38 4 ▶sub _apply_initial_cfg {
Subroutine _apply_initial_cfg: called CC=3 SCAR=11.2
39 4 my ($ns, $cfg) = @_;
40 4 foreach my $key (keys %$cfg) {
41 8 ▶ if (substr($key, -2, 2) eq '::') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($key, -2, 2) eq '::') { } 0 8 8
42 0 _apply_initial_cfg($ns.$key, $cfg->{$key});
43 } else {
44 8 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
45 8 *{"NS::$ns$key"} = \$cfg->{$key};
46 }
47 }
48 }
49  
50 14 ▶sub _process_file {
Subroutine _process_file: called CC=8 SCAR=20.8
51 14 my ($file, $ns) = @_;
52 14 ▶ open my $fh, '<', $file or die "Panda::Config::Perl: cannot open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "Panda::Config::Perl: cannot open ${file}: $!" 13 1 14
53 13 my $content;
54 {
55 13 local $/ = undef;
56 13 $content = <$fh>;
57 }
58 13 close $fh;
59  
60 13 my $curdir = '';
61 13 ▶ if ((my $pos = rindex($file, '/')) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $pos = rindex($file, "/")) >= 0) 13 0 13
62 13 $curdir = substr($file, 0, $pos);
63 }
64  
65 13 $content =~ s/^[^\S\r\n]*#(namespace|namespace-abs|include)(?:[^\S\r\n]+(.+))?$/_process_directive($curdir, $ns, $1, $2)/gme;
66  
67 13 ▶ my $pkg = $ns ? "NS::$ns" : "NS";
Branch: 1 branch
BranchTrueFalseTotal
$ns ? : 6 7 13
68 13 $content = "package $pkg; sub { $content;\n }";
69 13 my $ok;
70 {
71 8 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 13 enable_op_tracking();
73 13 my $sub = eval $content;
74 13 disable_op_tracking();
75 13 ▶ $ok = eval { $sub->(); 1 } if $sub;
Branch: 1 branch
BranchTrueFalseTotal
if $sub 12 1 13
76 }
77 13 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 12 13
78 1 my $err = $@;
79 1 ▶ die $err if $err =~ /Error-prone code/;
Branch: 1 branch
BranchTrueFalseTotal
if $err =~ /Error-prone code/u 0 1 1
80 1 ▶ $err =~ s/Panda::Config::Perl: //g unless ref $err;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $err 1 0 1
81 1 die "Panda::Config::Perl: error while processing config $file: $err\n".
82 "================ Error-prone code ================\n".
83 _content_linno($content).
84 "==================================================";
85 }
86  
87 12 return;
88 }
89  
90 24 ▶sub _process_directive {
Subroutine _process_directive: called CC=7 SCAR=19.5
91 24 my ($curdir, $ns, $directive, $rest) = @_;
92 24 $rest =~ s/\s+$//;
93 24 ▶ $rest //= '';
Condition100%2/2
$rest //= ""
l!l
222
MC/DC100%1/1
$rest //= ""
$rest
Truth table100%2/2
A
A= $rest
Aresult
11
00
94 24 ▶ if (index($directive, 'namespace') == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (index($directive, 'namespace') == 0) { } 19 5 24
elsif ($directive eq 'include') { } 5 0 5
95 19 ▶ $ns = '' if $directive eq 'namespace-abs';
Branch: 1 branch
BranchTrueFalseTotal
if $directive eq "namespace-abs" 2 17 19
96 19 ▶ my $pkg = $ns ? "NS::$ns" : 'NS';
Branch: 1 branch
BranchTrueFalseTotal
$ns ? : 4 15 19
97 19 ▶ $pkg .= "::$1" if $rest =~ /\s*(\S+)/;
Branch: 1 branch
BranchTrueFalseTotal
if $rest =~ /\s*(\S+)/u 17 2 19
98 19 return "package $pkg;";
99 }
100 elsif ($directive eq 'include') {
101 5 return "Panda::Config::Perl::_INCLUDE('$curdir', __PACKAGE__, $rest);";
102 }
103 }
104  
105 5 ▶sub _INCLUDE {
Subroutine _INCLUDE: called CC=6 SCAR=18.1
106 5 my ($dir, $curpkg, $file) = @_;
107 5 my $ns = '';
108 5 ▶ if ($curpkg ne 'NS') {
Branch: 1 branch
BranchTrueFalseTotal
if ($curpkg ne "NS") 5 0 5
109 5 $ns = $curpkg;
110 5 substr($ns, 0, 4, ''); # remove /^NS::/
111 }
112 5 ▶ unless (substr($file, 0, 1) eq '/') { $file = $dir ? "$dir/$file" : $file }
Branch: 2 branches
BranchTrueFalseTotal
unless (substr($file, 0, 1) eq "/") 5 0 5
$dir ? : 5 0 5
113 5 ▶ if (index($file, '*') >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($file, '*') != -1) { } 1 4 5
114 8 ▶ _process_file($_, $ns) for glob($file);
Subroutine BEGIN: called CC=1 SCAR=0.0
115 } else {
116 4 _process_file($file, $ns);
117 }
118 }
119  
120 23 ▶sub _get_config {
Subroutine _get_config: called CC=15 SCAR=29.0
121 23 my ($dest, $stash, $config, $ns) = @_;
122 23 my @ns_list;
123  
124 my $assign_proc;
125 23 ▶ $assign_proc = $config->{assign_proc} if defined $config->{assign_proc};
Branch: 1 branch
BranchTrueFalseTotal
if defined $config->{"assign_proc"} 0 23 23
126 23 foreach my $key (keys %$stash) {
127 91 ▶ next if $key eq 'BEGIN' or $key eq 'DESTROY' or $key eq 'AUTOLOAD' or index($key, '__ANON__') == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "BEGIN" or $key eq "DESTROY" or $key eq "AUTOLOAD" or index($key, "__ANON__") == 0 8 83 91
Condition44%4/9
$key eq "BEGIN" or $key eq "DESTROY" or $key eq "AUTOLOAD" or index($key, "__ANON__") == 0
l!l&&r!l&&!r
0883
$key eq "BEGIN" or $key eq "DESTROY" or $key eq "AUTOLOAD"
l!l&&r!l&&!r
0091
$key eq "BEGIN" or $key eq "DESTROY"
l!l&&r!l&&!r
0091
MC/DC25%1/4
$key eq "BEGIN" or $key eq "DESTROY" or $key eq "AUTOLOAD" or index($key, "__ANON__") == 0
$key eq "BEGIN" $key eq "DESTROY" $key eq "AUTOLOAD" index($key, "__ANON__") == 0
Truth table40%2/5
A or B or C or D
A= $key eq "BEGIN"B= $key eq "DESTROY"C= $key eq "AUTOLOAD"D= index($key, "__ANON__") == 0
ABCDresult
1XXX1
01XX1
001X1
00011
00000
128 83 ▶ if (substr($key, -2, 2) eq '::') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($key, -2, 2) eq "::") 18 65 83
129 18 push @ns_list, $key;
130 18 next;
131 }
132 65 ▶ my $glob = $stash->{$key} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $glob = $stash->{$key} or next 65 0 65
133 65 ▶ next if !defined $$glob and defined *$glob{CODE};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $$glob and defined *$glob{"CODE"} 0 65 65
Condition66%2/3
not defined $$glob and defined *$glob{"CODE"}
!ll&&!rl&&r
6410
MC/DC0%0/2
not defined $$glob and defined *$glob{"CODE"}
not defined $$glob defined *$glob{"CODE"}
Truth table66%2/3
not(A) and B
A= not defined $$globB= defined *$glob{"CODE"}
ABresult
0X0
100
111
134 65 ▶ if(defined $assign_proc){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $assign_proc) { } 0 65 65
135 0 $dest->{$key} = undef;
136 0 $assign_proc->($dest->{$key}, $$glob);
137 } else {
138 65 $dest->{$key} = $$glob;
139 }
140 }
141  
142 23 foreach my $subns (@ns_list) {
143 18 my $substash = \%{$stash->{$subns}};
144 18 substr($subns, -2, 2, '');
145 18 ▶ my $subns_full = $ns ? "${ns}::$subns" : $subns;
Branch: 1 branch
BranchTrueFalseTotal
$ns ? : 8 10 18
146 18 ▶ if (exists $dest->{$subns}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $dest->{$subns}) 1 17 18
147 1 die "Panda::Config::Perl: conflict between variable '$subns' in namespace '$ns' and a namespace '$subns_full'. ".
148 "You shouldn't have variables that overlap with namespaces as they would merge into the same hash.\n";
149 }
150 17 _get_config($dest->{$subns} = {}, $substash, $config, $subns_full);
151 }
152 }
153  
154 1 ▶sub _content_linno {
Subroutine _content_linno: called CC=1 SCAR=0.0
155 1 my $content = shift;
156 1 my $i = 0;
157 1 $content =~ s/^(.*)$/$i++; "$i: $1"/mge;
158 1 return $content;
159 }
160  
161 8 1;
↑ Summary Perl.xs »