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 $cmd =~ s{\bcurl (\S*)([^|]*)}{
35 my ($url, $params) = ($1, $2);
36 my $cache = 'sample/data/';
37 -w $cache or skip($url, 2);
39 $cmd =~ /\bxml/ ? 'xml' :
40 $cmd =~ / jq / ? 'json' :
41 $cmd =~ /[=.]csv\b/ ? 'csv' :
44 my ($domain, $path) = $url =~ m{//([^/]+) .*/ ([^/]*) \z}x;
45 $path =~ s/\.$ext\z//;
46 $cache .= join '.', $path =~ tr/./_/r, $domain, $ext;
47 my $cached = -e $cache;
50 skip($url, 1) if $cached;
51 $cached = defined runres("curl -sSf $url$params -o $cache");
52 ok($cached, $url) or diag("download at $ref: $@");
54 $cached or skip($url, 1);
58 # compose an identifier from significant parts
60 s/^\h+//; # indentation
61 s/\\\n\s*//g; # line continuations
62 s/^[(\h]+//; # subshell
63 s/^echo\ .*?\|\s*//; # preceding input
64 s/'(\S+)[^']*'/$1/g; # quoted arguments
65 s/\h*\|.*//; # subsequent pipes
66 s/^cat\ (?:\S+\/)?//; # local file
67 } for my $name = $cmd;
69 # prepare shell command to execute
70 while (my ($subcmd, $args) = each %CMDARGS) {
71 $subcmd .= " \\K", $args .= ' ' unless $subcmd =~ m/\\K/;
72 $cmd =~ s/\b$subcmd/$args/;
75 for my $param ($cmd =~ m{^[(\h]* (\w\S*)}gx) {
77 runres(['which', $param])
78 or diag("dependency $param missing at $ref\n$cmd"), skip($name, 1);
81 # run and report unexpected results
82 my $output = runres($cmd);
84 or diag("command at $ref\n$cmd\n" . ($@ || 'empty output'));
85 defined $output or next;
87 # record output for review
88 my $numprefix = sprintf '%02d', Test::More->builder->current_test;
89 if (open my $record, '>', "sample/out/t$numprefix-$name.txt") {
90 print {$record} $output;
97 or $cmd = [bash => -c => "set -o pipefail\n$cmd"];
99 run($cmd, \undef, \my $output, \my $error);
100 die("error message:\n".($error =~ s/^/ /gr)."\n") if $error;
101 $? == 0 or die "exit status ", $? >> 8, "\n";