1#!/usr/bin/perl -w 2use strict; 3use Test::More; 4use FindBin qw($Bin); 5use constant TMPFILE => "$Bin/unlink_test_delete_me"; 6use constant NO_SUCH_FILE => 'this_file_had_better_not_be_here_at_all'; 7 8make_file(TMPFILE); 9 10# Check that file now exists 11-e TMPFILE or plan skip_all => "Failed to create test file"; 12 13# Check we can unlink 14unlink TMPFILE; 15 16# Check it's gone 17if(-e TMPFILE) {plan skip_all => "Failed to delete test file: $!";} 18 19# Re-create file 20make_file(TMPFILE); 21 22# Check that file now exists 23-e TMPFILE or plan skip_all => "Failed to create test file"; 24 25plan tests => 10; 26 27# Try to delete file (this should succeed) 28eval { 29 use autodie; 30 31 unlink TMPFILE; 32}; 33is($@, "", "Unlink appears to have been successful"); 34ok(! -e TMPFILE, "File does not exist"); 35 36# Try to delete file again (this should fail) 37eval { 38 use autodie; 39 40 unlink TMPFILE; 41}; 42ok($@, "Re-unlinking file causes failure."); 43isa_ok($@, "autodie::exception", "... errors are of the correct type"); 44ok($@->matches("unlink"), "... it's also a unlink object"); 45ok($@->matches(":filesys"), "... and a filesys object"); 46 47# Autodie should throw if we delete a LIST of files, but can only 48# delete some of them. 49 50make_file(TMPFILE); 51ok(-e TMPFILE, "Sanity: file exists"); 52 53eval { 54 use autodie; 55 56 unlink TMPFILE, NO_SUCH_FILE; 57}; 58 59ok($@, "Failure when trying to delete missing file in list."); 60isa_ok($@, "autodie::exception", "... errors are of the correct type"); 61is($@->return,1, "Failure on deleting missing file but true return value"); 62 63sub make_file { 64 open(my $fh, ">", $_[0]) 65 or plan skip_all => "Unable to create test file $_[0]: $!"; 66 print {$fh} "Test\n"; 67 close $fh; 68} 69