File: test_archives.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 (136 lines) | stat: -rw-r--r-- 4,451 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
# -*- encoding: UTF-8
from __future__ import unicode_literals

import os
import stat
from six import text_type

from fs import errors, walk
from fs.enums import ResourceType
from fs.opener import open_fs
from fs.test import UNICODE_TEXT


class ArchiveTestCases(object):
    def make_source_fs(self):
        return open_fs("temp://")

    def build_source(self, fs):
        fs.makedirs("foo/bar/baz")
        fs.makedir("tmp")
        fs.writetext("Файл", "unicode filename")
        fs.writetext("top.txt", "Hello, World")
        fs.writetext("top2.txt", "Hello, World")
        fs.writetext("foo/bar/egg", "foofoo")
        fs.makedir("unicode")
        fs.writetext("unicode/text.txt", UNICODE_TEXT)

    def compress(self, fs):
        pass

    def load_archive(self):
        pass

    def remove_archive(self):
        pass

    def setUp(self):
        self.source_fs = source_fs = self.make_source_fs()
        self.build_source(source_fs)
        self.compress(source_fs)
        self.fs = self.load_archive()

    def tearDown(self):
        self.source_fs.close()
        self.fs.close()
        self.remove_archive()

    def test_repr(self):
        repr(self.fs)

    def test_str(self):
        self.assertIsInstance(text_type(self.fs), text_type)

    def test_readonly(self):
        with self.assertRaises(errors.ResourceReadOnly):
            self.fs.makedir("newdir")
        with self.assertRaises(errors.ResourceReadOnly):
            self.fs.remove("top.txt")
        with self.assertRaises(errors.ResourceReadOnly):
            self.fs.removedir("foo/bar/baz")
        with self.assertRaises(errors.ResourceReadOnly):
            self.fs.create("foo.txt")
        with self.assertRaises(errors.ResourceReadOnly):
            self.fs.setinfo("foo.txt", {})

    def test_getinfo(self):
        root = self.fs.getinfo("/", ["details"])
        self.assertEqual(root.name, "")
        self.assertTrue(root.is_dir)
        self.assertEqual(root.get("details", "type"), ResourceType.directory)

        bar = self.fs.getinfo("foo/bar", ["details"])
        self.assertEqual(bar.name, "bar")
        self.assertTrue(bar.is_dir)
        self.assertEqual(bar.get("details", "type"), ResourceType.directory)

        top = self.fs.getinfo("top.txt", ["details", "access"])
        self.assertEqual(top.size, 12)
        self.assertFalse(top.is_dir)

        try:
            source_syspath = self.source_fs.getsyspath("/top.txt")
        except errors.NoSysPath:
            pass
        else:
            if top.has_namespace("access"):
                self.assertEqual(
                    top.permissions.mode, stat.S_IMODE(os.stat(source_syspath).st_mode)
                )

        self.assertEqual(top.get("details", "type"), ResourceType.file)

    def test_listdir(self):
        self.assertEqual(
            sorted(self.source_fs.listdir("/")), sorted(self.fs.listdir("/"))
        )
        for name in self.fs.listdir("/"):
            self.assertIsInstance(name, text_type)
        with self.assertRaises(errors.DirectoryExpected):
            self.fs.listdir("top.txt")
        with self.assertRaises(errors.ResourceNotFound):
            self.fs.listdir("nothere")

    def test_open(self):
        with self.fs.open("top.txt") as f:
            chars = []
            while True:
                c = f.read(2)
                if not c:
                    break
                chars.append(c)
            self.assertEqual("".join(chars), "Hello, World")
        with self.assertRaises(errors.ResourceNotFound):
            with self.fs.open("nothere.txt") as f:
                pass
        with self.assertRaises(errors.FileExpected):
            with self.fs.open("foo") as f:
                pass

    def test_gets(self):
        self.assertEqual(self.fs.readtext("top.txt"), "Hello, World")
        self.assertEqual(self.fs.readtext("foo/bar/egg"), "foofoo")
        self.assertEqual(self.fs.readbytes("top.txt"), b"Hello, World")
        self.assertEqual(self.fs.readbytes("foo/bar/egg"), b"foofoo")
        with self.assertRaises(errors.ResourceNotFound):
            self.fs.readbytes("what.txt")

    def test_walk_files(self):
        source_files = sorted(walk.walk_files(self.source_fs))
        archive_files = sorted(walk.walk_files(self.fs))

        self.assertEqual(source_files, archive_files)

    def test_implied_dir(self):
        self.fs.getinfo("foo/bar")
        self.fs.getinfo("foo")