This repository was archived by the owner on Aug 6, 2024. It is now read-only.
forked from PyFilesystem/pyfilesystem2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_move.py
208 lines (179 loc) · 8.78 KB
/
test_move.py
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
from __future__ import unicode_literals
import unittest
try:
from unittest import mock
except ImportError:
import mock
from parameterized import parameterized, parameterized_class
import fs.move
from fs import open_fs
from fs.errors import FSError, ResourceReadOnly
from fs.path import join
from fs.wrap import read_only
@parameterized_class(("preserve_time",), [(True,), (False,)])
class TestMoveCheckTime(unittest.TestCase):
def test_move_fs(self):
namespaces = ("details", "modified")
src_fs = open_fs("mem://")
src_fs.makedirs("foo/bar")
src_fs.touch("test.txt")
src_fs.touch("foo/bar/baz.txt")
src_file1_info = src_fs.getinfo("test.txt", namespaces)
src_file2_info = src_fs.getinfo("foo/bar/baz.txt", namespaces)
dst_fs = open_fs("mem://")
dst_fs.create("test.txt")
dst_fs.setinfo("test.txt", {"details": {"modified": 1000000}})
fs.move.move_fs(src_fs, dst_fs, preserve_time=self.preserve_time)
self.assertTrue(src_fs.isempty("/"))
self.assertTrue(dst_fs.isdir("foo/bar"))
self.assertTrue(dst_fs.isfile("test.txt"))
self.assertTrue(dst_fs.isfile("foo/bar/baz.txt"))
if self.preserve_time:
dst_file1_info = dst_fs.getinfo("test.txt", namespaces)
dst_file2_info = dst_fs.getinfo("foo/bar/baz.txt", namespaces)
self.assertEqual(dst_file1_info.modified, src_file1_info.modified)
self.assertEqual(dst_file2_info.modified, src_file2_info.modified)
def test_move_file(self):
namespaces = ("details", "modified")
with open_fs("mem://") as src_fs, open_fs("mem://") as dst_fs:
src_fs.writetext("source.txt", "Source")
src_fs_file_info = src_fs.getinfo("source.txt", namespaces)
fs.move.move_file(
src_fs,
"source.txt",
dst_fs,
"dest.txt",
preserve_time=self.preserve_time,
)
self.assertFalse(src_fs.exists("source.txt"))
self.assertEqual(dst_fs.readtext("dest.txt"), "Source")
if self.preserve_time:
dst_fs_file_info = dst_fs.getinfo("dest.txt", namespaces)
self.assertEqual(src_fs_file_info.modified, dst_fs_file_info.modified)
def test_move_dir(self):
namespaces = ("details", "modified")
src_fs = open_fs("mem://")
src_fs.makedirs("foo/bar")
src_fs.touch("test.txt")
src_fs.touch("foo/bar/baz.txt")
src_file2_info = src_fs.getinfo("foo/bar/baz.txt", namespaces)
dst_fs = open_fs("mem://")
dst_fs.create("test.txt")
dst_fs.setinfo("test.txt", {"details": {"modified": 1000000}})
fs.move.move_dir(src_fs, "/foo", dst_fs, "/", preserve_time=self.preserve_time)
self.assertFalse(src_fs.exists("foo"))
self.assertTrue(src_fs.isfile("test.txt"))
self.assertTrue(dst_fs.isdir("bar"))
self.assertTrue(dst_fs.isfile("bar/baz.txt"))
if self.preserve_time:
dst_file2_info = dst_fs.getinfo("bar/baz.txt", namespaces)
self.assertEqual(dst_file2_info.modified, src_file2_info.modified)
class TestMove(unittest.TestCase):
def test_move_file_tempfs(self):
with open_fs("temp://") as src, open_fs("temp://") as dst:
src_dir = src.makedir("Some subfolder")
src_dir.writetext("file.txt", "Content")
dst_dir = dst.makedir("dest dir")
fs.move.move_file(src_dir, "file.txt", dst_dir, "target.txt")
self.assertFalse(src.exists("Some subfolder/file.txt"))
self.assertEqual(dst.readtext("dest dir/target.txt"), "Content")
def test_move_file_fs_urls(self):
# create a temp dir to work on
with open_fs("temp://") as tmp:
path = tmp.getsyspath("/")
tmp.makedir("subdir_src")
tmp.writetext("subdir_src/file.txt", "Content")
tmp.makedir("subdir_dst")
fs.move.move_file(
"osfs://" + join(path, "subdir_src"),
"file.txt",
"osfs://" + join(path, "subdir_dst"),
"target.txt",
)
self.assertFalse(tmp.exists("subdir_src/file.txt"))
self.assertEqual(tmp.readtext("subdir_dst/target.txt"), "Content")
def test_move_file_same_fs_read_only_source(self):
with open_fs("temp://") as tmp:
path = tmp.getsyspath("/")
tmp.writetext("file.txt", "Content")
src = read_only(open_fs(path))
dst = tmp.makedir("sub")
with self.assertRaises(ResourceReadOnly):
fs.move.move_file(src, "file.txt", dst, "target_file.txt")
self.assertTrue(src.exists("file.txt"))
self.assertFalse(
dst.exists("target_file.txt"), "file should not have been copied over"
)
def test_move_file_read_only_mem_source(self):
with open_fs("mem://") as src, open_fs("mem://") as dst:
src.writetext("file.txt", "Content")
dst_sub = dst.makedir("sub")
src_ro = read_only(src)
with self.assertRaises(ResourceReadOnly):
fs.move.move_file(src_ro, "file.txt", dst_sub, "target.txt")
self.assertTrue(src.exists("file.txt"))
self.assertFalse(
dst_sub.exists("target.txt"), "file should not have been copied over"
)
def test_move_file_read_only_mem_dest(self):
with open_fs("mem://") as src, open_fs("mem://") as dst:
src.writetext("file.txt", "Content")
dst_ro = read_only(dst)
with self.assertRaises(ResourceReadOnly):
fs.move.move_file(src, "file.txt", dst_ro, "target.txt")
self.assertTrue(src.exists("file.txt"))
self.assertFalse(
dst_ro.exists("target.txt"), "file should not have been copied over"
)
@parameterized.expand([("temp", "temp://"), ("mem", "mem://")])
def test_move_file_overwrite(self, _, fs_url):
# we use TempFS and MemoryFS in order to make sure the optimized code path
# behaves like the regular one (TempFS tests the optmized code path).
with open_fs(fs_url) as src, open_fs(fs_url) as dst:
src.writetext("file.txt", "source content")
dst.writetext("target.txt", "target content")
self.assertTrue(src.exists("file.txt"))
self.assertFalse(src.exists("target.txt"))
self.assertFalse(dst.exists("file.txt"))
self.assertTrue(dst.exists("target.txt"))
fs.move.move_file(src, "file.txt", dst, "target.txt")
self.assertFalse(src.exists("file.txt"))
self.assertFalse(src.exists("target.txt"))
self.assertFalse(dst.exists("file.txt"))
self.assertTrue(dst.exists("target.txt"))
self.assertEqual(dst.readtext("target.txt"), "source content")
@parameterized.expand([("temp", "temp://"), ("mem", "mem://")])
def test_move_file_overwrite_itself(self, _, fs_url):
# we use TempFS and MemoryFS in order to make sure the optimized code path
# behaves like the regular one (TempFS tests the optmized code path).
with open_fs(fs_url) as tmp:
tmp.writetext("file.txt", "content")
fs.move.move_file(tmp, "file.txt", tmp, "file.txt")
self.assertTrue(tmp.exists("file.txt"))
self.assertEqual(tmp.readtext("file.txt"), "content")
@parameterized.expand([("temp", "temp://"), ("mem", "mem://")])
def test_move_file_overwrite_itself_relpath(self, _, fs_url):
# we use TempFS and MemoryFS in order to make sure the optimized code path
# behaves like the regular one (TempFS tests the optmized code path).
with open_fs(fs_url) as tmp:
new_dir = tmp.makedir("dir")
new_dir.writetext("file.txt", "content")
fs.move.move_file(tmp, "dir/../dir/file.txt", tmp, "dir/file.txt")
self.assertTrue(tmp.exists("dir/file.txt"))
self.assertEqual(tmp.readtext("dir/file.txt"), "content")
@parameterized.expand([(True,), (False,)])
def test_move_file_cleanup_on_error(self, cleanup):
with open_fs("mem://") as src, open_fs("mem://") as dst:
src.writetext("file.txt", "Content")
with mock.patch.object(src, "remove") as mck:
mck.side_effect = FSError
with self.assertRaises(FSError):
fs.move.move_file(
src,
"file.txt",
dst,
"target.txt",
cleanup_dst_on_error=cleanup,
)
self.assertTrue(src.exists("file.txt"))
self.assertEqual(not dst.exists("target.txt"), cleanup)