1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
import errno
import gc
import mmap
import os
import time
pjoin = os.path.join
import pytest
from snakeoil import _fileutils, currying, fileutils
from snakeoil.fileutils import AtomicWriteFile, write_file
from snakeoil.test import random_str
class TestTouch:
@pytest.fixture
def random_path(self, tmp_path):
return tmp_path / random_str(10)
def test_file_creation(self, random_path):
orig_um = os.umask(0o000)
try:
fileutils.touch(random_path)
finally:
exiting_umask = os.umask(orig_um)
assert exiting_umask == 0o000
assert random_path.exists()
assert random_path.stat().st_mode & 0o4777 == 0o644
def test_set_times(self, random_path):
fileutils.touch(random_path)
orig_stat = random_path.stat()
time.sleep(1)
fileutils.touch(random_path)
new_stat = random_path.stat()
assert orig_stat.st_atime != new_stat.st_atime
assert orig_stat.st_mtime != new_stat.st_mtime
def test_set_custom_times(self, random_path):
fileutils.touch(random_path)
orig_stat = random_path.stat()
times = (1, 1)
fileutils.touch(random_path, times=times)
new_stat = random_path.stat()
assert orig_stat != new_stat
assert 1 == new_stat.st_atime
assert 1 == new_stat.st_mtime
def test_set_custom_nstimes(self, random_path):
fileutils.touch(random_path)
orig_stat = random_path.stat()
ns = (1, 1)
fileutils.touch(random_path, ns=ns)
new_stat = random_path.stat()
# system doesn't have nanosecond precision, try microseconds
if new_stat.st_atime == 0:
ns = (1000, 1000)
fileutils.touch(random_path, ns=ns)
new_stat = random_path.stat()
assert orig_stat != new_stat
assert ns[0] == new_stat.st_atime_ns
assert ns[0] == new_stat.st_mtime_ns
class TestAtomicWriteFile:
kls = AtomicWriteFile
def test_normal_ops(self, tmp_path):
fp = tmp_path / "target"
write_file(fp, "w", "me")
af = self.kls(fp)
af.write("dar")
assert fileutils.readfile_ascii(fp) == "me"
af.close()
assert fileutils.readfile_ascii(fp) == "dar"
def test_perms(self, tmp_path):
fp = tmp_path / "target"
orig_um = os.umask(0o777)
try:
af = self.kls(fp, perms=0o644)
af.write("dar")
af.close()
finally:
exiting_umask = os.umask(orig_um)
assert exiting_umask == 0o777
assert os.stat(fp).st_mode & 0o4777 == 0o644
def test_del(self, tmp_path):
fp = tmp_path / "target"
write_file(fp, "w", "me")
assert fileutils.readfile_ascii(fp) == "me"
af = self.kls(fp)
af.write("dar")
del af
gc.collect()
assert fileutils.readfile_ascii(fp) == "me"
assert len(os.listdir(tmp_path)) == 1
def test_close(self, tmp_path):
# verify that we handle multiple closes; no exception is good.
af = self.kls(tmp_path / "target")
af.close()
af.close()
def test_discard(self, tmp_path):
fp = tmp_path / "target"
write_file(fp, "w", "me")
assert fileutils.readfile_ascii(fp) == "me"
af = self.kls(fp)
af.write("dar")
af.discard()
assert not os.path.exists(af._temp_fp)
af.close()
assert fileutils.readfile_ascii(fp) == "me"
# finally validate that it handles multiple discards properly.
af = self.kls(fp)
af.write("dar")
af.discard()
af.discard()
af.close()
def cpy_setup_class(scope, func_name):
if getattr(fileutils, 'native_%s' % func_name) \
is getattr(fileutils, func_name):
scope['skip'] = 'extensions disabled'
else:
scope['func'] = staticmethod(getattr(fileutils, func_name))
class Test_readfile:
func = staticmethod(fileutils.readfile)
test_cases = ['asdf\nfdasswer\1923', '', '987234']
default_encoding = 'ascii'
none_on_missing_ret_data = 'dar'
@staticmethod
def convert_data(data, encoding):
if isinstance(data, bytes):
return data
if encoding:
return data.encode(encoding)
return data
def test_it(self, tmp_path):
fp = tmp_path / 'testfile'
for expected in self.test_cases:
raised = None
encoding = self.default_encoding
if isinstance(expected, tuple):
if len(expected) == 3:
raised = expected[2]
if expected[1] is not None:
encoding = expected[1]
expected = expected[0]
write_file(fp, 'wb', self.convert_data(expected, encoding))
if raised:
with pytest.raises(raised):
self.assertFunc(fp, expected)
else:
self.assertFunc(fp, expected)
def assertFunc(self, path, expected):
assert self.func(path) == expected
def test_none_on_missing(self, tmp_path):
fp = tmp_path / 'nonexistent'
with pytest.raises(FileNotFoundError):
self.func(fp)
assert self.func(fp, True) is None
write_file(fp, 'wb', self.convert_data('dar', 'ascii'))
assert self.func(fp, True) == self.none_on_missing_ret_data
# ensure it handles paths that go through files-
# still should be suppress
assert self.func(fp / 'extra', True) is None
class Test_readfile_ascii(Test_readfile):
func = staticmethod(fileutils.readfile_ascii)
class Test_readfile_utf8(Test_readfile):
func = staticmethod(fileutils.readfile_utf8)
default_encoding = 'utf8'
class Test_readfile_bytes(Test_readfile):
func = staticmethod(fileutils.readfile_bytes)
default_encoding = None
test_cases = list(map(
currying.post_curry(Test_readfile.convert_data, 'ascii'),
Test_readfile.test_cases))
test_cases.append('\ua000fa'.encode("utf8"))
none_on_missing_ret_data = Test_readfile.convert_data(
Test_readfile.none_on_missing_ret_data, 'ascii')
class readlines_mixin:
def assertFunc(self, path, expected):
expected = tuple(expected.split())
if expected == ('',):
expected = ()
if 'utf8' not in self.encoding_mode:
assert tuple(self.func(path)) == expected
return
assert tuple(self.func(path)) == expected
def test_none_on_missing(self, tmp_path):
fp = tmp_path / 'nonexistent'
with pytest.raises(FileNotFoundError):
self.func(fp)
assert not tuple(self.func(fp, False, True))
write_file(fp, 'wb', self.convert_data('dar', 'ascii'))
assert tuple(self.func(fp, True)) == (self.none_on_missing_ret_data,)
assert not tuple(self.func(fp / 'missing', False, True))
def test_strip_whitespace(self, tmp_path):
fp = tmp_path / 'data'
write_file(fp, 'wb', self.convert_data(' dar1 \ndar2 \n dar3\n',
'ascii'))
results = tuple(self.func(fp, True))
expected = ('dar1', 'dar2', 'dar3')
if self.encoding_mode == 'bytes':
expected = tuple(x.encode("ascii") for x in expected)
assert results == expected
# this time without the trailing newline...
write_file(fp, 'wb', self.convert_data(' dar1 \ndar2 \n dar3',
'ascii'))
results = tuple(self.func(fp, True))
assert results == expected
# test a couple of edgecases; underly c extension has gotten these
# wrong before.
write_file(fp, 'wb', self.convert_data('0', 'ascii'))
results = tuple(self.func(fp, True))
expected = ('0',)
if self.encoding_mode == 'bytes':
expected = tuple(x.encode("ascii") for x in expected)
assert results == expected
write_file(fp, 'wb', self.convert_data('0\n', 'ascii'))
results = tuple(self.func(fp, True))
expected = ('0',)
if self.encoding_mode == 'bytes':
expected = tuple(x.encode("ascii") for x in expected)
assert results == expected
write_file(fp, 'wb', self.convert_data('0 ', 'ascii'))
results = tuple(self.func(fp, True))
expected = ('0',)
if self.encoding_mode == 'bytes':
expected = tuple(x.encode("ascii") for x in expected)
assert results == expected
def mk_readlines_test(scope, mode):
func_name = 'readlines_%s' % mode
base = globals()['Test_readfile_%s' % mode]
class kls(readlines_mixin, base):
func = staticmethod(getattr(fileutils, func_name))
encoding_mode = mode
kls.__name__ = "Test_%s" % func_name
scope["Test_%s" % func_name] = kls
for case in ("ascii", "bytes", "utf8"):
name = 'readlines_%s' % case
mk_readlines_test(locals(), case)
class TestBrokenStats:
test_cases = ['/proc/crypto', '/sys/devices/system/cpu/present']
def test_readfile(self):
for path in self.test_cases:
self._check_path(path, fileutils.readfile)
def test_readlines(self):
for path in self.test_cases:
self._check_path(path, fileutils.readlines, True)
def _check_path(self, path, func, split_it=False):
try:
with open(path, 'r') as handle:
data = handle.read()
except EnvironmentError as e:
if e.errno not in (errno.ENOENT, errno.EPERM):
raise
return
func_data = func(path)
if split_it:
func_data = list(func_data)
data = [x for x in data.split('\n') if x]
func_data = [x for x in func_data if x]
assert func_data == data
class Test_mmap_or_open_for_read:
func = staticmethod(fileutils.mmap_or_open_for_read)
def test_zero_length(self, tmp_path):
path = tmp_path / 'target'
write_file(path, 'w', '')
m, f = self.func(path)
assert m is None
assert f.read() == b''
f.close()
def test_mmap(self, tmp_path, data=b'foonani'):
path = tmp_path / 'target'
write_file(path, 'wb', data)
m, f = self.func(path)
assert len(m) == len(data)
assert m.read(len(data)) == data
m.close()
assert f is None
class Test_mmap_and_close:
def test_it(self, tmp_path):
path = tmp_path / 'target'
data = b'asdfasdf'
write_file(path, 'wb', [data])
fd, m = None, None
try:
fd = os.open(path, os.O_RDONLY)
m = _fileutils.mmap_and_close(
fd, len(data), mmap.MAP_PRIVATE, mmap.PROT_READ)
# and ensure it closed the fd...
with pytest.raises(EnvironmentError):
os.read(fd, 1)
fd = None
assert len(m) == len(data)
assert m.read(len(data)) == data
finally:
if m is not None:
m.close()
if fd is not None:
os.close(fd)
|