<s> from __future__ import with_statement <EOL> import mailbox <EOL> import os <EOL> import time <EOL> import unittest <EOL> from test import test_support <EOL> try : <EOL> os . unlink ( test_support . TESTFN ) <EOL> except os . error : <EOL> pass <EOL> FROM_ = "<STR_LIT>" <EOL> DUMMY_MESSAGE = """<STR_LIT>""" <EOL> class MaildirTestCase ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . _dir = test_support . TESTFN <EOL> os . mkdir ( self . _dir ) <EOL> os . mkdir ( os . path . join ( self . _dir , "<STR_LIT>" ) ) <EOL> os . mkdir ( os . path . join ( self . _dir , "<STR_LIT>" ) ) <EOL> os . mkdir ( os . path . join ( self . _dir , "<STR_LIT>" ) ) <EOL> self . _counter = <NUM_LIT:1> <EOL> self . _msgfiles = [ ] <EOL> def tearDown ( self ) : <EOL> map ( os . unlink , self . _msgfiles ) <EOL> os . rmdir ( os . path . join ( self . _dir , "<STR_LIT>" ) ) <EOL> os . rmdir ( os . path . join ( self . _dir , "<STR_LIT>" ) ) <EOL> os . rmdir ( os . path . join ( self . _dir , "<STR_LIT>" ) ) <EOL> os . rmdir ( self . _dir ) <EOL> def createMessage ( self , dir , mbox = False ) : <EOL> t = int ( time . time ( ) % <NUM_LIT> ) <EOL> pid = self . _counter <EOL> self . _counter += <NUM_LIT:1> <EOL> filename = os . extsep . join ( ( str ( t ) , str ( pid ) , "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> tmpname = os . path . join ( self . _dir , "<STR_LIT>" , filename ) <EOL> newname = os . path . join ( self . _dir , dir , filename ) <EOL> fp = open ( tmpname , "<STR_LIT:w>" ) <EOL> self . _msgfiles . append ( tmpname ) <EOL> if mbox : <EOL> fp . write ( FROM_ ) <EOL> fp . write ( DUMMY_MESSAGE ) <EOL> fp . close ( ) <EOL> if hasattr ( os , "<STR_LIT>" ) : <EOL> os . link ( tmpname , newname ) <EOL> else : <EOL> fp = open ( newname , "<STR_LIT:w>" ) <EOL> fp . write ( DUMMY_MESSAGE ) <EOL> fp . close ( ) <EOL> self . _msgfiles . append ( newname ) <EOL> return tmpname <EOL> def assert_and_close ( self , message ) : <EOL> self . assert_ ( message is not None ) <EOL> message . fp . close ( ) <EOL> def test_empty_maildir ( self ) : <EOL> """<STR_LIT>""" <EOL> self . mbox = mailbox . Maildir ( test_support . TESTFN ) <EOL> self . assert_ ( len ( self . mbox ) == <NUM_LIT:0> ) <EOL> self . assert_ ( self . mbox . next ( ) is None ) <EOL> self . assert_ ( self . mbox . next ( ) is None ) <EOL> def test_nonempty_maildir_cur ( self ) : <EOL> self . createMessage ( "<STR_LIT>" ) <EOL> self . mbox = mailbox . Maildir ( test_support . TESTFN ) <EOL> self . assert_ ( len ( self . mbox ) == <NUM_LIT:1> ) <EOL> self . assert_and_close ( self . mbox . next ( ) ) <EOL> self . assert_ ( self . mbox . next ( ) is None ) <EOL> self . assert_ ( self . mbox . next ( ) is None ) <EOL> def test_nonempty_maildir_new ( self ) : <EOL> self . createMessage ( "<STR_LIT>" ) <EOL> self . mbox = mailbox . Maildir ( test_support . TESTFN ) <EOL> self . assert_ ( len ( self . mbox ) == <NUM_LIT:1> ) <EOL> self . assert_and_close ( self . mbox . next ( ) ) <EOL> self . assert_ ( self . mbox . next ( ) is None ) <EOL> self . assert_ ( self . mbox . next ( ) is None ) <EOL> def test_nonempty_maildir_both ( self ) : <EOL> self . createMessage ( "<STR_LIT>" ) <EOL> self . createMessage ( "<STR_LIT>" ) <EOL> self . mbox = mailbox . Maildir ( test_support . TESTFN ) <EOL> self . assert_ ( len ( self . mbox ) == <NUM_LIT:2> ) <EOL> self . assert_and_close ( self . mbox . next ( ) ) <EOL> self . assert_and_close ( self . mbox . next ( ) ) <EOL> self . assert_ ( self . mbox . next ( ) is None ) <EOL> self . assert_ ( self . mbox . next ( ) is None ) <EOL> def test_unix_mbox ( self ) : <EOL> import email . Parser <EOL> fname = self . createMessage ( "<STR_LIT>" , True ) <EOL> n = <NUM_LIT:0> <EOL> with open ( fname ) as fp : <EOL> for msg in mailbox . PortableUnixMailbox ( fp , <EOL> email . Parser . Parser ( ) . parse ) : <EOL> n += <NUM_LIT:1> <EOL> self . assertEqual ( msg [ "<STR_LIT>" ] , "<STR_LIT>" ) <EOL> self . assertEqual ( len ( str ( msg ) ) , len ( FROM_ ) + len ( DUMMY_MESSAGE ) ) <EOL> self . assertEqual ( n , <NUM_LIT:1> ) <EOL> class MboxTestCase ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . _path = test_support . TESTFN <EOL> def tearDown ( self ) : <EOL> os . unlink ( self . _path ) <EOL> def test_from_regex ( self ) : <EOL> f = open ( self . _path , '<STR_LIT:w>' ) <EOL> f . write ( """<STR_LIT>""" ) <EOL> f . close ( ) <EOL> box = mailbox . UnixMailbox ( open ( self . _path , '<STR_LIT:r>' ) ) <EOL> messages = list ( iter ( box ) ) <EOL> self . assert_ ( len ( messages ) == <NUM_LIT:4> ) <EOL> for message in messages : <EOL> message . fp . close ( ) <EOL> def test_main ( ) : <EOL> test_support . run_unittest ( MaildirTestCase , MboxTestCase ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test_main ( ) </s>
