Issues (2)

tests/test_path.py (2 issues)

1
# -*- coding: utf-8 -*-
2
from __future__ import print_function
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, doc
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_doc_decorator():
24
    def hasdoc():
25
        "hasdoc-docstring"
26
        pass
27
28
    def nodoc():
29
        pass
30
31
    @doc(hasdoc)
32
    def t1():
33
        "t1doc"
34
        pass
35
36
    @doc(nodoc)
37
    def t2():
38
        "t2doc"
39
40
    assert hasdoc() == nodoc() == t1() == t2()
41
    assert t1.__doc__ == "t1-docstring"
42
    assert t2.__doc__ is None
43
44
45
def test_open():
46
    files = """
47
        b: hello
48
    """
49
    with create_files(files) as root:
50
        assert (path.Path(root) / 'b').open().read() == 'hello'
51
52
53
def test_read():
54
    files = """
55
        b: hello
56
    """
57
    with create_files(files) as root:
58
        assert (path.Path(root) / 'b').read() == 'hello'
59
60
61
def test_write():
62
    files = """
63
        b: hello
64
    """
65
    with create_files(files) as root:
66
        r = path.Path(root)
67
        bfile = r / 'b'
68
        bfile.write('goodbye')
69
        assert bfile.read() == 'goodbye'
70
        cfile = r / 'c'
71
        cfile.write('world')
72
        assert cfile.read() == 'world'
73
74
75
def test_append():
76
    files = """
77
        b: hello
78
    """
79
    with create_files(files) as root:
80
        r = path.Path(root)
81
        bfile = r / 'b'
82
        bfile.append(' world')
83
        assert bfile.read() == 'hello world'
84
85
86
def test_iter():
87
    files = """
88
        - .dotfile
89
        - .dotdir:
90
            - d
91
            - e
92
        - a
93
        - b
94
        - c:
95
            - d
96
    """
97
    with create_files(files) as _root:
98
        root = path.Path(_root)
99
        assert _relcontent(root) == {'a', 'b', opjoin('c', 'd')}
100
101
102
def test_contains():
103
    files = """
104
        - a
105
        - b
106
    """
107
    with create_files(files) as _root:
108
        root = path.Path(_root)
109
        assert 'a' in root
110
        assert 'b' in root
111
        assert 'c' not in root
112
113
114
def test_rmtree():
115
    files = """
116
        a:
117
            b:
118
                c:
119
                    d.txt: hello world
120
        e:
121
            f.txt: thanks for all the fish
122
    """
123
    with create_files(files) as _root:
124
        root = path.Path(_root)
125
        print("FILES:", root.contents())
126
        # print "LISTALL:", root.listall()
127
        (root / 'a').rmtree('b')
128
        assert root.contents() == [path.Path('e') / 'f.txt']
129
        (root / 'e').rmtree()
130
        assert root.contents() == []
131
132
133
def test_curdir():
134
    assert path.Path.curdir() == os.path.normcase(os.getcwd())
135
136
137
def test_touch_existing():
138
    # needs enough files that it takes a perceivable amount of time to create
139
    # them
140
    files = """
141
        a: hello
142
        b: beautiful
143
        c: world
144
        d:
145
            a: hello
146
            b: beautiful
147
            c: world
148
            d:
149
                a: hello
150
                b: beautiful
151
                c: world
152
                d:
153
                    a: hello
154
                    b: beautiful
155
                    c: world
156
                    d:
157
                        a: hello
158
                        b: beautiful
159
                        c: world
160
    """
161
    before = time.time()
162
    with create_files(files) as _root:
163
        after = time.time()
164
        assert before < after
165
        print('before/after', before, after, after - before)
166
        root = path.Path(_root)
167
        print("FILES:", root.contents())
168
        assert 'a' in root
169
        a = root / 'a'
170
        a_before_touch = a.getmtime()
171
        # assert before <= a_before_touch <= after
172
        a.touch()
173
        after_touch = time.time()
174
        a_after_touch = a.getmtime()
175
        print("LOCALS:", locals())
176
        assert a_before_touch <= a_after_touch
177
        # assert a_after_touch > after
178
        # assert a_after_touch <= after_touch
179
180
181
def test_touch_not_exist():
182
    files = """
183
        a: hello
184
    """
185
    with create_files(files) as _root:
186
        root = path.Path(_root)
187
        a = root / 'a'
188
        with pytest.raises(OSError):
189
            a.touch(exist_ok=False)
190
191
192
def test_touch_new():
193
    files = """
194
        a: hello
195
    """
196
    with create_files(files) as _root:
197
        root = path.Path(_root)
198
        assert 'a' in root
199
        assert 'b' not in root
200
        b = root / 'b'
201
        assert not b.exists()
202
        b.touch()
203
        assert b.exists()
204
        assert 'b' in root
205
206
207
def test_parents():
208
    files = """
209
        a:
210
            b:
211
                c:
212
                    d.txt: hello world
213
    """
214
    with create_files(files) as _root:
215
        root = path.Path(_root).abspath()
216
        d = root / 'a' / 'b' / 'c' / 'd.txt'
217
        assert d.open().read() == "hello world"
218
        print("PARTS:", d.parts())
219
        print("PARENTS:", d.parents)
220
        assert d.parents == [
221
            root / 'a' / 'b' / 'c',
222
            root / 'a' / 'b',
223
            root / 'a',
224
            root
225
        ] + root.parents
226
        assert d.parent == root / 'a' / 'b' / 'c'
227
228
229
def test_dirops():
230
    files = """
231
        - a:
232
            - b
233
            - c
234
        - d: []
235
        - e:
236
            - f:
237
                - g: []
238
    """
239
    with create_files(files) as directory:
240
        p = path.Path(directory)
241
        (p / 'a').chdir()
242
        assert set(os.listdir(p / 'a')) == {'b', 'c'}
243
244
        (p / 'd').rmdir()
245
        assert set(os.listdir(p)) == {'a', 'e'}
246
247
        (p / 'e' / 'f' / 'g').removedirs()
248
        assert set(os.listdir(p)) == {'a'}
249
250
251
def test_move():
252
    files = """
253
       - a:
254
       - b:
255
         - c: []
256
    """
257
    with create_files(files) as _root:
258
        root = path.Path(_root)
259
        assert os.listdir(root) == ['a', 'b']
260
        assert os.listdir(os.path.join(_root, 'b', 'c')) == []
261
        a = root / 'a'
262
        a.move('b/c')
263
        assert os.listdir(root) == ['b']
264
        assert os.listdir(os.path.join(_root, 'b', 'c')) == ['a']
265
266
267
def test_rename():
268
    files = """
269
        a
270
    """
271
    with create_files(files) as _root:
272
        root = path.Path(_root)
273
        assert os.listdir(root) == ['a']
274
        (root / 'a').rename('b')
275
        assert os.listdir(root) == ['b']
276
277
278
def test_renames():
279
    files = """
280
    - foo:
281
        - a:
282
            - b
283
            - c
284
        - d:
285
            - empty
286
        - e:
287
            - f:
288
                - g: |
289
                    hello world
290
    """
291
    with create_files(files) as _root:
292
        root = path.Path(_root)
293
        (root / 'foo').renames('bar')
294
        newfiles = [f.relpath(root).replace('\\', '/')
295 View Code Duplication
                    for f in root.glob('**/*')]
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
296
        print(newfiles)
297
        assert 'bar/a/b' in newfiles
298
        assert 'bar/a/c' in newfiles
299
        assert 'bar/e/f/g' in newfiles
300
        assert 'bar/d' not in newfiles
301
302
303
def test_utime():
304
    files = """
305
        a
306
    """
307
    with create_files(files) as _root:
308
        root = path.Path(_root)
309
        t = time.time()
310
        _stat = root.utime()
311
        assert abs(_stat.st_atime - t) < 1
312
313 View Code Duplication
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
314
def test_chmod():
315
    files = """
316
        a
317
    """
318
    with create_files(files) as _root:
319
        root = path.Path(_root)
320
        (root / 'a').chmod(0o0400)  # only read for only current user
321
        # (root / 'a').chmod(stat.S_IREAD)
322
        if sys.platform == 'win32':
323
            # doesn't appear to be any way for a user to create a file that he
324
            # can't unlink on linux.
325
            with pytest.raises(OSError):
326
                (root / 'a').unlink()
327
        assert root.listdir() == ['a']
328
        (root / 'a').chmod(stat.S_IWRITE)
329
        (root / 'a').unlink()
330
        assert root.listdir() == []
331
332
333
def test_unlink():
334
    files = """
335
        - a
336
        - b
337
    """
338
    with create_files(files) as _root:
339
        root = path.Path(_root)
340
        assert {p.relpath(root) for p in root} == {'a', 'b'}
341
342
        b = root / 'b'
343
        b.unlink()
344
        assert [p.relpath(root) for p in root] == ['a']
345
346
        a = root / 'a'
347
        a.remove()
348
        assert [p.relpath(root) for p in root] == []
349
350
351
def test_rm():
352
    files = """
353
        - a
354
        - b
355
    """
356
    with create_files(files) as _root:
357
        root = path.Path(_root)
358
        assert {p.relpath(root) for p in root} == {'a', 'b'}
359
360
        b = root / 'b'
361
        b.rm()
362
        assert [p.relpath(root) for p in root] == ['a']
363
364
        root.rm('a')
365
        assert [p.relpath(root) for p in root] == []
366
367
        root.rm('c')  # shouldn't raise
368
        assert [p.relpath(root) for p in root] == []
369
370
371
def test_glob():
372
    files = """
373
        - a.py
374
        - b:
375
            - a.txt
376
            - aa.txt
377
        - d
378
        - e:
379
            - a:
380
                - b
381
        - f
382
    """
383
    with create_files(files) as _root:
384
        root = path.Path(_root)
385
        assert [p.relpath(root) for p in root.glob('**/*.py')] == ['a.py']
386
        assert [p.relpath(root) for p in root.glob('*.py')] == ['a.py']
387
        assert [p.relpath(root) for p in root.glob('b/a?.txt')] == [
388
            opjoin('b', 'aa.txt')
389
        ]
390
        assert [p.relpath(root) for p in root.glob('**/a.*')] == [
391
            'a.py', opjoin('b', 'a.txt')
392
        ]
393
394
395
def test_subdirs():
396
    files = """
397
        - a.py
398
        - b:
399
            - a.txt
400
            - aa.txt
401
        - d
402
        - e:
403
            - a:
404
                - b
405
        - f
406
    """
407
    with create_files(files) as _root:
408
        root = path.Path(_root)
409
        assert [d.relpath(root) for d in root.subdirs()] == ['b', 'e']
410
411
412
def test_files():
413
    files = """
414
        - a.py
415
        - b:
416
            - a.txt
417
            - aa.txt
418
        - d
419
        - e:
420
            - a:
421
                - b
422
        - f
423
    """
424
    with create_files(files) as _root:
425
        root = path.Path(_root)
426
        print("LISTDIR:", os.listdir('.'))
427
        assert {d.relpath(root) for d in root.files()} == {
428
            'a.py', 'd', 'f'
429
        }
430
431
432
def test_makedirs():
433
    files = """
434
        a:
435
            - b:
436
                - empty
437
    """
438
    with create_files(files) as _root:
439
        root = path.Path(_root)
440
        e = root.makedirs('a/b/c/d')
441
        # print "root", root
442
        # print 'E:', e
443
        assert e.isdir()
444
        assert e.relpath(root) == path.Path('a')/'b'/'c'/'d'
445
446
        e2 = root.makedirs('a/b/c/d')
447
        assert e2.isdir()
448
449
        b = root.mkdir('b')
450
        assert b.isdir()
451
452
453
def test_commonprefix():
454
    files = """
455
        - a.py
456
        - b:
457
            - a.txt
458
            - aa.txt
459
        - d
460
        - e:
461
            - a:
462
                - b
463
        - f
464
    """
465
    with create_files(files) as _root:
466
        r = path.Path(_root)
467
        assert r.commonprefix(r) == r
468
        assert r.commonprefix(r/'a.py', r/'d', r/'b'/'a.txt') == r
469
470
471
def test_abspath():
472
    assert os.path.normcase(os.path.abspath('empty')) == path.Path('empty').abspath()
473
474
475
def test_drive():
476
    assert os.path.splitdrive('empty')[0] == path.Path('empty').drive()
477
478
479
def test_drivepath():
480
    assert os.path.splitdrive('empty')[1] == path.Path('empty').drivepath()
481
482
483
def test_basename():
484
    assert os.path.basename('empty') == path.Path('empty').basename()
485
486
487
def test_dirname():
488
    assert os.path.dirname('empty') == path.Path('empty').dirname()
489
490
491
def test_exists():
492
    assert os.path.exists('empty') == path.Path('empty').exists()
493
494
495
def test_expanduser():
496
    assert os.path.expanduser('empty') == path.Path('empty').expanduser()
497
498
499
def test_expandvars():
500
    assert os.path.expandvars('empty') == path.Path('empty').expandvars()
501
502
503
def test_getatime():
504
    files = """
505
        b: hello
506
    """
507
    with create_files(files) as _root:
508
        root = path.Path(_root)
509
        assert os.path.getatime(root/'b') == (root/'b').getatime()
510
511
512
def test_getctime():
513
    files = """
514
        b: hello
515
    """
516
    with create_files(files) as _root:
517
        root = path.Path(_root)
518
        assert os.path.getctime(root/'b') == (root/'b').getctime()
519
520
521
def test_getmtime():
522
    files = """
523
        b: hello
524
    """
525
    with create_files(files) as _root:
526
        root = path.Path(_root)
527
        assert os.path.getmtime(root/'b') == (root/'b').getmtime()
528
529
530
def test_access():
531
    files = """
532
        b: hello
533
    """
534
    with create_files(files) as _root:
535
        b = path.Path(_root) / 'b'
536
        assert b.access(os.R_OK)
537
538
539
def test_getsize():
540
    assert os.path.getsize(__file__) == path.Path(__file__).getsize()
541
542
543
def test_isabs():
544
    assert os.path.isabs('empty') == path.Path('empty').isabs()
545
546
547
def test_isdir():
548
    assert os.path.isdir('empty') == path.Path('empty').isdir()
549
550
551
def test_isfile():
552
    assert os.path.isfile('empty') == path.Path('empty').isfile()
553
554
555
def test_islink():
556
    assert os.path.islink('empty') == path.Path('empty').islink()
557
558
559
def test_ismount():
560
    assert os.path.ismount('empty') == path.Path('empty').ismount()
561
562
563
def test_join():
564
    assert os.path.join('empty') == path.Path('empty').join()
565
566
567
def test_lexists():
568
    assert os.path.lexists('empty') == path.Path('empty').lexists()
569
570
571
def test_normcase():
572
    assert os.path.normcase('empty') == path.Path('empty').normcase()
573
574
575
def test_normpath():
576
    assert os.path.normpath('empty') == path.Path('empty').normpath()
577
578
579
def test_realpath():
580
    assert os.path.normcase(os.path.realpath('empty')) == path.Path('empty').realpath()
581
582
583
def test_relpath():
584
    assert os.path.relpath('empty') == path.Path('empty').relpath()
585
586
587
def test_split():
588
    assert os.path.split('empty') == path.Path('empty').split()
589
    assert 'string variable'.split() == path.Path('string variable').split()
590
591
592
def test_splitdrive():
593
    assert os.path.splitdrive('empty') == path.Path('empty').splitdrive()
594
595
596
def test_splitext():
597
    assert os.path.splitext('empty') == path.Path('empty').splitext()
598
599
600
def test_ext():
601
    assert path.Path('hello.world').ext == '.world'
602
603
604
def test_list():
605
    assert path.Path('.').list(lambda x: False) == []
606
607
608
def test_list2():
609
    files = """
610
      - a
611
      - b
612
    """
613
    with create_files(files) as d:
614
        assert len(path.Path(d).list()) == 2
615
        assert len(path.Path(d).list(lambda fname: fname[-1] > 'a')) == 1
616
617
618
def test_listdir():
619
    assert [os.path.normcase(p) for p in os.listdir('.')] == path.Path('.').listdir()
620
621
622
def test_lstat():
623
    assert os.lstat(__file__) == path.Path(__file__).lstat()
624
625
626
def test_stat():
627
    assert os.stat(__file__) == path.Path(__file__).stat()
628
629
630
def test_cd():
631
    files = """
632
        a:
633
          - b
634
    """
635
    with create_files(files) as _root:
636
        root = path.Path(_root)
637
        assert 'a' in os.listdir('.')
638
        with (root/'a').cd():
639
            assert 'b' in os.listdir('.')
640
        assert 'a' in os.listdir('.')
641
642
643
def test_cd_contextmanager():
644
    files = """
645
        a:
646
          - b
647
    """
648
    with create_files(files) as _root:
649
        # root = path.Path(_root)
650
        assert 'a' in os.listdir('.')
651
        with cd('a'):
652
            assert 'b' in os.listdir('.')
653
        assert 'a' in os.listdir('.')
654