From b7fb38421c30b0419a30311c1bde34706aa60fb8 Mon Sep 17 00:00:00 2001 From: Lorry Tar Creator Date: Sun, 28 Jun 2015 17:50:34 +0000 Subject: Path-Tiny-0.070 --- t/lib/TestUtils.pm | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 t/lib/TestUtils.pm (limited to 't/lib/TestUtils.pm') diff --git a/t/lib/TestUtils.pm b/t/lib/TestUtils.pm new file mode 100644 index 0000000..d91bedb --- /dev/null +++ b/t/lib/TestUtils.pm @@ -0,0 +1,51 @@ +use 5.008001; +use strict; +use warnings; + +package TestUtils; + +use Carp; +use Cwd qw/getcwd/; +use File::Temp 0.19 (); + +use Exporter; +our @ISA = qw/Exporter/; +our @EXPORT = qw( + exception + tempd +); + +# If we have Test::FailWarnings, use it +BEGIN { + eval { require Test::FailWarnings; 1 } and do { Test::FailWarnings->import }; +} + +sub exception(&) { + my $code = shift; + my $success = eval { $code->(); 1 }; + my $err = $@; + return '' if $success; + croak "Execution died, but the error was lost" unless $@; + return $@; +} + +sub tempd { + my $guard = TestUtils::_Guard->new( + { + temp => File::Temp->newdir, + origin => getcwd(), + code => sub { chdir $_[0]{origin} }, + } + ); + chdir $guard->{temp} + or croak("Couldn't chdir: $!"); + return $guard; +} + +package TestUtils::_Guard; + +sub new { bless $_[1], $_[0] } + +sub DESTROY { $_[0]{code}->( $_[0] ) } + +1; -- cgit v1.2.1