Completed
Push — master ( b39d46...1d4b4f )
by Bjorn
01:05
created

test_touch_new()   B

Complexity

Conditions 7

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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