File: 70inplace.t

package info (click to toggle)
libmail-box-perl 2.117-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,828 kB
  • ctags: 1,564
  • sloc: perl: 23,381; makefile: 2
file content (218 lines) | stat: -rw-r--r-- 5,075 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env perl

#
# Test writing of mbox folders using the inplace policy.
#

use strict;
use warnings;

use lib qw(. .. tests);
use Tools;

use Test::More tests => 116;
use File::Compare;
use File::Copy;

use Mail::Box::Mbox;

#
# We will work with a copy of the original to avoid that we write
# over our test file.
#

unlink $cpy;
copy $src, $cpy
    or die "Cannot create test folder $cpy: $!\n";

my $folder = new Mail::Box::Mbox
  ( folder       => "=$cpyfn"
  , folderdir    => $folderdir
  , lock_type    => 'NONE'
  , extract      => 'LAZY'
  , access       => 'rw'
  , log          => 'NOTICES'
#, trace => 'NOTICES'
  );

die "Couldn't read $cpy: $!\n"
     unless $folder;

#
# None of the messages should be modified.
#

my $modified = 0;
$modified ||= $_->modified foreach $folder->messages;
ok(!$modified);

#
# Write unmodified folder.  This should be ready immediately.
#

ok($folder->write(policy => 'INPLACE'));
my @progress = $folder->report('PROGRESS');
ok(grep m/not changed/, @progress);

#
# All messages must still be delayed.
#

my $msgnr = 0;
foreach ($folder->messages)
{   my $body = $_->body;
    if($body->isDelayed || $body->isNested || $body->isMultipart) {ok(1)}
    else { warn "Warn: failed message $msgnr.\n"; ok(0) }
    $msgnr++;
}

#
# Now MODIFY the folder, and write it again.
#

my $modmsgnr = 30;
$folder->message($modmsgnr)->modified(1);

ok($folder->write(policy => 'INPLACE'));
ok(not $folder->modified);

#
# All before messages before $modmsgnr must still be delayed.
#

$msgnr = 0;
foreach ($folder->messages)
{   my $body = $_->body;
    my $right = ($body->isDelayed || $body->isMultipart || $body->isNested)
        ? ($msgnr < $modmsgnr) : ($msgnr >= $modmsgnr);
    ok($right,         "delayed message $msgnr");
    $msgnr++;
}

my @folder_subjects = sort map {$_->get('subject')||''} $folder->messages;
my $folder_messages = $folder->messages;

ok(not $folder->modified);
$folder->close;

# Check also if the subjects are the same.
# Try to read it back

my $copy = new Mail::Box::Mbox
  ( folder    => "=$cpyfn"
  , folderdir => $folderdir
  , lock_type => 'NONE'
  , extract   => 'ALWAYS'
  );

ok(defined $copy);
cmp_ok($copy->messages, "==", $folder_messages);

# Check also if the subjects are the same.

my @copy_subjects   = sort map {$_->get('subject')||''} $copy->messages;
my $msg12subject    = $copy->message(12)->get('subject');
ok(defined $msg12subject, "got msg12 subject");

while(@folder_subjects)
{   last unless shift(@folder_subjects) eq shift(@copy_subjects);
}
ok(!@folder_subjects);

#
# Check wether inplace rewrite works when a few messages are deleted.
#

$copy = new Mail::Box::Mbox
  ( folder       => "=$cpyfn"
  , folderdir    => $folderdir
  , lock_type    => 'NONE'
  , extract      => 'LAZY'
  , access       => 'rw'
  , log          => 'NOTICES'
#, trace => 'NOTICES'
  );

die "Couldn't read $cpyfn: $!\n"
     unless $copy;

$copy->message(-1)->delete;   # last flagged for deletion
ok($copy->message(-1)->deleted);

ok($copy->write(policy => 'INPLACE'), "write folder with fewer messsages");

$copy = new Mail::Box::Mbox
  ( folder    => "=$cpyfn"
  , folderdir => $folderdir
  , lock_type => 'NONE'
  , extract   => 'ALWAYS'
  );

ok(defined $copy,                                 "Reopen succesful");
cmp_ok($copy->messages+1, "==", $folder_messages, "1 message less");

#
# Rewrite it again, with again 1 fewer message
#

$copy->close;
ok(! defined $copy,                             "Folder is really closed");

$copy = new Mail::Box::Mbox
  ( folder    => "=$cpyfn"
  , folderdir => $folderdir
  , lock_type => 'NONE'
  , extract   => 'ALWAYS'
  , access    => 'rw'
  );

cmp_ok($copy->messages, "==", $folder_messages-1, "1 message still away");

$copy->message(10)->delete;   # some other, doesn't matter
ok($copy->message(10)->deleted);

ok($copy->write(policy => 'INPLACE'), "write folder with fewer messsages");

$copy = new Mail::Box::Mbox
  ( folder    => "=$cpyfn"
  , folderdir => $folderdir
  , lock_type => 'NONE'
  , extract   => 'ALWAYS'
  );

cmp_ok($copy->messages, "==", $folder_messages-2, "2 messages fewer");
is($copy->message(11)->get('subject'), $msg12subject, "move message");

#
# Rewrite it again, with again 1 fewer message: this time the first message
#

$copy->close;
ok(! defined $copy,                             "Folder is really closed");

$copy = new Mail::Box::Mbox
  ( folder    => "=$cpyfn"
  , folderdir => $folderdir
  , lock_type => 'NONE'
  , extract   => 'ALWAYS'
  , access    => 'rw'
  );

cmp_ok($copy->messages, "==", $folder_messages-2, "2 message still away");

$copy->message(0)->delete;    # first flagged for deletion
ok($copy->message(0)->deleted);

ok($copy->write(policy => 'INPLACE'), "write folder with fewer messsages");

$copy = new Mail::Box::Mbox
  ( folder    => "=$cpyfn"
  , folderdir => $folderdir
  , lock_type => 'NONE'
  , extract   => 'ALWAYS'
  );

cmp_ok($copy->messages, "==", $folder_messages-3, "3 messages fewer");
is($copy->message(10)->get('subject'), $msg12subject, "move message");

unlink $cpy;