Pad/Config.pm

filter: Statementstmt 99.4% 170 / 171 Branchbran 93.1% 41 / 44 Conditioncond 87.5% 14 / 16 MC/DCmcdc 80.0% 8 / 10 Subroutinesub 100% 19 / 19 Podpod 0.0% 0 / 11 totaltotal 92.9% 252 / 271 CC 35 SCAR 14.0
CC
35
Cov
97%
autoloadValues
6.0
commandValues
6.0
groupValues
6.0
CRAP 4.1
« Pad/Registry.pm ↑ Summary Pad/Type/Number.pm »
1 10 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 10 ▶use Object::Pad;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 10 ▶use Getopt::Pad::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 class Getopt::Pad::Config :strict(params) {
7 10 ▶ use Fcntl qw(O_WRONLY O_CREAT O_EXCL);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶ use Feature::Compat::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶ use Getopt::Pad::Util qw(expandTilde);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 10 our $VERSION = '0.05';
12  
13 10 field $format :param;
14 10 field $formatName :param;
15 10 field $paths :param;
16 10 field $defaultPath :param;
17 10 field $autoload :param;
18  
19 # The key of a Level's section that holds its command sections.
20 10 ▶ use constant COMMANDS_KEY => 'commands';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 # Config files are UTF-8 on disk. The layer is applied here, on every
23 # read and write, so a Format only ever sees text.
24 10 my $fileLayer = ':encoding(UTF-8)';
25  
26 # Every load returns the option values each Level of the Spec gets from
27 # config files, keyed by Level path (the root's is ''): a mapping of
28 # Primary names to raw values per Level.
29 12 ▶ method explicitValues($root, $rawPath) {
Subroutine explicitValues: called CC=4 SCAR=13.9
Pod undocumented
30 12 ▶ my $path = length $rawPath ? $rawPath : $defaultPath;
Branch: 1 branch
BranchTrueFalseTotal
length $rawPath ? : 10 2 12
31 12 ▶ Getopt::Pad::Error->throw("--config without a path, and the spec sets no defaultPath") if !defined $path || !length $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and length $path 1 11 12
Condition66%2/3
defined $path and length $path
!ll&&!rl&&r
1011
MC/DC50%1/2
defined $path and length $path
defined $path length $path
Truth table66%2/3
A and B
A= defined $pathB= length $path
ABresult
0X0
100
111
32 11 return $self->loadFile($root, $path, 1);
33 10 }
34  
35 # Later files override earlier ones option by option, on every Level.
36 36 ▶ method autoloadValues($root) {
Subroutine autoloadValues: called CC=6 SCAR=17.9
Pod undocumented
37 36 ▶ return {} if !$autoload;
Branch: 1 branch
BranchTrueFalseTotal
unless $autoload 1 35 36
38  
39 35 my %merged;
40 35 foreach my $path ($paths->@*) {
41 39 ▶ next if !-e expandTilde($path);
Branch: 1 branch
BranchTrueFalseTotal
unless -e expandTilde($path) 1 38 39
42 38 my $loaded = $self->loadFile($root, $path, 0);
43 23 ▶ $merged{$_} = { ($merged{$_} // {})->%*, $loaded->{$_}->%* } foreach keys $loaded->%*;
Condition100%2/2
$merged{$_} // {}
l!l
530
MC/DC100%1/1
$merged{$_} // {}
$merged{$_}
Truth table100%2/2
A
A= $merged{$_}
Aresult
11
00
44 }
45 20 return \%merged;
46 10 }
47  
48 49 ▶ method loadFile($root, $path, $isExplicit) {
Subroutine loadFile: called CC=4 SCAR=13.9
Pod undocumented
49 49 my $expanded = expandTilde($path);
50 49 ▶ Getopt::Pad::Error->throw("config file '%s' does not exist", $path) if $isExplicit && !-e $expanded;
Branch: 1 branch
BranchTrueFalseTotal
if $isExplicit and not -e $expanded 1 48 49
Condition100%3/3
$isExplicit and not -e $expanded
!ll&&!rl&&r
38101
MC/DC100%2/2
$isExplicit and not -e $expanded
$isExplicit not -e $expanded
Truth table100%3/3
A and not(B)
A= $isExplicitB= not -e $expanded
ABresult
0X0
100
111
51  
52 48 my $text = $self->readText($path, $expanded);
53 48 my $data;
54 48 try {
55 48 $data = $format->parse($text);
56 }
57 catch ($error) {
58 # The parser's message is for the end user; the Perl source
59 # location it ends with is not.
60 1 my $message = "$error" =~ s/ at \S+ line \d+\.?\s*\z//r =~ s/\s+\z//r;
61 1 Getopt::Pad::Error->throw("config file '%s': %s", $path, $message);
62 }
63  
64 47 ▶ Getopt::Pad::Error->throw("config file '%s' must contain a mapping of group names", $path) if ref $data ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $data ne "HASH" 1 46 47
65 46 return $self->levelValues($root, $path, $data);
66 10 }
67  
68 48 ▶ method readText($path, $expanded) {
Subroutine readText: called CC=3 SCAR=11.1
Pod undocumented
69 48 ▶ open my $handle, "<$fileLayer", $expanded or Getopt::Pad::Error->throw("cannot read config file '%s': %s", $path, $!);
Branch: 1 branch
BranchTrueFalseTotal
open my $handle, "<$fileLayer", $expanded or "Getopt::Pad::Error"->throw("cannot read config file '%s': %s", $path, $!) 48 0 48
70 48 local $/;
71 48 my $text = readline($handle);
72 48 close $handle;
73 48 ▶ return $text // '';
Condition50%1/2
$text // ""
l!l
480
MC/DC0%0/1
$text // ""
$text
Truth table50%1/2
A
A= $text
Aresult
11
00
74 10 }
75  
76 # Creates the file exclusively: an existing file, or a symlink to
77 # anywhere, is refused by the open itself, so nothing can slip in
78 # between a check and the creation. Windows follows a dangling symlink
79 # even then, so symlinks are refused up front as well. The file is
80 # written raw under the encoding layer: LF line endings everywhere.
81 7 ▶ method createFile($path, $expanded, $text) {
Subroutine createFile: called CC=5 SCAR=16.2
Pod undocumented
82 7 ▶ Getopt::Pad::Error->throw("config file '%s' already exists", $path) if -l $expanded;
Branch: 1 branch
BranchTrueFalseTotal
if -l $expanded 1 6 7
83 6 ▶ sysopen(my $handle, $expanded, O_WRONLY | O_CREAT | O_EXCL) or do {
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $handle, $expanded, 193 or do { "Getopt::Pad::Error"->throw("config file '%s' already exists", $path) if $!{"EEXIST"}; "Getopt::Pad::Error"->throw("cannot write config file '%s': %s", $path, $!) } 5 1 6
84 7 ▶ Getopt::Pad::Error->throw("config file '%s' already exists", $path) if $!{EEXIST};
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EEXIST"} 1 0 1
85 0 Getopt::Pad::Error->throw("cannot write config file '%s': %s", $path, $!);
86 };
87 5 binmode $handle, ":raw$fileLayer";
88 5 print {$handle} $text;
89 5 ▶ close $handle or Getopt::Pad::Error->throw("cannot write config file '%s': %s", $path, $!);
Branch: 1 branch
BranchTrueFalseTotal
close $handle or "Getopt::Pad::Error"->throw("cannot write config file '%s': %s", $path, $!) 5 0 5
90 5 return;
91 10 }
92  
93 # A config file mirrors the Level tree. Each key of a Level's section is
94 # a group name holding a mapping of option names, except the
95 # COMMANDS_KEY of a Level with commands: it holds one section per
96 # command name. Every section is validated, whether this run selects
97 # its Level or not; the values are checked by the Levels that are.
98 71 ▶ method levelValues($level, $path, $section) {
Subroutine levelValues: called CC=3 SCAR=11.0
Pod undocumented
99 71 my %groups = $section->%*;
100 71 ▶ my $hasCommandsKey = $level->hasCommands && exists $groups{+COMMANDS_KEY};
Condition100%3/3
$level->hasCommands && exists $groups{'commands'}
!ll&&!rl&&r
40229
MC/DC100%2/2
$level->hasCommands && exists $groups{'commands'}
$level->hasCommands exists $groups{'commands'}
Truth table100%3/3
A && B
A= $level->hasCommandsB= exists $groups{'commands'}
ABresult
0X0
100
111
101 71 ▶ my %valuesBelow = $hasCommandsKey ? $self->commandValues($level, $path, delete $groups{+COMMANDS_KEY})->%* : ();
Branch: 1 branch
BranchTrueFalseTotal
$hasCommandsKey ? : 29 42 71
102 60 my %values = map { $self->groupValues($level, $path, $_, $groups{$_})->%* } sort keys %groups;
103 51 return { %valuesBelow, $level->path => \%values };
104 10 }
105  
106 29 ▶ method commandValues($level, $path, $sections) {
Subroutine commandValues: called CC=6 SCAR=17.9
Pod undocumented
107 29 ▶ Getopt::Pad::Error->throw("config file '%s': '%s'%s must contain a mapping of command names", $path, COMMANDS_KEY, $self->ofCommand($level)) if ref $sections ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $sections ne "HASH" 1 28 29
108  
109 28 my %valuesBelow;
110 28 foreach my $name (sort keys $sections->%*) {
111 28 my $command = $level->command($name);
112 28 ▶ if (!defined $command) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $command) 2 26 28
113 2 ▶ my $wordPath = $level->isRoot ? $name : $level->path . ' ' . $name;
Branch: 1 branch
BranchTrueFalseTotal
$level->isRoot ? : 1 1 2
114 2 Getopt::Pad::Error->throw("config file '%s': unknown command '%s', expected one of: %s", $path, $wordPath, join(', ', $level->commandNames));
115 }
116  
117 26 my $section = $sections->{$name};
118 26 ▶ Getopt::Pad::Error->throw("config file '%s': command '%s' must contain a mapping of group names", $path, $command->path) if ref $section ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $section ne "HASH" 1 25 26
119 25 %valuesBelow = (%valuesBelow, $self->levelValues($command, $path, $section)->%*);
120 }
121 18 return \%valuesBelow;
122 10 }
123  
124 46 ▶ method groupValues($level, $path, $group, $entries) {
Subroutine groupValues: called CC=6 SCAR=17.9
Pod undocumented
125 46 my $ofCommand = $self->ofCommand($level);
126 46 ▶ Getopt::Pad::Error->throw("config file '%s': group '%s'%s must contain a mapping of option names", $path, $group, $ofCommand) if ref $entries ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $entries ne "HASH" 2 44 46
127  
128 44 foreach my $name (sort keys $entries->%*) {
129 53 my $option = $level->optionByName($name);
130 53 ▶ Getopt::Pad::Error->throw("config file '%s': unknown option '%s' in group '%s'%s", $path, $name, $group, $ofCommand) if !defined $option || $option->auto;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $option or $option->auto 5 48 53
Condition100%3/3
not defined $option or $option->auto
l!l&&r!l&&!r
3248
MC/DC100%2/2
not defined $option or $option->auto
not defined $option $option->auto
Truth table100%3/3
not(A) or B
A= not defined $optionB= $option->auto
ABresult
1X1
011
000
131 48 ▶ Getopt::Pad::Error->throw("config file '%s': option '%s'%s belongs to group '%s', not '%s'", $path, $name, $ofCommand, $option->group, $group) if $option->group ne $group;
Branch: 1 branch
BranchTrueFalseTotal
if $option->group ne $group 2 46 48
132 }
133 37 return { $entries->%* };
134 10 }
135  
136 47 ▶ method ofCommand($level) {
Subroutine ofCommand: called CC=2 SCAR=6.9
Pod undocumented
137 47 ▶ return $level->isRoot ? '' : sprintf(" of command '%s'", $level->path);
Branch: 1 branch
BranchTrueFalseTotal
$level->isRoot ? : 28 19 47
138 10 }
139  
140 8 ▶ method writeDefaultFile($root, $target) {
Subroutine writeDefaultFile: called CC=2 SCAR=6.9
Pod undocumented
141 8 ▶ Getopt::Pad::Error->throw("config format '%s' cannot write config files", $formatName) if !$format->can('dump');
Branch: 1 branch
BranchTrueFalseTotal
unless $format->can("dump") 1 7 8
142  
143 # Serialize first, so a failing dump leaves no empty file behind.
144 7 my $text = $format->dump($self->defaultSection($root));
145 7 $self->createFile($target, expandTilde($target), $text);
146 5 return $target;
147 10 }
148  
149 # The defaults of $level and every Level below it, in the config file
150 # structure. Groups and command sections without defaults are left out.
151 10 ▶ method defaultSection($level) {
Subroutine defaultSection: called CC=4 SCAR=13.9
Pod undocumented
152 10 my %section;
153 10 $section{$_->group}{$_->name} = $_->default foreach grep { $_->hasDefault } $level->declaredOptions;
154 10 foreach my $name ($level->commandNames) {
155 3 my $commandSection = $self->defaultSection($level->command($name));
156 3 ▶ $section{+COMMANDS_KEY}{$name} = $commandSection if %$commandSection;
Branch: 1 branch
BranchTrueFalseTotal
if %$commandSection 2 1 3
157 }
158 10 return \%section;
159 10 }
160 }
161  
162 10 1;
163  
164 __END__
« Pad/Registry.pm ↑ Summary Pad/Type/Number.pm »