8 { # silence fail diagnostics because of single caller
9 no warnings 'redefine';
10 sub Test::Builder::_ok_debug {}
15 'cat \Khttpd/' => '/var/log/apache2/',
18 my $filename = 'barcat';
19 open my $input, '<', $filename
20 or die "Cannot read documentation from $filename script\n";
23 while (readline $input) {
24 # find scriptlets in the appropriate section
25 /^=head1 EXAMPLES/ ... /^=head1/ or next;
26 /^\h/ or next; # indented code snippet
27 /\A\h*>/ and next; # psql prompt
30 my $ref = "$filename line $.";
32 # store curl downloads
33 s{\bcurl (\S*)(?<param>[^|]*)}{
35 my @params = split ' ', $+{param};
37 $cmd =~ /\bxml/ ? 'xml' :
38 $cmd =~ / jq / ? 'json' :
39 $cmd =~ /[=.]csv\b/ ? 'csv' :
42 my ($domain, $path) = $url =~ m{//([^/]+) .*/ ([^/]*) \z}x;
43 $path =~ s/\.$ext\z//;
44 my $cache = join '.', $path =~ tr/./_/r, $domain, $ext;
45 $cache = "sample/data/$cache";
47 -e $cache and skip($url, 1);
48 ok(defined runres(['curl', '-sS', $url, '-o', $cache, @params]), $url)
49 or diag("download at $ref: $@");
54 # compose an identifier from significant parts
56 s/^\h+//; # indentation
57 s/\\\n\s*//g; # line continuations
58 s/^[(\h]+//; # subshell
59 s/^echo\ .*?\|\s*//; # preceding input
60 s/'(\S+)[^']*'/$1/g; # quoted arguments
61 s/\h*\|.*//; # subsequent pipes
62 s/^cat\ (?:\S+\/)?//; # local file
63 } for my $name = $cmd;
65 # prepare shell command to execute
66 while (my ($subcmd, $args) = each %CMDARGS) {
67 $subcmd .= " \\K", $args .= ' ' unless $subcmd =~ m/\\K/;
68 $cmd =~ s/\b$subcmd/$args/;
71 # run and report unexpected results
72 my $output = runres($cmd);
74 or diag("command at $ref\n$cmd\n" . ($@ || 'empty output'));
75 defined $output or next;
77 # record output for review
78 my $numprefix = sprintf '%02d', Test::More->builder->current_test;
79 if (open my $record, '>', "sample/out/t$numprefix-$name.txt") {
80 print {$record} $output;
87 or $cmd = [bash => -c => "set -o pipefail\n$cmd"];
89 run($cmd, \undef, \my $output, \my $error);
90 die("error message:\n".($error =~ s/^/ /gr)."\n") if $error;
91 $? == 0 or die "exit status ", $? >> 8, "\n";