2
2
3
3
import unittest
4
4
5
+ from fs .errors import ResourceNotFound
5
6
from fs .mountfs import MountError , MountFS
6
7
from fs .memoryfs import MemoryFS
7
8
from fs .tempfs import TempFS
11
12
class TestMountFS (FSTestCases , unittest .TestCase ):
12
13
"""Test OSFS implementation."""
13
14
14
- def make_fs (self ):
15
- fs = MountFS ()
16
- mem_fs = MemoryFS ()
17
- fs .mount ("/" , mem_fs )
18
- return fs
19
-
20
-
21
- class TestMountFS2 (FSTestCases , unittest .TestCase ):
22
- """Test OSFS implementation."""
23
-
24
15
def make_fs (self ):
25
16
fs = MountFS ()
26
17
mem_fs = MemoryFS ()
@@ -35,21 +26,44 @@ def test_bad_mount(self):
35
26
mount_fs .mount ("foo" , 5 )
36
27
with self .assertRaises (TypeError ):
37
28
mount_fs .mount ("foo" , b"bar" )
29
+ m1 = MemoryFS ()
30
+ with self .assertRaises (MountError ):
31
+ mount_fs .mount ("" , m1 )
32
+ with self .assertRaises (MountError ):
33
+ mount_fs .mount ("/" , m1 )
38
34
39
35
def test_listdir (self ):
40
36
mount_fs = MountFS ()
41
37
self .assertEqual (mount_fs .listdir ("/" ), [])
42
38
m1 = MemoryFS ()
43
39
m3 = MemoryFS ()
44
40
m4 = TempFS ()
41
+ m5 = MemoryFS ()
45
42
mount_fs .mount ("/m1" , m1 )
46
43
mount_fs .mount ("/m2" , "temp://" )
47
44
mount_fs .mount ("/m3" , m3 )
48
45
with self .assertRaises (MountError ):
49
46
mount_fs .mount ("/m3/foo" , m4 )
50
47
self .assertEqual (sorted (mount_fs .listdir ("/" )), ["m1" , "m2" , "m3" ])
48
+ mount_fs .makedir ("/m2/foo" )
49
+ self .assertEqual (sorted (mount_fs .listdir ("/m2" )), ["foo" ])
51
50
m3 .makedir ("foo" )
52
51
self .assertEqual (sorted (mount_fs .listdir ("/m3" )), ["foo" ])
52
+ mount_fs .mount ("/subdir/m4" , m4 )
53
+ self .assertEqual (sorted (mount_fs .listdir ("/" )), ["m1" , "m2" , "m3" , "subdir" ])
54
+ self .assertEqual (mount_fs .listdir ("/subdir" ), ["m4" ])
55
+ self .assertEqual (mount_fs .listdir ("/subdir/m4" ), [])
56
+ mount_fs .mount ("/subdir/m5" , m5 )
57
+ self .assertEqual (sorted (mount_fs .listdir ("/subdir" )), ["m4" , "m5" ])
58
+ self .assertEqual (mount_fs .listdir ("/subdir/m5" ), [])
59
+ mount_fs .makedir ("/subdir/m4/foo" )
60
+ mount_fs .makedir ("/subdir/m5/bar" )
61
+ self .assertEqual (mount_fs .listdir ("/subdir/m4" ), ["foo" ])
62
+ self .assertEqual (mount_fs .listdir ("/subdir/m5" ), ["bar" ])
63
+ self .assertEqual (m4 .listdir ("/" ), ["foo" ])
64
+ self .assertEqual (m5 .listdir ("/" ), ["bar" ])
65
+ m5 .removedir ("/bar" )
66
+ self .assertEqual (mount_fs .listdir ("/subdir/m5" ), [])
53
67
54
68
def test_auto_close (self ):
55
69
"""Test MountFS auto close is working"""
@@ -85,8 +99,60 @@ def test_empty(self):
85
99
def test_mount_self (self ):
86
100
mount_fs = MountFS ()
87
101
with self .assertRaises (ValueError ):
88
- mount_fs .mount ("/" , mount_fs )
102
+ mount_fs .mount ("/m1 " , mount_fs )
89
103
90
104
def test_desc (self ):
91
105
mount_fs = MountFS ()
92
106
mount_fs .desc ("/" )
107
+
108
+ def test_makedirs (self ):
109
+ mount_fs = MountFS ()
110
+ with self .assertRaises (ResourceNotFound ):
111
+ mount_fs .makedir ("empty" )
112
+ m1 = MemoryFS ()
113
+ m2 = MemoryFS ()
114
+ with self .assertRaises (ResourceNotFound ):
115
+ mount_fs .makedirs ("/m1/foo/bar" , recreate = True )
116
+ mount_fs .mount ("/m1" , m1 )
117
+ mount_fs .makedirs ("/m1/foo/bar" , recreate = True )
118
+ self .assertEqual (m1 .listdir ("foo" ), ["bar" ])
119
+ with self .assertRaises (ResourceNotFound ):
120
+ mount_fs .makedirs ("/subdir/m2/bar/foo" , recreate = True )
121
+ mount_fs .mount ("/subdir/m2" , m2 )
122
+ mount_fs .makedirs ("/subdir/m2/bar/foo" , recreate = True )
123
+ self .assertEqual (m2 .listdir ("bar" ), ["foo" ])
124
+ with self .assertRaises (ResourceNotFound ):
125
+ mount_fs .makedir ("/subdir/m3" , recreate = True )
126
+
127
+ def test_unmount (self ):
128
+ mount_fs = MountFS ()
129
+ m1 = MemoryFS ()
130
+ m2 = MemoryFS ()
131
+ m3 = MemoryFS ()
132
+ m4 = MemoryFS ()
133
+ mount_fs .mount ("/m1" , m1 )
134
+ with self .assertRaises (ValueError ):
135
+ mount_fs .unmount ("/m2" )
136
+ mount_fs .mount ("/m2" , m2 )
137
+ self .assertEqual (sorted (mount_fs .listdir ("/" )), ["m1" , "m2" ])
138
+ mount_fs .unmount ("/m1" )
139
+ with self .assertRaises (ResourceNotFound ):
140
+ mount_fs .listdir ("/m1" )
141
+ self .assertEqual (mount_fs .listdir ("/" ), ["m2" ])
142
+ with self .assertRaises (ValueError ):
143
+ mount_fs .unmount ("/m1" )
144
+ mount_fs .mount ("/subdir/m3" , m3 )
145
+ with self .assertRaises (ValueError ):
146
+ mount_fs .unmount ("/subdir" )
147
+ mount_fs .mount ("/subdir/m4" , m4 )
148
+ self .assertEqual (sorted (mount_fs .listdir ("/" )), ["m2" , "subdir" ])
149
+ mount_fs .makedir ("/subdir/m4/foo" )
150
+ with self .assertRaises (ValueError ):
151
+ mount_fs .unmount ("/subdir/m4/foo" )
152
+ mount_fs .unmount ("/subdir/m4" )
153
+ self .assertEqual (sorted (mount_fs .listdir ("/" )), ["m2" , "subdir" ])
154
+ self .assertEqual (mount_fs .listdir ("/subdir" ), ["m3" ])
155
+ mount_fs .unmount ("/subdir/m3" )
156
+ self .assertEqual (mount_fs .listdir ("/" ), ["m2" ])
157
+ with self .assertRaises (ResourceNotFound ):
158
+ mount_fs .listdir ("/subdir" )
0 commit comments