Skip to content

Commit

Permalink
Throw an exception if the program exited non-zero
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur Axel 'fREW' Schmidt committed May 16, 2015
1 parent c556ede commit dd8d1d5
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Revision history for {{$dist->name}}

{{$NEXT}}
- Throw an exception if the program exited non-zero

0.001001 2014-06-02 17:15:33-05:00 America/Chicago
- Hopefully fix tests on MSWin32
Expand Down
1 change: 1 addition & 0 deletions cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ requires 'Moose';
requires 'Pod::Weaver::Role::Dialect';
requires 'Pod::Elemental::Transformer';
requires 'Capture::Tiny';
requires 'IPC::System::Simple';
requires 'Moose::Autobox';
requires 'namespace::clean';

Expand Down
15 changes: 13 additions & 2 deletions lib/Pod/Weaver/Plugin/Exec.pm
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ package
use Moose;
with 'Pod::Elemental::Transformer';

use Capture::Tiny 'capture_stdout';
use Capture::Tiny 'capture';
use IPC::System::Simple 'system';
use Moose::Autobox;
use namespace::clean;

Expand All @@ -35,7 +36,17 @@ package
and $para->children->[0]->isa('Pod::Elemental::Element::Pod5::Data');

chomp(my $command = $para->children->[0]->content);
my $out = capture_stdout { system($command) };

local $@;
my ($out, $err, $ok) = capture {
eval { system $command; 1 };
};

chomp $out;
chomp $err;
die "Command '$command' failed\nSTDOUT: $out\nSTDERR: $err\n"
unless $ok;

$out = join "\n", map " $_", split /\n/, $out;

my $new_doc =
Expand Down
39 changes: 39 additions & 0 deletions t/fail-on-error.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use strict;
use warnings;

use Test::More;
use Test::Fatal;

use Pod::Elemental;
use Pod::Elemental::Transformer::Pod5;
use Pod::Weaver::Plugin::Exec;

my $str = do { local $/; <DATA> };

my $doc = Pod::Elemental->read_string($str);

Pod::Elemental::Transformer::Pod5->new->transform_node($doc);

my $e = exception {
Pod::Elemental::Transformer::Exec->new->transform_node($doc)
};

ok $e, 'correctly got exception';
like $e, qr/STDOUT: station!/, 'STDOUT included';
like $e, qr/STDERR: got an error :\(/, 'STDERR included';
like $e, qr/Command 'perl -E.*' failed/, 'Command included';

note "Exception: $e";


done_testing;

__DATA__
=pod
=head1 Welcome to Pod!
=for exec
perl -E"say 'station!'; die 'got an error :('"
Right??
2 changes: 2 additions & 0 deletions t/simple.t
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Right??
=cut
POD

note('POD: ' . $doc->as_pod_string);

done_testing;

__DATA__
Expand Down

0 comments on commit dd8d1d5

Please sign in to comment.