@@ -1310,6 +1310,73 @@ def test_message_separator(self):
13101310 data = f .read ()
13111311 self .assertEqual (data [- 3 :], '0\n \n ' )
13121312
1313+ # Test reading an mbox file with un-prefixed From in body text
1314+ # currently generates 2 messages
1315+ def _test_read_mbox (self , matcher = 0 , count = 2 ):
1316+ # create a basic mbox file
1317+ self ._box .add ('From: foo\n \n Hello\n ' )
1318+ # Add an un-prefixed From to create a second entry
1319+ self ._box ._file .write (b'From time to time\n ' )
1320+ self ._box .close ()
1321+ # re-read it using the provided matcher
1322+ if matcher == 0 : # not provided, so omit
1323+ self ._box = mailbox .mbox (self ._path , create = False )
1324+ else :
1325+ self ._box = mailbox .mbox (self ._path , create = False , from_matcher = matcher )
1326+ # How many messages were found?
1327+ self .assertEqual (len (self ._box .keys ()), count )
1328+
1329+ def test_read_mbox_omitted (self ):
1330+ self ._test_read_mbox ()
1331+
1332+ def test_read_mbox_none (self ):
1333+ self ._test_read_mbox (None )
1334+
1335+ def test_read_mbox_default (self ):
1336+ self ._test_read_mbox (lambda line : re .match (b'From ' , line ))
1337+
1338+ def test_read_mbox_full1 (self ):
1339+ self ._test_read_mbox ('full' , count = 1 )
1340+
1341+ def test_read_mbox_regex1 (self ):
1342+ import re
1343+ # stricter matching should only find one message
1344+ self ._test_read_mbox (lambda line : re .match (b'From .+ \\ d\\ d\\ d\\ d\\ r?\\ n' , line ), count = 1 )
1345+
1346+ def test_read_mbox_regex2 (self ):
1347+ import re
1348+ # invalid, so don't find any messages
1349+ self ._test_read_mbox (lambda line : re .match (b'From .+ \\ d\\ d\\ d\\ r?\\ n' , line ), count = 0 )
1350+
1351+ class TestMboxFromFile (unittest .TestCase ):
1352+ # test class without default setUp/tearDown which we don't want
1353+
1354+ def setUp (self ):
1355+ self ._box = None
1356+ self ._path = None
1357+
1358+ def tearDown (self ):
1359+ if self ._box is not None :
1360+ self ._box .close ()
1361+ # Don't delete it!
1362+
1363+ def checkmbox (self , name , matcher , count ):
1364+ self ._path = os .path .join (os .path .dirname (__file__ ), 'mailbox_data' , name )
1365+ self ._box = mailbox .mbox (self ._path , create = False , from_matcher = matcher )
1366+ self .assertEqual (len (self ._box .keys ()), count )
1367+
1368+ # default matcher finds two messages as there are 2 From lines
1369+ def test_read_mbox_None_01 (self ):
1370+ self .checkmbox ('mailbox_01.mbox' , None , 2 )
1371+
1372+ def test_read_mbox_None_02 (self ):
1373+ self .checkmbox ('mailbox_02.mbox' , None , 2 )
1374+
1375+ def test_read_mbox_full_01 (self ):
1376+ self .checkmbox ('mailbox_01.mbox' , 'full' , 1 )
1377+
1378+ def test_read_mbox_full_02 (self ):
1379+ self .checkmbox ('mailbox_02.mbox' , 'full' , 0 ) # From line has extra non-space chars after YYYY
13131380
13141381class TestMMDF (_TestMboxMMDF , unittest .TestCase ):
13151382
0 commit comments