t/examples: record resulting output for review
[barcat.git] / t / examples.t
index 02019b9b4a8e9a72fbf593c484f3e9ce3bde9d82..a331ce352d5322d9aa26b8f4e0af1973ea7c1555 100755 (executable)
@@ -1,12 +1,18 @@
 #!/usr/bin/env perl
 use 5.014;
 use warnings;
+use re '/ms';
+use IPC::Run 'run';
+
 use Test::More;
+{ # silence fail diagnostics because of single caller
+       no warnings 'redefine';
+       sub Test::Builder::_ok_debug {}
+}
 
 my %CMDARGS = (
        ping => '-c 1',
-       curl => '-sS',
-       'cat \Klog/' => '/var/log/apache2/',
+       'cat \Khttpd/' => '/var/log/apache2/',
 );
 
 my $filename = 'barcat';
@@ -15,26 +21,76 @@ open my $input, '<', $filename
 
 local $/ = "\n\n";
 while (readline $input) {
-       # find code snippets in the appropriate section
+       # find scriptlets in the appropriate section
        /^=head1 EXAMPLES/ ... /^=head1/ or next;
-       /^\h/ or next;
+       /^\h/ or next;  # indented code snippet
+       /\A\h*>/ and next;  # psql prompt
        chomp;
+       my $cmd = $_;
+       my $ref = "$filename line $.";
 
-       my ($name) = /[\h(]*([^|]+)/;
+       # store curl downloads
+       s{\bcurl (\S*)(?<param>[^|]*)}{
+               my $url = $1;
+               my @params = split ' ', $+{param};
+               my $ext = (
+                       $cmd =~ /\bxml/     ? 'xml'  :
+                       $cmd =~ / jq /      ? 'json' :
+                       $cmd =~ /[=.]csv\b/ ? 'csv'  :
+                                             'txt'
+               );
+               my ($domain, $path) = $url =~ m{//([^/]+) .*/ ([^/]*) \z}x;
+               $path =~ s/\.$ext\z//;
+               my $cache = join '.', $path =~ tr/./_/r, $domain, $ext;
+               $cache = "sample/data/$cache";
+               SKIP: {
+                       -e $cache and skip($url, 1);
+                       ok(defined runres(['curl', '-sS', $url, '-o', $cache, @params]), $url)
+                               or diag("download at $ref: $@");
+               }
+               "cat $cache"
+       }e;
+
+       # compose an identifier from significant parts
+       do {
+               s/^\h+//;             # indentation
+               s/\\\n\s*//g;         # line continuations
+               s/^[(\h]+//;          # subshell
+               s/^echo\ .*?\|\s*//;  # preceding input
+               s/'(\S+)[^']*'/$1/g;  # quoted arguments
+               s/\h*\|.*//;          # subsequent pipes
+               s/^cat\ (?:\S+\/)?//; # local file
+       } for my $name = $cmd;
 
        # prepare shell command to execute
-       my $cmd = $_;
        while (my ($subcmd, $args) = each %CMDARGS) {
                $subcmd .= " \\K", $args .= ' ' unless $subcmd =~ m/\\K/;
                $cmd =~ s/\b$subcmd/$args/;
        }
-       $cmd =~ s/'/'\\''/g, $cmd = "bash -c 'set -o pipefail\n$cmd'";
 
        # run and report unexpected results
-       ok(eval {
-               qx($cmd) or return;
-               return $? == 0;
-       }, $name);
+       my $output = runres($cmd);
+       ok(!!$output, $name)
+               or diag("command at $ref\n$cmd\n" . ($@ || 'empty output'));
+       defined $output or next;
+
+       # record output for review
+       my $numprefix = sprintf '%02d', Test::More->builder->current_test;
+       if (open my $record, '>', "sample/out/t$numprefix-$name.txt") {
+               print {$record} $output;
+       }
+}
+
+sub runres {
+       my ($cmd) = @_;
+       ref $cmd eq 'ARRAY'
+               or $cmd = [bash => -c => "set -o pipefail\n$cmd"];
+       eval {
+               run($cmd, \undef, \my $output, \my $error);
+               die("error message:\n".($error =~ s/^/    /gr)."\n") if $error;
+               $? == 0 or die "exit status ", $? >> 8, "\n";
+               return $output;
+       };
 }
 
 done_testing();