-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfileutils_tests.py
More file actions
245 lines (219 loc) · 7.32 KB
/
Copy pathfileutils_tests.py
File metadata and controls
245 lines (219 loc) · 7.32 KB
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
#!/usr/bin/python
#
# Unit tests for cs.fileutils.
# - Cameron Simpson <cs@cskk.id.au>
#
from __future__ import print_function, absolute_import
import sys
from io import StringIO
import os
import os.path
import errno
from threading import Lock
import time
import unittest
from tempfile import NamedTemporaryFile
from .fileutils import compare, rewrite, rewrite_cmgr, Pathname, \
file_property, \
make_files_property, \
BackedFile, BackedFile_TestMethods
from .gimmicks import TimeoutError
from .timeutils import sleep
from .x import X
class ThingWithFileProperty(object):
''' An object with an `@file_property`.
'''
def __init__(self):
self._test1__filename = 'testfileprop1'
self._test1_lock = Lock()
self._test2__filename = 'testfileprop2'
self._test2_lock = Lock()
def write1(self, data):
with open(self._test1__filename, "w") as fp:
fp.write(data)
def write2(self, data):
with open(self._test2__filename, "w") as fp:
fp.write(data)
@file_property
def test1(self, filename):
with open(filename) as fp:
data = fp.read()
return data
class ThingWithFilesProperty(object):
''' An object with an `@files_property`.
'''
def __init__(self):
self._test1__filenames = ('testfileprop1',)
self._test1_lock = Lock()
def write1(self, data):
with open(self._test1s[0], "w") as fp:
fp.write(data)
def write2(self, data):
with open(self._test2s[0], "w") as fp:
fp.write(data)
##@files_property
##def test1(self, path0):
## with open(path0) as fp:
## data = fp.read()
## return (path0,), data
@make_files_property(poll_rate=0.3)
def test2(self, paths):
with open(paths[0]) as fp:
data = fp.read()
return (paths[0],), data
class Test_Misc(unittest.TestCase):
''' Tests.
'''
def setUp(self):
self.proppath = 'cs.fileutils_tests_tstprop'
self.lockbase = 'cs.fileutils_tests_testlock'
self.lockext = '.lock'
self.lockpath = self.lockbase + self.lockext
self.fileprop = None
self.filesprop = None
def tearDown(self):
tidyup = [self.proppath, self.lockpath]
if self.fileprop:
tidyup.append(self.fileprop._test1__filename)
tidyup.append(self.fileprop._test2__filename)
if self.filesprop:
tidyup.extend(self.filesprop._test1s)
tidyup.extend(self.filesprop._test2s)
for path in tidyup:
try:
os.remove(path)
except OSError as e:
if e.errno != errno.ENOENT:
raise
def test_compare(self):
data = "here are some data\n"
with NamedTemporaryFile(mode='w') as T1:
T1.write(data)
T1.flush()
with NamedTemporaryFile(mode='w') as T2:
T2.write(data)
T2.flush()
with open(T1.name) as t1fp:
t1data = t1fp.read()
with open(T2.name) as t2fp:
t2data = t2fp.read()
self.assertEqual(t1data, data, "bad data in %s" % (T1.name,))
self.assertEqual(t2data, data, "bad data in %s" % (T2.name,))
self.assertTrue(
compare(T1.name, T2.name),
"mismatched data in %s and %s" % (T1.name, T2.name)
)
def test_rewrite(self):
olddata = "old data\n"
newdata = "new data\n"
with NamedTemporaryFile(mode='w') as T1:
T1.write(olddata)
T1.flush()
with open(T1.name) as t1fp:
t1data = t1fp.read()
self.assertEqual(t1data, olddata, "bad old data in %s" % (T1.name,))
rewrite(T1.name, StringIO(newdata), mode='w')
with open(T1.name) as t1fp:
t1data = t1fp.read()
self.assertEqual(t1data, newdata, "bad new data in %s" % (T1.name,))
def test_rewrite_cmgr(self):
olddata = "old data\n"
newdata = "new data\n"
with NamedTemporaryFile(mode='w') as T1:
T1.write(olddata)
T1.flush()
with open(T1.name) as t1fp:
t1data = t1fp.read()
self.assertEqual(t1data, olddata, "bad old data in %s" % (T1.name,))
with rewrite_cmgr(T1.name) as tfp:
tfp.write(newdata)
with open(T1.name) as t1fp:
t1data = t1fp.read()
self.assertEqual(t1data, newdata, "bad new data in %s" % (T1.name,))
def test_file_property_00(self):
PC = self.fileprop = ThingWithFileProperty()
self.assertTrue(not os.path.exists(PC._test1__filename))
data1 = PC.test1
self.assertTrue(data1 is None)
PC.write1("data1 value 1")
self.assertTrue(os.path.exists(PC._test1__filename))
sleep(1.1)
data1 = PC.test1
self.assertEqual(data1, "data1 value 1")
# NB: data value changes length because the file timestamp might not
# due to 1s resolution in stat structures
PC.write1("data1 value 1b")
self.assertTrue(os.path.exists(PC._test1__filename))
data1 = PC.test1
# too soon after last poll
self.assertEqual(data1, "data1 value 1")
sleep(1)
data1 = PC.test1
self.assertEqual(data1, "data1 value 1b")
os.remove(PC._test1__filename)
self.assertTrue(not os.path.exists(PC._test1__filename))
data1 = PC.test1
# too soon to poll
self.assertEqual(data1, "data1 value 1b")
sleep(1)
# poll should return None
data1 = PC.test1
self.assertEqual(data1, None)
PC.write1("data1 value 1bc")
self.assertTrue(os.path.exists(PC._test1__filename))
sleep(1)
# poll should succeed and load new value
data1 = PC.test1
self.assertEqual(data1, "data1 value 1bc")
def _eq(self, a, b, opdesc):
''' Convenience wrapper for assertEqual.
'''
##if a == b:
## print("OK: %s: %r == %r" % (opdesc, a, b), file=sys.stderr)
self.assertEqual(a, b, "%s: got %r, expected %r" % (opdesc, a, b))
def test_Pathname_01_attrs(self):
home = '/a/b'
maildir = '/a/b/mail'
prefixes = (('$MAILDIR/', '+'), ('$HOME/', '~/'))
environ = {'HOME': home, 'MAILDIR': maildir}
for path, shortpath in (
("a", "a"),
("a/b", "a/b"),
("a/b/c", "a/b/c"),
("/a/b/c", "~/c"),
("/a/b/mail", "~/mail"),
("/a/b/mail/folder", "+folder"),
):
P = Pathname(path)
self._eq(P.dirname, os.path.dirname(path), "%r.dirname" % (path,))
self._eq(P.basename, os.path.basename(path), "%r.basename" % (path,))
self._eq(P.abs, os.path.abspath(path), "%r.abs" % (path,))
self._eq(P.isabs, os.path.isabs(path), "%r.isabs" % (path,))
self._eq(
P.shorten(environ=environ, prefixes=prefixes), shortpath,
"%r.shorten(environ=%r, prefixes=%r)" % (path, environ, prefixes)
)
for spec, expected in (
("{!r}", repr(P)),
("{.basename}", os.path.basename(path)),
("{.dirname}", os.path.dirname(path)),
("{.abs}", os.path.abspath(path)),
):
self._eq(format(P, spec), expected, "format(%r, %r)" % (P, spec))
class Test_BackedFile(unittest.TestCase, BackedFile_TestMethods):
''' tests for `BackedFile`.
'''
def setUp(self, backing_filename=None):
if backing_filename is None:
backing_filename = __file__
self.backing_filename = backing_filename
self.backing_fp = open(self.backing_filename, "rb")
self.backing_text = self.backing_fp.read()
self.backed_fp = BackedFile(self.backing_fp)
def tearDown(self):
self.backed_fp.close()
self.backing_fp.close()
def selftest(argv):
unittest.main(__name__, None, argv, failfast=True)
if __name__ == '__main__':
selftest(sys.argv)