t/examples: optional sample/media as exiftool input
[barcat.git] / t / examples.t
1 #!/usr/bin/env perl
2 use 5.014;
3 use warnings;
4 use re '/ms';
5 use IPC::Run 'run';
6
7 use Test::More;
8 { # silence fail diagnostics because of single caller
9         no warnings 'redefine';
10         sub Test::Builder::_ok_debug {}
11 }
12
13 my %CMDARGS = (
14         ping => '-c 1 ',
15         'cat \Khttpd/' => '/var/log/apache2/',
16         ' \K\*(?=\h*\|)' => 'sample/media/*.*',
17         find => 'sample/media -name \*.\* ',
18 );
19
20 my $filename = 'barcat';
21 open my $input, '<', $filename
22         or die "Cannot read documentation from $filename script\n";
23
24 local $/ = "\n\n";
25 while (readline $input) {
26 SKIP: {
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
31         chomp;
32         my $cmd = $_;
33         my $ref = "$filename line $.";
34
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);
40                 my $ext = (
41                         $cmd =~ /\bxml/     ? 'xml'  :
42                         $cmd =~ / jq /      ? 'json' :
43                         $cmd =~ /[=.]csv\b/ ? 'csv'  :
44                                               'txt'
45                 );
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;
50                 SKIP: {
51                         # download to file
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: $@");
55                 }
56                 $cached or skip($url, 1);
57                 "cat $cache"
58         }e;
59
60         # compose an identifier from significant parts
61         do {
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;
70
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/;
75         }
76
77         for my $param ($cmd =~ m{^[(\h]* (\w\S*)}gx) {
78                 $param eq 'cat' or
79                 runres(['which', $param])
80                         or diag("dependency $param missing at $ref\n$cmd"), skip($name, 1);
81         }
82
83         # run and report unexpected results
84         my $output = runres($cmd);
85         ok(!!$output, $name)
86                 or diag("command at $ref\n$cmd\n" . ($@ || 'empty output'));
87         defined $output or next;
88
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;
93         }
94 }}
95
96 sub runres {
97         my ($cmd) = @_;
98         ref $cmd eq 'ARRAY'
99                 or $cmd = [bash => -c => "set -o pipefail\n$cmd"];
100         eval {
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";
104                 return $output;
105         };
106 }
107
108 done_testing();