Completed
Push — master ( ff259f...fab5c0 )
by Bjorn
01:06
created

test_curdir()   A

Complexity

Conditions 2

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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