compare media example (exiftool, ffprobe)
[barcat.git] / t / examples.t
index cd7f1b37edef4281c600b05efbe3c9b0746dfb20..874b94adbdcccb783fd735f4120ff17b7533c791 100755 (executable)
@@ -1,12 +1,19 @@
 #!/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,18 +22,44 @@ open my $input, '<', $filename
 
 local $/ = "\n\n";
 while (readline $input) {
+       # 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 ($name) = /[\h(]*([^|]+)/;
+       # 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/\|.*//;             # subsequent pipes
+               s/^cat\ //;           # local file
+               s/^curl\ // and do {  # remote url
+                       s/\ -.+//g;                 # download options
+                       s{//[^/\s]+/\K\S*(?=/)}{};  # subdirectories
+                       s{^https?://}{};            # http protocol
+               };
+       } for my $name = $_;
 
+       # 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/;
        }
-       ok(qx($cmd), $name);
+       my @cmd = (bash => -c => "set -o pipefail\n$cmd");
+
+       # run and report unexpected results
+       ok(eval {
+               run(\@cmd, \undef, \my $output, \my $error);
+               die("error message:\n    $error\n") if $error;
+               $? == 0 or die "exit status ", $? >> 8, "\n";
+               length $output or die "empty output\n";
+               return 1;
+       }, $name) or diag("Failed command\n@cmd\nfrom $filename line $.: $@");
 }
 
 done_testing();