1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
#!/usr/bin/perl -T
#
# Test creation/deletion and listing of folders.
#
use strict;
use warnings;
use lib qw(. .. tests);
use Tools;
use Test::More tests => 28;
use File::Copy;
use File::Spec;
use Mail::Box::Mbox;
use Mail::Message::Construct;
my $top = File::Spec->catdir('folders', 'Mail');
clean_dir $top;
#
# Create a nice structure which looks like a set of mbox folders.
#
sub dir($;$)
{ my $dirname = shift;
$dirname = File::Spec->catdir($dirname, shift) if @_;
die "Cannot create $dirname: $!\n"
unless -d $dirname || mkdir $dirname, 0700;
$dirname;
}
sub folder($$;$)
{ my $filename = File::Spec->catfile(shift, shift);
my $content = shift || $src; # by default copies whole default mbox
copy $content, $filename
or die "Cannot copy $content to $filename: $!\n";
}
clean_dir $top; # restart
dir $top;
folder $top, "f1", $0;
folder $top, "f2";
{ # Create an empty file.
my $f = IO::File->new(File::Spec->catfile($top,'f3'), 'w')
or die "Empty? $top/f3: $!";
$f->close;
}
my $dir = dir $top, "sub1";
folder $dir, "s1f1";
folder $dir, "s1f2";
folder $dir, "s1f3";
dir $top, "sub2"; # empty dir
folder $top, "f4";
$dir = dir $top, "f4.d"; # fake subfolder
folder $dir, "f4f1";
folder $dir, "f4f2";
folder $dir, "f4f3";
my $success =
compare_lists [ sort Mail::Box::Mbox->listSubFolders(folderdir => $top) ]
, [ qw/f1 f2 f3 f4 sub1 sub2/ ];
ok($success, 'Initial tree creation');
unless($success)
{ require File::Find;
File::Find::find( { wanted => sub {print STDERR "$_\n" }
, no_chdir => 1
}, $top);
exit 1;
}
ok(compare_lists [ sort Mail::Box::Mbox->listSubFolders(folderdir => $top) ]
, [ qw/f1 f2 f3 f4 sub1 sub2/ ]
);
ok(compare_lists [ sort Mail::Box::Mbox->listSubFolders
( folderdir => $top
, skip_empty => 1
) ]
, [ qw/f1 f2 f4 sub1/ ]
);
ok(compare_lists [ sort Mail::Box::Mbox->listSubFolders
( folderdir => $top
, check => 1
) ]
, [ qw/f2 f3 f4 sub1 sub2/ ]
);
ok(compare_lists [ sort Mail::Box::Mbox->listSubFolders
( folderdir => File::Spec->catfile($top, "f4.d")
) ]
, [ qw/f4f1 f4f2 f4f3/ ]
);
ok(compare_lists [ sort Mail::Box::Mbox->listSubFolders
( folderdir => $top
, folder => "=f4.d"
)
]
, [ qw/f4f1 f4f2 f4f3/ ]
);
ok(compare_lists [ sort Mail::Box::Mbox->listSubFolders
( folder => File::Spec->catfile($top, "f4")) ]
, [ qw/f4f1 f4f2 f4f3/ ]
);
#
# Open a folder in a sub-dir which uses the extention.
#
my $folder = Mail::Box::Mbox->new
( folderdir => $top
, folder => '=f4/f4f2'
, lock_type => 'NONE'
);
ok(defined $folder, 'open =f4/f4f2');
die unless defined $folder;
cmp_ok($folder->messages, "==", 45, 'found all messages');
$folder->close;
#
# Open a new folder.
#
ok(! -f File::Spec->catfile($top, 'f4', 'newfolder'));
Mail::Box::Mbox->create('=f4/newfolder', folderdir => $top);
ok(-f File::Spec->catfile($top, "f4.d", "newfolder"));
$folder = Mail::Box::Mbox->new
( folderdir => $top
, folder => '=f4/newfolder'
, access => 'rw'
, lock_type => 'NONE'
);
ok($folder);
cmp_ok($folder->messages, "==", 0);
my $msg = Mail::Message->build
( From => 'me@example.com'
, To => 'you@anywhere.aq'
, Subject => 'Just a try'
, data => [ "a short message\n", "of two lines.\n" ]
);
$folder->addMessage($msg);
cmp_ok($folder->messages, "==", 1);
$folder->close;
ok(-s File::Spec->catfile($top, 'f4.d', 'newfolder'));
#
# Delete a folder.
#
$folder = Mail::Box::Mbox->new
( folderdir => $top
, folder => '=f4'
, access => 'rw'
, lock_type => 'NONE'
);
ok(defined $folder. 'open folder =f4');
die unless defined $folder;
ok(-f File::Spec->catfile($top, "f4"), 'folder-file found');
$folder->delete(recurse=>1); # remove folder contents
$folder->close if defined $folder;
ok(! -f File::Spec->catfile($top, "f4"), 'empty folder clean-up');
ok(! -d File::Spec->catfile($top, "f4.d"), 'subfolder dir clean-up');
#
# Write a folder, but at the same place is a subdir. The subdir should
# be moved to a name ending on `.d'
#
my $sub1 = File::Spec->catfile($top, "sub1");
ok(-d $sub1, 'dir to be promoted');
ok(Mail::Box::Mbox->create('=sub1', folderdir => $top),
'promote dir to subfolder');
ok(-d File::Spec->catfile($top, "sub1.d"), 'check promotion');
ok(-f $sub1, 'new folder exists');
ok(-z $sub1, 'new folder is empty');
$folder = Mail::Box::Mbox->new
( folderdir => $top
, folder => '=sub1'
, access => 'rw'
, lock_type => 'NONE'
);
ok(defined $folder, 'open empty subfolder');
cmp_ok($folder->messages, "==", 0, 'subfolder is empty');
my $msg2 = Mail::Message->build
( From => 'me@example.com'
, To => 'you@anywhere.aq'
, Subject => 'Just a try'
, data => [ "a short message\n", "of two lines.\n" ]
);
$folder->addMessage($msg2);
cmp_ok($folder->messages, "==", 1, 'one message into empty folder');
$folder->close;
ok(-s $sub1, 'subfolder must exist now');
|