cfe0e3f73e2c75e76618c47389933c1306015b55
[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 );
17
18 my $filename = 'barcat';
19 open my $input, '<', $filename
20         or die "Cannot read documentation from $filename script\n";
21
22 local $/ = "\n\n";
23 while (readline $input) {
24 SKIP: {
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
29         chomp;
30         my $cmd = $_;
31         my $ref = "$filename line $.";
32
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);
38                 my $ext = (
39                         $cmd =~ /\bxml/     ? 'xml'  :
40                         $cmd =~ / jq /      ? 'json' :
41                         $cmd =~ /[=.]csv\b/ ? 'csv'  :
42                                               'txt'
43                 );
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;
48                 SKIP: {
49                         # download to file
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: $@");
53                 }
54                 $cached or skip($url, 1);
55                 "cat $cache"
56         }e;
57
58         # compose an identifier from significant parts
59         do {
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;
68
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/;
73         }
74
75         for my $param ($cmd =~ m{^[(\h]* (\w\S*)}gx) {
76                 $param eq 'cat' or
77                 runres(['which', $param])
78                         or diag("dependency $param missing at $ref\n$cmd"), skip($name, 1);
79         }
80
81         # run and report unexpected results
82         my $output = runres($cmd);
83         ok(!!$output, $name)
84                 or diag("command at $ref\n$cmd\n" . ($@ || 'empty output'));
85         defined $output or next;
86
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;
91         }
92 }}
93
94 sub runres {
95         my ($cmd) = @_;
96         ref $cmd eq 'ARRAY'
97                 or $cmd = [bash => -c => "set -o pipefail\n$cmd"];
98         eval {
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";
102                 return $output;
103         };
104 }
105
106 done_testing();