1#!/usr/bin/perl -w 2use strict; 3use Test::More; 4use FindBin qw($Bin); 5use constant TMPFILE => "$Bin/unlink_test_delete_me"; 6 7# Create a file to practice unlinking 8open(my $fh, ">", TMPFILE) 9 or plan skip_all => "Unable to create test file: $!"; 10print {$fh} "Test\n"; 11close $fh; 12 13# Check that file now exists 14-e TMPFILE or plan skip_all => "Failed to create test file"; 15 16# Check we can unlink 17unlink TMPFILE; 18 19# Check it's gone 20if(-e TMPFILE) {plan skip_all => "Failed to delete test file: $!";} 21 22# Re-create file 23open(my $fh2, ">", TMPFILE) 24 or plan skip_all => "Unable to create test file: $!"; 25print {$fh2} "Test\n"; 26close $fh2; 27 28# Check that file now exists 29-e TMPFILE or plan skip_all => "Failed to create test file"; 30 31plan tests => 6; 32 33# Try to delete directory (this should succeed) 34eval { 35 use autodie; 36 37 unlink TMPFILE; 38}; 39is($@, "", "Unlink appears to have been successful"); 40ok(! -e TMPFILE, "File does not exist"); 41 42# Try to delete file again (this should fail) 43eval { 44 use autodie; 45 46 unlink TMPFILE; 47}; 48ok($@, "Re-unlinking file causes failure."); 49isa_ok($@, "autodie::exception", "... errors are of the correct type"); 50ok($@->matches("unlink"), "... it's also a unlink object"); 51ok($@->matches(":filesys"), "... and a filesys object"); 52 53