File: test_move.py

package info (click to toggle)
python-fs 2.4.16-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,944 kB
  • sloc: python: 13,048; makefile: 226; sh: 3
file content (169 lines) | stat: -rw-r--r-- 6,774 bytes parent folder | download | duplicates (2)
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
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([(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)