Pad/Config.pm

filter: Statementstmt 99.4% 170 / 171 Branchbran 93.1% 41 / 44 Conditioncond 89.4% 17 / 19 MC/DCmcdc 83.3% 10 / 12 Subroutinesub 100% 19 / 19 Podpod 0.0% 0 / 11 totaltotal 93.1% 257 / 276 CC 36 SCAR 14.2
CC
36
Cov
97%
autoloadValues
6.0
commandValues
6.0
groupValues
6.0
CRAP 4.2
« Pad/Help.pm ↑ Summary Pad/Type/Path.pm »
1 11 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 11 ▶use Object::Pad;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 11 ▶use Getopt::Pad::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 class Getopt::Pad::Config :strict(params) {
7 11 ▶ use Fcntl qw(O_WRONLY O_CREAT O_EXCL);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶ use Feature::Compat::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶ use Getopt::Pad::Util qw(expandTilde);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 11 our $VERSION = '0.06';
12  
13 11 field $format :param;
14 11 field $formatName :param;
15 11 field $paths :param;
16 11 field $defaultPath :param;
17 11 field $autoload :param;
18  
19 # The key of a Level's section that holds its command sections.
20 11 ▶ 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 11 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 11 }
34  
35 # Later files override earlier ones option by option, on every Level.
36 45 ▶ method autoloadValues($root) {
Subroutine autoloadValues: called CC=6 SCAR=17.9
Pod undocumented
37 45 ▶ return {} if !$autoload;
Branch: 1 branch
BranchTrueFalseTotal
unless $autoload 1 44 45
38  
39 44 my %merged;
40 44 foreach my $path ($paths->@*) {
41 48 ▶ next if !-e expandTilde($path);
Branch: 1 branch
BranchTrueFalseTotal
unless -e expandTilde($path) 1 47 48
42 47 my $loaded = $self->loadFile($root, $path, 0);
43 31 ▶ $merged{$_} = { ($merged{$_} // {})->%*, $loaded->{$_}->%* } foreach keys $loaded->%*;
Condition100%2/2
$merged{$_} // {}
l!l
538
MC/DC100%1/1
$merged{$_} // {}
$merged{$_}
Truth table100%2/2
A
A= $merged{$_}
Aresult
11
00
44 }
45 28 return \%merged;
46 11 }
47  
48 58 ▶ method loadFile($root, $path, $isExplicit) {
Subroutine loadFile: called CC=4 SCAR=13.9
Pod undocumented
49 58 my $expanded = expandTilde($path);
50 58 ▶ 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 57 58
Condition100%3/3
$isExplicit and not -e $expanded
!ll&&!rl&&r
47101
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 57 my $text = $self->readText($path, $expanded);
53 57 my $data;
54 57 try {
55 57 $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 56 ▶ 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 55 56
65 55 return $self->levelValues($root, $path, $data);
66 11 }
67  
68 57 ▶ method readText($path, $expanded) {
Subroutine readText: called CC=3 SCAR=11.1
Pod undocumented
69 57 ▶ 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, $!) 57 0 57
70 57 local $/;
71 57 my $text = readline($handle);
72 57 close $handle;
73 57 ▶ return $text // '';
Condition50%1/2
$text // ""
l!l
570
MC/DC0%0/1
$text // ""
$text
Truth table50%1/2
A
A= $text
Aresult
11
00
74 11 }
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 8 ▶ method createFile($path, $expanded, $text) {
Subroutine createFile: called CC=5 SCAR=16.2
Pod undocumented
82 8 ▶ Getopt::Pad::Error->throw("config file '%s' already exists", $path) if -l $expanded;
Branch: 1 branch
BranchTrueFalseTotal
if -l $expanded 1 7 8
83 7 ▶ 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, $!) } 6 1 7
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 6 binmode $handle, ":raw$fileLayer";
88 6 print {$handle} $text;
89 6 ▶ 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, $!) 6 0 6
90 6 return;
91 11 }
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 81 ▶ method levelValues($level, $path, $section) {
Subroutine levelValues: called CC=3 SCAR=11.0
Pod undocumented
99 81 my %groups = $section->%*;
100 81 ▶ my $hasCommandsKey = $level->hasCommands && exists $groups{+COMMANDS_KEY};
Condition100%3/3
$level->hasCommands && exists $groups{'commands'}
!ll&&!rl&&r
49230
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 81 ▶ my %valuesBelow = $hasCommandsKey ? $self->commandValues($level, $path, delete $groups{+COMMANDS_KEY})->%* : ();
Branch: 1 branch
BranchTrueFalseTotal
$hasCommandsKey ? : 30 51 81
102 69 my %values = map { $self->groupValues($level, $path, $_, $groups{$_})->%* } sort keys %groups;
103 59 return { %valuesBelow, $level->path => \%values };
104 11 }
105  
106 30 ▶ method commandValues($level, $path, $sections) {
Subroutine commandValues: called CC=6 SCAR=17.9
Pod undocumented
107 30 ▶ 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 29 30
108  
109 29 my %valuesBelow;
110 29 foreach my $name (sort keys $sections->%*) {
111 29 my $command = $level->command($name);
112 29 ▶ if (!defined $command) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $command) 2 27 29
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 27 my $section = $sections->{$name};
118 27 ▶ 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 26 27
119 26 %valuesBelow = (%valuesBelow, $self->levelValues($command, $path, $section)->%*);
120 }
121 18 return \%valuesBelow;
122 11 }
123  
124 51 ▶ method groupValues($level, $path, $group, $entries) {
Subroutine groupValues: called CC=6 SCAR=17.9
Pod undocumented
125 51 my $ofCommand = $self->ofCommand($level);
126 51 ▶ 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 49 51
127  
128 49 foreach my $name (sort keys $entries->%*) {
129 61 my $option = $level->optionByName($name);
130 61 ▶ 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 6 55 61
Condition100%3/3
not defined $option or $option->auto
l!l&&r!l&&!r
4255
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 55 ▶ 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 53 55
132 }
133 41 return { $entries->%* };
134 11 }
135  
136 52 ▶ method ofCommand($level) {
Subroutine ofCommand: called CC=2 SCAR=6.9
Pod undocumented
137 52 ▶ return $level->isRoot ? '' : sprintf(" of command '%s'", $level->path);
Branch: 1 branch
BranchTrueFalseTotal
$level->isRoot ? : 32 20 52
138 11 }
139  
140 9 ▶ method writeDefaultFile($root, $target) {
Subroutine writeDefaultFile: called CC=2 SCAR=6.9
Pod undocumented
141 9 ▶ 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 8 9
142  
143 # Serialize first, so a failing dump leaves no empty file behind.
144 8 my $text = $format->dump($self->defaultSection($root));
145 8 $self->createFile($target, expandTilde($target), $text);
146 6 return $target;
147 11 }
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 # An undefined default means no default: written as null, the program
152 # would reject the file it wrote.
153 11 ▶ method defaultSection($level) {
Subroutine defaultSection: called CC=5 SCAR=16.1
Pod undocumented
154 11 my %section;
155 11 ▶ $section{$_->group}{$_->name} = $_->presentedDefault foreach grep { $_->hasDefault && defined $_->presentedDefault } $level->declaredOptions;
Condition100%3/3
$_->hasDefault && defined $_->presentedDefault
!ll&&!rl&&r
2019
MC/DC100%2/2
$_->hasDefault && defined $_->presentedDefault
$_->hasDefault defined $_->presentedDefault
Truth table100%3/3
A && B
A= $_->hasDefaultB= defined $_->presentedDefault
ABresult
0X0
100
111
156 11 foreach my $name ($level->commandNames) {
157 3 my $commandSection = $self->defaultSection($level->command($name));
158 3 ▶ $section{+COMMANDS_KEY}{$name} = $commandSection if %$commandSection;
Branch: 1 branch
BranchTrueFalseTotal
if %$commandSection 2 1 3
159 }
160 11 return \%section;
161 11 }
162 }
163  
164 11 1;
165  
166 __END__
« Pad/Help.pm ↑ Summary Pad/Type/Path.pm »