|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
import glob |
|
3
|
|
|
import os |
|
4
|
|
|
from dkfileutils import path |
|
5
|
|
|
from yamldirs import create_files |
|
6
|
|
|
|
|
7
|
|
|
opjoin = os.path.join |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
def _relcontent(root): |
|
11
|
|
|
return [p.relpath(root) for p in root] |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
def test_open(): |
|
15
|
|
|
files = """ |
|
16
|
|
|
b: hello |
|
17
|
|
|
""" |
|
18
|
|
|
with create_files(files) as root: |
|
19
|
|
|
assert (path.Path(root) / 'b').open().read() == 'hello' |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
def test_iter(): |
|
23
|
|
|
files = """ |
|
24
|
|
|
- .dotfile |
|
25
|
|
|
- .dotdir: |
|
26
|
|
|
- d |
|
27
|
|
|
- e |
|
28
|
|
|
- a |
|
29
|
|
|
- b |
|
30
|
|
|
- c: |
|
31
|
|
|
- d |
|
32
|
|
|
""" |
|
33
|
|
|
with create_files(files) as _root: |
|
34
|
|
|
root = path.Path(_root) |
|
35
|
|
|
assert _relcontent(root) == ['a', 'b', opjoin('c', 'd')] |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
def test_contains(): |
|
39
|
|
|
files = """ |
|
40
|
|
|
- a |
|
41
|
|
|
- b |
|
42
|
|
|
""" |
|
43
|
|
|
with create_files(files) as _root: |
|
44
|
|
|
root = path.Path(_root) |
|
45
|
|
|
assert 'a' in root |
|
46
|
|
|
assert 'b' in root |
|
47
|
|
|
assert 'c' not in root |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
def test_unlink(): |
|
51
|
|
|
files = """ |
|
52
|
|
|
- a |
|
53
|
|
|
- b |
|
54
|
|
|
""" |
|
55
|
|
|
with create_files(files) as _root: |
|
56
|
|
|
root = path.Path(_root) |
|
57
|
|
|
assert [p.relpath(root) for p in root] == ['a', 'b'] |
|
58
|
|
|
|
|
59
|
|
|
b = root / 'b' |
|
60
|
|
|
b.unlink() |
|
61
|
|
|
assert [p.relpath(root) for p in root] == ['a'] |
|
62
|
|
|
|
|
63
|
|
|
a = root / 'a' |
|
64
|
|
|
a.remove() |
|
65
|
|
|
assert [p.relpath(root) for p in root] == [] |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
def test_glob(): |
|
69
|
|
|
files = """ |
|
70
|
|
|
- a.py |
|
71
|
|
|
- b: |
|
72
|
|
|
- a.txt |
|
73
|
|
|
- aa.txt |
|
74
|
|
|
- d |
|
75
|
|
|
- e: |
|
76
|
|
|
- a: |
|
77
|
|
|
- b |
|
78
|
|
|
- f |
|
79
|
|
|
""" |
|
80
|
|
|
with create_files(files) as _root: |
|
81
|
|
|
root = path.Path(_root) |
|
82
|
|
|
assert [p.relpath(root) for p in root.glob('**/*.py')] == ['a.py'] |
|
83
|
|
|
assert [p.relpath(root) for p in root.glob('*.py')] == ['a.py'] |
|
84
|
|
|
assert [p.relpath(root) for p in root.glob('b/a?.txt')] == [ |
|
85
|
|
|
opjoin('b', 'aa.txt') |
|
86
|
|
|
] |
|
87
|
|
|
assert [p.relpath(root) for p in root.glob('**/a.*')] == [ |
|
88
|
|
|
'a.py', opjoin('b', 'a.txt') |
|
89
|
|
|
] |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
def test_subdirs(): |
|
93
|
|
|
files = """ |
|
94
|
|
|
- a.py |
|
95
|
|
|
- b: |
|
96
|
|
|
- a.txt |
|
97
|
|
|
- aa.txt |
|
98
|
|
|
- d |
|
99
|
|
|
- e: |
|
100
|
|
|
- a: |
|
101
|
|
|
- b |
|
102
|
|
|
- f |
|
103
|
|
|
""" |
|
104
|
|
|
with create_files(files) as _root: |
|
105
|
|
|
root = path.Path(_root) |
|
106
|
|
|
assert [d.relpath(root) for d in root.subdirs()] == ['b', 'e'] |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
def test_files(): |
|
110
|
|
|
files = """ |
|
111
|
|
|
- a.py |
|
112
|
|
|
- b: |
|
113
|
|
|
- a.txt |
|
114
|
|
|
- aa.txt |
|
115
|
|
|
- d |
|
116
|
|
|
- e: |
|
117
|
|
|
- a: |
|
118
|
|
|
- b |
|
119
|
|
|
- f |
|
120
|
|
|
""" |
|
121
|
|
|
with create_files(files) as _root: |
|
122
|
|
|
root = path.Path(_root) |
|
123
|
|
|
assert [d.relpath(root) for d in root.files()] == ['a.py', 'd', 'f'] |
|
124
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
def test_makedirs(): |
|
127
|
|
|
files = """ |
|
128
|
|
|
a: |
|
129
|
|
|
- b: |
|
130
|
|
|
- empty |
|
131
|
|
|
""" |
|
132
|
|
|
with create_files(files) as _root: |
|
133
|
|
|
root = path.Path(_root) |
|
134
|
|
|
e = root.makedirs('a/b/c/d') |
|
135
|
|
|
# print "root", root |
|
136
|
|
|
# print 'E:', e |
|
137
|
|
|
assert e.isdir() |
|
138
|
|
|
assert e.relpath(root) == path.Path('a')/'b'/'c'/'d' |
|
139
|
|
|
|
|
140
|
|
|
e2 = root.makedirs('a/b/c/d') |
|
141
|
|
|
assert e2.isdir() |
|
142
|
|
|
|
|
143
|
|
|
b = root.mkdir('b') |
|
144
|
|
|
assert b.isdir() |
|
145
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
def test_commonprefix(): |
|
148
|
|
|
files = """ |
|
149
|
|
|
- a.py |
|
150
|
|
|
- b: |
|
151
|
|
|
- a.txt |
|
152
|
|
|
- aa.txt |
|
153
|
|
|
- d |
|
154
|
|
|
- e: |
|
155
|
|
|
- a: |
|
156
|
|
|
- b |
|
157
|
|
|
- f |
|
158
|
|
|
""" |
|
159
|
|
|
with create_files(files) as _root: |
|
160
|
|
|
root = path.Path(_root) |
|
161
|
|
|
assert root.commonprefix(root) == root |
|
162
|
|
|
assert root.commonprefix(root/'a.py', root/'d', root/'b'/'a.txt') == root |
|
163
|
|
|
|
|
164
|
|
|
|
|
165
|
|
|
def test_abspath(): |
|
166
|
|
|
assert os.path.abspath('empty') == path.Path('empty').abspath() |
|
167
|
|
|
|
|
168
|
|
|
|
|
169
|
|
|
def test_drive(): |
|
170
|
|
|
assert os.path.splitdrive('empty')[0] == path.Path('empty').drive() |
|
171
|
|
|
|
|
172
|
|
|
|
|
173
|
|
|
def test_drivepath(): |
|
174
|
|
|
assert os.path.splitdrive('empty')[1] == path.Path('empty').drivepath() |
|
175
|
|
|
|
|
176
|
|
|
|
|
177
|
|
|
def test_basename(): |
|
178
|
|
|
assert os.path.basename('empty') == path.Path('empty').basename() |
|
179
|
|
|
|
|
180
|
|
|
|
|
181
|
|
|
# def test_commonprefix(): |
|
182
|
|
|
# assert os.path.commonprefix('empty', 'empty') == path.Path('empty').commonprefix('empty') |
|
183
|
|
|
|
|
184
|
|
|
|
|
185
|
|
|
def test_dirname(): |
|
186
|
|
|
assert os.path.dirname('empty') == path.Path('empty').dirname() |
|
187
|
|
|
|
|
188
|
|
|
|
|
189
|
|
|
def test_exists(): |
|
190
|
|
|
assert os.path.exists('empty') == path.Path('empty').exists() |
|
191
|
|
|
|
|
192
|
|
|
|
|
193
|
|
|
def test_expanduser(): |
|
194
|
|
|
assert os.path.expanduser('empty') == path.Path('empty').expanduser() |
|
195
|
|
|
|
|
196
|
|
|
|
|
197
|
|
|
def test_expandvars(): |
|
198
|
|
|
assert os.path.expandvars('empty') == path.Path('empty').expandvars() |
|
199
|
|
|
|
|
200
|
|
|
|
|
201
|
|
|
def test_getatime(): |
|
202
|
|
|
files = """ |
|
203
|
|
|
b: hello |
|
204
|
|
|
""" |
|
205
|
|
|
with create_files(files) as _root: |
|
206
|
|
|
root = path.Path(_root) |
|
207
|
|
|
assert os.path.getatime(root/'b') == (root/'b').getatime() |
|
208
|
|
|
|
|
209
|
|
|
|
|
210
|
|
|
def test_getctime(): |
|
211
|
|
|
files = """ |
|
212
|
|
|
b: hello |
|
213
|
|
|
""" |
|
214
|
|
|
with create_files(files) as _root: |
|
215
|
|
|
root = path.Path(_root) |
|
216
|
|
|
assert os.path.getctime(root/'b') == (root/'b').getctime() |
|
217
|
|
|
|
|
218
|
|
|
|
|
219
|
|
|
def test_getmtime(): |
|
220
|
|
|
files = """ |
|
221
|
|
|
b: hello |
|
222
|
|
|
""" |
|
223
|
|
|
with create_files(files) as _root: |
|
224
|
|
|
root = path.Path(_root) |
|
225
|
|
|
assert os.path.getmtime(root/'b') == (root/'b').getmtime() |
|
226
|
|
|
|
|
227
|
|
|
|
|
228
|
|
|
def test_access(): |
|
229
|
|
|
files = """ |
|
230
|
|
|
b: hello |
|
231
|
|
|
""" |
|
232
|
|
|
with create_files(files) as _root: |
|
233
|
|
|
b = path.Path(_root) / 'b' |
|
234
|
|
|
assert b.access(os.R_OK) |
|
235
|
|
|
|
|
236
|
|
|
|
|
237
|
|
|
def test_getsize(): |
|
238
|
|
|
assert os.path.getsize(__file__) == path.Path(__file__).getsize() |
|
239
|
|
|
|
|
240
|
|
|
|
|
241
|
|
|
def test_isabs(): |
|
242
|
|
|
assert os.path.isabs('empty') == path.Path('empty').isabs() |
|
243
|
|
|
|
|
244
|
|
|
|
|
245
|
|
|
def test_isdir(): |
|
246
|
|
|
assert os.path.isdir('empty') == path.Path('empty').isdir() |
|
247
|
|
|
|
|
248
|
|
|
|
|
249
|
|
|
def test_isfile(): |
|
250
|
|
|
assert os.path.isfile('empty') == path.Path('empty').isfile() |
|
251
|
|
|
|
|
252
|
|
|
|
|
253
|
|
|
def test_islink(): |
|
254
|
|
|
assert os.path.islink('empty') == path.Path('empty').islink() |
|
255
|
|
|
|
|
256
|
|
|
|
|
257
|
|
|
def test_ismount(): |
|
258
|
|
|
assert os.path.ismount('empty') == path.Path('empty').ismount() |
|
259
|
|
|
|
|
260
|
|
|
|
|
261
|
|
|
def test_join(): |
|
262
|
|
|
assert os.path.join('empty') == path.Path('empty').join() |
|
263
|
|
|
|
|
264
|
|
|
|
|
265
|
|
|
def test_lexists(): |
|
266
|
|
|
assert os.path.lexists('empty') == path.Path('empty').lexists() |
|
267
|
|
|
|
|
268
|
|
|
|
|
269
|
|
|
def test_normcase(): |
|
270
|
|
|
assert os.path.normcase('empty') == path.Path('empty').normcase() |
|
271
|
|
|
|
|
272
|
|
|
|
|
273
|
|
|
def test_normpath(): |
|
274
|
|
|
assert os.path.normpath('empty') == path.Path('empty').normpath() |
|
275
|
|
|
|
|
276
|
|
|
|
|
277
|
|
|
def test_realpath(): |
|
278
|
|
|
assert os.path.realpath('empty') == path.Path('empty').realpath() |
|
279
|
|
|
|
|
280
|
|
|
|
|
281
|
|
|
def test_relpath(): |
|
282
|
|
|
assert os.path.relpath('empty') == path.Path('empty').relpath() |
|
283
|
|
|
|
|
284
|
|
|
|
|
285
|
|
|
def test_split(): |
|
286
|
|
|
assert os.path.split('empty') == path.Path('empty').split() |
|
287
|
|
|
|
|
288
|
|
|
|
|
289
|
|
|
def test_splitdrive(): |
|
290
|
|
|
assert os.path.splitdrive('empty') == path.Path('empty').splitdrive() |
|
291
|
|
|
|
|
292
|
|
|
|
|
293
|
|
|
def test_splitext(): |
|
294
|
|
|
assert os.path.splitext('empty') == path.Path('empty').splitext() |
|
295
|
|
|
|
|
296
|
|
|
|
|
297
|
|
|
def test_ext(): |
|
298
|
|
|
assert path.Path('hello.world').ext == '.world' |
|
299
|
|
|
|
|
300
|
|
|
|
|
301
|
|
|
def test_listdir(): |
|
302
|
|
|
assert os.listdir('.') == path.Path('.').listdir() |
|
303
|
|
|
|
|
304
|
|
|
|
|
305
|
|
|
def test_lstat(): |
|
306
|
|
|
assert os.lstat(__file__) == path.Path(__file__).lstat() |
|
307
|
|
|
|
|
308
|
|
|
|
|
309
|
|
|
def test_stat(): |
|
310
|
|
|
assert os.stat(__file__) == path.Path(__file__).stat() |
|
311
|
|
|
|
|
312
|
|
|
|
|
313
|
|
|
# def test_utime(): |
|
314
|
|
|
# assert os.utime('empty') == path.Path('empty').utime() |
|
315
|
|
|
|