diff options
author | Lorry Tar Creator <lorry-tar-importer@lorry> | 2015-06-28 17:50:34 +0000 |
---|---|---|
committer | Lorry Tar Creator <lorry-tar-importer@lorry> | 2015-06-28 17:50:34 +0000 |
commit | b7fb38421c30b0419a30311c1bde34706aa60fb8 (patch) | |
tree | 57f2bcea8db83589377c8bc0253a10c66df46d72 /t/lib | |
download | Path-Tiny-tarball-b7fb38421c30b0419a30311c1bde34706aa60fb8.tar.gz |
Path-Tiny-0.070HEADPath-Tiny-0.070master
Diffstat (limited to 't/lib')
-rw-r--r-- | t/lib/TestUtils.pm | 51 |
1 files changed, 51 insertions, 0 deletions
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; |