Completed
Push — master ( c3dae1...b09cff )
by Bjorn
01:05
created

test_parents()   B

Complexity

Conditions 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
dl 0
loc 20
rs 8.5454
c 1
b 0
f 1
1
# -*- coding: utf-8 -*-
2
import glob
3
import os
4
import stat
5
6
import time
7
8
import pytest
9
import sys
10
11
from dkfileutils import path
12
from yamldirs import create_files
13
14
opjoin = os.path.join
15
16
17
def _relcontent(root):
18
    return {p.relpath(root) for p in root}
19
20
21
def test_open():
22
    files = """
23
        b: hello
24
    """
25
    with create_files(files) as root:
26
        assert (path.Path(root) / 'b').open().read() == 'hello'
27
28
29
def test_iter():
30
    files = """
31
        - .dotfile
32
        - .dotdir:
33
            - d
34
            - e
35
        - a
36
        - b
37
        - c:
38
            - d
39
    """
40
    with create_files(files) as _root:
41
        root = path.Path(_root)
42
        assert _relcontent(root) == {'a', 'b', opjoin('c', 'd')}
43
44
45
def test_contains():
46
    files = """
47
        - a
48
        - b
49
    """
50
    with create_files(files) as _root:
51
        root = path.Path(_root)
52
        assert 'a' in root
53
        assert 'b' in root
54
        assert 'c' not in root
55
56
57
def test_rmtree():
58
    files = """
59
        a:
60
            b:
61
                c:
62
                    d.txt: hello world
63
        e:
64
            f.txt: thanks for all the fish
65
    """
66
    with create_files(files) as _root:
67
        root = path.Path(_root)
68
        print "FILES:", root.contents()
69
        # print "LISTALL:", root.listall()
70
        (root / 'a').rmtree('b')
71
        assert root.contents() == [path.Path('e') / 'f.txt']
72
        (root / 'e').rmtree()
73
        assert root.contents() == []
74
75
76
def test_parents():
77
    files = """
78
        a:
79
            b:
80
                c:
81
                    d.txt: hello world
82
    """
83
    with create_files(files) as _root:
84
        root = path.Path(_root)
85
        d = root / 'a' / 'b' / 'c' / 'd.txt'
86
        assert d.open().read() == "hello world"
87
        print "PARTS:", d.parts()
88
        print "PARENTS:", d.parents
89
        assert d.parents == [
90
            root / 'a' / 'b' / 'c',
91
            root / 'a' / 'b',
92
            root / 'a',
93
            root
94
        ] + root.parents
95
        assert d.parent == root / 'a' / 'b' / 'c'
96
97
98
def test_dirops():
99
    files = """
100
        - a:
101
            - b
102
            - c
103
        - d: []
104
        - e:
105
            - f:
106
                - g: []
107
    """
108
    with create_files(files) as directory:
109
        p = path.Path(directory)
110
        (p / 'a').chdir()
111
        assert set(os.listdir(p / 'a')) == {'b', 'c'}
112
113
        (p / 'd').rmdir()
114
        assert set(os.listdir(p)) == {'a', 'e'}
115
116
        (p / 'e' / 'f' / 'g').removedirs()
117
        assert set(os.listdir(p)) == {'a'}
118
119
120
def test_rename():
121
    files = """
122
        a
123
    """
124
    with create_files(files) as _root:
125
        root = path.Path(_root)
126
        assert os.listdir(root) == ['a']
127
        (root / 'a').rename('b')
128
        assert os.listdir(root) == ['b']
129
130
131
def test_renames():
132
    files = """
133
    - foo:
134
        - a:
135
            - b
136
            - c
137
        - d:
138
            - empty
139
        - e:
140
            - f:
141
                - g: |
142
                    hello world
143
    """
144
    with create_files(files) as _root:
145
        root = path.Path(_root)
146
        (root / 'foo').renames('bar')
147
        newfiles = [f.relpath(root).replace('\\', '/') for f in root.glob('**/*')]
148
        print newfiles
149
        assert 'bar/a/b' in newfiles
150
        assert 'bar/a/c' in newfiles
151
        assert 'bar/e/f/g' in newfiles
152
        assert 'bar/d' not in newfiles
153
154
155
def test_utime():
156
    files = """
157
        a
158
    """
159
    with create_files(files) as _root:
160
        root = path.Path(_root)
161
        t = time.time()
162
        stat = root.utime()
163
        assert abs(stat.st_atime - t) < 1
164
165
166
def test_chmod():
167
    files = """
168
        a
169
    """
170
    with create_files(files) as _root:
171
        root = path.Path(_root)
172
        (root / 'a').chmod(00400)  # only read for only current user
173
        # (root / 'a').chmod(stat.S_IREAD)
174
        if sys.platform == 'win32':
175
            # doesn't appear to be any way for a user to create a file that he
176
            # can't unlink on linux.
177
            with pytest.raises(OSError):
178
                (root / 'a').unlink()
179
        assert root.listdir() == ['a']
180
        (root / 'a').chmod(stat.S_IWRITE)
181
        (root / 'a').unlink()
182
        assert root.listdir() == []
183
184
185
def test_unlink():
186
    files = """
187
        - a
188
        - b
189
    """
190
    with create_files(files) as _root:
191
        root = path.Path(_root)
192
        assert {p.relpath(root) for p in root} == {'a', 'b'}
193
194
        b = root / 'b'
195
        b.unlink()
196
        assert [p.relpath(root) for p in root] == ['a']
197
198
        a = root / 'a'
199
        a.remove()
200
        assert [p.relpath(root) for p in root] == []
201
202
203
def test_glob():
204
    files = """
205
        - a.py
206
        - b:
207
            - a.txt
208
            - aa.txt
209
        - d
210
        - e:
211
            - a:
212
                - b
213
        - f
214
    """
215
    with create_files(files) as _root:
216
        root = path.Path(_root)
217
        assert [p.relpath(root) for p in root.glob('**/*.py')] == ['a.py']
218
        assert [p.relpath(root) for p in root.glob('*.py')] == ['a.py']
219
        assert [p.relpath(root) for p in root.glob('b/a?.txt')] == [
220
            opjoin('b', 'aa.txt')
221
        ]
222
        assert [p.relpath(root) for p in root.glob('**/a.*')] == [
223
            'a.py', opjoin('b', 'a.txt')
224
        ]
225
226
227
def test_subdirs():
228
    files = """
229
        - a.py
230
        - b:
231
            - a.txt
232
            - aa.txt
233
        - d
234
        - e:
235
            - a:
236
                - b
237
        - f
238
    """
239
    with create_files(files) as _root:
240
        root = path.Path(_root)
241
        assert [d.relpath(root) for d in root.subdirs()] == ['b', 'e']
242
243
244
def test_files():
245
    files = """
246
        - a.py
247
        - b:
248
            - a.txt
249
            - aa.txt
250
        - d
251
        - e:
252
            - a:
253
                - b
254
        - f
255
    """
256
    with create_files(files) as _root:
257
        root = path.Path(_root)
258
        print "LISTDIR:", os.listdir('.')
259
        assert {d.relpath(root) for d in root.files()} == {
260
            'a.py', 'd', 'f'
261
        }
262
263
264
def test_makedirs():
265
    files = """
266
        a:
267
            - b:
268
                - empty
269
    """
270
    with create_files(files) as _root:
271
        root = path.Path(_root)
272
        e = root.makedirs('a/b/c/d')
273
        # print "root", root
274
        # print 'E:', e
275
        assert e.isdir()
276
        assert e.relpath(root) == path.Path('a')/'b'/'c'/'d'
277
278
        e2 = root.makedirs('a/b/c/d')
279
        assert e2.isdir()
280
281
        b = root.mkdir('b')
282
        assert b.isdir()
283
284
285
def test_commonprefix():
286
    files = """
287
        - a.py
288
        - b:
289
            - a.txt
290
            - aa.txt
291
        - d
292
        - e:
293
            - a:
294
                - b
295
        - f
296
    """
297
    with create_files(files) as _root:
298
        root = path.Path(_root)
299
        assert root.commonprefix(root) == root
300
        assert root.commonprefix(root/'a.py', root/'d', root/'b'/'a.txt') == root
301
302
303
def test_abspath():
304
    assert os.path.abspath('empty') == path.Path('empty').abspath()
305
306
307
def test_drive():
308
    assert os.path.splitdrive('empty')[0] == path.Path('empty').drive()
309
310
311
def test_drivepath():
312
    assert os.path.splitdrive('empty')[1] == path.Path('empty').drivepath()
313
314
315
def test_basename():
316
    assert os.path.basename('empty') == path.Path('empty').basename()
317
318
319
# def test_commonprefix():
320
#     assert os.path.commonprefix('empty', 'empty') == path.Path('empty').commonprefix('empty')
321
322
323
def test_dirname():
324
    assert os.path.dirname('empty') == path.Path('empty').dirname()
325
326
327
def test_exists():
328
    assert os.path.exists('empty') == path.Path('empty').exists()
329
330
331
def test_expanduser():
332
    assert os.path.expanduser('empty') == path.Path('empty').expanduser()
333
334
335
def test_expandvars():
336
    assert os.path.expandvars('empty') == path.Path('empty').expandvars()
337
338
339
def test_getatime():
340
    files = """
341
        b: hello
342
    """
343
    with create_files(files) as _root:
344
        root = path.Path(_root)
345
        assert os.path.getatime(root/'b') == (root/'b').getatime()
346
347
348
def test_getctime():
349
    files = """
350
        b: hello
351
    """
352
    with create_files(files) as _root:
353
        root = path.Path(_root)
354
        assert os.path.getctime(root/'b') == (root/'b').getctime()
355
356
357
def test_getmtime():
358
    files = """
359
        b: hello
360
    """
361
    with create_files(files) as _root:
362
        root = path.Path(_root)
363
        assert os.path.getmtime(root/'b') == (root/'b').getmtime()
364
365
366
def test_access():
367
    files = """
368
        b: hello
369
    """
370
    with create_files(files) as _root:
371
        b = path.Path(_root) / 'b'
372
        assert b.access(os.R_OK)
373
374
375
def test_getsize():
376
    assert os.path.getsize(__file__) == path.Path(__file__).getsize()
377
378
379
def test_isabs():
380
    assert os.path.isabs('empty') == path.Path('empty').isabs()
381
382
383
def test_isdir():
384
    assert os.path.isdir('empty') == path.Path('empty').isdir()
385
386
387
def test_isfile():
388
    assert os.path.isfile('empty') == path.Path('empty').isfile()
389
390
391
def test_islink():
392
    assert os.path.islink('empty') == path.Path('empty').islink()
393
394
395
def test_ismount():
396
    assert os.path.ismount('empty') == path.Path('empty').ismount()
397
398
399
def test_join():
400
    assert os.path.join('empty') == path.Path('empty').join()
401
402
403
def test_lexists():
404
    assert os.path.lexists('empty') == path.Path('empty').lexists()
405
406
407
def test_normcase():
408
    assert os.path.normcase('empty') == path.Path('empty').normcase()
409
410
411
def test_normpath():
412
    assert os.path.normpath('empty') == path.Path('empty').normpath()
413
414
415
def test_realpath():
416
    assert os.path.realpath('empty') == path.Path('empty').realpath()
417
418
419
def test_relpath():
420
    assert os.path.relpath('empty') == path.Path('empty').relpath()
421
422
423
def test_split():
424
    assert os.path.split('empty') == path.Path('empty').split()
425
    assert 'string variable'.split() == path.Path('string variable').split()
426
427
428
def test_splitdrive():
429
    assert os.path.splitdrive('empty') == path.Path('empty').splitdrive()
430
431
432
def test_splitext():
433
    assert os.path.splitext('empty') == path.Path('empty').splitext()
434
435
436
def test_ext():
437
    assert path.Path('hello.world').ext == '.world'
438
439
440
def test_listdir():
441
    assert os.listdir('.') == path.Path('.').listdir()
442
443
444
def test_lstat():
445
    assert os.lstat(__file__) == path.Path(__file__).lstat()
446
447
448
def test_stat():
449
    assert os.stat(__file__) == path.Path(__file__).stat()
450
451
452
def test_cd_contextmanager():
453
    files = """
454
        a:
455
          - b
456
    """
457
    with create_files(files) as _root:
458
        root = path.Path(_root)
459
        assert 'a' in os.listdir('.')
460
        with (root/'a').cd():
461
            assert 'b' in os.listdir('.')
462
        assert 'a' in os.listdir('.')
463
464
465
# def test_utime():
466
#     assert os.utime('empty') == path.Path('empty').utime()
467