Commit 851fadda by willmcgugan

Tests now pass on Windows

parent d6848353
...@@ -604,33 +604,37 @@ class FS(object): ...@@ -604,33 +604,37 @@ class FS(object):
dst_syspath = self.getsyspath(dst, allow_none=True) dst_syspath = self.getsyspath(dst, allow_none=True)
if src_syspath is not None and dst_syspath is not None: if src_syspath is not None and dst_syspath is not None:
shutil.move(src_syspath, dst_syspath) try:
else: shutil.move(src_syspath, dst_syspath)
return
except WindowsError:
pass
def movefile_noerrors(src, dst): def movefile_noerrors(src, dst):
try: try:
return self.move(src, dst) return self.move(src, dst)
except FSError: except FSError:
return return
if ignore_errors: if ignore_errors:
movefile = movefile_noerrors movefile = movefile_noerrors
else: else:
movefile = self.move movefile = self.move
self.makedir(dst, allow_recreate=True) self.makedir(dst, allow_recreate=True)
for dirname, filenames in self.walk(src, search="depth"): for dirname, filenames in self.walk(src, search="depth"):
dst_dirname = makerelative(dirname[len(src):]) dst_dirname = makerelative(dirname[len(src):])
dst_dirpath = pathjoin(dst, dst_dirname) dst_dirpath = pathjoin(dst, dst_dirname)
self.makedir(dst_dirpath, allow_recreate=True, recursive=True) self.makedir(dst_dirpath, allow_recreate=True, recursive=True)
for filename in filenames: for filename in filenames:
src_filename = pathjoin(dirname, filename) src_filename = pathjoin(dirname, filename)
dst_filename = pathjoin(dst_dirpath, filename) dst_filename = pathjoin(dst_dirpath, filename)
movefile(src_filename, dst_filename, chunk_size=chunk_size) movefile(src_filename, dst_filename, chunk_size=chunk_size)
self.removedir(dirname) self.removedir(dirname)
......
...@@ -28,7 +28,7 @@ class OSFS(FS): ...@@ -28,7 +28,7 @@ class OSFS(FS):
__repr__ = __str__ __repr__ = __str__
def getsyspath(self, path, allow_none=False): def getsyspath(self, path, allow_none=False):
sys_path = os.path.join(self.root_path, makerelative(self._resolve(path))) sys_path = os.path.join(self.root_path, makerelative(self._resolve(path))).replace('/', os.sep)
return sys_path return sys_path
def open(self, path, mode="r", **kwargs): def open(self, path, mode="r", **kwargs):
...@@ -77,10 +77,17 @@ class OSFS(FS): ...@@ -77,10 +77,17 @@ class OSFS(FS):
os.mkdir(sys_path, mode) os.mkdir(sys_path, mode)
except OSError, e: except OSError, e:
if allow_recreate: if allow_recreate:
if e.errno !=17: if e.errno != 17:
raise OperationFailedError("MAKEDIR_FAILED", path) raise OperationFailedError("MAKEDIR_FAILED", path)
else: else:
raise OperationFailedError("MAKEDIR_FAILED", path) raise OperationFailedError("MAKEDIR_FAILED", path)
except WindowsError, e:
if allow_recreate:
if e.errno != 183:
raise OperationFailedError("MAKEDIR_FAILED", path)
else:
raise OperationFailedError("MAKEDIR_FAILED", path)
except OSError, e: except OSError, e:
if e.errno == 17: if e.errno == 17:
return return
......
...@@ -28,9 +28,10 @@ class TempFS(OSFS): ...@@ -28,9 +28,10 @@ class TempFS(OSFS):
def __unicode__(self): def __unicode__(self):
return unicode(self.__str__()) return unicode(self.__str__())
def _cleanup(self): def close(self):
"""Called by __del__ to remove the temporary directory. Can be called directly, """Removes the temporary directory.
but it is probably not neccesary.""" This will be called automatically when the object is cleaned up by Python.
Note that once this method has been called, the FS object may no longer be used."""
if not self._cleaned: if not self._cleaned:
self._lock.acquire() self._lock.acquire()
...@@ -41,7 +42,7 @@ class TempFS(OSFS): ...@@ -41,7 +42,7 @@ class TempFS(OSFS):
self._lock.release() self._lock.release()
def __del__(self): def __del__(self):
self._cleanup() self.close()
if __name__ == "__main__": if __name__ == "__main__":
......
...@@ -145,7 +145,7 @@ class TestOSFS(unittest.TestCase): ...@@ -145,7 +145,7 @@ class TestOSFS(unittest.TestCase):
self.fs = osfs.OSFS(self.temp_dir) self.fs = osfs.OSFS(self.temp_dir)
print "Temp dir is", self.temp_dir print "Temp dir is", self.temp_dir
def tearDown(self): def tearDown(self):
shutil.rmtree(self.temp_dir) shutil.rmtree(self.temp_dir)
def check(self, p): def check(self, p):
...@@ -357,9 +357,13 @@ class TestOSFS(unittest.TestCase): ...@@ -357,9 +357,13 @@ class TestOSFS(unittest.TestCase):
def test_readwriteappendseek(self): def test_readwriteappendseek(self):
def checkcontents(path, check_contents): def checkcontents(path, check_contents):
f = self.fs.open(path, "rb") f = None
read_contents = f.read() try:
f.close() f = self.fs.open(path, "rb")
read_contents = f.read()
finally:
if f is not None:
f.close()
return read_contents == check_contents return read_contents == check_contents
test_strings = ["Beautiful is better than ugly.", test_strings = ["Beautiful is better than ugly.",
"Explicit is better than implicit.", "Explicit is better than implicit.",
...@@ -390,15 +394,15 @@ class TestOSFS(unittest.TestCase): ...@@ -390,15 +394,15 @@ class TestOSFS(unittest.TestCase):
f4.write(test_strings[2]) f4.write(test_strings[2])
f4.close() f4.close()
self.assert_(checkcontents("b.txt", test_strings[2])) self.assert_(checkcontents("b.txt", test_strings[2]))
f5 = self.fs.open("c.txt", "wt") f5 = self.fs.open("c.txt", "wb")
for s in test_strings: for s in test_strings:
f5.write(s+"\n") f5.write(s+"\n")
f5.close() f5.close()
f6 = self.fs.open("c.txt", "rt") f6 = self.fs.open("c.txt", "rb")
for s, t in zip(f6, test_strings): for s, t in zip(f6, test_strings):
self.assertEqual(s, t+"\n") self.assertEqual(s, t+"\n")
f6.close() f6.close()
f7 = self.fs.open("c.txt", "rt") f7 = self.fs.open("c.txt", "rb")
f7.seek(13) f7.seek(13)
word = f7.read(6) word = f7.read(6)
self.assertEqual(word, "better") self.assertEqual(word, "better")
...@@ -409,7 +413,8 @@ class TestOSFS(unittest.TestCase): ...@@ -409,7 +413,8 @@ class TestOSFS(unittest.TestCase):
word = f7.read(7) word = f7.read(7)
self.assertEqual(word, "complex") self.assertEqual(word, "complex")
f7.close() f7.close()
self.assertEqual(self.fs.getcontents("a.txt"), all_strings) self.assertEqual(self.fs.getcontents("a.txt"), all_strings)
class TestSubFS(TestOSFS): class TestSubFS(TestOSFS):
...@@ -466,7 +471,7 @@ class TestTempFS(TestOSFS): ...@@ -466,7 +471,7 @@ class TestTempFS(TestOSFS):
def tearDown(self): def tearDown(self):
td = self.fs._temp_dir td = self.fs._temp_dir
del self.fs self.fs.close()
self.assert_(not os.path.exists(td)) self.assert_(not os.path.exists(td))
def check(self, p): def check(self, p):
......
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