a331ce352d5322d9aa26b8f4e0af1973ea7c1555
[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         # find scriptlets in the appropriate section
25         /^=head1 EXAMPLES/ ... /^=head1/ or next;
26         /^\h/ or next;  # indented code snippet
27         /\A\h*>/ and next;  # psql prompt
28         chomp;
29         my $cmd = $_;
30         my $ref = "$filename line $.";
31
32         # store curl downloads
33         s{\bcurl (\S*)(?<param>[^|]*)}{
34                 my $url = $1;
35                 my @params = split ' ', $+{param};
36                 my $ext = (
37                         $cmd =~ /\bxml/     ? 'xml'  :
38                         $cmd =~ / jq /      ? 'json' :
39                         $cmd =~ /[=.]csv\b/ ? 'csv'  :
40                                               'txt'
41                 );
42                 my ($domain, $path) = $url =~ m{//([^/]+) .*/ ([^/]*) \z}x;
43                 $path =~ s/\.$ext\z//;
44                 my $cache = join '.', $path =~ tr/./_/r, $domain, $ext;
45                 $cache = "sample/data/$cache";
46                 SKIP: {
47                         -e $cache and skip($url, 1);
48                         ok(defined runres(['curl', '-sS', $url, '-o', $cache, @params]), $url)
49                                 or diag("download at $ref: $@");
50                 }
51                 "cat $cache"
52         }e;
53
54         # compose an identifier from significant parts
55         do {
56                 s/^\h+//;             # indentation
57                 s/\\\n\s*//g;         # line continuations
58                 s/^[(\h]+//;          # subshell
59                 s/^echo\ .*?\|\s*//;  # preceding input
60                 s/'(\S+)[^']*'/$1/g;  # quoted arguments
61                 s/\h*\|.*//;          # subsequent pipes
62                 s/^cat\ (?:\S+\/)?//; # local file
63         } for my $name = $cmd;
64
65         # prepare shell command to execute
66         while (my ($subcmd, $args) = each %CMDARGS) {
67                 $subcmd .= " \\K", $args .= ' ' unless $subcmd =~ m/\\K/;
68                 $cmd =~ s/\b$subcmd/$args/;
69         }
70
71         # run and report unexpected results
72         my $output = runres($cmd);
73         ok(!!$output, $name)
74                 or diag("command at $ref\n$cmd\n" . ($@ || 'empty output'));
75         defined $output or next;
76
77         # record output for review
78         my $numprefix = sprintf '%02d', Test::More->builder->current_test;
79         if (open my $record, '>', "sample/out/t$numprefix-$name.txt") {
80                 print {$record} $output;
81         }
82 }
83
84 sub runres {
85         my ($cmd) = @_;
86         ref $cmd eq 'ARRAY'
87                 or $cmd = [bash => -c => "set -o pipefail\n$cmd"];
88         eval {
89                 run($cmd, \undef, \my $output, \my $error);
90                 die("error message:\n".($error =~ s/^/    /gr)."\n") if $error;
91                 $? == 0 or die "exit status ", $? >> 8, "\n";
92                 return $output;
93         };
94 }
95
96 done_testing();