Commit b379d4c1 by willmcgugan

Added more zipfs tests, and fixed a few issues

parent 45fbe7c8
...@@ -77,7 +77,11 @@ class FSError(Exception): ...@@ -77,7 +77,11 @@ class FSError(Exception):
self.details = details self.details = details
def __str__(self): def __str__(self):
if self.details is None:
msg = self.msg % dict((k, str(v)) for k, v in self.__dict__.iteritems()) msg = self.msg % dict((k, str(v)) for k, v in self.__dict__.iteritems())
else:
msg = self.msg % dict((k, str(v)) for k, v in self.__dict__.iteritems())
msg += ", "+str(self.details)
return '%s. %s' % (self.code, msg) return '%s. %s' % (self.code, msg)
...@@ -516,7 +520,7 @@ class FS(object): ...@@ -516,7 +520,7 @@ class FS(object):
""" """
if self.isdir(dst): if self.isdir(dst):
dst = pathjoin( getroot(dst), getresourcename(src) ) dst = pathjoin( dirname(dst), resourcename(src) )
if not self.isfile(src): if not self.isfile(src):
raise ResourceInvalid("WRONG_TYPE", src, msg="Source is not a file: %(path)s") raise ResourceInvalid("WRONG_TYPE", src, msg="Source is not a file: %(path)s")
......
...@@ -82,23 +82,23 @@ def pathsplit(path): ...@@ -82,23 +82,23 @@ def pathsplit(path):
return ('', split[0]) return ('', split[0])
return tuple(split) return tuple(split)
def getroot(path): def dirname(path):
"""Returns the root directory of a path. """Returns the parent directory of a path.
path -- A path path -- A FS path
>>> getroot('foo/bar/baz') >>> dirname('foo/bar/baz')
'foo/bar' 'foo/bar'
""" """
return pathsplit(path)[0] return pathsplit(path)[0]
def getresourcename(path): def resourcename(path):
"""Returns the resource references by a path. """Returns the resource references by a path.
path -- A path path -- A FS path
>>> getresourcename('foo/bar/baz') >>> resourcename('foo/bar/baz')
'baz' 'baz'
""" """
...@@ -154,4 +154,4 @@ def issamedir(path1, path2): ...@@ -154,4 +154,4 @@ def issamedir(path1, path2):
>>> issamedir("foo/bar/baz/txt", "spam/eggs/spam.txt") >>> issamedir("foo/bar/baz/txt", "spam/eggs/spam.txt")
False False
""" """
return pathsplit(path1)[0] == pathsplit(path2)[0] return pathsplit(resolvepath(path1))[0] == pathsplit(resolvepath(path2))[0]
...@@ -74,6 +74,9 @@ class OSFS(FS): ...@@ -74,6 +74,9 @@ class OSFS(FS):
else: else:
raise OperationFailedError("MAKEDIR_FAILED", path) raise OperationFailedError("MAKEDIR_FAILED", path)
except OSError, e: except OSError, e:
if e.errno == 17:
return
else:
raise OperationFailedError("MAKEDIR_FAILED", path, details=e) raise OperationFailedError("MAKEDIR_FAILED", path, details=e)
def remove(self, path): def remove(self, path):
......
...@@ -522,6 +522,13 @@ class TestReadZipFS(unittest.TestCase): ...@@ -522,6 +522,13 @@ class TestReadZipFS(unittest.TestCase):
check_contents("1.txt", "1") check_contents("1.txt", "1")
check_contents("foo/bar/baz.txt", "baz") check_contents("foo/bar/baz.txt", "baz")
def test_is(self):
self.assert_(self.fs.isfile('a.txt'))
self.assert_(self.fs.isfile('1.txt'))
self.assert_(self.fs.isfile('foo/bar/baz.txt'))
self.assert_(self.fs.isdir('foo'))
self.assert_(self.fs.isdir('foo/bar'))
def test_listdir(self): def test_listdir(self):
def check_listing(path, expected): def check_listing(path, expected):
...@@ -531,6 +538,75 @@ class TestReadZipFS(unittest.TestCase): ...@@ -531,6 +538,75 @@ class TestReadZipFS(unittest.TestCase):
check_listing('foo', ['second.txt', 'bar']) check_listing('foo', ['second.txt', 'bar'])
check_listing('foo/bar', ['baz.txt']) check_listing('foo/bar', ['baz.txt'])
class TestWriteZipFS(unittest.TestCase):
def setUp(self):
self.temp_filename = "".join(random.choice("abcdefghijklmnopqrstuvwxyz") for _ in range(6))+".zip"
self.temp_filename = os.path.abspath(self.temp_filename)
zip_fs = zipfs.ZipFS(self.temp_filename, 'w')
def makefile(filename, contents):
if dirname(filename):
zip_fs.makedir(dirname(filename), recursive=True, allow_recreate=True)
f = zip_fs.open(filename, 'w')
f.write(contents)
f.close()
makefile("a.txt", "Hello, World!")
makefile("b.txt", "b")
makefile("foo/bar/baz.txt", "baz")
makefile("foo/second.txt", "hai")
zip_fs.close()
def tearDown(self):
os.remove(self.temp_filename)
def test_valid(self):
zf = zipfile.ZipFile(self.temp_filename, "r")
self.assert_(zf.testzip() is None)
zf.close()
def test_creation(self):
zf = zipfile.ZipFile(self.temp_filename, "r")
def check_contents(filename, contents):
zcontents = zf.read(filename)
self.assertEqual(contents, zcontents)
check_contents("a.txt", "Hello, World!")
check_contents("b.txt", "b")
check_contents("foo/bar/baz.txt", "baz")
check_contents("foo/second.txt", "hai")
class TestAppendZipFS(TestWriteZipFS):
def setUp(self):
self.temp_filename = "".join(random.choice("abcdefghijklmnopqrstuvwxyz") for _ in range(6))+".zip"
self.temp_filename = os.path.abspath(self.temp_filename)
zip_fs = zipfs.ZipFS(self.temp_filename, 'w')
def makefile(filename, contents):
if dirname(filename):
zip_fs.makedir(dirname(filename), recursive=True, allow_recreate=True)
f = zip_fs.open(filename, 'w')
f.write(contents)
f.close()
makefile("a.txt", "Hello, World!")
makefile("b.txt", "b")
zip_fs.close()
zip_fs = zipfs.ZipFS(self.temp_filename, 'a')
makefile("foo/bar/baz.txt", "baz")
makefile("foo/second.txt", "hai")
zip_fs.close()
if __name__ == "__main__": if __name__ == "__main__":
#t = TestFS() #t = TestFS()
#t.setUp() #t.setUp()
......
...@@ -179,7 +179,7 @@ class ZipFS(FS): ...@@ -179,7 +179,7 @@ class ZipFS(FS):
return self._path_fs.isdir(path) return self._path_fs.isdir(path)
def isfile(self, path): def isfile(self, path):
return self._path_fs.isdir(path) return self._path_fs.isfile(path)
def exists(self, path): def exists(self, path):
return self._path_fs.exists(path) return self._path_fs.exists(path)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment