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