Changeset 119

Show
Ignore:
Timestamp:
2002-03-30 16:48:07 (7 years ago)
Author:
schwa
Message:
MockSession: added methods _remove_trailing_slash and transfercmd.
MockSession/MockSocket: provided code so that a call to
    MockSession.transfercmd returns an appropriate MockSocket object
    whose makefile method uses the file content determined by
    MockSession.file_content. This means that this intended file content
    is passed through from MockSession to MockSocket.
Files:

Legend:

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

    r112 r119  
    3030# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
    3131 
    32 # $Id: _mock_ftplib.py,v 1.7 2002/03/30 14:53:01 schwa Exp $ 
     32# $Id: _mock_ftplib.py,v 1.8 2002/03/30 16:48:07 schwa Exp $ 
    3333 
    3434""" 
     
    4141 
    4242import ftplib 
    43  
    44 try: 
    45     import cStringIO as StringIO 
    46 except ImportError: 
    47     import StringIO 
    48  
    49 DEBUG = 0 
    50  
    51 class MockSocket: 
    52     def __init__(self, mock_socket_file_contents=''): 
    53         self.mock_socket_file_contents = mock_socket_file_contents 
    54  
    55     def makefile(self, mode): 
    56         return StringIO.StringIO(self.mock_socket_file_contents) 
     43import StringIO 
    5744 
    5845 
     
    323310 
    324311 
     312DEBUG = 0 
     313 
     314# class MockFileObject(StringIO.StringIO): 
     315#     """ 
     316#     Mock class for the file objects _contained in_ _FTPFile 
     317#     objects (not for _FTPFile objects themselves!). 
     318#     """ 
     319#     contents = '' 
     320#  
     321#     def __init__(self, contents, mode='r'): 
     322         
     323     
     324class MockSocket: 
     325    """ 
     326    Mock class which is used to return something from 
     327    MockSession.transfercmd. 
     328    """ 
     329    def __init__(self, mock_file_content=''): 
     330        self.mock_file_content = mock_file_content 
     331 
     332    def makefile(self, mode): 
     333        return StringIO.StringIO(self.mock_file_content) 
     334 
     335 
    325336class MockSession: 
    326  
     337    """ 
     338    Mock class which works like ftplib.FTP for the purpose of the 
     339    unit tests. 
     340    """ 
    327341    # used by MockSession.cwd and MockSession.pwd 
    328342    current_dir = '/home/sschwarzer' 
     
    344358drwxr-sr-x   6 45854    200           512 Sep 20  1999 scios2"""} 
    345359 
     360    # file content to be used (indirectly) with transfercmd 
     361    mock_file_content = '' 
     362     
    346363    def __init__(self, host='', user='', password=''): 
    347         self.pwd_result_index = 0 
    348  
     364        pass 
     365 
     366    def _remove_trailing_slash(self, path): 
     367        if path.endswith('/'): 
     368            path = path[:-1] 
     369        return path 
     370         
    349371    def voidcmd(self, cmd): 
     372        if DEBUG: 
     373            print cmd 
    350374        if cmd == 'STAT': 
    351375            return 'MockSession server awaiting your commands ;-)' 
     376        elif cmd.startswith('TYPE '): 
     377            return 
    352378        else: 
    353379            raise ftplib.error_perm 
     
    356382        return self.current_dir 
    357383 
     384    def cwd(self, path): 
     385        path = self._remove_trailing_slash(path) 
     386        self.current_dir = path 
     387 
    358388    def dir(self, path, callback=None): 
     389        "Provide a callback function with each line of a directory listing." 
    359390        if DEBUG: 
    360391            print 'dir: %s' % path 
    361         if path.endswith('/'): 
    362             path = path[:-1] 
     392        path = self._remove_trailing_slash(path) 
    363393        if not self.dir_contents.has_key(path): 
    364394            raise ftplib.error_perm 
     
    370400                callback(line) 
    371401 
     402    def transfercmd(self, cmd): 
     403        """ 
     404        Return a MockSocket object whose makefile method will return 
     405        a mock file object. 
     406        """ 
     407        if DEBUG: 
     408            print cmd 
     409        return MockSocket(self.mock_file_content) 
     410 
    372411 
    373412class FailOnLoginSession(MockSession): 
     
    375414        raise ftplib.error_perm 
    376415 
     416