Completed
Push — master ( 8f3e72...b93e94 )
by Bjorn
59s
created

tests.test_cd_contextmanager()   B

Complexity

Conditions 6

Size

Total Lines 11

Duplication

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