1#!/usr/bin/perl -w
2use strict;
3use Test::More;
4use FindBin qw($Bin);
5use constant TMPDIR => "$Bin/mkdir_test_delete_me";
6use constant ERROR_REGEXP => qr{Can't mkdir\('\Q${\(TMPDIR)}\E', 0777\):};
7use constant SINGLE_DIGIT_ERROR_REGEXP => qr{Can't mkdir\('\Q${\(TMPDIR)}\E', 0010\):};
8
9# Delete our directory if it's there
10rmdir TMPDIR;
11
12# See if we can create directories and remove them
13mkdir TMPDIR or plan skip_all => "Failed to make test directory";
14
15# Test the directory was created
16-d TMPDIR or plan skip_all => "Failed to make test directory";
17
18# Try making it a second time (this should fail)
19if(mkdir TMPDIR) { plan skip_all => "Attempt to remake a directory succeeded";}
20
21# See if we can remove the directory
22rmdir TMPDIR or plan skip_all => "Failed to remove directory";
23
24# Check that the directory was removed
25if(-d TMPDIR) { plan skip_all => "Failed to delete test directory"; }
26
27# Try to delete second time
28if(rmdir TMPDIR) { plan skip_all => "Able to rmdir directory twice"; }
29
30plan tests => 18;
31
32# Create a directory (this should succeed)
33eval {
34	use autodie;
35
36	mkdir TMPDIR;
37};
38is($@, "", "mkdir returned success");
39ok(-d TMPDIR, "Successfully created test directory");
40
41# Try to create it again (this should fail)
42eval {
43	use autodie;
44
45	mkdir TMPDIR, 0777;
46};
47ok($@, "Re-creating directory causes failure.");
48isa_ok($@, "autodie::exception", "... errors are of the correct type");
49ok($@->matches("mkdir"), "... it's also a mkdir object");
50ok($@->matches(":filesys"), "... and a filesys object");
51like($@, ERROR_REGEXP, "Message should include numeric mask in octal form");
52
53eval {
54        use autodie;
55
56        mkdir TMPDIR, 8;
57};
58ok($@, "Re-creating directory causes failure.");
59isa_ok($@, "autodie::exception", "... errors are of the correct type");
60ok($@->matches("mkdir"), "... it's also a mkdir object");
61ok($@->matches(":filesys"), "... and a filesys object");
62like($@, SINGLE_DIGIT_ERROR_REGEXP, "Message should include numeric mask in octal form");
63
64# Try to delete directory (this should succeed)
65eval {
66	use autodie;
67
68	rmdir TMPDIR;
69};
70is($@, "", "rmdir returned success");
71ok(! -d TMPDIR, "Successfully removed test directory");
72
73# Try to delete directory again (this should fail)
74eval {
75	use autodie;
76
77	rmdir TMPDIR;
78};
79ok($@, "Re-deleting directory causes failure.");
80isa_ok($@, "autodie::exception", "... errors are of the correct type");
81ok($@->matches("rmdir"), "... it's also a rmdir object");
82ok($@->matches(":filesys"), "... and a filesys object");
83
84