summaryrefslogtreecommitdiff
path: root/fs/tests/test_path.py
blob: dc023a340dc31a8ce6e694f94014068ebc38b9a0 (plain)
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
"""

  fs.tests.test_path:  testcases for the fs path functions

"""


import unittest
import fs.tests

from fs.path import *

class TestPathFunctions(unittest.TestCase):
    """Testcases for FS path functions."""

    def test_normpath(self):
        tests = [   ("\\a\\b\\c", "\\a\\b\\c"),
                    (".", ""),
                    ("./", ""),
                    ("", ""),
                    ("/.", "/"),
                    ("/a/b/c", "/a/b/c"),
                    ("a/b/c", "a/b/c"),
                    ("a/b/../c/", "a/c"),
                    ("/","/"),
                    (u"a/\N{GREEK SMALL LETTER BETA}/c",u"a/\N{GREEK SMALL LETTER BETA}/c"),
                    ]
        for path, result in tests:
            self.assertEqual(normpath(path), result)

    def test_pathjoin(self):
        tests = [   ("", "a", "a"),
                    ("a", "a", "a/a"),
                    ("a/b", "../c", "a/c"),
                    ("a/b/../c", "d", "a/c/d"),
                    ("/a/b/c", "d", "/a/b/c/d"),
                    ("/a/b/c", "../../../d", "/d"),
                    ("a", "b", "c", "a/b/c"),
                    ("a/b/c", "../d", "c", "a/b/d/c"),
                    ("a/b/c", "../d", "/a", "/a"),
                    ("aaa", "bbb/ccc", "aaa/bbb/ccc"),
                    ("aaa", "bbb\\ccc", "aaa/bbb\\ccc"),
                    ("aaa", "bbb", "ccc", "/aaa", "eee", "/aaa/eee"),
                    ("a/b", "./d", "e", "a/b/d/e"),
                    ("/", "/", "/"),
                    ("/", "", "/"),
                    (u"a/\N{GREEK SMALL LETTER BETA}","c",u"a/\N{GREEK SMALL LETTER BETA}/c"),
        ]
        for testpaths in tests:
            paths = testpaths[:-1]
            result = testpaths[-1]
            self.assertEqual(pathjoin(*paths), result)

        self.assertRaises(ValueError, pathjoin, "..")
        self.assertRaises(ValueError, pathjoin, "../")
        self.assertRaises(ValueError, pathjoin, "/..")
        self.assertRaises(ValueError, pathjoin, "./../")
        self.assertRaises(ValueError, pathjoin, "a/b", "../../..")
        self.assertRaises(ValueError, pathjoin, "a/b/../../../d")

    def test_relpath(self):
        tests = [   ("/a/b", "a/b"),
                    ("a/b", "a/b"),
                    ("/", "") ]

        for path, result in tests:
            self.assertEqual(relpath(path), result)

    def test_abspath(self):
        tests = [   ("/a/b", "/a/b"),
                    ("a/b", "/a/b"),
                    ("/", "/") ]

        for path, result in tests:
            self.assertEqual(abspath(path), result)

    def test_iteratepath(self):
        tests = [   ("a/b", ["a", "b"]),
                    ("", [] ),
                    ("aaa/bbb/ccc", ["aaa", "bbb", "ccc"]),
                    ("a/b/c/../d", ["a", "b", "d"]) ]

        for path, results in tests:
            for path_component, expected in zip(iteratepath(path), results):
                self.assertEqual(path_component, expected)

        self.assertEqual(list(iteratepath("a/b/c/d", 1)), ["a", "b/c/d"])
        self.assertEqual(list(iteratepath("a/b/c/d", 2)), ["a", "b", "c/d"])

    def test_pathsplit(self):
        tests = [   ("a/b", ("a", "b")),
                    ("a/b/c", ("a/b", "c")),
                    ("a", ("", "a")),
                    ("", ("", "")),
                    ("/", ("/", "")),
                    ("/foo", ("/", "foo")),
                    ("foo/bar", ("foo", "bar")),
                    ("foo/bar/baz", ("foo/bar", "baz")),
                ]
        for path, result in tests:
            self.assertEqual(pathsplit(path), result)

    def test_recursepath(self):
        self.assertEquals(recursepath("/"),["/"])
        self.assertEquals(recursepath("hello"),["/","/hello"])
        self.assertEquals(recursepath("/hello/world/"),["/","/hello","/hello/world"])
        self.assertEquals(recursepath("/hello/world/",reverse=True),["/hello/world","/hello","/"])
        self.assertEquals(recursepath("hello",reverse=True),["/hello","/"])
        self.assertEquals(recursepath("",reverse=True),["/"])

    def test_isdotfile(self):
        for path in ['.foo',
                     '.svn',
                     'foo/.svn',
                     'foo/bar/.svn',
                     '/foo/.bar']:
            self.assert_(isdotfile(path))

        for path in ['asfoo',
                     'df.svn',
                     'foo/er.svn',
                     'foo/bar/test.txt',
                     '/foo/bar']:
            self.assertFalse(isdotfile(path))

    def test_dirname(self):
        tests = [('foo', ''),
                 ('foo/bar', 'foo'),
                 ('foo/bar/baz', 'foo/bar'),
                 ('/foo/bar', '/foo'),
                 ('/foo', '/'),
                 ('/', '/')]
        for path, test_dirname in tests:
            self.assertEqual(dirname(path), test_dirname)

    def test_basename(self):
        tests = [('foo', 'foo'),
                 ('foo/bar', 'bar'),
                 ('foo/bar/baz', 'baz'),
                 ('/', '')]
        for path, test_basename in tests:
            self.assertEqual(basename(path), test_basename)

    def test_iswildcard(self):
        self.assert_(iswildcard('*'))
        self.assert_(iswildcard('*.jpg'))
        self.assert_(iswildcard('foo/*'))
        self.assert_(iswildcard('foo/{}'))
        self.assertFalse(iswildcard('foo'))
        self.assertFalse(iswildcard('img.jpg'))
        self.assertFalse(iswildcard('foo/bar'))


class Test_PathMap(unittest.TestCase):

    def test_basics(self):
        map = PathMap()
        map["hello"] = "world"
        self.assertEquals(map["/hello"],"world")
        self.assertEquals(map["/hello/"],"world")
        self.assertEquals(map.get("hello"),"world")

    def test_iteration(self):
        map = PathMap()
        map["hello/world"] = 1
        map["hello/world/howareya"] = 2
        map["hello/world/iamfine"] = 3
        map["hello/kitty"] = 4
        map["hello/kitty/islame"] = 5
        map["batman/isawesome"] = 6
        self.assertEquals(set(map.iterkeys()),set(("/hello/world","/hello/world/howareya","/hello/world/iamfine","/hello/kitty","/hello/kitty/islame","/batman/isawesome")))
        self.assertEquals(sorted(map.values()),range(1,7))
        self.assertEquals(sorted(map.items("/hello/world/")),[("/hello/world",1),("/hello/world/howareya",2),("/hello/world/iamfine",3)])
        self.assertEquals(zip(map.keys(),map.values()),map.items())
        self.assertEquals(zip(map.keys("batman"),map.values("batman")),map.items("batman"))
        self.assertEquals(set(map.iternames("hello")),set(("world","kitty")))
        self.assertEquals(set(map.iternames("/hello/kitty")),set(("islame",)))

        del map["hello/kitty/islame"]
        self.assertEquals(set(map.iternames("/hello/kitty")),set())
        self.assertEquals(set(map.iterkeys()),set(("/hello/world","/hello/world/howareya","/hello/world/iamfine","/hello/kitty","/batman/isawesome")))
        self.assertEquals(set(map.values()),set(range(1,7)) - set((5,)))