lib/App/FuguBench/Deps.pm

filter: Statementstmt 8.0% 56 / 695 Branchbran 0.0% 0 / 374 Conditioncond 0.0% 0 / 88 MC/DCmcdc 0.0% 0 / 54 Subroutinesub 23.0% 18 / 78 Podpod 100% 1 / 1 totaltotal 5.8% 75 / 1290 CC 249 SCAR 45.1
CC
249
Cov
5%
_run
380.0
_bins
240.0
_manifest
210.0
CRAP 90.8
↑ Summary lib/App/FuguBench/Update.pm »
1 # ex:ts=8 sw=4:
2 # $OpenBSD$
3 #
4 # Copyright (c) 2026 Dick Olsson <hi@senzilla.io>
5 #
6 # Permission to use, copy, modify, and distribute this software for any
7 # purpose with or without fee is hereby granted, provided that the above
8 # copyright notice and this permission notice appear in all copies.
9 #
10 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  
18 package App::FuguBench::Deps;
19 117 our $VERSION = '0.1.1';
20  
21 117 ▶use v5.34;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 117 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 117 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 117 ▶no feature qw(indirect multidimensional bareword_filehandles);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 117 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27 117 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
28 117 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29 117 ▶use POSIX qw(uname);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 117 ▶use App::FuguBench::Fetch;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 117 ▶use Fugu::CLI qw(EXIT_SUCCESS EXIT_ERROR);
Subroutine BEGIN: called CC=1 SCAR=0.0
34 117 ▶use Fugu::Curl;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 117 ▶use Fugu::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 117 ▶use Fugu::Process;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 117 ▶use Fugu::Signify;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 # App::FuguBench::Deps - the deps verb.
40 #
41 # The verb reads the external tools, the Perl distributions, the CPAN
42 # modules, and the prebuilt binaries that deps/<OS>.txt names, and it
43 # installs each one. --dry-run prints the command of each install,
44 # and it runs none of them.
45 #
46 # The verb reads deps/<OS>.txt relative to the start directory, and
47 # it walks up to no checkout (CLI-CHECKOUT-5). A guest runs
48 # `make deps` out of an extracted tarball, and that tree holds no
49 # .toolingrc.
50 #
51 # The verb validates every line of the manifest before the first
52 # install, and not the lines of the wanted environment alone
53 # (DEPS-MANIFEST-3). A bad line of another environment would
54 # otherwise stay invisible until someone installs that environment.
55 #
56 # Every download goes through Fugu::Curl (DEPS-FETCH-1). Each
57 # download that a manifest names takes its check before anything
58 # reads it (DEPS-TIER-1), and the standalone cpanm script is the one
59 # download that no manifest names (DEPS-INSTALL-4). A recorded digest
60 # of deps/SHA256.txt comes first. Without one, the verb fetches the
61 # signed SHA256 manifest that sits beside the download, and
62 # Fugu::Signify verifies it in-process against the declared keys
63 # (DEPS-TIER-8).
64 #
65 # --dry-run prints each command and asks no network, so it reads the
66 # manifest, the digest file and the key set alone (DEPS-MANIFEST-6).
67 #
68 # Every other run installs. The trace names each command, and
69 # $app->command then runs it as a child: a package manager, cpanm,
70 # and the mkdir, tar, unzip, cp and chmod of a bin entry. No line of
71 # a child reaches standard output (CLI-PROGRAM-4), and a child that
72 # exits non-zero stops the run.
73  
74 # The environments of a manifest line (DEPS-MANIFEST-2).
75 117 ▶use constant ENVIRONMENTS => qw(tool runtime test develop);
Subroutine BEGIN: called CC=1 SCAR=0.0
76  
77 # The types of a manifest line, in the install order of
78 # DEPS-MANIFEST-4. A package can give the toolchain that a dist build
79 # needs, and a dist can give a module that the cpan list builds on. A
80 # binary depends on nothing here.
81 117 ▶use constant TYPES => qw(pkg dist cpan bin);
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83 # The standalone cpanm script. It is the one download that no
84 # manifest names, so it carries no check (DEPS-INSTALL-4).
85 117 ▶use constant CPANM_URL => 'https://cpanmin.us';
Subroutine BEGIN: called CC=1 SCAR=0.0
86  
87 # The asset spellings of one system name and one machine name, in
88 # preference order (DEPS-ALIAS-2). GitHub answers an asset path in
89 # any letter case, so macOS also covers macos. Two spellings of one
90 # name would name one download twice, and raise a false ambiguity.
91 117 my %OS_ALIAS = (
92 Darwin => [qw(darwin macOS osx)],
93 Linux => [qw(linux)],
94 OpenBSD => [qw(openbsd)],
95 );
96  
97 117 my %ARCH_ALIAS = (
98 x86_64 => [qw(amd64 x64 x86_64)],
99 amd64 => [qw(amd64 x64 x86_64)],
100 aarch64 => [qw(arm64 aarch64)],
101 arm64 => [qw(arm64 aarch64)],
102 );
103  
104 # The shape of a word that becomes a file name (DEPS-INSTALL-8): the
105 # command name of a bin entry, and the value of --os and --arch. The
106 # first character is a letter or a digit, so the word is neither an
107 # option of tar and unzip, nor a parent segment of a path.
108 117 my $FILE_NAME = qr{\A[A-Za-z0-9][A-Za-z0-9._-]*\z};
109  
110 # App::FuguBench::Deps->command($verb):
111 # The entry of the Fugu::CLI table. The module holds one verb,
112 # so it ignores the name.
113 117 ▶sub command ( $, $ )
Subroutine command: called CC=1 SCAR=1.2
Pod documented
114 {
115 return {
116 summary => 'install one dependency environment, or record'
117 . ' the digests',
118 usage => '[--dry-run] [--os <name>] [--arch <name>]'
119 . ' <tool|runtime|test|develop>'
120 . ' | --update-sums [--force] [--os <name>]'
121 . ' [--arch <name>]',
122 options => {
123 'dry-run' => 'print each command, and run none of them',
124 'update-sums' => 'record the digest of each download',
125 'force' => 'rewrite a recorded digest, and pin a'
126 . ' stable name',
127 'os=s' => 'the system name, in place of uname',
128 'arch=s' => 'the machine name, in place of uname',
129 },
130 0 ▶ run => sub ( $app, @argv ) { return _run( $app, @argv ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
131 117 };
132 }
133  
134 # _run($app, @argv):
135 # The body of the verb. The argument of an install is one
136 # environment word, and a refresh takes none. Every other
137 # command line is a usage error.
138 0 ▶sub _run ( $app, @argv )
Subroutine _run: not called CC=19 SCAR=59.4
139 {
140 0 my $cli = $app->cli;
141 0 my $log = $cli->log;
142 0 ▶ my $update = $cli->option('update-sums') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$cli->option('update-sums') ? : 0 0 0
143  
144 # A refresh reads every environment of one manifest, so it
145 # keeps no environment word. The install path keeps one.
146 0 my $env;
147 0 ▶ if ($update) {
Branch: 1 branch
BranchTrueFalseTotal
if ($update) { } 0 0 0
148 0 ▶ return $cli->command_usage_error('deps')
Branch: 1 branch
BranchTrueFalseTotal
unless _refresh_usage($app, @argv) 0 0 0
149 unless _refresh_usage( $app, @argv );
150 }
151 else {
152 0 $env = _install_usage( $app, @argv );
153 0 ▶ return $cli->command_usage_error('deps') unless defined $env;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $env 0 0 0
154 }
155  
156 0 ▶ my $os = $cli->option('os') // ( uname() )[0];
Condition0%0/3
$cli->option("os") // (uname())[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$cli->option("os") // (uname())[0]
$cli->option("os") (uname())[0]
Truth table0%0/3
A // B
A= $cli->option("os")B= (uname())[0]
ABresult
1X1
011
000
157 0 ▶ my $arch = $cli->option('arch') // ( uname() )[4];
Condition0%0/3
$cli->option("arch") // (uname())[4]
l!l&&r!l&&!r
000
MC/DC0%0/2
$cli->option("arch") // (uname())[4]
$cli->option("arch") (uname())[4]
Truth table0%0/3
A // B
A= $cli->option("arch")B= (uname())[4]
ABresult
1X1
011
000
158  
159 # Both words reach a file name: the system name names the
160 # manifest, and each one names a download of the alias
161 # expansion.
162 0 for my $word ( [ os => $os ], [ arch => $arch ] ) {
163 0 ▶ next if $word->[1] =~ $FILE_NAME;
Branch: 1 branch
BranchTrueFalseTotal
if $word->[1] =~ /$FILE_NAME/u 0 0 0
164 0 $log->error(
165 q{the %s word '%s' holds letters, digits, a dot, a}
166 . ' dash, and an underscore',
167 @$word
168 );
169 0 return $cli->command_usage_error('deps');
170 }
171  
172 0 my $dir = File::Spec->catdir( $app->start, 'deps' );
173 0 my $manifest = File::Spec->catfile( $dir, "$os.txt" );
174 0 ▶ unless ( -f $manifest ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $manifest) 0 0 0
175 0 $log->notice( 'no dependencies for %s', $os );
176 0 return EXIT_SUCCESS;
177 }
178  
179 0 my $by_type = _manifest( $app, $manifest, $env );
180 0 ▶ return Fugu::CLI::EXIT_CONFIG_ERROR() unless $by_type;
Branch: 1 branch
BranchTrueFalseTotal
unless $by_type 0 0 0
181  
182 # A file-name key stops an install (DEPS-TIER-5), and a
183 # refresh drops the one that a recorded URL replaces
184 # (DEPS-SUMS-11).
185 0 my $file = File::Spec->catfile( $dir, 'SHA256.txt' );
186 0 ▶ my $digests =
Branch: 1 branch
BranchTrueFalseTotal
$update ? : 0 0 0
187 $update ? _recorded( $app, $file ) : _digests( $app, $file );
188 0 ▶ return Fugu::CLI::EXIT_CONFIG_ERROR() unless $digests;
Branch: 1 branch
BranchTrueFalseTotal
unless $digests 0 0 0
189  
190 0 my $keys = _keys( $app, $dir );
191 0 ▶ return Fugu::CLI::EXIT_CONFIG_ERROR() unless $keys;
Branch: 1 branch
BranchTrueFalseTotal
unless $keys 0 0 0
192  
193 0 ▶ my $ctx = {
Branch: 2 branches
BranchTrueFalseTotal
$cli->option('dry-run') ? : 0 0 0
$cli->option('force') ? : 0 0 0
194 app => $app,
195 os => $os,
196 arch => $arch,
197 digests => $digests,
198 keys => $keys,
199 dry => $cli->option('dry-run') ? 1 : 0,
200 force => $cli->option('force') ? 1 : 0,
201 hold => [],
202 };
203  
204 # The dry-run trace is standard output, and each line of a
205 # diagnostic is standard error, which Fugu::Log holds
206 # unbuffered. In a pipe that joins the two, a buffered trace
207 # line would land after the diagnostic that follows it.
208 0 STDOUT->autoflush(1);
209  
210 0 ▶ return _update_sums( $ctx, $file, $by_type ) if $update;
Branch: 1 branch
BranchTrueFalseTotal
if $update 0 0 0
211  
212 0 my $code = _install( $ctx, $by_type );
213 0 ▶ return $code unless $code == EXIT_SUCCESS;
Branch: 1 branch
BranchTrueFalseTotal
unless $code == 0 0 0 0
214  
215 # The one result line of the verb (DEPS-INSTALL-10). A dry run
216 # installs nothing, so its trace is the whole standard output.
217 0 ▶ say "installed the dependencies of $env" unless $ctx->{dry};
Branch: 1 branch
BranchTrueFalseTotal
unless $ctx->{"dry"} 0 0 0
218  
219 0 return EXIT_SUCCESS;
220 }
221  
222 # _refresh_usage($app, @argv):
223 # 1 for a command line that a refresh takes, and 0 for every
224 # other one, which the method reports (DEPS-SUMS-13).
225 #
226 # A refresh reads every environment of one manifest, so it takes
227 # no environment word. It writes the digest file, so it takes no
228 # --dry-run.
229 0 ▶sub _refresh_usage ( $app, @argv )
Subroutine _refresh_usage: not called CC=3 SCAR=24.8
230 {
231 0 my $cli = $app->cli;
232 0 my $log = $cli->log;
233  
234 0 ▶ if (@argv) {
Branch: 1 branch
BranchTrueFalseTotal
if (@argv) 0 0 0
235 0 $log->error( '--update-sums reads every environment of the'
236 . ' manifest, so it takes no environment word' );
237 0 return 0;
238 }
239 0 ▶ if ( $cli->option('dry-run') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cli->option("dry-run")) 0 0 0
240 0 $log->error( '--update-sums writes the digest file, so it'
241 . ' takes no --dry-run' );
242 0 return 0;
243 }
244  
245 0 return 1;
246 }
247  
248 # _install_usage($app, @argv):
249 # The environment word of an install, or undef for a command
250 # line that no install takes, which the method reports.
251 #
252 # --force belongs to --update-sums (DEPS-SUMS-13). An install
253 # that took it would write nothing, and the operator would read
254 # no word about the option that they gave.
255 0 ▶sub _install_usage ( $app, @argv )
Subroutine _install_usage: not called CC=4 SCAR=30.0
256 {
257 0 my $cli = $app->cli;
258 0 my $log = $cli->log;
259  
260 0 ▶ if ( $cli->option('force') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cli->option("force")) 0 0 0
261 0 $log->error(q{--force belongs to '--update-sums'});
262 0 return;
263 }
264 0 ▶ return unless @argv == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @argv == 1 0 0 0
265  
266 0 my ($env) = @argv;
267 0 ▶ return $env if grep { $_ eq $env } ENVIRONMENTS;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $env;} ("tool", "runtime", "test", "develop") 0 0 0
268  
269 0 $log->error( q{unknown environment '%s': the environments are %s},
270 $env, join ', ', ENVIRONMENTS );
271  
272 0 return;
273 }
274  
275 # _install($ctx, $by_type):
276 # Take each type of one environment, in the order of
277 # DEPS-MANIFEST-4. The method returns the exit code of the run.
278 0 ▶sub _install ( $ctx, $by_type )
Subroutine _install: not called CC=4 SCAR=30.0
279 {
280 0 my %handler = (
281 pkg => \&_packages,
282 dist => \&_dists,
283 cpan => \&_modules,
284 bin => \&_bins,
285 );
286  
287 0 for my $type (TYPES) {
288 0 my $entries = $by_type->{$type};
289 0 ▶ next unless @$entries;
Branch: 1 branch
BranchTrueFalseTotal
unless @$entries 0 0 0
290  
291 0 my $code = $handler{$type}->( $ctx, @$entries );
292 0 ▶ return $code if $code != EXIT_SUCCESS;
Branch: 1 branch
BranchTrueFalseTotal
if $code != 0 0 0 0
293 }
294  
295 0 return EXIT_SUCCESS;
296 }
297  
298 # _manifest($app, $file, $want):
299 # The entries of one manifest, as a hash of type to array
300 # (DEPS-MANIFEST-2). Whitespace separates the three fields of a
301 # line, and a # at the start of the first word starts a comment.
302 #
303 # The method validates every line, and it keeps the lines of the
304 # wanted environment (DEPS-MANIFEST-3). It reports the first bad
305 # line and returns undef.
306 0 ▶sub _manifest ( $app, $file, $want )
Subroutine _manifest: not called CC=14 SCAR=53.5
307 {
308 0 my $log = $app->cli->log;
309 0 my $text = Fugu::File->read($file);
310 0 ▶ unless ( defined $text ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $text) 0 0 0
311 0 $log->error( 'cannot read %s', $file );
312 0 return;
313 }
314  
315 0 my %by_type = map { $_ => [] } TYPES;
316 0 my $n = 0;
317 0 for my $line ( split /\n/, $text ) {
318 0 $n++;
319 0 my $where = "$file:$n";
320 0 my ( $env, $type, $name ) = split ' ', $line, 3;
321  
322 0 ▶ next unless defined $env;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $env 0 0 0
323 0 ▶ next if index( $env, '#' ) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($env, "#") == 0 0 0 0
324  
325 # The split with a limit keeps the tail of the line
326 # intact, so the trailing whitespace goes here.
327 0 ▶ $name =~ s/\s+\z// if defined $name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name 0 0 0
328  
329 0 ▶ if ( !defined $type || !defined $name || $name eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $type && defined $name or $name eq "") 0 0 0
Condition0%0/6
not defined $type && defined $name or $name eq ""
l!l&&r!l&&!r
000
defined $type && defined $name
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $type && defined $name or $name eq ""
defined $type defined $name $name eq ""
Truth table0%0/4
not(A && B) or C
A= defined $typeB= defined $nameC= $name eq ""
ABCresult
0XX1
10X1
1111
1100
330 0 $log->error(
331 '%s: a line holds an environment, a type, and'
332 . ' a name: %s',
333 $where, $line
334 );
335 0 return;
336 }
337  
338 # An unknown environment word makes a line invisible: it
339 # matches no wanted environment, and the run then claims
340 # success for an install that never ran.
341 0 ▶ unless ( grep { $_ eq $env } ENVIRONMENTS ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $env;} ("tool", "runtime", "test", "develop")) 0 0 0
342 0 $log->error(
343 q{%s: unknown environment '%s': the}
344 . ' environments are %s',
345 $where, $env, join ', ', ENVIRONMENTS
346 );
347 0 return;
348 }
349 0 ▶ unless ( exists $by_type{$type} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $by_type{$type}) 0 0 0
350 0 $log->error( q{%s: unknown type '%s': the types are %s},
351 $where, $type, join ', ', TYPES );
352 0 return;
353 }
354  
355 0 ▶ return unless _check_entry( $app, $type, $name, $where, $line );
Branch: 1 branch
BranchTrueFalseTotal
unless _check_entry($app, $type, $name, $where, $line) 0 0 0
356  
357 0 ▶ next if defined $want && $env ne $want;
Branch: 1 branch
BranchTrueFalseTotal
if defined $want and $env ne $want 0 0 0
Condition0%0/3
defined $want and $env ne $want
!ll&&!rl&&r
000
MC/DC0%0/2
defined $want and $env ne $want
defined $want $env ne $want
Truth table0%0/3
A and B
A= defined $wantB= $env ne $want
ABresult
0X0
100
111
358 0 push @{ $by_type{$type} }, $name;
359 }
360  
361 0 return \%by_type;
362 }
363  
364 # _check_entry($app, $type, $name, $where, $line):
365 # Hold the name of one manifest line to the shape of its type. A
366 # dist name is one URL, and a bin name holds the command name,
367 # the URL, and, for an archive, the path of the file in the
368 # archive (DEPS-MANIFEST-8). A pkg name and a cpan name reach a
369 # package manager, so neither reads as an option, and neither
370 # names a download that no tier covers (DEPS-MANIFEST-7).
371 0 ▶sub _check_entry ( $app, $type, $name, $where, $line )
Subroutine _check_entry: not called CC=13 SCAR=52.0
372 {
373 0 my $log = $app->cli->log;
374  
375 0 ▶ return _check_url( $app, $name, $where ) if $type eq 'dist';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "dist" 0 0 0
376  
377 0 ▶ if ( $type eq 'cpan' || $type eq 'pkg' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "cpan" or $type eq "pkg") 0 0 0
Condition0%0/3
$type eq "cpan" or $type eq "pkg"
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "cpan" or $type eq "pkg"
$type eq "cpan" $type eq "pkg"
Truth table0%0/3
A or B
A= $type eq "cpan"B= $type eq "pkg"
ABresult
1X1
011
000
378 0 ▶ if ( $name =~ /\A-/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /\A-/u) 0 0 0
379 0 $log->error( q{%s: a %s name must not start with a}
380 . ' dash: %s',
381 $where, $type, $name );
382 0 return 0;
383 }
384 0 ▶ if ( $name =~ m{\A[a-z][a-z0-9+.-]*://}i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ m[\A[a-z][a-z0-9+.-]*://]iu) 0 0 0
385 0 $log->error( '%s: a %s name must not be a URL: %s',
386 $where, $type, $name );
387 0 return 0;
388 }
389  
390 0 return 1;
391 }
392  
393 0 my ( $command, $url, $member ) = split ' ', $name, 3;
394 0 ▶ if ( !defined $url || $url eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $url or $url eq "") 0 0 0
Condition0%0/3
not defined $url or $url eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $url or $url eq ""
not defined $url $url eq ""
Truth table0%0/3
not(A) or B
A= not defined $urlB= $url eq ""
ABresult
1X1
011
000
395 0 $log->error(
396 '%s: a bin line holds the command name, the URL, and'
397 . ' the path in an archive: %s',
398 $where, $line
399 );
400 0 return 0;
401 }
402 0 ▶ if ( _archive_type($url) && !defined $member ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_archive_type($url) and not defined $member) 0 0 0
Condition0%0/3
_archive_type($url) and not defined $member
!ll&&!rl&&r
000
MC/DC0%0/2
_archive_type($url) and not defined $member
_archive_type($url) not defined $member
Truth table0%0/3
A and not(B)
A= _archive_type($url)B= not defined $member
ABresult
0X0
100
111
403 0 $log->error(
404 '%s: an archive URL needs the path of the file'
405 . ' in the archive: %s',
406 $where, $url
407 );
408 0 return 0;
409 }
410 0 ▶ if ( !_archive_type($url) && defined $member ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not _archive_type($url) and defined $member) 0 0 0
Condition0%0/3
not _archive_type($url) and defined $member
!ll&&!rl&&r
000
MC/DC0%0/2
not _archive_type($url) and defined $member
not _archive_type($url) defined $member
Truth table0%0/3
not(A) and B
A= not _archive_type($url)B= defined $member
ABresult
0X0
100
111
411 0 $log->error(
412 '%s: a path in the archive needs an archive URL:'
413 . ' %s',
414 $where, $url
415 );
416 0 return 0;
417 }
418  
419 0 ▶ return 0 unless _check_url( $app, $url, $where );
Branch: 1 branch
BranchTrueFalseTotal
unless _check_url($app, $url, $where) 0 0 0
420  
421 0 return _check_bin_names( $app, $command, $member, $where );
422 }
423  
424 # _check_url($app, $url, $where):
425 # Hold one download URL to the shapes that the downloader and
426 # the digest file allow.
427 #
428 # The scheme check is the shared one of the fetch verb
429 # (DEPS-FETCH-4), because both reach one downloader.
430 #
431 # The URL becomes a key of deps/SHA256.txt, whose line format
432 # reserves the parenthesis and the space (DEPS-TIER-12). It must
433 # also name a file, which becomes the name of the download in a
434 # temporary directory.
435 0 ▶sub _check_url ( $app, $url, $where )
Subroutine _check_url: not called CC=5 SCAR=34.0
436 {
437 0 my $log = $app->cli->log;
438 0 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
unless App::FuguBench::Fetch::check_url($app, $url, $where) 0 0 0
439 unless App::FuguBench::Fetch::check_url( $app, $url, $where );
440  
441 0 my ($file) = $url =~ m{([^/]+)\z};
442 0 ▶ if ( !defined $file || $file eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $file or $file eq "") 0 0 0
Condition0%0/3
not defined $file or $file eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $file or $file eq ""
not defined $file $file eq ""
Truth table0%0/3
not(A) or B
A= not defined $fileB= $file eq ""
ABresult
1X1
011
000
443 0 $log->error( '%s: the URL names no file: %s', $where, $url );
444 0 return 0;
445 }
446 0 ▶ if ( $url =~ /[()\s]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ /[()\s]/u) 0 0 0
447 0 $log->error(
448 '%s: the URL holds a parenthesis or a space, which the'
449 . ' line format of deps/SHA256.txt reserves: %s',
450 $where, $url
451 );
452 0 return 0;
453 }
454  
455 0 return 1;
456 }
457  
458 # _check_bin_names($app, $name, $member, $where):
459 # Hold the command name and the archive path of one bin entry to
460 # the shapes that a file name allows (DEPS-INSTALL-8). The
461 # command name becomes a file name in the install directory, and
462 # the archive path becomes one in a temporary directory. tar and
463 # unzip read a leading dash as an option, and one of those
464 # options runs a command.
465 0 ▶sub _check_bin_names ( $app, $name, $member, $where )
Subroutine _check_bin_names: not called CC=6 SCAR=37.4
466 {
467 0 my $log = $app->cli->log;
468 0 ▶ unless ( $name =~ $FILE_NAME ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name =~ /$FILE_NAME/u) 0 0 0
469 0 $log->error(
470 q{%s: the command name '%s' holds letters, digits, a}
471 . ' dot, a dash, and an underscore',
472 $where, $name
473 );
474 0 return 0;
475 }
476  
477 0 ▶ return 1 unless defined $member;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $member 0 0 0
478  
479 0 my @part = split m{/}, $member;
480 0 ▶ if ( grep { $_ eq q{..} || $_ eq q{} } @part ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq ".." or $_ eq "";} @part) 0 0 0
Condition0%0/3
$_ eq '..' || $_ eq ''
l!l&&r!l&&!r
000
MC/DC0%0/2
$_ eq '..' || $_ eq ''
$_ eq '..' $_ eq ''
Truth table0%0/3
A || B
A= $_ eq '..'B= $_ eq ''
ABresult
1X1
011
000
481 0 $log->error(
482 q{%s: the archive path '%s' must hold no empty and no}
483 . ' parent segment',
484 $where, $member
485 );
486 0 return 0;
487 }
488 0 ▶ if ( $member =~ /\A-/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($member =~ /\A-/u) 0 0 0
489 0 $log->error(
490 q{%s: the archive path '%s' must not start with}
491 . ' a dash',
492 $where, $member
493 );
494 0 return 0;
495 }
496  
497 0 return 1;
498 }
499  
500 # _signify():
501 # The one Fugu::Signify of the run. The perl engine parses the
502 # signify(1) formats itself and checks a signature with
503 # Fugu::Ed25519, so no host needs signify(1) (DEPS-TIER-8).
504 #
505 # The object holds no key set, and each verification names its
506 # own keys. It also parses the SHA256 manifest form, which both
507 # tiers read, and the public key file of a key line.
508 0 ▶sub _signify ()
Subroutine _signify: not called CC=1 SCAR=6.9
509 {
510 0 state $signify = Fugu::Signify->new( engine => 'perl' );
511  
512 0 return $signify;
513 }
514  
515 # _parse_digests($app, $file):
516 # The digest of each line of one SHA256 manifest, as a hash of
517 # key to digest, through the manifest reader of Fugu::Signify
518 # (DEPS-TIER-3). The reader rejects a bad line, a digest that is
519 # not 64 hexadecimal characters, a blank line, and a duplicate
520 # key (DEPS-TIER-4). A silent skip would drop a check.
521 #
522 # A file with no line gives the empty set. The method reports a
523 # file that it cannot read or parse, and returns undef.
524 0 ▶sub _parse_digests ( $app, $file )
Subroutine _parse_digests: not called CC=4 SCAR=30.0
525 {
526 0 my $log = $app->cli->log;
527 0 my $text = Fugu::File->read($file);
528 0 ▶ unless ( defined $text ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $text) 0 0 0
529 0 $log->error( 'cannot read %s', $file );
530 0 return;
531 }
532 0 ▶ return {} if $text =~ /\A\s*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\A\s*\z/u 0 0 0
533  
534 0 my $digests = _signify()->parse_manifest($text);
535 0 ▶ unless ($digests) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($digests) 0 0 0
536 0 $log->error( '%s: %s', $file, _signify()->error );
537 0 return;
538 }
539  
540 0 return $digests;
541 }
542  
543 # _digests($app, $file):
544 # The digest of each download that deps/SHA256.txt records, as a
545 # hash of URL to digest (DEPS-TIER-3). An absent file, and a
546 # file with no line, each give the empty set.
547 #
548 # Each key of this file holds a scheme, because the file gathers
549 # many upstreams and keys on the whole download URL. A key that
550 # is a file name comes from an older file (DEPS-TIER-5). It
551 # matches no download of this verb, so the entry would lose its
552 # pin without a word.
553 0 ▶sub _digests ( $app, $file )
Subroutine _digests: not called CC=4 SCAR=30.0
554 {
555 0 my $digests = _recorded( $app, $file );
556 0 ▶ return unless $digests;
Branch: 1 branch
BranchTrueFalseTotal
unless $digests 0 0 0
557  
558 0 my @legacy = _legacy($digests);
559 0 ▶ return $digests unless @legacy;
Branch: 1 branch
BranchTrueFalseTotal
unless @legacy 0 0 0
560  
561 0 my $log = $app->cli->log;
562 0 $log->error(
563 '%s keys on the file name, and the verb keys on the whole'
564 . ' download URL',
565 $file
566 );
567 0 $log->error( ' %s', $_ ) for @legacy;
568 0 $log->error( q{ run 'deps --update-sums' for each operating system}
569 . ' that deps/ holds a manifest for' );
570  
571 0 return;
572 }
573  
574 # _recorded($app, $file):
575 # The digest set of deps/SHA256.txt, with every key that the
576 # file holds. An absent file, and a file with no line, each give
577 # the empty set.
578 #
579 # A refresh reads the file through this method, because it drops
580 # a file-name key that it can replace, and keeps every other one
581 # (DEPS-SUMS-11). An install reads it through _digests, which
582 # stops on such a key.
583 0 ▶sub _recorded ( $app, $file )
Subroutine _recorded: not called CC=2 SCAR=17.9
584 {
585 0 ▶ return {} unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 0 0
586  
587 0 return _parse_digests( $app, $file );
588 }
589  
590 # _legacy($digests):
591 # Each key of a digest set that is a file name, in sorted order.
592 # Such a key comes from an older file, and the verb keys on the
593 # whole download URL (DEPS-TIER-5).
594 0 ▶sub _legacy ($digests)
Subroutine _legacy: not called CC=1 SCAR=6.9
595 {
596 0 return grep { !m{\A[a-z][a-z0-9+.-]*://}i } sort keys %$digests;
597 }
598  
599 # _keys($app, $dir):
600 # The declared signify keys, in the order that deps/KEYS.txt and
601 # then deps/KEYS.local.txt name them (DEPS-KEYS-1). The order is
602 # the trust order, and the current key comes first
603 # (DEPS-KEYS-3). An absent file carries no key, and an empty set
604 # is valid (DEPS-KEYS-5).
605 #
606 # A # at the start of a line starts a comment. The method
607 # reports the first bad line and returns undef.
608 0 ▶sub _keys ( $app, $dir )
Subroutine _keys: not called CC=8 SCAR=42.8
609 {
610 0 my $log = $app->cli->log;
611  
612 0 my ( @keys, %seen );
613 0 for my $name (qw(KEYS.txt KEYS.local.txt)) {
614 0 my $file = File::Spec->catfile( $dir, $name );
615 0 ▶ next unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 0 0
616  
617 0 my $text = Fugu::File->read($file);
618 0 ▶ unless ( defined $text ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $text) 0 0 0
619 0 $log->error( 'cannot read %s', $file );
620 0 return;
621 }
622  
623 0 my $n = 0;
624 0 for my $line ( split /\n/, $text ) {
625 0 $n++;
626 0 ▶ next if $line =~ /\A\s*(?:#|\z)/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A\s*(?:#|\z)/u 0 0 0
627  
628 0 my $where = "$file:$n";
629 0 my $key = _key( $app, [ split q{ }, $line ], $where );
630 0 ▶ return unless $key;
Branch: 1 branch
BranchTrueFalseTotal
unless $key 0 0 0
631  
632 0 ▶ if ( $seen{ $key->{name} }++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$key->{"name"}}++) 0 0 0
633 $log->error( '%s: duplicate key name: %s',
634 0 $where, $key->{name} );
635 0 return;
636 }
637 0 push @keys, $key;
638 }
639 }
640  
641 0 return \@keys;
642 }
643  
644 # _key($app, $field, $where):
645 # One key of a key line, as a hash with the name and either the
646 # key body or the URL and the digest (DEPS-KEYS-2). Two fields
647 # give the body form, and three give the URL form, whose digest
648 # is the trust anchor.
649 #
650 # The URL of the URL form reaches the downloader, so it takes
651 # the shape check of the fetch verb (DEPS-FETCH-4).
652 #
653 # The method reports a bad line and returns undef (DEPS-KEYS-4).
654 0 ▶sub _key ( $app, $field, $where )
Subroutine _key: not called CC=8 SCAR=42.8
655 {
656 0 my $log = $app->cli->log;
657  
658 # The name becomes a file name in a temporary directory, so it
659 # holds no path.
660 0 ▶ unless ( defined $field->[0] && $field->[0] =~ $FILE_NAME ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $field->[0] and $field->[0] =~ /$FILE_NAME/u) 0 0 0
Condition0%0/3
defined $field->[0] and $field->[0] =~ /$FILE_NAME/u
!ll&&!rl&&r
000
MC/DC0%0/2
defined $field->[0] and $field->[0] =~ /$FILE_NAME/u
defined $field->[0] $field->[0] =~ /$FILE_NAME/u
Truth table0%0/3
A and B
A= defined $field->[0]B= $field->[0] =~ /$FILE_NAME/u
ABresult
0X0
100
111
661 0 $log->error(
662 '%s: a key name holds letters, digits, a dot, a dash,'
663 . ' and an underscore',
664 $where
665 );
666 0 return;
667 }
668  
669 0 ▶ if ( @$field == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$field == 2) 0 0 0
670 0 my $key = { name => $field->[0], body => $field->[1] };
671  
672 # The body is the second line of a signify public key
673 # file, so the parser of Fugu::Signify holds it to the
674 # 42 bytes and the prefix of that form.
675 0 ▶ return $key if _signify()->parse_public_key( _key_text($key) );
Branch: 1 branch
BranchTrueFalseTotal
if _signify()->parse_public_key(_key_text($key)) 0 0 0
676  
677 0 $log->error( '%s: not a signify key body: %s',
678 $where, _signify()->error );
679 0 return;
680 }
681  
682 0 ▶ if ( @$field == 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$field == 3) 0 0 0
683 0 ▶ unless ( $field->[2] =~ /\A[0-9a-f]{64}\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($field->[2] =~ /\A[0-9a-f]{64}\z/u) 0 0 0
684 0 $log->error( '%s: not a sha256 digest: %s',
685 $where, $field->[2] );
686 0 return;
687 }
688  
689 return
690 0 ▶ unless App::FuguBench::Fetch::check_url( $app, $field->[1],
Branch: 1 branch
BranchTrueFalseTotal
unless App::FuguBench::Fetch::check_url($app, $field->[1], $where) 0 0 0
691 $where );
692  
693 return {
694 0 name => $field->[0],
695 url => $field->[1],
696 digest => $field->[2],
697 };
698 }
699  
700 0 $log->error( '%s: a key line holds two or three fields', $where );
701  
702 0 return;
703 }
704  
705 # _key_text($key):
706 # The two lines of a signify public key file. signify(1) reads
707 # the comment line and carries no trust in it, so one word of a
708 # key line holds the whole key.
709 0 ▶sub _key_text ($key)
Subroutine _key_text: not called CC=1 SCAR=6.9
710 {
711 0 return "untrusted comment: $key->{name} public key\n$key->{body}\n";
712 }
713  
714 # _packages($ctx, @pkgs):
715 # Give the package list to the package manager of the platform
716 # (DEPS-INSTALL-1). The verb gives every package of the
717 # environment in one command.
718 0 ▶sub _packages ( $ctx, @pkgs )
Subroutine _packages: not called CC=8 SCAR=42.8
719 {
720 0 my $os = $ctx->{os};
721 0 _note( $ctx, 'the OS packages: %s', join q{ }, @pkgs );
722  
723 0 ▶ if ( $os eq 'OpenBSD' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($os eq 'OpenBSD') { } 0 0 0
elsif ($os eq 'Linux') { } 0 0 0
elsif ($os eq 'Darwin') { } 0 0 0
724 0 ▶ return EXIT_ERROR
Branch: 1 branch
BranchTrueFalseTotal
unless _command($ctx, "pkg_add", @pkgs) 0 0 0
725 unless _command( $ctx, 'pkg_add', @pkgs );
726 }
727 elsif ( $os eq 'Linux' ) {
728 0 ▶ return EXIT_ERROR
Branch: 1 branch
BranchTrueFalseTotal
unless _command($ctx, "sudo", "apt-get", "update") 0 0 0
729 unless _command( $ctx, 'sudo', 'apt-get', 'update' );
730 0 ▶ return EXIT_ERROR
Branch: 1 branch
BranchTrueFalseTotal
unless _command($ctx, "sudo", "apt-get", "install", "-y", @pkgs) 0 0 0
731 unless _command( $ctx, 'sudo', 'apt-get', 'install', '-y',
732 @pkgs );
733 }
734 elsif ( $os eq 'Darwin' ) {
735 0 ▶ return EXIT_ERROR
Branch: 1 branch
BranchTrueFalseTotal
unless _command($ctx, "brew", "install", @pkgs) 0 0 0
736 unless _command( $ctx, 'brew', 'install', @pkgs );
737 }
738 else {
739 $ctx->{app}
740 0 ->cli->log->error( 'no package manager for %s', $os );
741 0 return EXIT_ERROR;
742 }
743  
744 0 return EXIT_SUCCESS;
745 }
746  
747 # _dists($ctx, @urls):
748 # Fetch each distribution tarball and give it to cpanm
749 # (DEPS-INSTALL-5). Every entry resolves, and takes its tier
750 # check, before the first fetch, so a set with one entry that no
751 # tier covers installs nothing (DEPS-INSTALL-9).
752 #
753 # The digest of an entry takes its check in the install loop, at
754 # the download of that entry. A mismatch there leaves an earlier
755 # entry of the set installed (DEPS-TIER-2).
756 0 ▶sub _dists ( $ctx, @urls )
Subroutine _dists: not called CC=8 SCAR=42.8
757 {
758 0 _note( $ctx, 'the distributions: %s', join q{ }, @urls );
759  
760 0 my @resolved;
761 0 for my $url (@urls) {
762 0 my ($value) = _resolve( $ctx, $url, undef );
763 0 ▶ return Fugu::CLI::EXIT_CONFIG_ERROR() unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
764  
765 # An entry that no tier covers stops the run before the
766 # first download (DEPS-INSTALL-9).
767 0 ▶ return EXIT_ERROR unless _check_tier( $ctx, $value );
Branch: 1 branch
BranchTrueFalseTotal
unless _check_tier($ctx, $value) 0 0 0
768 0 push @resolved, $value;
769 }
770  
771 0 my @cpanm = _cpanm($ctx);
772 0 ▶ return EXIT_ERROR unless @cpanm;
Branch: 1 branch
BranchTrueFalseTotal
unless @cpanm 0 0 0
773  
774 0 for my $url (@resolved) {
775 0 my ($asset) = _asset( $ctx, $url );
776 0 ▶ return EXIT_ERROR unless defined $asset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $asset 0 0 0
777  
778 0 ▶ return EXIT_ERROR
Branch: 1 branch
BranchTrueFalseTotal
unless _command($ctx, @cpanm, _options($ctx), $asset) 0 0 0
779 unless _command( $ctx, @cpanm, _options($ctx), $asset );
780 }
781  
782 0 return EXIT_SUCCESS;
783 }
784  
785 # _modules($ctx, @modules):
786 # Install the CPAN modules of one environment with cpanm
787 # (DEPS-INSTALL-2).
788 0 ▶sub _modules ( $ctx, @modules )
Subroutine _modules: not called CC=3 SCAR=24.8
789 {
790 0 _note( $ctx, 'the CPAN modules: %s', join q{ }, @modules );
791  
792 0 my @cpanm = _cpanm($ctx);
793 0 ▶ return EXIT_ERROR unless @cpanm;
Branch: 1 branch
BranchTrueFalseTotal
unless @cpanm 0 0 0
794  
795 0 ▶ return EXIT_ERROR
Branch: 1 branch
BranchTrueFalseTotal
unless _command($ctx, @cpanm, _options($ctx), @modules) 0 0 0
796 unless _command( $ctx, @cpanm, _options($ctx), @modules );
797  
798 0 return EXIT_SUCCESS;
799 }
800  
801 # _bins($ctx, @bins):
802 # Install the prebuilt binaries of one environment into
803 # ~/.local/bin (DEPS-INSTALL-6). A bin entry holds the command
804 # name, the download URL and, for an archive, the path of the
805 # file in the archive.
806 #
807 # Every entry resolves, and takes its shape check again, before
808 # the first fetch. The resolution asks no network
809 # (DEPS-ALIAS-4), so an entry that no tier covers stops the run
810 # ahead of the mkdir, and a host with no install directory keeps
811 # none (DEPS-INSTALL-9).
812 #
813 # The digest of an entry takes its check in the install loop, at
814 # the download of that entry. A mismatch there leaves an earlier
815 # entry of the set installed, and a mismatch on the first entry
816 # leaves the new install directory behind (DEPS-TIER-2).
817 0 ▶sub _bins ( $ctx, @bins )
Subroutine _bins: not called CC=15 SCAR=54.8
818 {
819 0 my $app = $ctx->{app};
820 0 my $log = $app->cli->log;
821 _note( $ctx, 'the binaries: %s',
822 0 join q{ }, map { ( split q{ }, $_, 2 )[0] } @bins );
823  
824 0 my $home = $ENV{HOME};
825 0 ▶ unless ( defined $home && $home ne q{} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $home and $home ne "") 0 0 0
Condition0%0/3
defined $home and $home ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $home and $home ne ""
defined $home $home ne ""
Truth table0%0/3
A and B
A= defined $homeB= $home ne ""
ABresult
0X0
100
111
826 0 $log->error( 'HOME is not set, and a bin entry installs'
827 . ' under it' );
828 0 return EXIT_ERROR;
829 }
830  
831 0 my $bindir = File::Spec->catdir( $home, '.local', 'bin' );
832  
833 0 my @entry;
834 0 for my $bin (@bins) {
835 0 my ( $name, $url, $member ) = split q{ }, $bin, 3;
836 0 ▶ return Fugu::CLI::EXIT_CONFIG_ERROR()
Branch: 1 branch
BranchTrueFalseTotal
unless _check_bin_names($app, $name, $member, "the entry $name") 0 0 0
837 unless _check_bin_names( $app, $name, $member,
838 "the entry $name" );
839  
840 0 ( $url, $member ) = _resolve( $ctx, $url, $member );
841 0 ▶ return Fugu::CLI::EXIT_CONFIG_ERROR() unless defined $url;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url 0 0 0
842  
843 # The alias words expand into the archive path, so the
844 # shape takes its check again after the expansion
845 # (DEPS-INSTALL-8).
846 0 ▶ return Fugu::CLI::EXIT_CONFIG_ERROR()
Branch: 1 branch
BranchTrueFalseTotal
unless _check_bin_names($app, $name, $member, "the resolved entry $name") 0 0 0
847 unless _check_bin_names( $app, $name, $member,
848 "the resolved entry $name" );
849  
850 # An entry that no tier covers stops the run here, ahead
851 # of the mkdir of the install directory
852 # (DEPS-INSTALL-9).
853 0 ▶ return EXIT_ERROR unless _check_tier( $ctx, $url );
Branch: 1 branch
BranchTrueFalseTotal
unless _check_tier($ctx, $url) 0 0 0
854  
855 0 push @entry, [ $name, $url, $member ];
856 }
857  
858 0 ▶ return EXIT_ERROR unless _command( $ctx, 'mkdir', '-p', $bindir );
Branch: 1 branch
BranchTrueFalseTotal
unless _command($ctx, "mkdir", "-p", $bindir) 0 0 0
859  
860 0 for my $bin (@entry) {
861 0 my ( $name, $url, $member ) = @$bin;
862 0 my $file = File::Spec->catfile( $bindir, $name );
863  
864 0 my ( $asset, $dir ) = _asset( $ctx, $url );
865 0 ▶ return EXIT_ERROR unless defined $asset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $asset 0 0 0
866  
867 0 ▶ if ( defined $member ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $member) { } 0 0 0
868 0 ▶ return EXIT_ERROR
Branch: 1 branch
BranchTrueFalseTotal
unless _extract($ctx, $dir, $file, $asset, $member) 0 0 0
869 unless _extract( $ctx, $dir, $file, $asset,
870 $member );
871 }
872 else {
873 0 ▶ return EXIT_ERROR
Branch: 1 branch
BranchTrueFalseTotal
unless _command($ctx, "cp", $asset, $file) 0 0 0
874 unless _command( $ctx, 'cp', $asset, $file );
875 }
876 0 ▶ return EXIT_ERROR
Branch: 1 branch
BranchTrueFalseTotal
unless _command($ctx, "chmod", 755, $file) 0 0 0
877 unless _command( $ctx, 'chmod', '755', $file );
878 }
879  
880 0 return EXIT_SUCCESS;
881 }
882  
883 # _extract($ctx, $dir, $file, $archive, $member):
884 # Install the one file $member of a checked archive as $file
885 # (DEPS-INSTALL-7). tar unpacks a tar archive, and unzip unpacks
886 # a zip archive. Both unpack $member alone, into the temporary
887 # directory that holds the archive.
888 #
889 # The method returns 1 after both commands, and 0 after a
890 # failure, which _command reports.
891 0 ▶sub _extract ( $ctx, $dir, $file, $archive, $member )
Subroutine _extract: not called CC=4 SCAR=30.0
892 {
893 0 ▶ if ( _archive_type($archive) eq 'tar' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_archive_type($archive) eq 'tar') { } 0 0 0
894 0 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
unless _command($ctx, "tar", "-xzf", $archive, "-C", $dir, $member) 0 0 0
895 unless _command( $ctx, 'tar', '-xzf', $archive, '-C',
896 $dir, $member );
897 }
898 else {
899 0 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
unless _command($ctx, "unzip", "-q", $archive, $member, "-d", $dir) 0 0 0
900 unless _command( $ctx, 'unzip', '-q', $archive, $member,
901 '-d', $dir );
902 }
903  
904 0 return _command( $ctx, 'cp',
905 File::Spec->catfile( $dir, split m{/}, $member ), $file );
906 }
907  
908 # _asset($ctx, $url):
909 # The path of one checked download, and the temporary directory
910 # that holds it, in that order (DEPS-TIER-1). The method writes
911 # the trace line of the fetch, which names the fetch verb
912 # (DEPS-FETCH-2).
913 #
914 # The signed manifest of a release lands beside the download, so
915 # a download named SHA256 would share one path with it. The
916 # download therefore takes a directory of its own.
917 #
918 # A dry run names the file and asks no network
919 # (DEPS-MANIFEST-6). Every other run downloads the file and
920 # checks it, and it returns the empty list on a failure.
921 0 ▶sub _asset ( $ctx, $url )
Subroutine _asset: not called CC=4 SCAR=30.0
922 {
923 0 my $dir = _tempdir($ctx);
924 0 my ($name) = $url =~ m{([^/]+)\z};
925  
926 0 my $asset = File::Spec->catdir( $dir, 'asset' );
927 0 ▶ unless ( mkdir $asset ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (mkdir $asset) 0 0 0
928 $ctx->{app}
929 0 ->cli->log->error( 'cannot make %s: %s', $asset, $! );
930 0 return;
931 }
932  
933 0 my $file = File::Spec->catfile( $asset, $name );
934 0 _trace( $ctx, $ctx->{app}->cli->name, 'fetch', $file, $url );
935  
936 0 ▶ return ( $file, $dir ) if $ctx->{dry};
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"dry"} 0 0 0
937 0 ▶ return unless _verified( $ctx, $url, $file, $dir );
Branch: 1 branch
BranchTrueFalseTotal
unless _verified($ctx, $url, $file, $dir) 0 0 0
938  
939 0 return ( $file, $dir );
940 }
941  
942 # _check_tier($ctx, $url):
943 # Report an entry that no tier can check (DEPS-TIER-9). The
944 # check reads the digest file and the key set alone, so a caller
945 # runs it over every entry before the first download.
946 #
947 # A tool entry takes the signify tier as every other entry does,
948 # because the check runs in-process and needs no signify(1) on
949 # the host (DEPS-TIER-8).
950 0 ▶sub _check_tier ( $ctx, $url )
Subroutine _check_tier: not called CC=3 SCAR=24.8
951 {
952 0 ▶ return 1 if exists $ctx->{digests}{$url};
Branch: 1 branch
BranchTrueFalseTotal
if exists $ctx->{"digests"}{$url} 0 0 0
953 0 ▶ return 1 if @{ $ctx->{keys} };
Branch: 1 branch
BranchTrueFalseTotal
if @{$ctx->{"keys"};} 0 0 0
954  
955 0 my $log = $ctx->{app}->cli->log;
956 0 $log->error(
957 '%s has no recorded digest, and no key is declared, so nothing'
958 . ' verifies it',
959 $url
960 );
961 0 $log->error( q{ a versioned URL takes a digest from}
962 . q{ 'deps --update-sums'} );
963 0 $log->error( ' a stable URL needs a signed SHA256 beside it, and a'
964 . ' key in deps/KEYS.local.txt' );
965  
966 0 return 0;
967 }
968  
969 # _verified($ctx, $url, $file, $dir):
970 # Download one file and hold it to its digest. The method
971 # returns 1 after a check that passes, and 0 after every
972 # failure, which it reports.
973 #
974 # The recorded digest of deps/SHA256.txt comes first
975 # (DEPS-TIER-6). Without one, the method derives SHA256 and
976 # SHA256.sig from the directory of the URL, verifies the
977 # signature, and holds the file to the signed manifest
978 # (DEPS-TIER-7). The signature covers the manifest, and the
979 # manifest covers the file, so the signature verifies before the
980 # file downloads.
981 0 ▶sub _verified ( $ctx, $url, $file, $dir )
Subroutine _verified: not called CC=11 SCAR=48.8
982 {
983 0 my $log = $ctx->{app}->cli->log;
984  
985 0 my $want = $ctx->{digests}{$url};
986 0 ▶ if ( defined $want ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $want) 0 0 0
987 0 ▶ return 0 unless _fetch_file( $ctx, $url, $file );
Branch: 1 branch
BranchTrueFalseTotal
unless _fetch_file($ctx, $url, $file) 0 0 0
988  
989 0 return _check_digest( $ctx, $file, $want, $url, 'recorded' );
990 }
991  
992 0 my $base = _base($url);
993 0 ▶ unless ( defined $base ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $base) 0 0 0
994 0 $log->error( 'the URL names no directory: %s', $url );
995 0 return 0;
996 }
997  
998 0 my $sums = File::Spec->catfile( $dir, 'SHA256' );
999 0 my $sig = File::Spec->catfile( $dir, 'SHA256.sig' );
1000 0 for my $part ( [ $sums, 'SHA256' ], [ $sig, 'SHA256.sig' ] ) {
1001 0 my $answer = _probe( $ctx, "$base/$part->[1]", $part->[0] );
1002 0 ▶ return 0 unless defined $answer;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $answer 0 0 0
1003 0 ▶ next if $answer;
Branch: 1 branch
BranchTrueFalseTotal
if $answer 0 0 0
1004  
1005 0 $log->error(
1006 'the signify tier needs %s/%s, and the server does not'
1007 . ' answer it',
1008 $base, $part->[1] );
1009 0 $log->error( ' a release that publishes no signature needs a'
1010 . ' recorded digest' );
1011 0 $log->error(
1012 q{ 'deps --update-sums' records a versioned URL, and}
1013 . q{ '--update-sums --force' records any URL} );
1014 0 return 0;
1015 }
1016  
1017 0 ▶ return 0 unless _verify( $ctx, $sums, $sig );
Branch: 1 branch
BranchTrueFalseTotal
unless _verify($ctx, $sums, $sig) 0 0 0
1018  
1019 # The signed manifest of a release covers one directory with
1020 # unique file names, so it keys on the file name.
1021 0 my $signed = _parse_digests( $ctx->{app}, $sums );
1022 0 ▶ return 0 unless $signed;
Branch: 1 branch
BranchTrueFalseTotal
unless $signed 0 0 0
1023  
1024 0 my ($name) = $url =~ m{([^/]+)\z};
1025 0 my $signed_want = $signed->{$name};
1026 0 ▶ unless ( defined $signed_want ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $signed_want) 0 0 0
1027 0 $log->error( 'the signed manifest of %s does not name %s',
1028 $base, $name );
1029 0 return 0;
1030 }
1031  
1032 0 ▶ return 0 unless _fetch_file( $ctx, $url, $file );
Branch: 1 branch
BranchTrueFalseTotal
unless _fetch_file($ctx, $url, $file) 0 0 0
1033  
1034 0 return _check_digest( $ctx, $file, $signed_want, $name, 'signed' );
1035 }
1036  
1037 # _verify($ctx, $sums, $sig, $level):
1038 # Verify the signature over one SHA256 manifest with the
1039 # declared keys, in trust order. The method returns the path of
1040 # the key file that verified it, and undef after a failure,
1041 # which it reports.
1042 #
1043 # Fugu::Signify reads each key file under the perl engine, so no
1044 # host needs signify(1) (DEPS-TIER-8). An empty key set stops
1045 # the signify tier (DEPS-KEYS-5).
1046 #
1047 # $level names the level of a failure. The install path takes
1048 # the default, because a failure there stops the run. The
1049 # signed-manifest probe of a refresh passes 'warning', because a
1050 # manifest that no key verifies keeps the entry off the digest
1051 # tier, and the run goes on (DEPS-SUMS-6).
1052 0 ▶sub _verify ( $ctx, $sums, $sig, $level = 'error' )
Subroutine _verify: not called CC=10 SCAR=47.0
1053 {
1054 0 my $log = $ctx->{app}->cli->log;
1055 0 my $keys = $ctx->{keys};
1056 my $report =
1057 $level eq 'warning'
1058 0 ▶ ? sub (@line) { return $log->warning(@line) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
1059 0 ▶ : sub (@line) { return $log->error(@line) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$level eq 'warning' ? : 0 0 0
1060  
1061 0 ▶ unless (@$keys) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$keys) 0 0 0
1062 0 $report->(
1063 'no key is declared, so no signature verifies %s',
1064 $sums
1065 );
1066 0 return;
1067 }
1068  
1069 # Each verification builds the key set again, in a directory
1070 # of its own (DEPS-KEYS-6). A cached copy would carry the
1071 # check of an earlier entry.
1072 0 my $dir = _tempdir($ctx);
1073 0 my ( @paths, %name, @failed );
1074 0 for my $key (@$keys) {
1075 0 my ( $path, $why ) = _key_file( $ctx, $dir, $key );
1076 0 ▶ unless ( defined $path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $path) 0 0 0
1077  
1078 # A key that fails its digest, and a key that no
1079 # server answers, must not stop the trust order
1080 # (DEPS-KEYS-7). The operator must still see it.
1081 $log->warning( 'the key %s did not load: %s',
1082 0 $key->{name}, $why );
1083 0 push @failed, "$key->{name}: $why";
1084 0 next;
1085 }
1086 0 $name{$path} = $key->{name};
1087 0 push @paths, $path;
1088 }
1089  
1090 0 ▶ unless (@paths) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@paths) 0 0 0
1091 0 $report->(
1092 'no declared key loaded, so no signature verifies'
1093 . ' %s',
1094 $sums
1095 );
1096 0 $report->( ' %s', $_ ) for @failed;
1097 0 return;
1098 }
1099  
1100 0 my $public = _signify()->verify(
1101 keys => \@paths,
1102 file => $sums,
1103 signature => $sig
1104 );
1105 0 ▶ unless ( defined $public ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $public) 0 0 0
1106 0 $report->(
1107 'no declared key verifies the signature of %s', $sums
1108 );
1109 $report->( ' %s', $_ )
1110 0 for _reasons( _signify()->error ), @failed;
1111 0 return;
1112 }
1113  
1114 0 _note( $ctx, 'verified the manifest with the key %s', $name{$public} );
1115  
1116 0 return $public;
1117 }
1118  
1119 # _key_file($ctx, $dir, $key):
1120 # The path of one public key file in a temporary directory, or
1121 # undef and the reason of the failure, in that order.
1122 #
1123 # The body form writes the two lines of a signify public key
1124 # file. The URL form downloads the published file and holds it
1125 # to the recorded digest, which is the trust anchor
1126 # (DEPS-KEYS-2). A failed check leaves no file behind for a
1127 # later use.
1128 0 ▶sub _key_file ( $ctx, $dir, $key )
Subroutine _key_file: not called CC=6 SCAR=37.4
1129 {
1130 0 my $path = File::Spec->catfile( $dir, "$key->{name}.pub" );
1131  
1132 0 ▶ if ( defined $key->{body} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key->{"body"}) 0 0 0
1133 0 ▶ return ( undef, "cannot write $path" )
Branch: 1 branch
BranchTrueFalseTotal
unless "Fugu::File"->write($path, _key_text($key)) 0 0 0
1134 unless Fugu::File->write( $path, _key_text($key) );
1135  
1136 0 return $path;
1137 }
1138  
1139 return ( undef, $ctx->{curl}->error )
1140 0 ▶ unless _fetch( $ctx, $key->{url}, $path );
Branch: 1 branch
BranchTrueFalseTotal
unless _fetch($ctx, $key->{"url"}, $path) 0 0 0
1141  
1142 0 my $got = _sha256($path);
1143 0 ▶ unless ( defined $got ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $got) 0 0 0
1144 0 my $why = "cannot read $path: $!";
1145 0 unlink $path;
1146 0 return ( undef, $why );
1147 }
1148 0 ▶ unless ( $got eq $key->{digest} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($got eq $key->{"digest"}) 0 0 0
1149 0 unlink $path;
1150 return ( undef,
1151 0 "the file holds $got, and the key line records"
1152 . " $key->{digest}" );
1153 }
1154  
1155 0 return $path;
1156 }
1157  
1158 # _check_digest($ctx, $path, $want, $key, $source):
1159 # Hold one downloaded file to the wanted digest. The method
1160 # returns 1 for a digest that matches, and 0 for every other
1161 # answer, which it reports.
1162 #
1163 # A mismatch names the repair of its tier (DEPS-TIER-10). The
1164 # source is 'recorded' or 'signed', and the key names the line
1165 # to repair: the URL for a recorded digest, and the file name
1166 # for a signed manifest.
1167 0 ▶sub _check_digest ( $ctx, $path, $want, $key, $source )
Subroutine _check_digest: not called CC=4 SCAR=30.0
1168 {
1169 0 my $log = $ctx->{app}->cli->log;
1170  
1171 0 my $got = _sha256($path);
1172 0 ▶ unless ( defined $got ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $got) 0 0 0
1173 0 $log->error( 'cannot read %s: %s', $path, $! );
1174 0 return 0;
1175 }
1176 0 ▶ return 1 if $got eq $want;
Branch: 1 branch
BranchTrueFalseTotal
if $got eq $want 0 0 0
1177  
1178 0 $log->error( '%s does not match its %s digest', $key, $source );
1179 0 $log->error( ' expected %s', $want );
1180 0 $log->error( ' got %s', $got );
1181 0 ▶ if ( $source eq 'signed' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source eq 'signed') { } 0 0 0
1182 0 $log->error( ' the signature verifies, so the served file'
1183 . ' disagrees with the release' );
1184 0 $log->error( ' report it upstream, and install nothing until'
1185 . ' it agrees' );
1186 }
1187 else {
1188 0 $log->error( ' compare the upstream checksum file, then'
1189 . q{ 'deps --update-sums --force' rewrites the line}
1190 );
1191 }
1192  
1193 0 return 0;
1194 }
1195  
1196 # _sha256($path):
1197 # The sha256 digest of one file, in lower-case hexadecimal, or
1198 # undef for a file that does not open. The digest streams from
1199 # the handle, so a large file never enters memory whole.
1200 0 ▶sub _sha256 ($path)
Subroutine _sha256: not called CC=2 SCAR=17.9
1201 {
1202 0 ▶ open my $fh, '<', $path or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or return 0 0 0
1203 0 binmode $fh;
1204 0 my $sha = Digest::SHA->new(256);
1205 0 $sha->addfile($fh);
1206 0 close $fh;
1207  
1208 0 return $sha->hexdigest;
1209 }
1210  
1211 # _fetch($ctx, $url, $path):
1212 # Download one URL to one path through Fugu::Curl
1213 # (DEPS-FETCH-1). The method returns 1 on success, and undef
1214 # with the reason in the error of the downloader.
1215 #
1216 # One Fugu::Curl serves the whole run, because it resolves the
1217 # command of the host one time. A failed download leaves no file
1218 # at the path.
1219 0 ▶sub _fetch ( $ctx, $url, $path )
Subroutine _fetch: not called CC=2 SCAR=17.9
1220 {
1221 0 ▶ $ctx->{curl} //= Fugu::Curl->new;
Condition0%0/3
$ctx->{"curl"} //= "Fugu::Curl"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$ctx->{"curl"} //= "Fugu::Curl"->new
$ctx->{"curl"} "Fugu::Curl"->new
Truth table0%0/3
A //= B
A= $ctx->{"curl"}B= "Fugu::Curl"->new
ABresult
1X1
011
000
1222  
1223 0 return $ctx->{curl}->fetch( $url, $path );
1224 }
1225  
1226 # _fetch_file($ctx, $url, $path):
1227 # One download that the run needs. The method reports a failure
1228 # and returns 0, and it returns 1 after a download that lands.
1229 0 ▶sub _fetch_file ( $ctx, $url, $path )
Subroutine _fetch_file: not called CC=2 SCAR=17.9
1230 {
1231 0 ▶ return 1 if _fetch( $ctx, $url, $path );
Branch: 1 branch
BranchTrueFalseTotal
if _fetch($ctx, $url, $path) 0 0 0
1232 0 $ctx->{app}->cli->log->error( '%s', $ctx->{curl}->error );
1233  
1234 0 return 0;
1235 }
1236  
1237 # _probe($ctx, $url, $path):
1238 # One download whose absence is a normal answer: 1 for a file
1239 # that lands, 0 for a 404, and undef for every other failure,
1240 # which the method reports (DEPS-FETCH-3).
1241 #
1242 # curl reports the HTTP status through --write-out, and a 404
1243 # behind a redirect exits 56 and not 22. Fugu::Curl owns that
1244 # classification, and the verb reads the status alone.
1245 0 ▶sub _probe ( $ctx, $url, $path )
Subroutine _probe: not called CC=6 SCAR=37.4
1246 {
1247 0 ▶ return 1 if _fetch( $ctx, $url, $path );
Branch: 1 branch
BranchTrueFalseTotal
if _fetch($ctx, $url, $path) 0 0 0
1248  
1249 0 my $curl = $ctx->{curl};
1250 0 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
if ($curl->status // "") eq "http" and ($curl->code // 0) == 404 0 0 0
Condition0%0/7
($curl->status // "") eq "http" and ($curl->code // 0) == 404
!ll&&!rl&&r
000
$curl->status // ""
l!l
00
$curl->code // 0
l!l
00
MC/DC0%0/2
($curl->status // "") eq "http" and ($curl->code // 0) == 404
$curl->status $curl->code
Truth table0%0/3
A and B
A= $curl->statusB= $curl->code
ABresult
0X0
100
111
1251 if ( $curl->status // q{} ) eq 'http'
1252 && ( $curl->code // 0 ) == 404;
1253  
1254 0 $ctx->{app}->cli->log->error( '%s', $curl->error );
1255  
1256 0 return;
1257 }
1258  
1259 # _base($url):
1260 # The directory part of one URL, or undef.
1261 0 ▶sub _base ($url)
Subroutine _base: not called CC=3 SCAR=24.8
1262 {
1263 0 my ($base) = $url =~ m{\A(.*)/[^/]+\z};
1264  
1265 0 ▶ return defined $base && $base ne q{} ? $base : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $base && $base ne '' ? : 0 0 0
Condition0%0/3
defined $base and $base ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $base and $base ne ""
defined $base $base ne ""
Truth table0%0/3
A and B
A= defined $baseB= $base ne ""
ABresult
0X0
100
111
1266 }
1267  
1268 # _reasons($text):
1269 # Each line of a multi-line reason, without its lead and its
1270 # trailing whitespace. One line of the log carries one reason.
1271 0 ▶sub _reasons ($text)
Subroutine _reasons: not called CC=4 SCAR=30.0
1272 {
1273 0 my @out;
1274 0 ▶ for my $line ( split /\n/, $text // q{} ) {
Condition0%0/2
$text // ""
l!l
00
MC/DC0%0/1
$text // ""
$text
Truth table0%0/2
A
A= $text
Aresult
11
00
1275 0 $line =~ s/\A\s+//;
1276 0 $line =~ s/\s+\z//;
1277 0 ▶ push @out, $line if length $line;
Branch: 1 branch
BranchTrueFalseTotal
if length $line 0 0 0
1278 }
1279  
1280 0 return @out;
1281 }
1282  
1283 # _cpanm($ctx):
1284 # The command that runs cpanm, as a list (DEPS-INSTALL-3). The
1285 # cpanm on PATH is the first choice, and the bare name keeps the
1286 # trace line readable.
1287 #
1288 # Without one, the method names the standalone cpanm script in a
1289 # temporary directory, and this perl runs it. An install of
1290 # App::cpanminus does not answer here: it lands in the local
1291 # library of the user, and PATH does not hold it.
1292 #
1293 # The method resolves the command one time, because the trace
1294 # holds the download of the script one time. It returns the
1295 # empty list after a failed download, which it reports.
1296 0 ▶sub _cpanm ($ctx)
Subroutine _cpanm: not called CC=5 SCAR=34.0
1297 {
1298 0 ▶ return @{ $ctx->{cpanm} } if $ctx->{cpanm};
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"cpanm"} 0 0 0
1299  
1300 0 my @cmd = ('cpanm');
1301 0 ▶ unless ( defined Fugu::Process->find_command('cpanm') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined "Fugu::Process"->find_command("cpanm")) 0 0 0
1302 0 _note( $ctx,
1303 'cpanm is absent, and the bootstrap'
1304 . ' downloads the standalone script' );
1305  
1306 0 my $script = File::Spec->catfile( _tempdir($ctx), 'cpanm' );
1307 _trace( $ctx, $ctx->{app}->cli->name,
1308 0 'fetch', $script, CPANM_URL );
1309  
1310 # This is the one download with no check, because no
1311 # manifest names it (DEPS-INSTALL-4).
1312 return
1313 unless $ctx->{dry}
1314 0 ▶ || _fetch_file( $ctx, CPANM_URL, $script );
Branch: 1 branch
BranchTrueFalseTotal
unless $ctx->{"dry"} or _fetch_file($ctx, "https://cpanmin.us", $script) 0 0 0
Condition0%0/3
$ctx->{"dry"} or _fetch_file($ctx, "https://cpanmin.us", $script)
l!l&&r!l&&!r
000
MC/DC0%0/2
$ctx->{"dry"} or _fetch_file($ctx, "https://cpanmin.us", $script)
$ctx->{"dry"} _fetch_file($ctx, "https://cpanmin.us", $script)
Truth table0%0/3
A or B
A= $ctx->{"dry"}B= _fetch_file($ctx, "https://cpanmin.us", $script)
ABresult
1X1
011
000
1315  
1316 0 @cmd = ( $^X, $script );
1317 }
1318 0 $ctx->{cpanm} = \@cmd;
1319  
1320 0 return @cmd;
1321 }
1322  
1323 # _options($ctx):
1324 # The options that every cpanm run shares (DEPS-INSTALL-2). With
1325 # PERL_LOCAL_LIB_ROOT in the environment, the install lands in
1326 # that directory. local::lib and the setup-perl action of CI
1327 # each set the variable.
1328 0 ▶sub _options ($ctx)
Subroutine _options: not called CC=4 SCAR=30.0
1329 {
1330 0 ▶ return @{ $ctx->{options} } if $ctx->{options};
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"options"} 0 0 0
1331  
1332 0 my @opts = ('--notest');
1333 0 my $local = $ENV{PERL_LOCAL_LIB_ROOT};
1334 0 ▶ if ( defined $local && length $local ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $local and length $local) 0 0 0
Condition0%0/3
defined $local and length $local
!ll&&!rl&&r
000
MC/DC0%0/2
defined $local and length $local
defined $local length $local
Truth table0%0/3
A and B
A= defined $localB= length $local
ABresult
0X0
100
111
1335 0 _note( $ctx, 'the local library: %s', $local );
1336 0 push @opts, "--local-lib=$local";
1337 }
1338 0 $ctx->{options} = \@opts;
1339  
1340 0 return @opts;
1341 }
1342  
1343 # _tempdir($ctx):
1344 # The path of one new temporary directory. The context holds
1345 # each directory object, so every directory of the run stays
1346 # until the run ends.
1347 0 ▶sub _tempdir ($ctx)
Subroutine _tempdir: not called CC=1 SCAR=6.9
1348 {
1349 0 my $dir = File::Temp->newdir(
1350 TEMPLATE => 'fugubench-XXXXXXXX',
1351 TMPDIR => 1
1352 );
1353 0 push @{ $ctx->{hold} }, $dir;
1354  
1355 0 return "$dir";
1356 }
1357  
1358 # _resolve($ctx, $url, $member):
1359 # The URL and the archive path with the platform words in place,
1360 # in that order (DEPS-ALIAS-1). The digest file selects the
1361 # spelling: the method forms one candidate for each alias pair,
1362 # and it takes the candidate that the file records
1363 # (DEPS-ALIAS-3). No match and more than one match are each an
1364 # error, and neither one asks the network (DEPS-ALIAS-4).
1365 #
1366 # The method reports the failure and returns the empty list.
1367 0 ▶sub _resolve ( $ctx, $url, $member )
Subroutine _resolve: not called CC=10 SCAR=47.0
1368 {
1369 0 my $log = $ctx->{app}->cli->log;
1370  
1371 # Each placeholder of the archive path must also sit in the
1372 # URL (DEPS-ALIAS-6). The archive path names a path inside the
1373 # archive, and only the URL selects the platform.
1374 0 for my $word (qw(os arch)) {
1375 0 ▶ next unless defined $member && $member =~ /\{$word\}/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $member and $member =~ /\{$word\}/u 0 0 0
Condition0%0/3
defined $member and $member =~ /\{$word\}/u
!ll&&!rl&&r
000
MC/DC0%0/2
defined $member and $member =~ /\{$word\}/u
defined $member $member =~ /\{$word\}/u
Truth table0%0/3
A and B
A= defined $memberB= $member =~ /\{$word\}/u
ABresult
0X0
100
111
1376 0 ▶ next if $url =~ /\{$word\}/;
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ /\{$word\}/u 0 0 0
1377 0 $log->error(
1378 'the archive path holds {%s}, and the URL does not:'
1379 . ' %s',
1380 $word, $url
1381 );
1382 0 return;
1383 }
1384  
1385 0 ▶ return ( $url, $member ) if $url !~ /\{(?:os|arch)\}/;
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ /\{(?:os|arch)\}/u 0 0 0
1386  
1387 # The digest file keys on the URL, so a candidate matches when
1388 # the file records that whole URL. Two candidates never share a
1389 # key, so more than one match is a real ambiguity.
1390 0 my @hit = grep { exists $ctx->{digests}{ $_->[0] } }
1391 0 _candidates( $url, $ctx->{os}, $ctx->{arch} );
1392  
1393 0 ▶ unless (@hit) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@hit) 0 0 0
1394 0 $log->error( 'deps/SHA256.txt records no digest for %s', $url );
1395 0 $log->error( 'the file keys on the whole download URL; run'
1396 . q{ 'deps --update-sums' and check each new line}
1397 );
1398 0 return;
1399 }
1400 0 ▶ if ( @hit > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@hit > 1) 0 0 0
1401 0 $log->error(
1402 'deps/SHA256.txt records more than one candidate'
1403 . ' of %s',
1404 $url
1405 );
1406 0 $log->error( ' %s', $_->[0] ) for @hit;
1407 0 $log->error( q{'deps --update-sums --force' keeps one}
1408 . ' candidate and drops the others' );
1409 0 return;
1410 }
1411  
1412 0 my ( $value, $os_word, $arch_word ) = @{ $hit[0] };
1413  
1414 0 ▶ return ( $value,
Branch: 1 branch
BranchTrueFalseTotal
defined $member ? : 0 0 0
1415 defined $member
1416 ? _expand( $member, $os_word, $arch_word )
1417 : undef );
1418 }
1419  
1420 # _candidates($text, $os, $arch):
1421 # Every distinct expansion of one text, in preference order,
1422 # with the word pair that made each one.
1423 0 ▶sub _candidates ( $text, $os, $arch )
Subroutine _candidates: not called CC=4 SCAR=30.0
1424 {
1425 0 my ( @out, %seen );
1426 0 for my $os_word ( _os_words($os) ) {
1427 0 for my $arch_word ( _arch_words($arch) ) {
1428 0 my $value = _expand( $text, $os_word, $arch_word );
1429 0 ▶ next if $seen{$value}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$value}++ 0 0 0
1430 0 push @out, [ $value, $os_word, $arch_word ];
1431 }
1432 }
1433  
1434 0 return @out;
1435 }
1436  
1437 # _os_words($os):
1438 # Every asset spelling of one system name, in preference order.
1439 # An unknown name gives its own lower case (DEPS-ALIAS-2).
1440 0 ▶sub _os_words ($os)
Subroutine _os_words: not called CC=2 SCAR=17.9
1441 {
1442 0 ▶ return @{ $OS_ALIAS{$os} // [ lc $os ] };
Condition0%0/2
$OS_ALIAS{$os} // [lc $os]
l!l
00
MC/DC0%0/1
$OS_ALIAS{$os} // [lc $os]
$OS_ALIAS{$os}
Truth table0%0/2
A
A= $OS_ALIAS{$os}
Aresult
11
00
1443 }
1444  
1445 # _arch_words($arch):
1446 # Every asset spelling of one machine name, in preference order.
1447 # An unknown name gives itself (DEPS-ALIAS-2).
1448 0 ▶sub _arch_words ($arch)
Subroutine _arch_words: not called CC=2 SCAR=17.9
1449 {
1450 0 ▶ return @{ $ARCH_ALIAS{$arch} // [$arch] };
Condition0%0/2
$ARCH_ALIAS{$arch} // [$arch]
l!l
00
MC/DC0%0/1
$ARCH_ALIAS{$arch} // [$arch]
$ARCH_ALIAS{$arch}
Truth table0%0/2
A
A= $ARCH_ALIAS{$arch}
Aresult
11
00
1451 }
1452  
1453 # _expand($text, $os_word, $arch_word):
1454 # One text with both placeholders replaced.
1455 0 ▶sub _expand ( $text, $os_word, $arch_word )
Subroutine _expand: not called CC=1 SCAR=6.9
1456 {
1457 0 my $out = $text;
1458 0 $out =~ s/\{os\}/$os_word/g;
1459 0 $out =~ s/\{arch\}/$arch_word/g;
1460  
1461 0 return $out;
1462 }
1463  
1464 # _archive_type($url):
1465 # The archive type that the suffix of the URL names: tar for
1466 # .tar.gz and .tgz, zip for .zip, and undef for a plain file.
1467 0 ▶sub _archive_type ($url)
Subroutine _archive_type: not called CC=3 SCAR=24.8
1468 {
1469 0 ▶ return 'tar' if $url =~ /\.(?:tar\.gz|tgz)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ /\.(?:tar\.gz|tgz)\z/u 0 0 0
1470 0 ▶ return 'zip' if $url =~ /\.zip\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ /\.zip\z/u 0 0 0
1471  
1472 0 return;
1473 }
1474  
1475 # _update_sums($ctx, $file, $by_type):
1476 # Record the digest of each download that the manifest names,
1477 # and write deps/SHA256.txt (DEPS-SUMS-1). The operator runs
1478 # this command, and the install path never does.
1479 #
1480 # The entries hold every environment of one manifest, because
1481 # one digest file records every download of one operating
1482 # system. The method returns the exit code of the run.
1483 0 ▶sub _update_sums ( $ctx, $file, $by_type )
Subroutine _update_sums: not called CC=4 SCAR=30.0
1484 {
1485 my $sums = {
1486 digests => $ctx->{digests},
1487 0 legacy => [ _legacy( $ctx->{digests} ) ],
1488 report => [],
1489 recorded => 0,
1490 missed => 0,
1491 };
1492  
1493 # The dist entries and the bin entries name every download of
1494 # the manifest. A pkg name and a cpan name reach a package
1495 # manager, which owns its own check.
1496 0 my @urls = @{ $by_type->{dist} };
1497 0 push @urls, ( split q{ }, $_, 3 )[1] for @{ $by_type->{bin} };
1498  
1499 0 for my $url (@urls) {
1500 0 my $code = _record( $ctx, $sums, $url );
1501 0 ▶ return $code if $code != EXIT_SUCCESS;
Branch: 1 branch
BranchTrueFalseTotal
if $code != 0 0 0 0
1502 }
1503  
1504 0 return _write_sums( $ctx, $sums, $file );
1505 }
1506  
1507 # _write_sums($ctx, $sums, $file):
1508 # End one refresh: report each line of the run, and write the
1509 # digest file when the run recorded something (DEPS-SUMS-12).
1510 # The method returns the exit code of the run.
1511 #
1512 # A run with an entry that reached no candidate writes nothing,
1513 # because a half state must not reach the recorded file. An
1514 # operator must also read no success line for a state that makes
1515 # the next install fail.
1516 0 ▶sub _write_sums ( $ctx, $sums, $file )
Subroutine _write_sums: not called CC=6 SCAR=37.4
1517 {
1518 0 my $log = $ctx->{app}->cli->log;
1519  
1520 0 ▶ if ( $sums->{missed} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sums->{"missed"}) 0 0 0
1521 $log->error(
1522 '%d entry of the manifest reached no candidate, and %s'
1523 . ' stays as it was',
1524 0 $sums->{missed}, $file
1525 );
1526 0 $log->error( ' check the URL of each entry, and check that'
1527 . ' --os and --arch name a platform of the release'
1528 );
1529 0 $log->error( ' a signed manifest that names no candidate'
1530 . ' needs a release with a per-platform file name'
1531 );
1532 0 return EXIT_ERROR;
1533 }
1534  
1535 0 say for @{ $sums->{report} };
1536  
1537 0 ▶ unless ( $sums->{recorded} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sums->{"recorded"}) 0 0 0
1538 0 say "recorded nothing, and $file stays as it was";
1539 0 return EXIT_SUCCESS;
1540 }
1541  
1542 # The writer sorts the keys, so a second refresh makes a stable
1543 # diff (DEPS-TIER-3).
1544 0 my $text = _signify()->write_manifest( $sums->{digests} );
1545 0 ▶ unless ( defined $text ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $text) 0 0 0
1546 0 $log->error( '%s: %s', $file, _signify()->error );
1547 0 return EXIT_ERROR;
1548 }
1549 0 ▶ unless ( Fugu::File->write( $file, $text ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("Fugu::File"->write($file, $text)) 0 0 0
1550 0 $log->error( 'cannot write %s: %s', $file, $! );
1551 0 return EXIT_ERROR;
1552 }
1553 0 say "wrote $file";
1554  
1555 0 return EXIT_SUCCESS;
1556 }
1557  
1558 # _record($ctx, $sums, $url):
1559 # Take one download of the manifest through the rules of the
1560 # refresh. The method returns the exit code of the run:
1561 # EXIT_SUCCESS for an entry that records, keeps, skips or
1562 # misses, and another code for a fault that stops the refresh.
1563 #
1564 # A candidate URL that the file records already stays, so a
1565 # version bump needs no hand edit. This branch leaves a file
1566 # that holds two candidates of one entry as it is, and --force
1567 # repairs that. --force also repairs a digest that no longer
1568 # matches the release.
1569 0 ▶sub _record ( $ctx, $sums, $url )
Subroutine _record: not called CC=9 SCAR=45.0
1570 {
1571 0 my $app = $ctx->{app};
1572  
1573 # The alias words expand into the URL, and a candidate becomes
1574 # a key of the digest file. The shape takes its check again
1575 # after the expansion, so no run writes a line that a later run
1576 # cannot read (DEPS-TIER-12).
1577 0 my @candidate = _candidates( $url, $ctx->{os}, $ctx->{arch} );
1578 0 for my $one (@candidate) {
1579 0 ▶ return Fugu::CLI::EXIT_CONFIG_ERROR()
Branch: 1 branch
BranchTrueFalseTotal
unless _check_url($app, $one->[0], "the resolved URL") 0 0 0
1580 unless _check_url( $app, $one->[0], 'the resolved URL' );
1581 }
1582  
1583 my @known =
1584 0 grep { exists $sums->{digests}{$_} } map { $_->[0] } @candidate;
1585 0 ▶ if ( @known && !$ctx->{force} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@known and not $ctx->{"force"}) 0 0 0
Condition0%0/3
@known and not $ctx->{"force"}
!ll&&!rl&&r
000
MC/DC0%0/2
@known and not $ctx->{"force"}
@known not $ctx->{"force"}
Truth table0%0/3
A and not(B)
A= @knownB= not $ctx->{"force"}
ABresult
0X0
100
111
1586  
1587 # The file holds the URL of this entry, so the file-name
1588 # key that it replaces goes (DEPS-SUMS-11).
1589 0 $sums->{recorded} += _drop_legacy( $sums, $known[0] );
1590 0 push @{ $sums->{report} }, "kept $known[0]";
1591 0 return EXIT_SUCCESS;
1592 }
1593  
1594 # A digest covers a versioned name, so a stable name stays on
1595 # the signify tier (DEPS-SUMS-3). The test reads the manifest,
1596 # and never the network.
1597 0 ▶ if ( !$ctx->{force} && _stable($url) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ctx->{"force"} and _stable($url)) 0 0 0
Condition0%0/3
not $ctx->{"force"} and _stable($url)
!ll&&!rl&&r
000
MC/DC0%0/2
not $ctx->{"force"} and _stable($url)
not $ctx->{"force"} _stable($url)
Truth table0%0/3
not(A) and B
A= not $ctx->{"force"}B= _stable($url)
ABresult
0X0
100
111
1598 0 push @{ $sums->{report} }, "skipped the stable name: $url";
1599 0 return EXIT_SUCCESS;
1600 }
1601  
1602 0 my $signed = _signed( $ctx, @candidate );
1603 0 ▶ return EXIT_ERROR unless $signed;
Branch: 1 branch
BranchTrueFalseTotal
unless $signed 0 0 0
1604  
1605 0 my $code = _signed_entry( $ctx, $sums, $url, $signed, \@known );
1606 0 ▶ return $code if defined $code;
Branch: 1 branch
BranchTrueFalseTotal
if defined $code 0 0 0
1607  
1608 0 return _download_sums( $ctx, $sums, $url, \@candidate, \@known );
1609 }
1610  
1611 # _stable($url):
1612 # 1 for a URL that names a stable download, and 0 for a
1613 # versioned one (DEPS-SUMS-2). A versioned name carries a digit
1614 # in the path of its URL. A stable name carries none, or it
1615 # holds /releases/latest/.
1616 #
1617 # An entry with a placeholder is never stable, because the
1618 # resolution reads the digest file, and that entry then needs a
1619 # recorded digest (DEPS-ALIAS-5).
1620 0 ▶sub _stable ($url)
Subroutine _stable: not called CC=5 SCAR=34.0
1621 {
1622 0 ▶ return 0 if $url =~ /\{(?:os|arch)\}/;
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ /\{(?:os|arch)\}/u 0 0 0
1623 0 ▶ return 1 if index( $url, '/releases/latest/' ) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($url, "/releases/latest/") != -1 0 0 0
1624  
1625 0 my ($path) = $url =~ m{\A[a-z][a-z0-9+.-]*://[^/]+(/.*)\z}i;
1626  
1627 0 ▶ return !defined $path || $path !~ /[0-9]/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
!(defined $path && $path =~ /[0-9]/u) ? : 0 0 0
Condition0%0/3
defined $path && $path =~ /[0-9]/u
!ll&&!rl&&r
000
MC/DC0%0/2
defined $path && $path =~ /[0-9]/u
defined $path $path =~ /[0-9]/u
Truth table0%0/3
A && B
A= defined $pathB= $path =~ /[0-9]/u
ABresult
0X0
100
111
1628 }
1629  
1630 # _signed($ctx, @candidate):
1631 # The answer of the signed-manifest probe over the candidates of
1632 # one entry, as a hash with 'found', 'verified' and 'taken'. The
1633 # method returns undef after a failure of the run, which it
1634 # reports.
1635 #
1636 # The probe asks each distinct resolved directory of the
1637 # candidates, and never the directory of the template, which
1638 # names no server (DEPS-SUMS-6). A manifest that answers keeps
1639 # the entry off the digest tier, whether or not a key verifies
1640 # it. A server that withholds the signature alone must not make
1641 # the refresh pin the bytes that it serves (DEPS-SUMS-3).
1642 #
1643 # 'taken' holds the first candidate of the answering directory
1644 # that the manifest names, with its digest, and the probe stops
1645 # there. A verified manifest that names no candidate must not
1646 # hide a later directory that holds the release (DEPS-SUMS-7).
1647 0 ▶sub _signed ( $ctx, @candidate )
Subroutine _signed: not called CC=11 SCAR=48.8
1648 {
1649 0 my $answer = { found => 0, verified => 0 };
1650  
1651 0 my %seen;
1652 0 for my $one (@candidate) {
1653 0 my $base = _base( $one->[0] );
1654 0 ▶ next unless defined $base;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $base 0 0 0
1655 0 ▶ next if $seen{$base}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$base}++ 0 0 0
1656  
1657 # Each probe takes a directory of its own. One shared
1658 # path would let a later probe that finds nothing keep
1659 # the manifest of an earlier answer.
1660 0 my $dir = _tempdir($ctx);
1661 0 my $sums = File::Spec->catfile( $dir, 'SHA256' );
1662 0 my $sig = File::Spec->catfile( $dir, 'SHA256.sig' );
1663  
1664 0 my $got = _probe( $ctx, "$base/SHA256", $sums );
1665 0 ▶ return unless defined $got;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $got 0 0 0
1666 0 ▶ next unless $got;
Branch: 1 branch
BranchTrueFalseTotal
unless $got 0 0 0
1667  
1668 0 $answer->{found} = 1;
1669 0 $got = _probe( $ctx, "$base/SHA256.sig", $sig );
1670 0 ▶ return unless defined $got;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $got 0 0 0
1671 0 ▶ next unless $got;
Branch: 1 branch
BranchTrueFalseTotal
unless $got 0 0 0
1672  
1673 # A manifest that no key verifies is a normal outcome
1674 # of the probe, so each line of it is a warning
1675 # (DEPS-SUMS-6).
1676 0 ▶ next unless _verify( $ctx, $sums, $sig, 'warning' );
Branch: 1 branch
BranchTrueFalseTotal
unless _verify($ctx, $sums, $sig, "warning") 0 0 0
1677 0 $answer->{verified} = 1;
1678  
1679 0 my $digests = _parse_digests( $ctx->{app}, $sums );
1680 0 ▶ return unless $digests;
Branch: 1 branch
BranchTrueFalseTotal
unless $digests 0 0 0
1681  
1682 0 $answer->{taken} = _taken( $base, $digests, @candidate );
1683 0 ▶ last if defined $answer->{taken};
Branch: 1 branch
BranchTrueFalseTotal
if defined $answer->{"taken"} 0 0 0
1684 }
1685  
1686 0 return $answer;
1687 }
1688  
1689 # _taken($base, $digests, @candidate):
1690 # The first candidate of one directory that the signed manifest
1691 # of that directory names, with its digest, or undef.
1692 #
1693 # The signed manifest keys on the file name, and the digest file
1694 # keys on the URL. The candidate carries both, so it joins the
1695 # two. A candidate of another directory names another release.
1696 0 ▶sub _taken ( $base, $digests, @candidate )
Subroutine _taken: not called CC=6 SCAR=37.4
1697 {
1698 0 for my $one (@candidate) {
1699 0 my $where = _base( $one->[0] );
1700 0 ▶ next unless defined $where && $where eq $base;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $where and $where eq $base 0 0 0
Condition0%0/3
defined $where and $where eq $base
!ll&&!rl&&r
000
MC/DC0%0/2
defined $where and $where eq $base
defined $where $where eq $base
Truth table0%0/3
A and B
A= defined $whereB= $where eq $base
ABresult
0X0
100
111
1701  
1702 0 my ($name) = $one->[0] =~ m{([^/]+)\z};
1703 0 ▶ next unless defined $name && exists $digests->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and exists $digests->{$name} 0 0 0
Condition0%0/3
defined $name and exists $digests->{$name}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $name and exists $digests->{$name}
defined $name exists $digests->{$name}
Truth table0%0/3
A and B
A= defined $nameB= exists $digests->{$name}
ABresult
0X0
100
111
1704  
1705 0 return [ $one->[0], $digests->{$name} ];
1706 }
1707  
1708 0 return;
1709 }
1710  
1711 # _signed_entry($ctx, $sums, $url, $signed, $known):
1712 # Take one entry whose directory holds a signed manifest. The
1713 # method returns the exit code of the entry, or undef when the
1714 # refresh must download the candidates itself.
1715 #
1716 # A URL without a placeholder installs through the signify tier,
1717 # so it needs no digest, and a recorded one would outrank the
1718 # signature (DEPS-SUMS-5). A URL with a placeholder needs a
1719 # digest, and the signed manifest is where it comes from
1720 # (DEPS-SUMS-4).
1721 #
1722 # --force overrides both, with a warning first (DEPS-SUMS-10).
1723 # An upstream that publishes a manifest which the operator
1724 # cannot verify would otherwise leave the entry unpinnable.
1725 0 ▶sub _signed_entry ( $ctx, $sums, $url, $signed, $known )
Subroutine _signed_entry: not called CC=8 SCAR=42.8
1726 {
1727 0 my $log = $ctx->{app}->cli->log;
1728  
1729 0 ▶ return unless $signed->{found};
Branch: 1 branch
BranchTrueFalseTotal
unless $signed->{"found"} 0 0 0
1730  
1731 0 ▶ if ( $url !~ /\{(?:os|arch)\}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url =~ /\{(?:os|arch)\}/u) 0 0 0
1732 0 ▶ unless ( $ctx->{force} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ctx->{"force"}) 0 0 0
1733 0 push @{ $sums->{report} },
1734 "skipped the signed entry: $url";
1735 0 return EXIT_SUCCESS;
1736 }
1737 0 _warn_force( $log, $url );
1738  
1739 0 return;
1740 }
1741  
1742 0 my $taken = $signed->{taken};
1743 0 ▶ if ( defined $taken ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $taken) 0 0 0
1744  
1745 # --force replaces the line of this entry, so every
1746 # other recorded candidate of it goes (DEPS-SUMS-10).
1747 0 ▶ _drop_siblings( $sums, $known, $taken->[0] ) if $ctx->{force};
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"force"} 0 0 0
1748 0 _drop_legacy( $sums, $taken->[0] );
1749 0 $sums->{digests}{ $taken->[0] } = $taken->[1];
1750 0 $sums->{recorded}++;
1751 0 push @{ $sums->{report} },
1752 "recorded $taken->[0] from the signed manifest";
1753  
1754 0 return EXIT_SUCCESS;
1755 }
1756  
1757 0 ▶ unless ( $ctx->{force} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ctx->{"force"}) 0 0 0
1758  
1759 # Without a digest this entry can never install, so the
1760 # run must not report success (DEPS-SUMS-7).
1761 $log->warning(
1762 $signed->{verified}
1763 0 ▶ ? 'the signed manifest beside %s names no candidate'
Branch: 1 branch
BranchTrueFalseTotal
$signed->{'verified'} ? : 0 0 0
1764 : 'nothing verifies the signed manifest beside %s',
1765 $url
1766 );
1767 0 $log->warning(q{ '--update-sums --force' pins the bytes}
1768 . ' that the server serves' );
1769 0 $sums->{missed}++;
1770  
1771 0 return EXIT_SUCCESS;
1772 }
1773 0 _warn_force( $log, $url );
1774  
1775 0 return;
1776 }
1777  
1778 # _warn_force($log, $url):
1779 # The warning of a --force that pins a URL of the signify tier
1780 # (DEPS-SUMS-10).
1781 0 ▶sub _warn_force ( $log, $url )
Subroutine _warn_force: not called CC=1 SCAR=6.9
1782 {
1783 0 $log->warning(
1784 '--force pins a URL that a signed manifest covers, and the'
1785 . ' recorded digest then outranks the signature: %s',
1786 $url
1787 );
1788  
1789 0 return;
1790 }
1791  
1792 # _download_sums($ctx, $sums, $url, $candidate, $known):
1793 # Download each candidate of one entry, and record the digest of
1794 # the first one that answers (DEPS-SUMS-9). The method returns
1795 # the exit code of the entry.
1796 #
1797 # Each candidate takes a directory of its own, because two
1798 # candidates can share a file name (DEPS-SUMS-8). A 404 is the
1799 # absent answer of a candidate, and every other failed download
1800 # stops the refresh. An entry that no candidate answers fails
1801 # the run (DEPS-SUMS-7).
1802 0 ▶sub _download_sums ( $ctx, $sums, $url, $candidate, $known )
Subroutine _download_sums: not called CC=11 SCAR=48.8
1803 {
1804 0 my $log = $ctx->{app}->cli->log;
1805  
1806 0 my @found;
1807 0 for my $one (@$candidate) {
1808 0 my ($name) = $one->[0] =~ m{([^/]+)\z};
1809 0 ▶ next unless defined $name && $name ne q{};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and $name ne "" 0 0 0
Condition0%0/3
defined $name and $name ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $name and $name ne ""
defined $name $name ne ""
Truth table0%0/3
A and B
A= defined $nameB= $name ne ""
ABresult
0X0
100
111
1810  
1811 0 my $path = File::Spec->catfile( _tempdir($ctx), $name );
1812 0 my $answer = _probe( $ctx, $one->[0], $path );
1813 0 ▶ return EXIT_ERROR unless defined $answer;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $answer 0 0 0
1814 0 ▶ next unless $answer;
Branch: 1 branch
BranchTrueFalseTotal
unless $answer 0 0 0
1815  
1816 0 push @found, [ $one->[0], $path ];
1817 }
1818  
1819 0 ▶ unless (@found) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@found) 0 0 0
1820 0 $log->warning( 'no candidate answers for %s', $url );
1821 0 $sums->{missed}++;
1822  
1823 0 return EXIT_SUCCESS;
1824 }
1825  
1826 0 my ( $taken, $path ) = @{ $found[0] };
1827 0 my $digest = _sha256($path);
1828 0 ▶ unless ( defined $digest ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $digest) 0 0 0
1829 0 $log->error( 'cannot read %s: %s', $path, $! );
1830 0 return EXIT_ERROR;
1831 }
1832  
1833 # --force replaces the line of this entry, so every other
1834 # recorded candidate of it goes (DEPS-SUMS-10).
1835 0 ▶ _drop_siblings( $sums, $known, $taken ) if $ctx->{force};
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"force"} 0 0 0
1836 0 _drop_legacy( $sums, $taken );
1837 0 $sums->{digests}{$taken} = $digest;
1838 0 $sums->{recorded}++;
1839 0 push @{ $sums->{report} }, "recorded $taken";
1840  
1841 0 ▶ return EXIT_SUCCESS if @found == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @found == 1 0 0 0
1842  
1843 # The install rejects a URL that more than one recorded
1844 # candidate covers, so the operator must see each other
1845 # answer. The line of each one names it, and the recorded
1846 # candidate stays out of that list.
1847 0 $log->warning(
1848 'more than one candidate answers for %s: the refresh records'
1849 . ' %s, and each candidate below stays out of the file',
1850 $url, $taken
1851 );
1852 0 $log->warning( ' %s', $_->[0] ) for @found[ 1 .. $#found ];
1853  
1854 0 return EXIT_SUCCESS;
1855 }
1856  
1857 # _drop_siblings($sums, $known, $taken):
1858 # Remove every other recorded candidate of one entry, and report
1859 # each one. Two recorded candidates make the install ambiguous
1860 # (DEPS-ALIAS-3).
1861 0 ▶sub _drop_siblings ( $sums, $known, $taken )
Subroutine _drop_siblings: not called CC=3 SCAR=24.8
1862 {
1863 0 for my $other (@$known) {
1864 0 ▶ next if $other eq $taken;
Branch: 1 branch
BranchTrueFalseTotal
if $other eq $taken 0 0 0
1865 0 delete $sums->{digests}{$other};
1866 0 push @{ $sums->{report} }, "removed $other";
1867 }
1868  
1869 0 return;
1870 }
1871  
1872 # _drop_legacy($sums, $taken):
1873 # Remove the file-name key that one recorded URL replaces, and
1874 # report each one. The method returns the number of keys that it
1875 # removed (DEPS-SUMS-11).
1876 #
1877 # One run reads the manifest of one operating system, so a key
1878 # that this run cannot replace stays. A blanket drop would take
1879 # the pin of every other platform with it.
1880 0 ▶sub _drop_legacy ( $sums, $taken )
Subroutine _drop_legacy: not called CC=5 SCAR=34.0
1881 {
1882 0 my ($name) = $taken =~ m{([^/]+)\z};
1883 0 ▶ return 0 unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 0 0
1884  
1885 0 my $dropped = 0;
1886 0 for my $key ( @{ $sums->{legacy} } ) {
1887 0 ▶ next unless $key eq $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $key eq $name 0 0 0
1888 0 ▶ next unless exists $sums->{digests}{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $sums->{"digests"}{$key} 0 0 0
1889 0 delete $sums->{digests}{$key};
1890 0 push @{ $sums->{report} }, "dropped the file-name key $key";
1891 0 $dropped++;
1892 }
1893  
1894 0 return $dropped;
1895 }
1896  
1897 # _command($ctx, @cmd):
1898 # Run one command of an install as a child. A dry run prints the
1899 # trace line and runs nothing (DEPS-MANIFEST-6). A real run
1900 # prints no line here, because $app->command traces each child
1901 # on standard error under --verbose (CLI-PROGRAM-7).
1902 #
1903 # The child takes the environment of the run, because a package
1904 # manager and cpanm read it. No line of a child reaches standard
1905 # output, because a child writes no part of the result
1906 # (CLI-PROGRAM-4). $app->command writes the standard error of
1907 # every child to standard error, and this method writes the
1908 # standard output of a child that exits 0 there.
1909 #
1910 # The method returns 1 for a child that exits 0, and 0 for every
1911 # other answer, which it reports. A failed command stops the run,
1912 # so no later command of the environment runs.
1913 0 ▶sub _command ( $ctx, @cmd )
Subroutine _command: not called CC=4 SCAR=30.0
1914 {
1915 0 ▶ if ( $ctx->{dry} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctx->{"dry"}) 0 0 0
1916 0 _trace( $ctx, @cmd );
1917 0 return 1;
1918 }
1919  
1920 0 my $app = $ctx->{app};
1921 0 my $out = $app->command( \@cmd );
1922 0 ▶ unless ( defined $out ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $out) 0 0 0
1923 0 $app->cli->log->error( '%s', $app->error );
1924 0 return 0;
1925 }
1926 0 ▶ print STDERR $out if length $out;
Branch: 1 branch
BranchTrueFalseTotal
if length $out 0 0 0
1927  
1928 0 return 1;
1929 }
1930  
1931 # _note($ctx, $fmt, @args):
1932 # Report one step of the run on standard error. The line goes
1933 # out under --verbose alone, because the program is silent on
1934 # success except for the result line (CLI-PROGRAM-7).
1935 # DEPS-MANIFEST-9 names the progress lines of this verb.
1936 #
1937 # A message that reports a fault takes the logger directly, at
1938 # the warning level or the error level, and no run hides it.
1939 0 ▶sub _note ( $ctx, $fmt, @args )
Subroutine _note: not called CC=2 SCAR=17.9
1940 {
1941 0 my $cli = $ctx->{app}->cli;
1942 0 ▶ $cli->log->info( $fmt, @args ) if $cli->option('verbose');
Branch: 1 branch
BranchTrueFalseTotal
if $cli->option("verbose") 0 0 0
1943  
1944 0 return;
1945 }
1946  
1947 # _trace($ctx, @cmd):
1948 # Trace one command of the run. A dry run prints it to standard
1949 # output, as the line that starts with '+ ' and holds each
1950 # argument shell-quoted (DEPS-MANIFEST-6).
1951 #
1952 # A real run prints no line to standard output, because standard
1953 # output carries the result of the verb alone (CLI-PROGRAM-4).
1954 # With --verbose the line goes to standard error, and this method
1955 # writes it (CLI-PROGRAM-7).
1956 #
1957 # A download of the verb runs in-process, so $app->command never
1958 # sees it and traces nothing for it. The verbose branch here
1959 # covers that download alone: _command hands every child to
1960 # $app->command, which writes its own 'run: ' line.
1961 #
1962 # That branch joins the words raw, as $app->command does, so one
1963 # run writes one form. The quoted form belongs to the dry-run
1964 # trace, whose reader is the oracle.
1965 #
1966 # A download of the verb names the fetch verb in its line
1967 # (DEPS-FETCH-2).
1968 0 ▶sub _trace ( $ctx, @cmd )
Subroutine _trace: not called CC=3 SCAR=24.8
1969 {
1970 0 ▶ if ( $ctx->{dry} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctx->{"dry"}) 0 0 0
1971 0 say '+ ', join q{ }, map { _quote($_) } @cmd;
1972 0 return;
1973 }
1974  
1975 $ctx->{app}->cli->log->info( 'run: %s', join q{ }, @cmd )
1976 0 ▶ if $ctx->{app}->cli->option('verbose');
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"app"}->cli->option("verbose") 0 0 0
1977  
1978 0 return;
1979 }
1980  
1981 # _quote($word):
1982 # Shell-quote one word of the dry-run trace line. The program
1983 # gives no word to a shell (CLI-PROGRAM-6). The quoting lets a
1984 # reader of the trace, and the oracle test, see that an argument
1985 # with a space is one argument.
1986 0 ▶sub _quote ($word)
Subroutine _quote: not called CC=3 SCAR=24.8
1987 {
1988 0 ▶ return $word unless $word eq q{} || $word =~ /[^\w.\/:=-]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $word eq "" or $word =~ m[[^\w./:=-]]u 0 0 0
Condition0%0/3
$word eq "" or $word =~ m[[^\w./:=-]]u
l!l&&r!l&&!r
000
MC/DC0%0/2
$word eq "" or $word =~ m[[^\w./:=-]]u
$word eq "" $word =~ m[[^\w./:=-]]u
Truth table0%0/3
A or B
A= $word eq ""B= $word =~ m[[^\w./:=-]]u
ABresult
1X1
011
000
1989  
1990 0 my $quoted = $word;
1991 0 $quoted =~ s/'/'\\''/g;
1992  
1993 0 return "'$quoted'";
1994 }
1995  
1996 117 1;
↑ Summary lib/App/FuguBench/Update.pm »