1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
from __future__ import unicode_literals
import operator
import unittest
try:
from unittest import mock
except ImportError:
import mock
import six
import fs.copy
import fs.errors
import fs.mirror
import fs.move
import fs.wrap
from fs import open_fs
from fs.info import Info
class TestWrapReadOnly(unittest.TestCase):
def setUp(self):
self.fs = open_fs("mem://")
self.ro = fs.wrap.read_only(self.fs)
def tearDown(self):
self.fs.close()
def assertReadOnly(self, func, *args, **kwargs):
self.assertRaises(fs.errors.ResourceReadOnly, func, *args, **kwargs)
def test_open_w(self):
self.assertReadOnly(self.ro.open, "foo", "w")
def test_appendtext(self):
self.assertReadOnly(self.ro.appendtext, "foo", "bar")
def test_appendbytes(self):
self.assertReadOnly(self.ro.appendbytes, "foo", b"bar")
def test_makedir(self):
self.assertReadOnly(self.ro.makedir, "foo")
def test_move(self):
self.assertReadOnly(self.ro.move, "foo", "bar")
def test_openbin_w(self):
self.assertReadOnly(self.ro.openbin, "foo", "w")
def test_remove(self):
self.assertReadOnly(self.ro.remove, "foo")
def test_removedir(self):
self.assertReadOnly(self.ro.removedir, "foo")
def test_removetree(self):
self.assertReadOnly(self.ro.removetree, "foo")
def test_setinfo(self):
self.assertReadOnly(self.ro.setinfo, "foo", {})
def test_settimes(self):
self.assertReadOnly(self.ro.settimes, "foo", {})
def test_copy(self):
self.assertReadOnly(self.ro.copy, "foo", "bar")
def test_create(self):
self.assertReadOnly(self.ro.create, "foo")
def test_writetext(self):
self.assertReadOnly(self.ro.writetext, "foo", "bar")
def test_writebytes(self):
self.assertReadOnly(self.ro.writebytes, "foo", b"bar")
def test_makedirs(self):
self.assertReadOnly(self.ro.makedirs, "foo/bar")
def test_touch(self):
self.assertReadOnly(self.ro.touch, "foo")
def test_upload(self):
self.assertReadOnly(self.ro.upload, "foo", six.BytesIO())
def test_writefile(self):
self.assertReadOnly(self.ro.writefile, "foo", six.StringIO())
def test_openbin_r(self):
self.fs.writebytes("file", b"read me")
with self.ro.openbin("file") as read_file:
self.assertEqual(read_file.read(), b"read me")
def test_open_r(self):
self.fs.writebytes("file", b"read me")
with self.ro.open("file", "rb") as read_file:
self.assertEqual(read_file.read(), b"read me")
class TestWrapReadOnlySyspath(unittest.TestCase):
# If the wrapped fs has a syspath, there is a chance that somewhere
# in fs.copy or fs.mirror we try to use it to our advantage, but
# we want to make sure these implementations don't circumvent the
# wrapper.
def setUp(self):
self.fs = open_fs("temp://")
self.ro = fs.wrap.read_only(self.fs)
self.src = open_fs("temp://")
self.src.touch("foo")
self.src.makedir("bar")
def tearDown(self):
self.fs.close()
self.src.close()
def assertReadOnly(self, func, *args, **kwargs):
self.assertRaises(fs.errors.ResourceReadOnly, func, *args, **kwargs)
def test_copy_fs(self):
self.assertReadOnly(fs.copy.copy_fs, self.src, self.ro)
def test_copy_fs_if_newer(self):
self.assertReadOnly(fs.copy.copy_fs_if_newer, self.src, self.ro)
def test_copy_file(self):
self.assertReadOnly(fs.copy.copy_file, self.src, "foo", self.ro, "foo")
def test_copy_file_if_newer(self):
self.assertReadOnly(fs.copy.copy_file_if_newer, self.src, "foo", self.ro, "foo")
def test_copy_structure(self):
self.assertReadOnly(fs.copy.copy_structure, self.src, self.ro)
def test_mirror(self):
self.assertReadOnly(fs.mirror.mirror, self.src, self.ro)
fs.mirror.mirror(self.src, self.fs)
self.fs.touch("baz")
self.assertReadOnly(fs.mirror.mirror, self.src, self.ro)
def test_move_fs(self):
self.assertReadOnly(fs.move.move_fs, self.src, self.ro)
self.src.removetree("/")
self.fs.touch("foo")
self.assertReadOnly(fs.move.move_fs, self.ro, self.src)
def test_move_file(self):
self.assertReadOnly(fs.move.move_file, self.src, "foo", self.ro, "foo")
self.fs.touch("baz")
self.assertReadOnly(fs.move.move_file, self.ro, "baz", self.src, "foo")
def test_move_dir(self):
self.assertReadOnly(fs.move.move_file, self.src, "bar", self.ro, "bar")
self.fs.makedir("baz")
self.assertReadOnly(fs.move.move_dir, self.ro, "baz", self.src, "baz")
class TestWrapCachedDir(unittest.TestCase):
def setUp(self):
self.fs = open_fs("mem://")
self.fs.makedirs("foo/bar/baz")
self.fs.touch("egg")
self.cached = fs.wrap.cache_directory(self.fs)
def tearDown(self):
self.fs.close()
def assertNotFound(self, func, *args, **kwargs):
self.assertRaises(fs.errors.ResourceNotFound, func, *args, **kwargs)
def test_scandir(self):
key = operator.attrgetter("name")
expected = [
Info({"basic": {"name": "egg", "is_dir": False}}),
Info({"basic": {"name": "foo", "is_dir": True}}),
]
with mock.patch.object(self.fs, "scandir", wraps=self.fs.scandir) as scandir:
self.assertEqual(sorted(self.cached.scandir("/"), key=key), expected)
scandir.assert_has_calls([mock.call("/", namespaces=None, page=None)])
with mock.patch.object(self.fs, "scandir", wraps=self.fs.scandir) as scandir:
self.assertEqual(sorted(self.cached.scandir("/"), key=key), expected)
scandir.assert_not_called()
def test_isdir(self):
with mock.patch.object(self.fs, "scandir", wraps=self.fs.scandir) as scandir:
self.assertTrue(self.cached.isdir("foo"))
self.assertFalse(self.cached.isdir("egg")) # is file
self.assertFalse(self.cached.isdir("spam")) # doesn't exist
scandir.assert_has_calls([mock.call("/", namespaces=None, page=None)])
with mock.patch.object(self.fs, "scandir", wraps=self.fs.scandir) as scandir:
self.assertTrue(self.cached.isdir("foo"))
self.assertFalse(self.cached.isdir("egg"))
self.assertFalse(self.cached.isdir("spam"))
scandir.assert_not_called()
def test_isfile(self):
with mock.patch.object(self.fs, "scandir", wraps=self.fs.scandir) as scandir:
self.assertTrue(self.cached.isfile("egg"))
self.assertFalse(self.cached.isfile("foo")) # is dir
self.assertFalse(self.cached.isfile("spam")) # doesn't exist
scandir.assert_has_calls([mock.call("/", namespaces=None, page=None)])
with mock.patch.object(self.fs, "scandir", wraps=self.fs.scandir) as scandir:
self.assertTrue(self.cached.isfile("egg"))
self.assertFalse(self.cached.isfile("foo"))
self.assertFalse(self.cached.isfile("spam"))
scandir.assert_not_called()
def test_getinfo(self):
with mock.patch.object(self.fs, "scandir", wraps=self.fs.scandir) as scandir:
self.assertEqual(self.cached.getinfo("foo"), self.fs.getinfo("foo"))
self.assertEqual(self.cached.getinfo("/"), self.fs.getinfo("/"))
self.assertNotFound(self.cached.getinfo, "spam")
scandir.assert_has_calls([mock.call("/", namespaces=None, page=None)])
with mock.patch.object(self.fs, "scandir", wraps=self.fs.scandir) as scandir:
self.assertEqual(self.cached.getinfo("foo"), self.fs.getinfo("foo"))
self.assertEqual(self.cached.getinfo("/"), self.fs.getinfo("/"))
self.assertNotFound(self.cached.getinfo, "spam")
scandir.assert_not_called()
|