| 1 |
#!/usr/bin/perl -w |
| 2 |
use strict; |
| 3 |
use Test::More; |
| 4 |
use FindBin qw($Bin); |
| 5 |
use constant TMPFILE => "$Bin/unlink_test_delete_me"; |
| 6 |
use constant NO_SUCH_FILE => 'this_file_had_better_not_be_here_at_all'; |
| 7 |
|
| 8 |
make_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 |
| 14 |
unlink TMPFILE; |
| 15 |
|
| 16 |
# Check it's gone |
| 17 |
if(-e TMPFILE) {plan skip_all => "Failed to delete test file: $!";} |
| 18 |
|
| 19 |
# Re-create file |
| 20 |
make_file(TMPFILE); |
| 21 |
|
| 22 |
# Check that file now exists |
| 23 |
-e TMPFILE or plan skip_all => "Failed to create test file"; |
| 24 |
|
| 25 |
plan tests => 10; |
| 26 |
|
| 27 |
# Try to delete file (this should succeed) |
| 28 |
eval { |
| 29 |
use autodie; |
| 30 |
|
| 31 |
unlink TMPFILE; |
| 32 |
}; |
| 33 |
is($@, "", "Unlink appears to have been successful"); |
| 34 |
ok(! -e TMPFILE, "File does not exist"); |
| 35 |
|
| 36 |
# Try to delete file again (this should fail) |
| 37 |
eval { |
| 38 |
use autodie; |
| 39 |
|
| 40 |
unlink TMPFILE; |
| 41 |
}; |
| 42 |
ok($@, "Re-unlinking file causes failure."); |
| 43 |
isa_ok($@, "autodie::exception", "... errors are of the correct type"); |
| 44 |
ok($@->matches("unlink"), "... it's also a unlink object"); |
| 45 |
ok($@->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 |
|
| 50 |
make_file(TMPFILE); |
| 51 |
ok(-e TMPFILE, "Sanity: file exists"); |
| 52 |
|
| 53 |
eval { |
| 54 |
use autodie; |
| 55 |
|
| 56 |
unlink TMPFILE, NO_SUCH_FILE; |
| 57 |
}; |
| 58 |
|
| 59 |
ok($@, "Failure when trying to delete missing file in list."); |
| 60 |
isa_ok($@, "autodie::exception", "... errors are of the correct type"); |
| 61 |
is($@->return,1, "Failure on deleting missing file but true return value"); |
| 62 |
|
| 63 |
sub 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 |
} |