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 224 225 226 227 228 229 230 231 232
|
#!/usr/bin/perl
# Test that we can reset a file midway through parsing.
use strict;
use Test::More;
use lib 't';
use Mail::Mbox::MessageParser;
use Mail::Mbox::MessageParser::Config;
use File::Spec::Functions qw(:ALL);
use Test::Utils;
use FileHandle;
eval 'require Storable;';
mkdir catfile('t','temp'), 0700;
plan (tests => 6 );
my $source_filename = 't/mailboxes/mailarc-1.txt';
my $mailbox_filename = 't/temp/tempmailbox';
{
print "Testing modified mailbox with Perl implementation\n";
InitializeMailbox1($source_filename,$mailbox_filename);
TestModifiedMailbox($source_filename,$mailbox_filename,0,0,
GetSecondPart1($source_filename));
}
SKIP:
{
print "Testing modified mailbox with Cache implementation\n";
skip('Storable not installed',1) unless defined $Storable::VERSION;
InitializeMailbox1($source_filename,$mailbox_filename);
InitializeCache($mailbox_filename);
TestModifiedMailbox($source_filename,$mailbox_filename,1,0,
GetSecondPart1($source_filename));
}
SKIP:
{
print "Testing modified mailbox with Grep implementation\n";
skip('GNU grep not available',1)
unless defined $Mail::Mbox::MessageParser::Config{'programs'}{'grep'};
InitializeMailbox1($source_filename,$mailbox_filename);
TestModifiedMailbox($source_filename,$mailbox_filename,0,1,
GetSecondPart1($source_filename));
}
{
print "Testing modified mailbox with Perl implementation\n";
InitializeMailbox2($source_filename,$mailbox_filename);
TestModifiedMailbox($source_filename,$mailbox_filename,0,0,
GetSecondPart2($source_filename));
}
SKIP:
{
print "Testing modified mailbox with Cache implementation\n";
skip('Storable not installed',1) unless defined $Storable::VERSION;
InitializeMailbox2($source_filename,$mailbox_filename);
InitializeCache($mailbox_filename);
TestModifiedMailbox($source_filename,$mailbox_filename,1,0,
GetSecondPart2($source_filename));
}
SKIP:
{
print "Testing modified mailbox with Grep implementation\n";
skip('GNU grep not available',1)
unless defined $Mail::Mbox::MessageParser::Config{'programs'}{'grep'};
InitializeMailbox2($source_filename,$mailbox_filename);
TestModifiedMailbox($source_filename,$mailbox_filename,0,1,
GetSecondPart2($source_filename));
}
# ---------------------------------------------------------------------------
sub InitializeMailbox1
{
my $source_filename = shift;
my $mailbox_filename = shift;
open SOURCE, $source_filename;
local $/ = undef;
my $mail = <SOURCE>;
close SOURCE;
my ($firstpart) = $mail =~ /(.*)From .*/s;
open MAILBOX, ">$mailbox_filename";
print MAILBOX $firstpart;
close MAILBOX;
}
# ---------------------------------------------------------------------------
sub InitializeMailbox2
{
my $source_filename = shift;
my $mailbox_filename = shift;
open SOURCE, $source_filename;
local $/ = undef;
my $mail = <SOURCE>;
close SOURCE;
my ($firstpart) = $mail =~ /(..*?)From .*/s;
open MAILBOX, ">$mailbox_filename";
print MAILBOX $firstpart;
close MAILBOX;
}
# ---------------------------------------------------------------------------
sub TestModifiedMailbox
{
my $source_filename = shift;
my $mailbox_filename = shift;
my $enable_cache = shift;
my $enable_grep = shift;
my $second_part = shift;
my $testname = [splitdir($0)]->[-1];
$testname =~ s/\.t//;
my ($folder_name) = $source_filename =~ /\/([^\/\\]*)\.txt$/;
my $output_filename = catfile('t','temp',
"${testname}_${folder_name}_${enable_cache}_${enable_grep}.stdout");
my $output = new FileHandle(">$output_filename");
binmode $output;
my $filehandle = new FileHandle($mailbox_filename);
my $cache_file = catfile('t','temp','cache');
Mail::Mbox::MessageParser::SETUP_CACHE({'file_name' => $cache_file})
if $enable_cache;
my $folder_reader =
new Mail::Mbox::MessageParser( {
'file_name' => $mailbox_filename,
'file_handle' => $filehandle,
'enable_cache' => $enable_cache,
'enable_grep' => $enable_grep,
} );
die $folder_reader unless ref $folder_reader;
print $output $folder_reader->prologue;
# Read just 1 email
print $output ${$folder_reader->read_next_email()};
AppendToMailbox($mailbox_filename, $second_part);
# This is the main loop. It's executed once for each email
while(!$folder_reader->end_of_file())
{
print $output ${ $folder_reader->read_next_email() };
}
$output->close();
CheckDiffs([$source_filename,$output_filename]);
}
# ---------------------------------------------------------------------------
sub GetSecondPart1
{
my $source_filename = shift;
open SOURCE, $source_filename;
local $/ = undef;
my $mail = <SOURCE>;
close SOURCE;
my ($secondpart) = $mail =~ /.*(From .*)/s;
return $secondpart;
}
# ---------------------------------------------------------------------------
sub GetSecondPart2
{
my $source_filename = shift;
open SOURCE, $source_filename;
local $/ = undef;
my $mail = <SOURCE>;
close SOURCE;
my ($secondpart) = $mail =~ /..*?(From .*)/s;
return $secondpart;
}
# ---------------------------------------------------------------------------
sub AppendToMailbox
{
my $mailbox_filename = shift;
my $email = shift;
open MAILBOX, ">>$mailbox_filename";
print MAILBOX $email;
close MAILBOX;
}
|