aboutsummaryrefslogtreecommitdiffstats
path: root/util/folder/path_ut.pyx
blob: be8d3ce35af5815b498264ac9ec29979750130da (plain) (blame)
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# cython: c_string_type=str, c_string_encoding=utf8

from util.folder.path cimport TFsPath
from util.generic.string cimport TString, TStringBuf
from util.generic.vector cimport TVector

import unittest
import yatest.common

import os.path


class TestPath(unittest.TestCase):
    def test_ctor1(self):
        cdef TFsPath path = TFsPath()
        self.assertEqual(path.IsDefined(), False)
        self.assertEqual(path.c_str(), "")

    def test_ctor2(self):
        cdef TString str_path = "/a/b/c"
        cdef TFsPath path = TFsPath(str_path)
        self.assertEqual(path.IsDefined(), True)
        self.assertEqual(path.c_str(), "/a/b/c")

    def test_ctor3(self):
        cdef TStringBuf buf_path = "/a/b/c"
        cdef TFsPath path = TFsPath(buf_path)
        self.assertEqual(path.IsDefined(), True)
        self.assertEqual(path.c_str(), "/a/b/c")

    def test_ctor4(self):
        cdef char* char_path = "/a/b/c"
        cdef TFsPath path = TFsPath(char_path)
        self.assertEqual(path.IsDefined(), True)
        self.assertEqual(path.c_str(), "/a/b/c")

    def test_assignment(self):
        cdef TFsPath path1 = TFsPath("/a/b")
        cdef TFsPath path2 = TFsPath("/a/c")

        self.assertEqual(path1.GetPath(), "/a/b")
        self.assertEqual(path2.GetPath(), "/a/c")

        path2 = path1

        self.assertEqual(path1.GetPath(), "/a/b")
        self.assertEqual(path2.GetPath(), "/a/b")

    def test_check_defined(self):
        cdef TFsPath path1 = TFsPath()
        with self.assertRaises(RuntimeError):
            path1.CheckDefined()
        self.assertEqual(path1.IsDefined(), False)
        if path1:
            assert False
        else:
            pass

        cdef TFsPath path2 = TFsPath("")
        with self.assertRaises(RuntimeError):
            path2.CheckDefined()
        self.assertEqual(path2.IsDefined(), False)
        if path2:
            assert False
        else:
            pass

        cdef TFsPath path3 = TFsPath("/")
        path3.CheckDefined()
        self.assertEqual(path3.IsDefined(), True)
        if path3:
            pass
        else:
            assert False

    def test_comparison(self):
        cdef TFsPath path1 = TFsPath("/a/b")
        cdef TFsPath path2 = TFsPath("/a/c")
        cdef TFsPath path3 = TFsPath("/a/b")

        self.assertEqual(path1 == path3, True)
        self.assertEqual(path1 != path2, True)
        self.assertEqual(path3 != path2, True)

    def test_concatenation(self):
        cdef TFsPath path1 = TFsPath("/a")
        cdef TFsPath path2 = TFsPath("b")
        cdef TFsPath path3 = path1 / path2
        cdef TFsPath path4 = TFsPath("/a/b")

        self.assertEqual(path3 == path4, True)

    def test_fix(self):
        cdef TFsPath path = TFsPath("test_fix/b/c/../d")
        cdef TFsPath fixed = path.Fix()
        self.assertEqual(fixed.GetPath(), "test_fix/b/d")

    def test_parts(self):
        cdef TFsPath path = TFsPath("/a/b/c")
        self.assertEqual(path.GetPath(), "/a/b/c")
        self.assertEqual(path.GetName(), "c")
        self.assertEqual(path.GetExtension(), "")
        self.assertEqual(path.Basename(), "c")
        self.assertEqual(path.Dirname(), "/a/b")

        cdef TFsPath path_ext = TFsPath("/a/b/c.ext")
        self.assertEqual(path_ext.GetPath(), "/a/b/c.ext")
        self.assertEqual(path_ext.GetName(), "c.ext")
        self.assertEqual(path_ext.GetExtension(), "ext")
        self.assertEqual(path_ext.Basename(), "c.ext")
        self.assertEqual(path_ext.Dirname(), "/a/b")

        cdef TFsPath path_only_ext = TFsPath("/a/b/.ext")
        self.assertEqual(path_only_ext.GetPath(), "/a/b/.ext")
        self.assertEqual(path_only_ext.GetName(), ".ext")
        self.assertEqual(path_only_ext.GetExtension(), "")
        self.assertEqual(path_only_ext.Basename(), ".ext")
        self.assertEqual(path_only_ext.Dirname(), "/a/b")

        cdef TFsPath path_dir = TFsPath("/a/b/")
        self.assertEqual(path_dir.GetPath(), "/a/b/")
        self.assertEqual(path_dir.GetName(), "b")
        self.assertEqual(path_dir.GetExtension(), "")
        self.assertEqual(path_dir.Basename(), "b")
        self.assertEqual(path_dir.Dirname(), "/a")

    def test_absolute(self):
        cdef TFsPath path_absolute = TFsPath("/a/b/c")
        self.assertEqual(path_absolute.IsAbsolute(), True)
        self.assertEqual(path_absolute.IsRelative(), False)

        self.assertEqual(path_absolute.IsSubpathOf(TFsPath("/a/b")), True)
        self.assertEqual(path_absolute.IsNonStrictSubpathOf(TFsPath("/a/b")), True)
        self.assertEqual(TFsPath("/a/b").IsContainerOf(path_absolute), True)

        self.assertEqual(path_absolute.IsSubpathOf(TFsPath("/a/b/c")), False)
        self.assertEqual(path_absolute.IsNonStrictSubpathOf(TFsPath("/a/b/c")), True)
        self.assertEqual(TFsPath("/a/b/c").IsContainerOf(path_absolute), False)

        self.assertEqual(path_absolute.IsSubpathOf(TFsPath("/a/c")), False)
        self.assertEqual(path_absolute.IsNonStrictSubpathOf(TFsPath("/a/c")), False)
        self.assertEqual(TFsPath("/a/c").IsContainerOf(path_absolute), False)

        with self.assertRaises(RuntimeError):
            path_absolute.RelativeTo(TFsPath("/a/c"))
        self.assertEqual(path_absolute.RelativePath(TFsPath("/a/с")).GetPath(), "../b/c")
        self.assertEqual(path_absolute.RelativeTo(TFsPath("/a")).GetPath(), "b/c")
        self.assertEqual(path_absolute.RelativePath(TFsPath("/a")).GetPath(), "b/c")
        self.assertEqual(path_absolute.RelativeTo(TFsPath("/")).GetPath(), "a/b/c")
        self.assertEqual(path_absolute.RelativePath(TFsPath("/")).GetPath(), "a/b/c")

        with self.assertRaises(RuntimeError):
            path_absolute.RelativeTo(TFsPath("./a"))
        with self.assertRaises(RuntimeError):
            path_absolute.RelativePath(TFsPath("d"))
        self.assertEqual(path_absolute.RelativePath(TFsPath("./a")).GetPath(), "b/c")

        self.assertEqual(path_absolute.Parent().GetPath(), "/a/b")
        self.assertEqual(path_absolute.Child("d").GetPath(), "/a/b/c/d")

    def test_relative(self):
        cdef TFsPath path_relative_1 = TFsPath("a/b/c")
        self.assertEqual(path_relative_1.IsAbsolute(), False)
        self.assertEqual(path_relative_1.IsRelative(), True)

        self.assertEqual(path_relative_1.IsSubpathOf(TFsPath("a/b")), True)
        self.assertEqual(path_relative_1.IsNonStrictSubpathOf(TFsPath("a/b")), True)
        self.assertEqual(TFsPath("a/b").IsContainerOf(path_relative_1), True)

        self.assertEqual(path_relative_1.IsSubpathOf(TFsPath("a/b/c")), False)
        self.assertEqual(path_relative_1.IsNonStrictSubpathOf(TFsPath("a/b/c")), True)
        self.assertEqual(TFsPath("a/b/c").IsContainerOf(path_relative_1), False)

        self.assertEqual(path_relative_1.IsSubpathOf(TFsPath("a/c")), False)
        self.assertEqual(path_relative_1.IsNonStrictSubpathOf(TFsPath("a/c")), False)
        self.assertEqual(TFsPath("a/c").IsContainerOf(path_relative_1), False)

        self.assertEqual(path_relative_1.Parent().GetPath(), "a/b")
        self.assertEqual(path_relative_1.Child("d").GetPath(), "a/b/c/d")

        cdef TFsPath path_relative_2 = TFsPath("./a/b/c")
        self.assertEqual(path_relative_2.IsAbsolute(), False)
        self.assertEqual(path_relative_2.IsRelative(), True)

        self.assertEqual(path_relative_2.IsSubpathOf(TFsPath("a/b")), True)
        self.assertEqual(path_relative_2.IsNonStrictSubpathOf(TFsPath("a/b")), True)
        self.assertEqual(TFsPath("a/b").IsContainerOf(path_relative_2), True)

        self.assertEqual(path_relative_2.IsSubpathOf(TFsPath("a/b/c")), False)
        self.assertEqual(path_relative_2.IsNonStrictSubpathOf(TFsPath("a/b/c")), True)
        self.assertEqual(TFsPath("a/b/c").IsContainerOf(path_relative_2), False)

        self.assertEqual(path_relative_2.IsSubpathOf(TFsPath("a/c")), False)
        self.assertEqual(path_relative_2.IsNonStrictSubpathOf(TFsPath("a/c")), False)
        self.assertEqual(TFsPath("a/c").IsContainerOf(path_relative_2), False)

        with self.assertRaises(RuntimeError):
            path_relative_2.RelativeTo(TFsPath("a/c"))
        self.assertEqual(path_relative_2.RelativePath(TFsPath("a/с")).GetPath(), "../b/c")
        self.assertEqual(path_relative_2.RelativeTo(TFsPath("a")).GetPath(), "b/c")
        self.assertEqual(path_relative_2.RelativePath(TFsPath("a")).GetPath(), "b/c")
        self.assertEqual(path_relative_2.RelativeTo(TFsPath("./")).GetPath(), "a/b/c")
        self.assertEqual(path_relative_2.RelativePath(TFsPath("/a")).GetPath(), "b/c")

        with self.assertRaises(RuntimeError):
            self.assertEqual(path_relative_2.RelativePath(TFsPath("./")).GetPath(), "a/b/c")

        with self.assertRaises(RuntimeError):
            path_relative_2.RelativeTo(TFsPath("/d"))
        with self.assertRaises(RuntimeError):
            path_relative_2.RelativePath(TFsPath("/d"))
        with self.assertRaises(RuntimeError):
            path_relative_2.RelativePath(TFsPath("/"))

        self.assertEqual(path_relative_2.Parent().GetPath(), "a/b")
        self.assertEqual(path_relative_2.Child("d").GetPath(), "a/b/c/d")

    def test_mkdir(self):
        cdef TFsPath directory = TFsPath("test_mkdir")
        cdef TFsPath full = directory / directory
        cdef TFsPath internal = full / directory
        with self.assertRaises(RuntimeError):
            full.MkDir()
        full.MkDirs()
        internal.MkDir()

    def test_list(self):
        cdef TFsPath dir = TFsPath("test_list")
        dir.MkDir()
        TFsPath("test_list/b").Touch()
        TFsPath("test_list/c").Touch()

        cdef TVector[TFsPath] files
        cdef TVector[TString] names

        dir.List(files)
        dir.ListNames(names)

        self.assertEqual(files.size(), 2)
        self.assertEqual(sorted([files[0].GetPath(), files[1].GetPath()]), ["test_list/b", "test_list/c"])
        self.assertEqual(names.size(), 2)
        self.assertEqual(sorted(list(names)), ["b", "c"])

    def test_contains(self):
        cdef TFsPath path = TFsPath("a/b/c")
        self.assertEqual(path.Contains("c"), True)
        self.assertEqual(path.Contains("b"), True)
        self.assertEqual(path.Contains("d"), False)

    def test_delete(self):
        cdef TFsPath root = TFsPath("/")
        with self.assertRaises(RuntimeError):
            root.DeleteIfExists()
        with self.assertRaises(RuntimeError):
            root.ForceDelete()

        cdef TFsPath directory = TFsPath("test_delete")
        cdef TFsPath full = directory / directory
        full.MkDirs()

        self.assertEqual(full.Exists(), True)
        with self.assertRaises(RuntimeError):
            directory.DeleteIfExists()
        self.assertEqual(directory.Exists(), True)
        directory.ForceDelete()
        self.assertEqual(directory.Exists(), False)

        cdef TFsPath local_file = TFsPath("test_delete_1")
        self.assertEqual(local_file.Exists(), False)
        local_file.DeleteIfExists()
        self.assertEqual(local_file.Exists(), False)
        local_file.ForceDelete()
        self.assertEqual(local_file.Exists(), False)

        local_file.Touch()
        self.assertEqual(local_file.Exists(), True)
        local_file.DeleteIfExists()
        self.assertEqual(local_file.Exists(), False)

        local_file.Touch()
        self.assertEqual(local_file.Exists(), True)
        local_file.ForceDelete()
        self.assertEqual(local_file.Exists(), False)

        full.MkDirs()
        self.assertEqual(full.Exists(), True)
        full.DeleteIfExists()
        self.assertEqual(full.Exists(), False)
        self.assertEqual(directory.Exists(), True)
        directory.DeleteIfExists()
        self.assertEqual(directory.Exists(), False)

    def test_checks(self):
        cdef TFsPath local_file = TFsPath("test_checks")
        with self.assertRaises(RuntimeError):
            local_file.CheckExists()
        local_file.Touch()
        self.assertEqual(local_file.Exists(), True)
        self.assertEqual(local_file.IsDirectory(), False)
        self.assertEqual(local_file.IsFile(), True)
        self.assertEqual(local_file.IsSymlink(), False)
        local_file.CheckExists()

        local_file.DeleteIfExists()
        local_file.MkDir()
        self.assertEqual(local_file.Exists(), True)
        self.assertEqual(local_file.IsDirectory(), True)
        self.assertEqual(local_file.IsFile(), False)
        self.assertEqual(local_file.IsSymlink(), False)
        local_file.CheckExists()

    def test_rename(self):
        cdef TFsPath path = TFsPath("test_rename_a")
        path.Touch()

        cdef TString path_str = "test_rename_b"
        cdef TFsPath path_from_str = TFsPath(path_str)
        self.assertEqual(path.Exists(), True)
        self.assertEqual(path_from_str.Exists(), False)
        path.RenameTo(path_str)
        self.assertEqual(path.Exists(), False)
        self.assertEqual(path_from_str.Exists(), True)

        cdef const char* path_char = "test_rename_c"
        cdef TFsPath path_from_char = TFsPath(path_char)
        self.assertEqual(path_from_str.Exists(), True)
        self.assertEqual(path_from_char.Exists(), False)
        path_from_str.RenameTo(path_char)
        self.assertEqual(path_from_str.Exists(), False)
        self.assertEqual(path_from_char.Exists(), True)

        path_from_char.RenameTo(path)

        self.assertEqual(path_from_char.Exists(), False)
        self.assertEqual(path.Exists(), True)

        path.ForceRenameTo(path_str)

        self.assertEqual(path_from_str.Exists(), True)
        self.assertEqual(path.Exists(), False)

        with self.assertRaises(RuntimeError):
            path_from_str.RenameTo("")

    def test_copy(self):
        cdef TString dst = "test_copy_dst"
        cdef TFsPath src_path = TFsPath("test_copy_src")
        cdef TFsPath dst_path = TFsPath(dst)
        self.assertEqual(src_path.Exists(), False)
        src_path.Touch()
        self.assertEqual(src_path.Exists(), True)
        src_path.CopyTo(dst, False)
        self.assertEqual(src_path.Exists(), True)
        self.assertEqual(dst_path.Exists(), True)

    def test_real_path(self):
        cdef TFsPath path = TFsPath("test_real_path_a")
        path.Touch()
        real_work_path = os.path.join(os.path.realpath(yatest.common.work_path()), "test_real_path_a")
        self.assertEqual(path.RealPath().GetPath(), real_work_path)
        self.assertEqual(path.RealLocation().GetPath(), real_work_path)
        with self.assertRaises(RuntimeError):
            path.ReadLink()

    def test_cwd(self):
        cdef TFsPath path = TFsPath.Cwd()
        self.assertEqual(path.GetPath(), os.path.realpath(yatest.common.work_path()))

    def test_swap(self):
        cdef TFsPath first = TFsPath("first")
        cdef TFsPath second = TFsPath("second")

        self.assertEqual(first.GetPath(), "first")
        self.assertEqual(second.GetPath(), "second")
        first.Swap(second)
        self.assertEqual(first.GetPath(), "second")
        self.assertEqual(second.GetPath(), "first")
        second.Swap(first)
        self.assertEqual(first.GetPath(), "first")
        self.assertEqual(second.GetPath(), "second")