2
2
3
3
import operator
4
4
import unittest
5
+
5
6
try :
6
7
from unittest import mock
7
8
except ImportError :
8
9
import mock
9
10
10
11
import six
11
12
13
+ import fs .copy
14
+ import fs .mirror
15
+ import fs .move
12
16
import fs .wrap
13
17
import fs .errors
14
18
from fs import open_fs
15
19
from fs .info import Info
16
20
17
21
18
22
class TestWrapReadOnly (unittest .TestCase ):
19
-
20
23
def setUp (self ):
21
24
self .fs = open_fs ("mem://" )
22
25
self .ro = fs .wrap .read_only (self .fs )
@@ -95,8 +98,65 @@ def test_open_r(self):
95
98
self .assertEqual (read_file .read (), b"read me" )
96
99
97
100
98
- class TestWrapCachedDir (unittest .TestCase ):
101
+ class TestWrapReadOnlySyspath (unittest .TestCase ):
102
+ # If the wrapped fs has a syspath, there is a chance that somewhere
103
+ # in fs.copy or fs.mirror we try to use it to our advantage, but
104
+ # we want to make sure these implementations don't circumvent the
105
+ # wrapper.
106
+
107
+ def setUp (self ):
108
+ self .fs = open_fs ("temp://" )
109
+ self .ro = fs .wrap .read_only (self .fs )
110
+ self .src = open_fs ("temp://" )
111
+ self .src .touch ("foo" )
112
+ self .src .makedir ("bar" )
113
+
114
+ def tearDown (self ):
115
+ self .fs .close ()
116
+ self .src .close ()
117
+
118
+ def assertReadOnly (self , func , * args , ** kwargs ):
119
+ self .assertRaises (fs .errors .ResourceReadOnly , func , * args , ** kwargs )
99
120
121
+ def test_copy_fs (self ):
122
+ self .assertReadOnly (fs .copy .copy_fs , self .src , self .ro )
123
+
124
+ def test_copy_fs_if_newer (self ):
125
+ self .assertReadOnly (fs .copy .copy_fs_if_newer , self .src , self .ro )
126
+
127
+ def test_copy_file (self ):
128
+ self .assertReadOnly (fs .copy .copy_file , self .src , "foo" , self .ro , "foo" )
129
+
130
+ def test_copy_file_if_newer (self ):
131
+ self .assertReadOnly (fs .copy .copy_file_if_newer , self .src , "foo" , self .ro , "foo" )
132
+
133
+ def test_copy_structure (self ):
134
+ self .assertReadOnly (fs .copy .copy_structure , self .src , self .ro )
135
+
136
+ def test_mirror (self ):
137
+ self .assertReadOnly (fs .mirror .mirror , self .src , self .ro )
138
+ fs .mirror .mirror (self .src , self .fs )
139
+ self .fs .touch ("baz" )
140
+ self .assertReadOnly (fs .mirror .mirror , self .src , self .ro )
141
+
142
+ def test_move_fs (self ):
143
+ self .assertReadOnly (fs .move .move_fs , self .src , self .ro )
144
+ self .src .removetree ("/" )
145
+ self .fs .touch ("foo" )
146
+ self .assertReadOnly (fs .move .move_fs , self .ro , self .src )
147
+
148
+ def test_move_file (self ):
149
+ self .assertReadOnly (fs .move .move_file , self .src , "foo" , self .ro , "foo" )
150
+ self .fs .touch ("baz" )
151
+ self .assertReadOnly (fs .move .move_file , self .ro , "baz" , self .src , "foo" )
152
+
153
+ def test_move_dir (self ):
154
+ self .assertReadOnly (fs .move .move_file , self .src , "bar" , self .ro , "bar" )
155
+ self .fs .makedir ("baz" )
156
+ self .assertReadOnly (fs .move .move_dir , self .ro , "baz" , self .src , "baz" )
157
+
158
+
159
+ class TestWrapCachedDir (unittest .TestCase ):
100
160
def setUp (self ):
101
161
self .fs = open_fs ("mem://" )
102
162
self .fs .makedirs ("foo/bar/baz" )
@@ -126,7 +186,7 @@ def test_isdir(self):
126
186
with mock .patch .object (self .fs , "scandir" , wraps = self .fs .scandir ) as scandir :
127
187
self .assertTrue (self .cached .isdir ("foo" ))
128
188
self .assertFalse (self .cached .isdir ("egg" )) # is file
129
- self .assertFalse (self .cached .isdir ("spam" )) # doesn't exist
189
+ self .assertFalse (self .cached .isdir ("spam" )) # doesn't exist
130
190
scandir .assert_called ()
131
191
with mock .patch .object (self .fs , "scandir" , wraps = self .fs .scandir ) as scandir :
132
192
self .assertTrue (self .cached .isdir ("foo" ))
@@ -138,7 +198,7 @@ def test_isfile(self):
138
198
with mock .patch .object (self .fs , "scandir" , wraps = self .fs .scandir ) as scandir :
139
199
self .assertTrue (self .cached .isfile ("egg" ))
140
200
self .assertFalse (self .cached .isfile ("foo" )) # is dir
141
- self .assertFalse (self .cached .isfile ("spam" )) # doesn't exist
201
+ self .assertFalse (self .cached .isfile ("spam" )) # doesn't exist
142
202
scandir .assert_called ()
143
203
with mock .patch .object (self .fs , "scandir" , wraps = self .fs .scandir ) as scandir :
144
204
self .assertTrue (self .cached .isfile ("egg" ))
0 commit comments