Completed
Push — master ( 497b99...fbe69a )
by Bjorn
26s
created

nodoc()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 2
rs 10
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_rename():
252
    files = """
253
        a
254
    """
255
    with create_files(files) as _root:
256
        root = path.Path(_root)
257
        assert os.listdir(root) == ['a']
258
        (root / 'a').rename('b')
259
        assert os.listdir(root) == ['b']
260
261
262
def test_renames():
263
    files = """
264
    - foo:
265
        - a:
266
            - b
267
            - c
268
        - d:
269
            - empty
270
        - e:
271
            - f:
272
                - g: |
273
                    hello world
274
    """
275
    with create_files(files) as _root:
276
        root = path.Path(_root)
277
        (root / 'foo').renames('bar')
278
        newfiles = [f.relpath(root).replace('\\', '/')
279
                    for f in root.glob('**/*')]
280
        print(newfiles)
281
        assert 'bar/a/b' in newfiles
282
        assert 'bar/a/c' in newfiles
283
        assert 'bar/e/f/g' in newfiles
284
        assert 'bar/d' not in newfiles
285
286
287
def test_utime():
288
    files = """
289
        a
290
    """
291
    with create_files(files) as _root:
292
        root = path.Path(_root)
293
        t = time.time()
294
        _stat = root.utime()
295 View Code Duplication
        assert abs(_stat.st_atime - t) < 1
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
296
297
298
def test_chmod():
299
    files = """
300
        a
301
    """
302
    with create_files(files) as _root:
303
        root = path.Path(_root)
304
        (root / 'a').chmod(0o0400)  # only read for only current user
305
        # (root / 'a').chmod(stat.S_IREAD)
306
        if sys.platform == 'win32':
307
            # doesn't appear to be any way for a user to create a file that he
308
            # can't unlink on linux.
309
            with pytest.raises(OSError):
310
                (root / 'a').unlink()
311
        assert root.listdir() == ['a']
312
        (root / 'a').chmod(stat.S_IWRITE)
313 View Code Duplication
        (root / 'a').unlink()
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
314
        assert root.listdir() == []
315
316
317
def test_unlink():
318
    files = """
319
        - a
320
        - b
321
    """
322
    with create_files(files) as _root:
323
        root = path.Path(_root)
324
        assert {p.relpath(root) for p in root} == {'a', 'b'}
325
326
        b = root / 'b'
327
        b.unlink()
328
        assert [p.relpath(root) for p in root] == ['a']
329
330
        a = root / 'a'
331
        a.remove()
332
        assert [p.relpath(root) for p in root] == []
333
334
335
def test_rm():
336
    files = """
337
        - a
338
        - b
339
    """
340
    with create_files(files) as _root:
341
        root = path.Path(_root)
342
        assert {p.relpath(root) for p in root} == {'a', 'b'}
343
344
        b = root / 'b'
345
        b.rm()
346
        assert [p.relpath(root) for p in root] == ['a']
347
348
        root.rm('a')
349
        assert [p.relpath(root) for p in root] == []
350
351
        root.rm('c')  # shouldn't raise
352
        assert [p.relpath(root) for p in root] == []
353
354
355
def test_glob():
356
    files = """
357
        - a.py
358
        - b:
359
            - a.txt
360
            - aa.txt
361
        - d
362
        - e:
363
            - a:
364
                - b
365
        - f
366
    """
367
    with create_files(files) as _root:
368
        root = path.Path(_root)
369
        assert [p.relpath(root) for p in root.glob('**/*.py')] == ['a.py']
370
        assert [p.relpath(root) for p in root.glob('*.py')] == ['a.py']
371
        assert [p.relpath(root) for p in root.glob('b/a?.txt')] == [
372
            opjoin('b', 'aa.txt')
373
        ]
374
        assert [p.relpath(root) for p in root.glob('**/a.*')] == [
375
            'a.py', opjoin('b', 'a.txt')
376
        ]
377
378
379
def test_subdirs():
380
    files = """
381
        - a.py
382
        - b:
383
            - a.txt
384
            - aa.txt
385
        - d
386
        - e:
387
            - a:
388
                - b
389
        - f
390
    """
391
    with create_files(files) as _root:
392
        root = path.Path(_root)
393
        assert [d.relpath(root) for d in root.subdirs()] == ['b', 'e']
394
395
396
def test_files():
397
    files = """
398
        - a.py
399
        - b:
400
            - a.txt
401
            - aa.txt
402
        - d
403
        - e:
404
            - a:
405
                - b
406
        - f
407
    """
408
    with create_files(files) as _root:
409
        root = path.Path(_root)
410
        print("LISTDIR:", os.listdir('.'))
411
        assert {d.relpath(root) for d in root.files()} == {
412
            'a.py', 'd', 'f'
413
        }
414
415
416
def test_makedirs():
417
    files = """
418
        a:
419
            - b:
420
                - empty
421
    """
422
    with create_files(files) as _root:
423
        root = path.Path(_root)
424
        e = root.makedirs('a/b/c/d')
425
        # print "root", root
426
        # print 'E:', e
427
        assert e.isdir()
428
        assert e.relpath(root) == path.Path('a')/'b'/'c'/'d'
429
430
        e2 = root.makedirs('a/b/c/d')
431
        assert e2.isdir()
432
433
        b = root.mkdir('b')
434
        assert b.isdir()
435
436
437
def test_commonprefix():
438
    files = """
439
        - a.py
440
        - b:
441
            - a.txt
442
            - aa.txt
443
        - d
444
        - e:
445
            - a:
446
                - b
447
        - f
448
    """
449
    with create_files(files) as _root:
450
        r = path.Path(_root)
451
        assert r.commonprefix(r) == r
452
        assert r.commonprefix(r/'a.py', r/'d', r/'b'/'a.txt') == r
453
454
455
def test_abspath():
456
    assert os.path.normcase(os.path.abspath('empty')) == path.Path('empty').abspath()
457
458
459
def test_drive():
460
    assert os.path.splitdrive('empty')[0] == path.Path('empty').drive()
461
462
463
def test_drivepath():
464
    assert os.path.splitdrive('empty')[1] == path.Path('empty').drivepath()
465
466
467
def test_basename():
468
    assert os.path.basename('empty') == path.Path('empty').basename()
469
470
471
def test_dirname():
472
    assert os.path.dirname('empty') == path.Path('empty').dirname()
473
474
475
def test_exists():
476
    assert os.path.exists('empty') == path.Path('empty').exists()
477
478
479
def test_expanduser():
480
    assert os.path.expanduser('empty') == path.Path('empty').expanduser()
481
482
483
def test_expandvars():
484
    assert os.path.expandvars('empty') == path.Path('empty').expandvars()
485
486
487
def test_getatime():
488
    files = """
489
        b: hello
490
    """
491
    with create_files(files) as _root:
492
        root = path.Path(_root)
493
        assert os.path.getatime(root/'b') == (root/'b').getatime()
494
495
496
def test_getctime():
497
    files = """
498
        b: hello
499
    """
500
    with create_files(files) as _root:
501
        root = path.Path(_root)
502
        assert os.path.getctime(root/'b') == (root/'b').getctime()
503
504
505
def test_getmtime():
506
    files = """
507
        b: hello
508
    """
509
    with create_files(files) as _root:
510
        root = path.Path(_root)
511
        assert os.path.getmtime(root/'b') == (root/'b').getmtime()
512
513
514
def test_access():
515
    files = """
516
        b: hello
517
    """
518
    with create_files(files) as _root:
519
        b = path.Path(_root) / 'b'
520
        assert b.access(os.R_OK)
521
522
523
def test_getsize():
524
    assert os.path.getsize(__file__) == path.Path(__file__).getsize()
525
526
527
def test_isabs():
528
    assert os.path.isabs('empty') == path.Path('empty').isabs()
529
530
531
def test_isdir():
532
    assert os.path.isdir('empty') == path.Path('empty').isdir()
533
534
535
def test_isfile():
536
    assert os.path.isfile('empty') == path.Path('empty').isfile()
537
538
539
def test_islink():
540
    assert os.path.islink('empty') == path.Path('empty').islink()
541
542
543
def test_ismount():
544
    assert os.path.ismount('empty') == path.Path('empty').ismount()
545
546
547
def test_join():
548
    assert os.path.join('empty') == path.Path('empty').join()
549
550
551
def test_lexists():
552
    assert os.path.lexists('empty') == path.Path('empty').lexists()
553
554
555
def test_normcase():
556
    assert os.path.normcase('empty') == path.Path('empty').normcase()
557
558
559
def test_normpath():
560
    assert os.path.normpath('empty') == path.Path('empty').normpath()
561
562
563
def test_realpath():
564
    assert os.path.normcase(os.path.realpath('empty')) == path.Path('empty').realpath()
565
566
567
def test_relpath():
568
    assert os.path.relpath('empty') == path.Path('empty').relpath()
569
570
571
def test_split():
572
    assert os.path.split('empty') == path.Path('empty').split()
573
    assert 'string variable'.split() == path.Path('string variable').split()
574
575
576
def test_splitdrive():
577
    assert os.path.splitdrive('empty') == path.Path('empty').splitdrive()
578
579
580
def test_splitext():
581
    assert os.path.splitext('empty') == path.Path('empty').splitext()
582
583
584
def test_ext():
585
    assert path.Path('hello.world').ext == '.world'
586
587
588
def test_list():
589
    assert path.Path('.').list(lambda x: False) == []
590
591
592
def test_list2():
593
    files = """
594
      - a
595
      - b
596
    """
597
    with create_files(files) as d:
598
        assert len(path.Path(d).list()) == 2
599
        assert len(path.Path(d).list(lambda fname: fname[-1] > 'a')) == 1
600
601
602
def test_listdir():
603
    assert [os.path.normcase(p) for p in os.listdir('.')] == path.Path('.').listdir()
604
605
606
def test_lstat():
607
    assert os.lstat(__file__) == path.Path(__file__).lstat()
608
609
610
def test_stat():
611
    assert os.stat(__file__) == path.Path(__file__).stat()
612
613
614
def test_cd():
615
    files = """
616
        a:
617
          - b
618
    """
619
    with create_files(files) as _root:
620
        root = path.Path(_root)
621
        assert 'a' in os.listdir('.')
622
        with (root/'a').cd():
623
            assert 'b' in os.listdir('.')
624
        assert 'a' in os.listdir('.')
625
626
627
def test_cd_contextmanager():
628
    files = """
629
        a:
630
          - b
631
    """
632
    with create_files(files) as _root:
633
        # root = path.Path(_root)
634
        assert 'a' in os.listdir('.')
635
        with cd('a'):
636
            assert 'b' in os.listdir('.')
637
        assert 'a' in os.listdir('.')
638