lib/App/optex/Config.pm

filter: Statementstmt 96.4% 108 / 112 Branchbran 67.5% 27 / 40 Conditioncond 54.5% 24 / 44 MC/DCmcdc 23.0% 6 / 26 Subroutinesub 100% 17 / 17 Podpod 100% 1 / 1 totaltotal 76.2% 183 / 240 CC 42 SCAR 20.3
CC
42
Cov
81%
_expand_include
13.7
_merge
11.0
_load_with_include
10.4
CRAP 7.6
« blib/lib/App/optex/util.pm ↑ Summary blib/lib/App/optex/Tmpfile.pm »
1 package App::optex::Config;
2 48 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 48 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 48 ▶use TOML ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 48 ▶use File::Glob qw(bsd_glob);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 48 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 48 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 48 ▶use Cwd qw(abs_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 48 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # load_config($path, ...)
12 48 ▶sub load_config {
Subroutine load_config: called CC=6 SCAR=18.4
Pod documented
13 48 my (@files) = @_;
14 48 ▶ die "load_config requires file\n" unless @files;
Branch: 1 branch
BranchTrueFalseTotal
unless @files 0 48 48
15  
16 48 my $merged = {};
17 48 for my $file (@files) {
18 49 ▶ die "load_config requires file\n" unless defined $file && length $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file and length $file 0 49 49
Condition33%1/3
defined $file and length $file
!ll&&!rl&&r
0049
MC/DC0%0/2
defined $file and length $file
defined $file length $file
Truth table33%1/3
A and B
A= defined $fileB= length $file
ABresult
0X0
100
111
19  
20 49 my $path = _maybe_config_path($file);
21 49 ▶ next unless $path;
Branch: 1 branch
BranchTrueFalseTotal
unless $path 30 19 49
22  
23 19 my %seen;
24 19 my $config = _load_with_include($path, \%seen, 0);
25 18 $merged = _merge($merged, $config);
26 }
27 47 return $merged;
28 }
29  
30 29 ▶sub _load_with_include {
Subroutine _load_with_include: called CC=10 SCAR=23.4
31 29 my ($file, $seen, $depth) = @_;
32 29 ▶ die "include depth too deep ($depth)\n" if $depth > 20;
Branch: 1 branch
BranchTrueFalseTotal
if $depth > 20 0 29 29
33  
34 29 my $path = _normalize_path($file);
35 29 ▶ die "config not found: $file\n" unless defined $path && -f $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and -f $path 0 29 29
Condition33%1/3
defined $path and -f $path
!ll&&!rl&&r
0029
MC/DC0%0/2
defined $path and -f $path
defined $path -f $path
Truth table33%1/3
A and B
A= defined $pathB= -f $path
ABresult
0X0
100
111
36  
37 29 ▶ if ($seen->{$path}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen->{$path}) 1 28 29
38 1 die "include loop detected: $path\n";
39 }
40 28 $seen->{$path} = 1;
41  
42 28 ▶ my $base = _toml_load($path) || {};
Condition50%1/2
_toml_load($path) || {}
l!l
280
MC/DC0%0/1
_toml_load($path) || {}
_toml_load($path)
Truth table50%1/2
A
A= _toml_load($path)
Aresult
11
00
43  
44 28 my $inc = delete $base->{include};
45 28 my $merged = {};
46  
47 28 ▶ if ($inc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inc) 10 18 28
48 10 my @files = _expand_include($inc, dirname($path));
49 10 for my $f (@files) {
50 13 my $child_path = _normalize_path($f);
51 13 ▶ next if !defined $child_path || $child_path eq $path; # skip self include
Branch: 1 branch
BranchTrueFalseTotal
if not defined $child_path or $child_path eq $path 3 10 13
Condition66%2/3
not defined $child_path or $child_path eq $path
l!l&&r!l&&!r
0310
MC/DC50%1/2
not defined $child_path or $child_path eq $path
not defined $child_path $child_path eq $path
Truth table66%2/3
not(A) or B
A= not defined $child_pathB= $child_path eq $path
ABresult
1X1
011
000
52 10 my $child = _load_with_include($child_path, $seen, $depth + 1);
53 7 $merged = _merge($merged, $child);
54 }
55 }
56 25 $merged = _merge($merged, $base);
57 25 delete $seen->{$path};
58 25 return $merged;
59 }
60  
61 28 ▶sub _toml_load {
Subroutine _toml_load: called CC=2 SCAR=7.0
62 28 my ($path) = @_;
63 28 my $txt = _slurp($path);
64 28 my ($data, $err) = TOML::from_toml($txt);
65 28 ▶ die "$path: $err\n" unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 28 28
66 28 return $data;
67 }
68  
69 28 ▶sub _slurp {
Subroutine _slurp: called CC=2 SCAR=7.0
70 28 my ($path) = @_;
71 28 ▶ open my $fh, '<:encoding(UTF-8)', $path or die "open $path: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(UTF-8)", $path or die "open ${path}: $!" 28 0 28
72 28 local $/;
73 28 my $txt = <$fh>;
74 28 close $fh;
75 28 return $txt;
76 }
77  
78 10 ▶sub _expand_include {
Subroutine _expand_include: called CC=10 SCAR=26.2
79 10 my ($inc, $base_dir) = @_;
80 10 my @items;
81 10 ▶ my $t = reftype($inc) || '';
Condition50%1/2
builtin::reftype($inc) || ''
l!l
010
MC/DC0%0/1
builtin::reftype($inc) || ''
builtin::reftype($inc)
Truth table50%1/2
A
A= builtin::reftype($inc)
Aresult
11
00
82  
83 10 ▶ if (!$t) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $t) { } 10 0 10
elsif ($t eq 'ARRAY') { } 0 0 0
84 10 @items = ($inc);
85 }
86 elsif ($t eq 'ARRAY') {
87 0 @items = @$inc;
88 }
89 else {
90 0 die "include must be string or array\n";
91 }
92  
93 10 my @files;
94 10 for my $item (@items) {
95 10 ▶ next unless defined $item && length $item;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $item and length $item 0 10 10
Condition33%1/3
defined $item and length $item
!ll&&!rl&&r
0010
MC/DC0%0/2
defined $item and length $item
defined $item length $item
Truth table33%1/3
A and B
A= defined $itemB= length $item
ABresult
0X0
100
111
96 10 $item = _expand_tilde($item);
97 10 ▶ if ($base_dir && !File::Spec->file_name_is_absolute($item)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base_dir and not "File::Spec"->file_name_is_absolute($item)) 3 7 10
Condition66%2/3
$base_dir and not "File::Spec"->file_name_is_absolute($item)
!ll&&!rl&&r
073
MC/DC50%1/2
$base_dir and not "File::Spec"->file_name_is_absolute($item)
$base_dir not "File::Spec"->file_name_is_absolute($item)
Truth table66%2/3
A and not(B)
A= $base_dirB= not "File::Spec"->file_name_is_absolute($item)
ABresult
0X0
100
111
98 3 $item = File::Spec->rel2abs($item, $base_dir);
99 }
100  
101 10 my @g = bsd_glob($item, File::Glob::GLOB_TILDE | File::Glob::GLOB_NOSORT);
102 10 ▶ if (@g) {
Branch: 1 branch
BranchTrueFalseTotal
if (@g) { } 10 0 10
103 10 push @files, sort @g;
104 }
105 else {
106 0 push @files, $item;
107 }
108 }
109 10 return @files;
110 }
111  
112 101 ▶sub _expand_tilde {
Subroutine _expand_tilde: called CC=4 SCAR=14.5
113 101 my ($path) = @_;
114 101 ▶ return $path unless $path =~ /^~/;
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ /^~/ 97 4 101
115 4 ▶ my $home = $ENV{HOME} || (getpwuid($<))[7] || '';
Condition40%2/5
$ENV{'HOME'} || (getpwuid $<)[7] || ''
l!l
40
$ENV{"HOME"} or (getpwuid $<)[7]
l!l&&r!l&&!r
400
MC/DC0%0/2
$ENV{'HOME'} || (getpwuid $<)[7] || ''
$ENV{"HOME"} (getpwuid $<)[7]
Truth table33%1/3
A or B
A= $ENV{"HOME"}B= (getpwuid $<)[7]
ABresult
1X1
011
000
116 4 $path =~ s/^~/$home/;
117 4 return $path;
118 }
119  
120 91 ▶sub _normalize_path {
Subroutine _normalize_path: called CC=3 SCAR=11.3
121 91 my ($path) = @_;
122 91 $path = _expand_tilde($path);
123 91 ▶ my $abs = File::Spec->file_name_is_absolute($path)
Branch: 1 branch
BranchTrueFalseTotal
'File::Spec'->file_name_is_absolute($path) ? : 91 0 91
124 ? $path
125 : File::Spec->rel2abs($path);
126 91 ▶ return abs_path($abs) || $abs;
Condition66%2/3
abs_path($abs) || $abs
l!l&&r!l&&!r
61300
MC/DC0%0/2
abs_path($abs) || $abs
abs_path($abs) $abs
Truth table66%2/3
A || B
A= abs_path($abs)B= $abs
ABresult
1X1
011
000
127 }
128  
129 49 ▶sub _maybe_config_path {
Subroutine _maybe_config_path: called CC=3 SCAR=11.0
130 49 my ($path) = @_;
131 49 my $norm = _normalize_path($path);
132 49 ▶ return unless defined $norm && -f $norm;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $norm and -f $norm 30 19 49
Condition66%2/3
defined $norm and -f $norm
!ll&&!rl&&r
03019
MC/DC50%1/2
defined $norm and -f $norm
defined $norm -f $norm
Truth table66%2/3
A and B
A= defined $normB= -f $norm
ABresult
0X0
100
111
133 19 return $norm;
134 }
135  
136 55 ▶sub _merge {
Subroutine _merge: called CC=10 SCAR=24.0
137 55 my ($a, $b) = @_;
138 55 ▶ $a ||= {};
Condition50%1/2
$a ||= {}
l!l
550
MC/DC0%0/1
$a ||= {}
$a
Truth table50%1/2
A
A= $a
Aresult
11
00
139 55 ▶ $b ||= {};
Condition50%1/2
$b ||= {}
l!l
550
MC/DC0%0/1
$b ||= {}
$b
Truth table50%1/2
A
A= $b
Aresult
11
00
140 55 my %out = %$a;
141  
142 55 for my $k (keys %$b) {
143 71 my $va = $out{$k};
144 71 my $vb = $b->{$k};
145 71 ▶ my $ta = reftype($va) || '';
Condition100%2/2
builtin::reftype($va) || ''
l!l
566
MC/DC100%1/1
builtin::reftype($va) || ''
builtin::reftype($va)
Truth table100%2/2
A
A= builtin::reftype($va)
Aresult
11
00
146 71 ▶ my $tb = reftype($vb) || '';
Condition100%2/2
builtin::reftype($vb) || ''
l!l
656
MC/DC100%1/1
builtin::reftype($vb) || ''
builtin::reftype($vb)
Truth table100%2/2
A
A= builtin::reftype($vb)
Aresult
11
00
147  
148 71 ▶ if ($ta eq 'HASH' && $tb eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ta eq 'HASH' and $tb eq 'HASH') { } 5 66 71
elsif ($ta eq 'ARRAY' and $tb eq 'ARRAY') { } 0 66 66
Condition66%2/3
$ta eq "HASH" and $tb eq "HASH"
!ll&&!rl&&r
6605
MC/DC50%1/2
$ta eq "HASH" and $tb eq "HASH"
$ta eq "HASH" $tb eq "HASH"
Truth table66%2/3
A and B
A= $ta eq "HASH"B= $tb eq "HASH"
ABresult
0X0
100
111
149 5 $out{$k} = _merge($va, $vb);
150 }
151 ▶ elsif ($ta eq 'ARRAY' && $tb eq 'ARRAY') {
Condition33%1/3
$ta eq "ARRAY" and $tb eq "ARRAY"
!ll&&!rl&&r
6600
MC/DC0%0/2
$ta eq "ARRAY" and $tb eq "ARRAY"
$ta eq "ARRAY" $tb eq "ARRAY"
Truth table33%1/3
A and B
A= $ta eq "ARRAY"B= $tb eq "ARRAY"
ABresult
0X0
100
111
152 0 $out{$k} = [ @$va, @$vb ];
153 }
154 else {
155 66 $out{$k} = $vb;
156 }
157 }
158 55 return \%out;
159 }
160  
161 48 1;
162  
163 =pod
164  
165 =encoding utf8
166  
167 =head1 NAME
168  
169 App::optex::Config - load optex config with recursive include support
170  
171 =head1 SYNOPSIS
172  
173 use App::optex::Config;
174  
175 my $config = App::optex::Config::load_config("$ENV{HOME}/.optex.d/config.toml");
176  
177 =head1 FUNCTIONS
178  
179 =head2 load_config
180  
181 my $config = App::optex::Config::load_config($path, ...);
182  
183 Loads a TOML config file and expands any top-level C<include> directives.
184 Returns a hashref of the merged configuration.
185  
186 =head1 DESCRIPTION
187  
188 =cut
189  
« blib/lib/App/optex/util.pm ↑ Summary blib/lib/App/optex/Tmpfile.pm »