Completed
Push — master ( 1d4b4f...bdf242 )
by Bjorn
01:04
created

test_touch_existing()   C

Complexity

Conditions 7

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

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