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