lib/App/FuguBench/Dist.pm

filter: Statementstmt 37.3% 40 / 107 Branchbran 3.3% 1 / 30 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 56.5% 13 / 23 Podpod 100% 3 / 3 totaltotal 29.0% 57 / 196 CC 23 SCAR 33.6
CC
23
Cov
26%
_install
90.0
_shim
42.0
install_paths
30.0
CRAP 28.8
« lib/App/FuguBench/Doctor.pm ↑ Summary lib/App/FuguBench/Hook.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::Dist;
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 Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27 117 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
28 117 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29 117 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 117 ▶use Fugu::CLI qw(EXIT_SUCCESS EXIT_ERROR);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 117 ▶use Fugu::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 117 ▶use Fugu::Sandbox;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 # App::FuguBench::Dist - the shim verb and the install verb.
36 #
37 # A consumer runs the program through a wrapper shim, and that shim
38 # holds the version that it runs (D-04). `shim` prints the shell of
39 # DIST-SHIM, and the org pack of FuguBSD/Tooling syncs it into a
40 # consumer (D-09). `install` copies the running program into the
41 # install directory of the operator (DIST-INSTALL-2).
42 #
43 # Neither verb reads a checkout (CLI-CHECKOUT-5). A fresh clone runs
44 # the shim before any install, and the install script of
45 # DIST-INSTALL-1 runs `install` in a home with no .toolingrc.
46 #
47 # Each verb opens a file of its own and runs no child, so each row
48 # unveils the paths that it opens (CLI-SANDBOX-2). shim_paths and
49 # install_paths hold the two lists.
50 #
51 # No verb here downloads. `shim` writes the URL into the shell text,
52 # and the shell of a consumer fetches it. So no URL of this module
53 # reaches Fugu::Curl, and none takes the shape check of DEPS-FETCH-4.
54  
55 117 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 # The packed file of one release (DIST-ASSETS-1). The release
58 # workflow publishes it under the tag of the version.
59 117 URL => 'https://github.com/FuguBSD/FuguBench'
60 . '/releases/download/v%s/fugubench',
61  
62 # The version of a build that no release stamped. scripts/dist
63 # writes it into a build of a tree with no tag, and a checkout
64 # carries no stamp at all (DIST-VERSION-1).
65 NO_STAMP => '0.0.0',
66  
67 # The mode of an installed program (DIST-INSTALL-2).
68 MODE => 0755,
69 117 };
70  
71 # App::FuguBench::Dist->command($verb):
72 # The entry of the Fugu::CLI table of one verb. The module
73 # holds two verbs, so it reads the name. An unknown name is a
74 # programming error of the verb table.
75 234 ▶sub command ( $, $verb )
Subroutine command: called CC=2 SCAR=8.4
Pod documented
76 {
77 my %entry = (
78 shim => {
79 summary => 'print the wrapper shim of a consumer',
80 0 ▶ run => sub ( $app, @argv ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
81 0 return _shim( $app, @argv );
82 },
83 },
84 install => {
85 summary => 'copy the program into ~/.local/bin',
86 0 ▶ run => sub ( $app, @argv ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
87 0 return _install( $app, @argv );
88 },
89 },
90 234 );
91  
92 234 ▶ my $entry = $entry{$verb} or die "no such verb: $verb";
Branch: 1 branch
BranchTrueFalseTotal
my $entry = $entry{$verb} or die "no such verb: $verb" 234 0 234
93  
94 234 return $entry;
95 }
96  
97 # App::FuguBench::Dist->shim_paths:
98 # The unveil list of the `shim` row (CLI-SANDBOX-2). The verb
99 # reads the running file and writes the shell text to standard
100 # output, so the row names the directory of that file and
101 # nothing else.
102 0 ▶sub shim_paths ( $, $ )
Subroutine shim_paths: not called CC=1 SCAR=6.9
Pod documented
103 {
104 0 return _common_paths();
105 }
106  
107 # App::FuguBench::Dist->install_paths:
108 # The unveil list of the `install` row (CLI-SANDBOX-2). The
109 # verb reads the running file and writes the copy, so the row
110 # adds the install directory.
111 #
112 # The install directory holds the running file after an
113 # install, and then one entry names both. unveil(2) returns
114 # EPERM on a second entry that widens a path, so the wider
115 # entry replaces the other one.
116 #
117 # The row makes that directory. unveil(2) hides every path that
118 # the list leaves out, and the list can name the install
119 # directory alone, so a mkdir under the parent would fail after
120 # the entry. The verb makes the directory as well, and it
121 # reports the failure, so the verb holds without this row.
122 0 ▶sub install_paths ( $, $ )
Subroutine install_paths: not called CC=5 SCAR=34.0
Pod documented
123 {
124 0 my @paths = _common_paths();
125  
126 0 my $dir = _install_dir();
127 0 ▶ return @paths unless defined $dir && Fugu::File->ensure_dir($dir);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dir and "Fugu::File"->ensure_dir($dir) 0 0 0
Condition0%0/3
defined $dir and "Fugu::File"->ensure_dir($dir)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $dir and "Fugu::File"->ensure_dir($dir)
defined $dir "Fugu::File"->ensure_dir($dir)
Truth table0%0/3
A and B
A= defined $dirB= "Fugu::File"->ensure_dir($dir)
ABresult
0X0
100
111
128  
129 # The comparison resolves each path, because HOME can hold a
130 # symlink and the entry of the running file holds the resolved
131 # form. 'rwc' reads as well, so the wider entry loses nothing.
132 0 ▶ my $same = Cwd::abs_path($dir) // $dir;
Condition0%0/3
Cwd::abs_path($dir) // $dir
l!l&&r!l&&!r
000
MC/DC0%0/2
Cwd::abs_path($dir) // $dir
Cwd::abs_path($dir) $dir
Truth table0%0/3
A // B
A= Cwd::abs_path($dir)B= $dir
ABresult
1X1
011
000
133 @paths =
134 0 ▶ grep { ( Cwd::abs_path( $_->[0] ) // $_->[0] ) ne $same } @paths;
Condition0%0/3
Cwd::abs_path($_->[0]) // $_->[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
Cwd::abs_path($_->[0]) // $_->[0]
Cwd::abs_path($_->[0]) $_->[0]
Truth table0%0/3
A // B
A= Cwd::abs_path($_->[0])B= $_->[0]
ABresult
1X1
011
000
135  
136 0 push @paths, [ $dir, 'rwc' ];
137  
138 0 return @paths;
139 }
140  
141 # _common_paths():
142 # The entries that both rows hold: the library directories of
143 # the interpreter, and the directory of the running file.
144 #
145 # perl loads a module on an error path, so the list holds the
146 # library directories. Each one is optional, because the build
147 # of a perl records a directory that the host can omit.
148 0 ▶sub _common_paths ()
Subroutine _common_paths: not called CC=1 SCAR=6.9
149 {
150 my @paths =
151 0 map { [ $_, 'r', { optional => 1 } ] } Fugu::Sandbox->perl_lib_dirs;
152 0 push @paths, [ File::Basename::dirname( _running_file() ), 'r' ];
153  
154 0 return @paths;
155 }
156  
157 # _running_file():
158 # The path of the running program. The absolute form names the
159 # same file after a chdir, and the row of the verb unveils its
160 # directory.
161 0 ▶sub _running_file ()
Subroutine _running_file: not called CC=2 SCAR=17.9
162 {
163 0 ▶ return Cwd::abs_path($0) // $0;
Condition0%0/3
Cwd::abs_path($0) // $0
l!l&&r!l&&!r
000
MC/DC0%0/2
Cwd::abs_path($0) // $0
Cwd::abs_path($0) $0
Truth table0%0/3
A // B
A= Cwd::abs_path($0)B= $0
ABresult
1X1
011
000
164 }
165  
166 # _install_dir():
167 # The install directory of DEPS-INSTALL-6, or undef when HOME
168 # is unset. A bin entry of `deps` installs there too, so one
169 # directory holds the tools of the operator.
170 0 ▶sub _install_dir ()
Subroutine _install_dir: not called CC=3 SCAR=24.8
171 {
172 0 my $home = $ENV{HOME};
173 0 ▶ return 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
174  
175 0 return File::Spec->catdir( $home, '.local', 'bin' );
176 }
177  
178 # _digest($app, $file):
179 # The sha256 digest of one file, in lower-case hex. The shim
180 # holds its download to this value (DIST-SHIM-1). The function
181 # returns undef, and reports, when the file does not open.
182 0 ▶sub _digest ( $app, $file )
Subroutine _digest: not called CC=2 SCAR=17.9
183 {
184 0 ▶ open my $fh, '<', $file or do {
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or do { $app->cli->log->error("cannot read %s: %s", $file, $!); return } 0 0 0
185 0 $app->cli->log->error( 'cannot read %s: %s', $file, $! );
186 0 return;
187 };
188 0 binmode $fh;
189 0 my $digest = Digest::SHA->new(256)->addfile($fh)->hexdigest;
190 0 close $fh;
191  
192 0 return $digest;
193 }
194  
195 # _shim($app, @argv):
196 # The body of the `shim` verb. It takes no argument, and an
197 # argument is a usage error.
198 #
199 # The shim pins one release, so it needs the version of that
200 # release and the digest of its packed file. A checkout carries
201 # no stamp, and a build of a tree with no tag carries 0.0.0. No
202 # release holds either one, so the verb reports and returns 1.
203 #
204 # The text below is the shim of DIST-SHIM. It holds three
205 # values, and each one enters through its token. sprintf takes
206 # no part here, because the text holds a per-cent sign of its
207 # own, in the parameter expansion that cuts the digest.
208 #
209 # A release asset answers a redirect, so each download follows
210 # one. curl needs -L for that, and without it curl writes the
211 # empty body of the redirect answer. wget and ftp follow a
212 # redirect with no option.
213 #
214 # The gate tests FUGUBENCH for a set variable, and not for a
215 # non-empty value. `FUGUBENCH=$(command -v fugubench)` writes
216 # an empty value on a failed lookup, and a download would then
217 # run a program that the developer did not name (DIST-SHIM-2).
218 0 ▶sub _shim ( $app, @argv )
Subroutine _shim: not called CC=6 SCAR=37.4
219 {
220 0 my $cli = $app->cli;
221 0 ▶ return $cli->command_usage_error('shim') if @argv;
Branch: 1 branch
BranchTrueFalseTotal
if @argv 0 0 0
222  
223 # The stamp itself, and not the VERSION method. That method
224 # parses the value: it truncates a value with a space, and it
225 # dies on a value with a letter. The check below is the gate,
226 # and it reports every value that it refuses.
227 0 my $version = $App::FuguBench::VERSION;
228 0 ▶ if ( !defined $version || $version eq NO_STAMP ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $version or $version eq "0.0.0") 0 0 0
Condition0%0/3
not defined $version or $version eq "0.0.0"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $version or $version eq "0.0.0"
not defined $version $version eq "0.0.0"
Truth table0%0/3
not(A) or B
A= not defined $versionB= $version eq "0.0.0"
ABresult
1X1
011
000
229 0 $cli->log->error( 'no release stamped this file: run shim'
230 . ' on the packed file of a release' );
231 0 return EXIT_ERROR;
232 }
233  
234 # The verb writes the version into shell text, so the value
235 # takes a shape check here. scripts/dist holds a build to the
236 # same shape, and a value with a space or a semicolon would
237 # write broken or injected shell.
238 0 ▶ if ( $version !~ /\A[0-9]+(?:\.[0-9]+)+\z/a ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($version =~ /\A[0-9]+(?:\.[0-9]+)+\z/a) 0 0 0
239 0 $cli->log->error( 'the version %s is no dotted-decimal number',
240 $version );
241 0 return EXIT_ERROR;
242 }
243  
244 0 my $digest = _digest( $app, _running_file() );
245 0 ▶ return EXIT_ERROR unless defined $digest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $digest 0 0 0
246  
247 0 my $url = sprintf URL, $version;
248 0 my $text = <<'SHIM';
249 #!/bin/sh
250 # The wrapper shim of fugubench @VERSION@. `fugubench shim` wrote it,
251 # and the org pack of FuguBSD/Tooling syncs it into a consumer. To
252 # take another release, sync the pack again.
253 set -eu
254  
255 version=@VERSION@
256 url=@URL@
257 want=@SUM@
258  
259 if [ -n "${FUGUBENCH+x}" ]; then
260 if [ -x "$FUGUBENCH" ]; then exec "$FUGUBENCH" "$@"; fi
261 echo "fugubench: FUGUBENCH=$FUGUBENCH is no executable" >&2
262 exit 1
263 fi
264  
265 dir=$HOME/.cache/fugubench/$version
266 file=$dir/fugubench
267 if [ ! -x "$file" ]; then
268 # A value of the environment must never pass for a tool.
269 get= sum= got=
270 for c in curl wget ftp; do
271 if command -v "$c" >/dev/null 2>&1; then get=$c; break; fi
272 done
273 for c in sha256 shasum sha256sum; do
274 if command -v "$c" >/dev/null 2>&1; then sum=$c; break; fi
275 done
276 if [ -z "$get" ]; then
277 echo "fugubench: install curl, wget or ftp" >&2
278 exit 1
279 fi
280 if [ -z "$sum" ]; then
281 echo "fugubench: install sha256, shasum or sha256sum" >&2
282 exit 1
283 fi
284 mkdir -p "$dir"
285 tmp=$dir/.download.$$
286 trap 'rm -f "$tmp"' EXIT HUP INT TERM
287 case $get in
288 curl) curl -fsSL -o "$tmp" "$url" ;;
289 wget) wget -q -O "$tmp" "$url" ;;
290 ftp) ftp -o "$tmp" "$url" ;;
291 esac
292 case $sum in
293 sha256) got=$(sha256 -q "$tmp") ;;
294 shasum) got=$(shasum -a 256 "$tmp") ;;
295 sha256sum) got=$(sha256sum "$tmp") ;;
296 esac
297 if [ "${got%% *}" != "$want" ]; then
298 echo "fugubench: $url" >&2
299 echo "fugubench: want $want" >&2
300 echo "fugubench: got ${got%% *}" >&2
301 exit 1
302 fi
303 chmod 755 "$tmp"
304 mv "$tmp" "$file"
305 fi
306  
307 exec "$file" "$@"
308 SHIM
309  
310 0 $text =~ s/\@VERSION\@/$version/g;
311 0 $text =~ s/\@URL\@/$url/g;
312 0 $text =~ s/\@SUM\@/$digest/g;
313  
314 0 print $text;
315  
316 0 return EXIT_SUCCESS;
317 }
318  
319 # _install($app, @argv):
320 # The body of the `install` verb. It takes no argument, and an
321 # argument is a usage error.
322 #
323 # The verb copies the running file, so the operator installs
324 # the file that the shim fetched, or the file that the install
325 # script downloaded. The copy is atomic, and the mode comes
326 # from a chmod, because the open of the write takes the umask
327 # of the operator (DIST-INSTALL-2).
328 0 ▶sub _install ( $app, @argv )
Subroutine _install: not called CC=9 SCAR=45.0
329 {
330 0 my $cli = $app->cli;
331 0 my $log = $cli->log;
332 0 ▶ return $cli->command_usage_error('install') if @argv;
Branch: 1 branch
BranchTrueFalseTotal
if @argv 0 0 0
333  
334 0 my $dir = _install_dir();
335 0 ▶ unless ( defined $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dir) 0 0 0
336 0 $log->error('HOME is not set, and the install writes under it');
337 0 return EXIT_ERROR;
338 }
339 0 ▶ return EXIT_ERROR unless Fugu::File->ensure_dir($dir);
Branch: 1 branch
BranchTrueFalseTotal
unless "Fugu::File"->ensure_dir($dir) 0 0 0
340  
341 0 my $file = _running_file();
342 0 my $text = Fugu::File->read($file);
343 0 ▶ unless ( defined $text ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $text) 0 0 0
344 0 $log->error( 'cannot read %s: %s', $file, $! );
345 0 return EXIT_ERROR;
346 }
347  
348 0 my $target = File::Spec->catfile( $dir, 'fugubench' );
349 0 ▶ return EXIT_ERROR
Branch: 1 branch
BranchTrueFalseTotal
unless "Fugu::File"->write_atomic($target, $text, "mode", 493) 0 0 0
350 unless Fugu::File->write_atomic( $target, $text, mode => MODE );
351 0 ▶ unless ( chmod MODE, $target ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (chmod 0755, $target) 0 0 0
352 0 $log->error( 'cannot set the mode of %s: %s', $target, $! );
353 0 return EXIT_ERROR;
354 }
355  
356 0 say $target;
357  
358 0 ▶ unless ( grep { $_ eq $dir } split /:/, ( $ENV{PATH} // q{} ), -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $dir;} split(/:/u, $ENV{"PATH"} // "", -1)) 0 0 0
Condition0%0/2
$ENV{"PATH"} // ""
l!l
00
MC/DC0%0/1
$ENV{"PATH"} // ""
$ENV{"PATH"}
Truth table0%0/2
A
A= $ENV{"PATH"}
Aresult
11
00
359 0 $log->warning(
360 'no PATH entry names %s: add it to the'
361 . ' profile of your shell',
362 $dir
363 );
364 }
365  
366 0 return EXIT_SUCCESS;
367 }
368  
369 117 1;
« lib/App/FuguBench/Doctor.pm ↑ Summary lib/App/FuguBench/Hook.pm »