File: test_opener.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 (271 lines) | stat: -rw-r--r-- 9,275 bytes parent folder | download
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
from __future__ import unicode_literals

import sys

import os
import shutil
import tempfile
import unittest

from fs import open_fs, opener
from fs.appfs import UserDataFS
from fs.memoryfs import MemoryFS
from fs.opener import errors, registry
from fs.opener.parse import ParseResult
from fs.opener.registry import Registry
from fs.osfs import OSFS

try:
    from unittest import mock
except ImportError:
    import mock


class TestParse(unittest.TestCase):
    def test_registry_repr(self):
        str(registry)
        repr(registry)

    def test_parse_not_url(self):
        with self.assertRaises(errors.ParseError):
            opener.parse("foo/bar")

    def test_parse_simple(self):
        parsed = opener.parse("osfs://foo/bar")
        expected = ParseResult("osfs", None, None, "foo/bar", {}, None)
        self.assertEqual(expected, parsed)

    def test_parse_credentials(self):
        parsed = opener.parse("ftp://user:pass@ftp.example.org")
        expected = ParseResult("ftp", "user", "pass", "ftp.example.org", {}, None)
        self.assertEqual(expected, parsed)

        parsed = opener.parse("ftp://user@ftp.example.org")
        expected = ParseResult("ftp", "user", "", "ftp.example.org", {}, None)
        self.assertEqual(expected, parsed)

    def test_parse_path(self):
        parsed = opener.parse("osfs://foo/bar!example.txt")
        expected = ParseResult("osfs", None, None, "foo/bar", {}, "example.txt")
        self.assertEqual(expected, parsed)

    def test_parse_params(self):
        parsed = opener.parse("ftp://ftp.example.org?proxy=ftp.proxy.org")
        expected = ParseResult(
            "ftp", None, None, "ftp.example.org", {"proxy": "ftp.proxy.org"}, None
        )
        self.assertEqual(expected, parsed)

    def test_parse_params_multiple(self):
        parsed = opener.parse("ftp://ftp.example.org?foo&bar=1")
        expected = ParseResult(
            "ftp", None, None, "ftp.example.org", {"foo": "", "bar": "1"}, None
        )
        self.assertEqual(expected, parsed)

    def test_parse_params_timeout(self):
        parsed = opener.parse("ftp://ftp.example.org?timeout=30")
        expected = ParseResult(
            "ftp", None, None, "ftp.example.org", {"timeout": "30"}, None
        )
        self.assertEqual(expected, parsed)

    def test_parse_user_password_proxy(self):
        parsed = opener.parse("ftp://user:password@ftp.example.org?proxy=ftp.proxy.org")
        expected = ParseResult(
            "ftp",
            "user",
            "password",
            "ftp.example.org",
            {"proxy": "ftp.proxy.org"},
            None,
        )
        self.assertEqual(expected, parsed)

    def test_parse_user_password_decode(self):
        parsed = opener.parse("ftp://user%40large:password@ftp.example.org")
        expected = ParseResult(
            "ftp", "user@large", "password", "ftp.example.org", {}, None
        )
        self.assertEqual(expected, parsed)

    def test_parse_resource_decode(self):
        parsed = opener.parse("ftp://user%40large:password@ftp.example.org/%7Econnolly")
        expected = ParseResult(
            "ftp", "user@large", "password", "ftp.example.org/~connolly", {}, None
        )
        self.assertEqual(expected, parsed)

    def test_parse_params_decode(self):
        parsed = opener.parse("ftp://ftp.example.org?decode=is%20working")
        expected = ParseResult(
            "ftp", None, None, "ftp.example.org", {"decode": "is working"}, None
        )
        self.assertEqual(expected, parsed)


class TestRegistry(unittest.TestCase):
    def test_protocols(self):
        self.assertIsInstance(opener.registry.protocols, list)

    def test_unknown_protocol(self):
        with self.assertRaises(errors.UnsupportedProtocol):
            opener.open_fs("unknown://")

    def test_install(self):
        """Test Registry.install works as a decorator."""
        registry = Registry()
        self.assertNotIn("foo", registry.protocols)

        @registry.install
        class FooOpener(opener.Opener):
            protocols = ["foo"]

            def open_fs(self, *args, **kwargs):
                pass

        self.assertIn("foo", registry.protocols)


class TestManageFS(unittest.TestCase):
    def test_manage_fs_url(self):
        with opener.manage_fs("mem://") as mem_fs:
            self.assertIsInstance(mem_fs, MemoryFS)
        self.assertTrue(mem_fs.isclosed())

    def test_manage_fs_obj(self):
        mem_fs = MemoryFS()
        with opener.manage_fs(mem_fs) as open_mem_fs:
            self.assertIs(mem_fs, open_mem_fs)
        self.assertFalse(mem_fs.isclosed())

    def test_manage_fs_error(self):
        try:
            with opener.manage_fs("mem://") as mem_fs:
                1 / 0
        except ZeroDivisionError:
            pass
        self.assertTrue(mem_fs.isclosed())


class TestOpeners(unittest.TestCase):
    def setUp(self):
        self.tmpdir = tempfile.mkdtemp()

    def tearDown(self):
        shutil.rmtree(self.tmpdir)

    def test_open_osfs(self):
        fs = opener.open_fs("osfs://.")
        self.assertIsInstance(fs, OSFS)

        # test default protocol
        fs = opener.open_fs("./")
        self.assertIsInstance(fs, OSFS)

    def test_open_memfs(self):
        fs = opener.open_fs("mem://")
        self.assertIsInstance(fs, MemoryFS)

    def test_open_zipfs(self):
        fh, zip_name = tempfile.mkstemp()
        os.close(fh)
        try:
            # Test creating zip
            with opener.open_fs("zip://" + zip_name, create=True) as make_zip:
                make_zip.writetext("foo.txt", "foofoo")
            # Test opening zip
            with opener.open_fs("zip://" + zip_name, writeable=False) as zip_fs:
                self.assertEqual(zip_fs.readtext("foo.txt"), "foofoo")
        finally:
            os.remove(zip_name)

    def test_open_tarfs(self):
        fh, tar_name = tempfile.mkstemp(suffix=".tar.gz")
        os.close(fh)
        try:
            # Test creating tar
            with opener.open_fs("tar://" + tar_name, create=True) as make_tar:
                self.assertEqual(make_tar.compression, "gz")
                make_tar.writetext("foo.txt", "foofoo")
            # Test opening tar
            with opener.open_fs("tar://" + tar_name, writeable=False) as tar_fs:
                self.assertEqual(tar_fs.readtext("foo.txt"), "foofoo")
        finally:
            os.remove(tar_name)

    def test_open_fs(self):
        mem_fs = opener.open_fs("mem://")
        mem_fs_2 = opener.open_fs(mem_fs)
        self.assertEqual(mem_fs, mem_fs_2)

    @mock.patch("platformdirs.{}".format(UserDataFS.app_dir), autospec=True, spec_set=True)
    def test_open_userdata(self, app_dir):
        app_dir.return_value = self.tmpdir

        with self.assertRaises(errors.OpenerError):
            opener.open_fs("userdata://foo:bar:baz:egg")

        app_fs = opener.open_fs("userdata://fstest:willmcgugan:1.0", create=True)
        self.assertEqual(app_fs.app_dirs.appname, "fstest")
        self.assertEqual(app_fs.app_dirs.appauthor, "willmcgugan")
        self.assertEqual(app_fs.app_dirs.version, "1.0")

    @mock.patch("platformdirs.{}".format(UserDataFS.app_dir), autospec=True, spec_set=True)
    def test_open_userdata_no_version(self, app_dir):
        app_dir.return_value = self.tmpdir

        app_fs = opener.open_fs("userdata://fstest:willmcgugan", create=True)
        self.assertEqual(app_fs.app_dirs.appname, "fstest")
        self.assertEqual(app_fs.app_dirs.appauthor, "willmcgugan")
        self.assertEqual(app_fs.app_dirs.version, None)

    @mock.patch("platformdirs.{}".format(UserDataFS.app_dir), autospec=True, spec_set=True)
    def test_user_data_opener(self, app_dir):
        app_dir.return_value = self.tmpdir

        user_data_fs = open_fs("userdata://fstest:willmcgugan:1.0", create=True)
        self.assertIsInstance(user_data_fs, UserDataFS)
        user_data_fs.makedir("foo", recreate=True)
        user_data_fs.writetext("foo/bar.txt", "baz")
        user_data_fs_foo_dir = open_fs("userdata://fstest:willmcgugan:1.0/foo/")
        self.assertEqual(user_data_fs_foo_dir.readtext("bar.txt"), "baz")

    @mock.patch("fs.ftpfs.FTPFS")
    def test_open_ftp(self, mock_FTPFS):
        open_fs("ftp://foo:bar@ftp.example.org")
        mock_FTPFS.assert_called_once_with(
            "ftp.example.org",
            passwd="bar",
            port=21,
            user="foo",
            proxy=None,
            timeout=10,
            tls=False,
        )

    @mock.patch("fs.ftpfs.FTPFS")
    def test_open_ftps(self, mock_FTPFS):
        open_fs("ftps://foo:bar@ftp.example.org")
        mock_FTPFS.assert_called_once_with(
            "ftp.example.org",
            passwd="bar",
            port=21,
            user="foo",
            proxy=None,
            timeout=10,
            tls=True,
        )

    @mock.patch("fs.ftpfs.FTPFS")
    def test_open_ftp_proxy(self, mock_FTPFS):
        open_fs("ftp://foo:bar@ftp.example.org?proxy=ftp.proxy.org")
        mock_FTPFS.assert_called_once_with(
            "ftp.example.org",
            passwd="bar",
            port=21,
            user="foo",
            proxy="ftp.proxy.org",
            timeout=10,
            tls=False,
        )