Completed
Push — master ( 2fcab0...ed35bb )
by Bjorn
01:54
created

tests.test_dirops()   B

Complexity

Conditions 5

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 5
dl 0
loc 22
rs 8.3411
1
# -*- coding: utf-8 -*-
2
import glob
3
import os
4
import stat
5
6
import time
7
8
import pytest
9
10
from dkfileutils import path
11
from yamldirs import create_files
12
13
opjoin = os.path.join
14
15
16
def _relcontent(root):
17
    return [p.relpath(root) for p in root]
18
19
20
def test_open():
21
    files = """
22
        b: hello
23
    """
24
    with create_files(files) as root:
25
        assert (path.Path(root) / 'b').open().read() == 'hello'
26
27
28
def test_iter():
29
    files = """
30
        - .dotfile
31
        - .dotdir:
32
            - d
33
            - e
34
        - a
35
        - b
36
        - c:
37
            - d
38
    """
39
    with create_files(files) as _root:
40
        root = path.Path(_root)
41
        assert _relcontent(root) == ['a', 'b', opjoin('c', 'd')]
42
43
44
def test_contains():
45
    files = """
46
        - a
47
        - b
48
    """
49
    with create_files(files) as _root:
50
        root = path.Path(_root)
51
        assert 'a' in root
52
        assert 'b' in root
53
        assert 'c' not in root
54
55
56
def test_dirops():
57
    files = """
58
        - a:
59
            - b
60
            - c
61
        - d:
62
            - empty
63
        - e:
64
            - f:
65
                - g:
66
                    - empty
67
    """
68
    with create_files(files) as directory:
69
        p = path.Path(directory)
70
        (p / 'a').chdir()
71
        assert set(os.listdir(p / 'a')) == {'b', 'c'}
72
73
        (p / 'd').rmdir()
74
        assert set(os.listdir(p)) == {'a', 'e'}
75
76
        (p / 'e' / 'f' / 'g').removedirs()
77
        assert set(os.listdir(p)) == {'a'}
78
79
80
def test_rename():
81
    files = """
82
        a
83
    """
84
    with create_files(files) as _root:
85
        root = path.Path(_root)
86
        assert os.listdir(root) == ['a']
87
        (root / 'a').rename('b')
88
        assert os.listdir(root) == ['b']
89
90
91
def test_renames():
92
    files = """
93
    - foo:
94
        - a:
95
            - b
96
            - c
97
        - d:
98
            - empty
99
        - e:
100
            - f:
101
                - g: |
102
                    hello world
103
    """
104
    with create_files(files) as _root:
105
        root = path.Path(_root)
106
        (root / 'foo').renames('bar')
107
        newfiles = [f.relpath(root).replace('\\', '/') for f in root.glob('**/*')]
108
        print newfiles
109
        assert 'bar/a/b' in newfiles
110
        assert 'bar/a/c' in newfiles
111
        assert 'bar/e/f/g' in newfiles
112
        assert 'bar/d' not in newfiles
113
114
115
def test_utime():
116
    files = """
117
        a
118
    """
119
    with create_files(files) as _root:
120
        root = path.Path(_root)
121
        t = time.time()
122
        stat = root.utime()
123
        assert abs(stat.st_atime - t) < 1
124
125
126
def test_chmod():
127
    files = """
128
        a
129
    """
130
    with create_files(files) as _root:
131
        root = path.Path(_root)
132
        (root / 'a').chmod(stat.S_IREAD)
133
        with pytest.raises(OSError):
134
            (root / 'a').unlink()
135
        assert root.listdir() == ['a']
136
        (root / 'a').chmod(stat.S_IWRITE)
137
        (root / 'a').unlink()
138
        assert root.listdir() == []
139
140
141
def test_unlink():
142
    files = """
143
        - a
144
        - b
145
    """
146
    with create_files(files) as _root:
147
        root = path.Path(_root)
148
        assert [p.relpath(root) for p in root] == ['a', 'b']
149
150
        b = root / 'b'
151
        b.unlink()
152
        assert [p.relpath(root) for p in root] == ['a']
153
154
        a = root / 'a'
155
        a.remove()
156
        assert [p.relpath(root) for p in root] == []
157
158
159
def test_glob():
160
    files = """
161
        - a.py
162
        - b:
163
            - a.txt
164
            - aa.txt
165
        - d
166
        - e:
167
            - a:
168
                - b
169
        - f
170
    """
171
    with create_files(files) as _root:
172
        root = path.Path(_root)
173
        assert [p.relpath(root) for p in root.glob('**/*.py')] == ['a.py']
174
        assert [p.relpath(root) for p in root.glob('*.py')] == ['a.py']
175
        assert [p.relpath(root) for p in root.glob('b/a?.txt')] == [
176
            opjoin('b', 'aa.txt')
177
        ]
178
        assert [p.relpath(root) for p in root.glob('**/a.*')] == [
179
            'a.py', opjoin('b', 'a.txt')
180
        ]
181
182
183
def test_subdirs():
184
    files = """
185
        - a.py
186
        - b:
187
            - a.txt
188
            - aa.txt
189
        - d
190
        - e:
191
            - a:
192
                - b
193
        - f
194
    """
195
    with create_files(files) as _root:
196
        root = path.Path(_root)
197
        assert [d.relpath(root) for d in root.subdirs()] == ['b', 'e']
198
199
200
def test_files():
201
    files = """
202
        - a.py
203
        - b:
204
            - a.txt
205
            - aa.txt
206
        - d
207
        - e:
208
            - a:
209
                - b
210
        - f
211
    """
212
    with create_files(files) as _root:
213
        root = path.Path(_root)
214
        assert [d.relpath(root) for d in root.files()] == ['a.py', 'd', 'f']
215
216
217
def test_makedirs():
218
    files = """
219
        a:
220
            - b:
221
                - empty
222
    """
223
    with create_files(files) as _root:
224
        root = path.Path(_root)
225
        e = root.makedirs('a/b/c/d')
226
        # print "root", root
227
        # print 'E:', e
228
        assert e.isdir()
229
        assert e.relpath(root) == path.Path('a')/'b'/'c'/'d'
230
231
        e2 = root.makedirs('a/b/c/d')
232
        assert e2.isdir()
233
234
        b = root.mkdir('b')
235
        assert b.isdir()
236
237
238
def test_commonprefix():
239
    files = """
240
        - a.py
241
        - b:
242
            - a.txt
243
            - aa.txt
244
        - d
245
        - e:
246
            - a:
247
                - b
248
        - f
249
    """
250
    with create_files(files) as _root:
251
        root = path.Path(_root)
252
        assert root.commonprefix(root) == root
253
        assert root.commonprefix(root/'a.py', root/'d', root/'b'/'a.txt') == root
254
255
256
def test_abspath():
257
    assert os.path.abspath('empty') == path.Path('empty').abspath()
258
259
260
def test_drive():
261
    assert os.path.splitdrive('empty')[0] == path.Path('empty').drive()
262
263
264
def test_drivepath():
265
    assert os.path.splitdrive('empty')[1] == path.Path('empty').drivepath()
266
267
268
def test_basename():
269
    assert os.path.basename('empty') == path.Path('empty').basename()
270
271
272
# def test_commonprefix():
273
#     assert os.path.commonprefix('empty', 'empty') == path.Path('empty').commonprefix('empty')
274
275
276
def test_dirname():
277
    assert os.path.dirname('empty') == path.Path('empty').dirname()
278
279
280
def test_exists():
281
    assert os.path.exists('empty') == path.Path('empty').exists()
282
283
284
def test_expanduser():
285
    assert os.path.expanduser('empty') == path.Path('empty').expanduser()
286
287
288
def test_expandvars():
289
    assert os.path.expandvars('empty') == path.Path('empty').expandvars()
290
291
292
def test_getatime():
293
    files = """
294
        b: hello
295
    """
296
    with create_files(files) as _root:
297
        root = path.Path(_root)
298
        assert os.path.getatime(root/'b') == (root/'b').getatime()
299
300
301
def test_getctime():
302
    files = """
303
        b: hello
304
    """
305
    with create_files(files) as _root:
306
        root = path.Path(_root)
307
        assert os.path.getctime(root/'b') == (root/'b').getctime()
308
309
310
def test_getmtime():
311
    files = """
312
        b: hello
313
    """
314
    with create_files(files) as _root:
315
        root = path.Path(_root)
316
        assert os.path.getmtime(root/'b') == (root/'b').getmtime()
317
318
319
def test_access():
320
    files = """
321
        b: hello
322
    """
323
    with create_files(files) as _root:
324
        b = path.Path(_root) / 'b'
325
        assert b.access(os.R_OK)
326
327
328
def test_getsize():
329
    assert os.path.getsize(__file__) == path.Path(__file__).getsize()
330
331
332
def test_isabs():
333
    assert os.path.isabs('empty') == path.Path('empty').isabs()
334
335
336
def test_isdir():
337
    assert os.path.isdir('empty') == path.Path('empty').isdir()
338
339
340
def test_isfile():
341
    assert os.path.isfile('empty') == path.Path('empty').isfile()
342
343
344
def test_islink():
345
    assert os.path.islink('empty') == path.Path('empty').islink()
346
347
348
def test_ismount():
349
    assert os.path.ismount('empty') == path.Path('empty').ismount()
350
351
352
def test_join():
353
    assert os.path.join('empty') == path.Path('empty').join()
354
355
356
def test_lexists():
357
    assert os.path.lexists('empty') == path.Path('empty').lexists()
358
359
360
def test_normcase():
361
    assert os.path.normcase('empty') == path.Path('empty').normcase()
362
363
364
def test_normpath():
365
    assert os.path.normpath('empty') == path.Path('empty').normpath()
366
367
368
def test_realpath():
369
    assert os.path.realpath('empty') == path.Path('empty').realpath()
370
371
372
def test_relpath():
373
    assert os.path.relpath('empty') == path.Path('empty').relpath()
374
375
376
def test_split():
377
    assert os.path.split('empty') == path.Path('empty').split()
378
379
380
def test_splitdrive():
381
    assert os.path.splitdrive('empty') == path.Path('empty').splitdrive()
382
383
384
def test_splitext():
385
    assert os.path.splitext('empty') == path.Path('empty').splitext()
386
387
388
def test_ext():
389
    assert path.Path('hello.world').ext == '.world'
390
391
392
def test_listdir():
393
    assert os.listdir('.') == path.Path('.').listdir()
394
395
396
def test_lstat():
397
    assert os.lstat(__file__) == path.Path(__file__).lstat()
398
399
400
def test_stat():
401
    assert os.stat(__file__) == path.Path(__file__).stat()
402
403
404
# def test_utime():
405
#     assert os.utime('empty') == path.Path('empty').utime()
406