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) {
25 # find scriptlets in the appropriate section
26 /^=head1 EXAMPLES/ ... /^=head1/ or next;
27 /^\h/ or next; # indented code snippet
28 /\A\h*>/ and next; # psql prompt
31 my $ref = "$filename line $.";
33 # store curl downloads
34 s{\bcurl (\S*)(?<param>[^|]*)}{
36 my @params = split ' ', $+{param};
38 $cmd =~ /\bxml/ ? 'xml' :
39 $cmd =~ / jq / ? 'json' :
40 $cmd =~ /[=.]csv\b/ ? 'csv' :
43 my ($domain, $path) = $url =~ m{//([^/]+) .*/ ([^/]*) \z}x;
44 $path =~ s/\.$ext\z//;
45 my $cache = join '.', $path =~ tr/./_/r, $domain, $ext;
46 $cache = "sample/data/$cache";
48 -e $cache and skip($url, 1);
49 ok(defined runres(['curl', '-sS', $url, '-o', $cache, @params]), $url)
50 or diag("download at $ref: $@");
55 # compose an identifier from significant parts
57 s/^\h+//; # indentation
58 s/\\\n\s*//g; # line continuations
59 s/^[(\h]+//; # subshell
60 s/^echo\ .*?\|\s*//; # preceding input
61 s/'(\S+)[^']*'/$1/g; # quoted arguments
62 s/\h*\|.*//; # subsequent pipes
63 s/^cat\ (?:\S+\/)?//; # local file
64 } for my $name = $cmd;
66 # prepare shell command to execute
67 while (my ($subcmd, $args) = each %CMDARGS) {
68 $subcmd .= " \\K", $args .= ' ' unless $subcmd =~ m/\\K/;
69 $cmd =~ s/\b$subcmd/$args/;
72 for my $param ($cmd =~ m{^[(\h]* (\w\S*)}gx) {
74 runres(['which', $param])
75 or diag("dependency $param missing at $ref\n$cmd"), skip($name, 1);
78 # run and report unexpected results
79 my $output = runres($cmd);
81 or diag("command at $ref\n$cmd\n" . ($@ || 'empty output'));
82 defined $output or next;
84 # record output for review
85 my $numprefix = sprintf '%02d', Test::More->builder->current_test;
86 if (open my $record, '>', "sample/out/t$numprefix-$name.txt") {
87 print {$record} $output;
94 or $cmd = [bash => -c => "set -o pipefail\n$cmd"];
96 run($cmd, \undef, \my $output, \my $error);
97 die("error message:\n".($error =~ s/^/ /gr)."\n") if $error;
98 $? == 0 or die "exit status ", $? >> 8, "\n";