Changeset 135

Show
Ignore:
Timestamp:
2002-03-30 21:50:25 (7 years ago)
Author:
schwa
Message:
Module: the path of a file is now passed from MockSession.transfercmd to
    MockSocket.makefile to MockFile's constructor. This makes it
    possible to access the MockFile object at any time via the
    module-global dictionary mock_files. The convenience function
    content_of(path) can be used to get the content for the requested path.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/_mock_ftplib.py

    r133 r135  
    3030# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
    3131 
    32 # $Id: _mock_ftplib.py,v 1.12 2002/03/30 21:20:28 schwa Exp $ 
     32# $Id: _mock_ftplib.py,v 1.13 2002/03/30 21:50:25 schwa Exp $ 
    3333 
    3434""" 
     
    312312DEBUG = 0 
    313313 
     314# Use a global dictionary of the form {path: mock_file, ...} 
     315#  to make "volatile" mock files accessible. This is used for 
     316#  testing the contents of a file after an FTPHost.upload call. 
     317mock_files = {} 
     318 
     319def content_of(path): 
     320    return mock_files[path].getvalue() 
     321 
    314322class MockFile(StringIO.StringIO): 
    315323    """ 
     
    317325    objects (not for _FTPFile objects themselves!). 
    318326    """ 
    319     def __init__(self, content=''): 
     327    def __init__(self, path, content=''): 
     328        global mock_files 
     329        mock_files[path] = self 
    320330        StringIO.StringIO.__init__(self, content) 
    321331 
     
    330340            self._value_after_close = StringIO.StringIO.getvalue(self) 
    331341        StringIO.StringIO.close(self) 
    332              
     342 
    333343     
    334344class MockSocket: 
     
    337347    MockSession.transfercmd. 
    338348    """ 
    339     def __init__(self, mock_file_content=''): 
     349    def __init__(self, path, mock_file_content=''): 
    340350        if DEBUG: 
    341351            print 'File content: *%s*' % mock_file_content 
     352        self.file_path = path 
    342353        self.mock_file_content = mock_file_content 
    343354 
    344355    def makefile(self, mode): 
    345         return MockFile(self.mock_file_content) 
     356        return MockFile(self.file_path, self.mock_file_content) 
    346357 
    347358    def close(self): 
     
    432443        # fail if path isn't available (this name is hard-coded here 
    433444        #  and has to be used for the corresponding tests) 
    434         if cmd == 'RETR' and path == 'notthere'
     445        if (cmd, path) == ('RETR', 'notthere')
    435446            raise ftplib.error_perm 
    436         return MockSocket(self.mock_file_content) 
    437  
     447        return MockSocket(path, self.mock_file_content) 
     448