Skip to content

Commit

Permalink
Refactor the sigwarn handler
Browse files Browse the repository at this point in the history
    Having the same code twice is ooglay
  • Loading branch information
sartak committed Oct 19, 2010
1 parent 4fa3a1b commit 4e645ce
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions lib/Plack/Middleware/Test/StashWarnings.pm
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,29 @@ sub call {
return [ 200, ["Content-Type", "application/x-storable"], [ $self->dump_warnings ] ];
}

local $SIG{__WARN__} = sub {
push @{ $self->{stashed_warnings} }, @_;
warn @_ if $ENV{TEST_VERBOSE};
};

my $ret = $self->app->($env);
my $ret = $self->_stash_warnings_for($self->app, $env);

# for the streaming API, we need to re-instate the dynamic sigwarn handler
# around the streaming callback
if (ref($ret) eq 'CODE') {
my $original_ret = $ret;
$ret = sub {
local $SIG{__WARN__} = sub {
push @{ $self->{stashed_warnings} }, @_;
warn @_ if $ENV{TEST_VERBOSE};
};
$original_ret->(@_);
};
return sub { $self->_stash_warnings_for($ret, @_) };
}

return $ret;
}

sub _stash_warnings_for {
my $self = shift;
my $code = shift;

local $SIG{__WARN__} = sub {
push @{ $self->{stashed_warnings} }, @_;
warn @_ if $ENV{TEST_VERBOSE};
};

return $code->(@_);
}

sub dump_warnings {
my $self = shift;

Expand Down

0 comments on commit 4e645ce

Please sign in to comment.