Skip to content

Commit

Permalink
Merge branch 'master' of github.com:codebynumbers/ftpretty
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Harrigan committed Mar 1, 2014
2 parents ec74cd8 + bce4d48 commit 52da6c5
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 11 deletions.
33 changes: 25 additions & 8 deletions tests/mock_ftp.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
import os
from collections import deque

class MockFTP(object):
""" Mock FTP lib for testing """
_current = '.'
_files = None
_size = 0
_dirlist = None

def __init__(self):
self._files = None
self._size = 0
self._dirlist = None
self._exists = True
self._stack = deque()
self._contents = ''

def storbinary(self, command, f):
f.seek(0, os.SEEK_END)
self._size = f.tell()

def retrbinary(self, command, callback):
callback(self._contents)
return

def pwd(self):
return self._current
return "/".join(self._stack)

def nlst(self, dirname=None):
return self._files
Expand All @@ -33,17 +39,22 @@ def rmd(self, dirname):
return

def delete(self, filename):
if '//' in filename:
if not self._exists:
raise Exception("Doesn't exist")
return True

def rename(self, fromname, toname):
return

def cwd(self, pathname):
if '//' in pathname:
if not self._exists:
self._exists = True
raise Exception("Doesn't exist")
self._current = pathname
for dir in pathname.split("/"):
if dir == '..':
self._stack.pop()
else:
self._stack.append(dir)

def size(self, filename):
return self._size
Expand All @@ -69,3 +80,9 @@ def _set_files(self, files):

def _set_dirlist(self, dirlist):
self._dirlist = dirlist

def _set_exists(self, exists):
self._exists = exists

def _set_contents(self, contents):
self._contents = contents
49 changes: 46 additions & 3 deletions tests/test_ftpretty.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,67 @@ def setUp(self):
def test_cd(self):
self.pretty.cd('photos/nature/mountains')
self.assertEquals(self.pretty.pwd(), 'photos/nature/mountains')
self.assertRaises(Exception, self.pretty.cd('photos//nature/mountains'))
self.pretty._set_exists(False)
self.assertRaises(Exception, self.pretty.cd('blah'))

def test_cd_up(self):
self.pretty.cd('photos/nature/mountains')
self.pretty.cd('../..')
self.assertEquals(self.pretty.pwd(), 'photos')

def test_descend(self):
self.pretty._set_exists(False)
self.pretty.descend('photos/nature', True)
self.pretty._set_exists(True)
self.pretty.cd('mountains')
self.assertEquals(self.pretty.pwd(), 'photos/nature/mountains')

def test_list(self):
self.mock_ftp._set_files(['a.txt', 'b.txt'])
self.assertEquals(len(self.pretty.list()), 2)

def test_put(self):
def test_put_filename(self):
size = self.pretty.put('AUTHORS.rst', 'AUTHORS.rst')
self.assertEquals(size, os.path.getsize('AUTHORS.rst'))

def test_put_file(self):
with open('AUTHORS.rst') as file:
size = self.pretty.put(file, 'AUTHORS.rst')
self.assertEquals(size, os.path.getsize('AUTHORS.rst'))

def test_put_contents(self):
size = self.pretty.put(None, 'AUTHORS.rst', 'test string')
self.assertEquals(size, len('test string'))

def test_get(self):
self.mock_ftp._set_contents('hello_get')
self.assertFalse(os.path.isfile('local_copy.txt'))
self.pretty.get('remote_file.txt', 'local_copy.txt')
self.assertTrue(os.path.isfile('local_copy.txt'))
with open('local_copy.txt') as file:
self.assertEquals(file.read(), 'hello_get')
os.unlink('local_copy.txt')

def test_get_filehandle(self):
self.mock_ftp._set_contents('hello_file')
self.assertFalse(os.path.isfile('local_copy.txt'))
outfile = open('local_copy.txt', 'w')
self.pretty.get('remote_file.txt', outfile)
outfile.close()
self.assertTrue(os.path.isfile('local_copy.txt'))
with open('local_copy.txt') as file:
self.assertEquals(file.read(), 'hello_file')
os.unlink('local_copy.txt')

def test_get_contents(self):
self.mock_ftp._set_contents('hello')
contents = self.pretty.get('remote_file.txt')
self.assertEquals(contents, 'hello')

def test_delete(self):
self.assertTrue(self.pretty.delete('remote_file.txt'))
self.assertRaises(Exception, self.pretty.delete('photos//nature/remote.txt'))
self.pretty._set_exists(False)
self.assertRaises(Exception, self.pretty.delete('photos/nature/remote.txt'))

def test_dir_parse(self):
self.mock_ftp._set_dirlist("-rw-rw-r-- 1 rharrigan www 47 Feb 20 11:39 Cool.txt\n" +
Expand Down

0 comments on commit 52da6c5

Please sign in to comment.