Completed
Push — master ( 574cdc...f85b61 )
by Bjorn
01:15
created

test_rm()   C

Complexity

Conditions 10

Size

Total Lines 18

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
cc 10
c 0
b 0
f 0
dl 18
loc 18
rs 6

How to fix   Complexity   

Complexity

Complex classes like test_rm() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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