Coverage for pyquickhelper/filehelper/ftp_mock.py: 100%

32 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-03 02:21 +0200

1""" 

2@file 

3@brief Mock FTP classes. 

4""" 

5from ..loghelper.flog import noLOG 

6from .ftp_transfer import TransferFTP 

7 

8 

9class MockTransferFTP(TransferFTP): 

10 """ 

11 mock @see cl TransferFTP 

12 """ 

13 

14 def __init__(self, ftps='FTP', fLOG=noLOG): 

15 """ 

16 @param ftps protocol 

17 @param fLOG logging function 

18 """ 

19 TransferFTP.__init__(self, None, "login", "password", ftps=ftps) 

20 self._store = {} 

21 

22 def run_command(self, command, *args, **kwargs): 

23 """ 

24 Mock method :meth:`run_command <pyquickhelper.filehelper.ftp_transfer.TransferFTP.run_commnad>` 

25 """ 

26 if command == self._ftp.mlsd and args == ('.',): 

27 return [('setup.py', {'name': 'setup.py'})] 

28 if command == self._ftp.cwd and args == ('.',): 

29 return None 

30 if command == self._ftp.pwd and len(args) == 0: 

31 return "." 

32 if command == self._ftp.cwd and args == ('..',): 

33 return None 

34 if command == self._ftp.storbinary and args[0] == 'STOR test_transfer_ftp.py': 

35 self._store[args[0]] = args 

36 return None 

37 if command == self._ftp.retrbinary and args[0] == 'RETR test_transfer_ftp.py': 

38 b = self._store[args[0].replace("RETR", "STOR")][1] 

39 return b'ee' 

40 if command == self._ftp.cwd and args == ('backup',): 

41 self._store[args[0]] = args 

42 return None 

43 if command == self._ftp.storbinary and args[0] == 'STOR setup.py': 

44 self._store[args[0]] = args 

45 return None 

46 if command == self._ftp.retrbinary and args[0] == 'RETR setup.py': 

47 b = self._store[args[0].replace("RETR", "STOR")][1] 

48 s = b.getbuffer() 

49 args[1](s) 

50 return len(s) 

51 raise RuntimeError( # pragma: no cover 

52 f"command='{command}'\nargs={args}")