X-Git-Url: http://git.shiar.nl/barcat.git/blobdiff_plain/38eb915e53017bb02788efc30a8650a8b322a753..fd1358b672c565160eb163da00b404325864114d:/t/examples.t diff --git a/t/examples.t b/t/examples.t index cd7f1b3..0b5e778 100755 --- a/t/examples.t +++ b/t/examples.t @@ -1,6 +1,7 @@ #!/usr/bin/env perl use 5.014; use warnings; +use re '/ms'; use Test::More; my %CMDARGS = ( @@ -15,18 +16,39 @@ open my $input, '<', $filename local $/ = "\n\n"; while (readline $input) { + # find code snippets in the appropriate section /^=head1 EXAMPLES/ ... /^=head1/ or next; /^\h/ or next; 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/\|.*//; # 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); + $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) or diag($cmd); } done_testing();