50
50
self.assertEquals(i['isError'], 1)
51
51
log.flushErrors(ig)
53
def testErrorsWithWhy(self):
54
for e, ig in [("hello world","hello world"),
55
(KeyError(), KeyError),
56
(failure.Failure(RuntimeError()), RuntimeError)]:
58
i = self.catcher.pop()
59
self.assertEquals(i['isError'], 1)
60
self.assertEquals(i['why'], 'foobar')
53
64
def testErroneousErrors(self):
56
log.addObserver(lambda events: events['isError'] or L1.append(events))
67
log.addObserver(lambda events: L1.append(events))
57
68
log.addObserver(lambda events: 1/0)
58
log.addObserver(lambda events: events['isError'] or L2.append(events))
69
log.addObserver(lambda events: L2.append(events))
59
70
log.msg("Howdy, y'all.")
61
excs = [f.type for f in log.flushErrors(ZeroDivisionError)]
72
# XXX - use private _flushErrors so we don't also catch
73
# the deprecation warnings
74
excs = [f.type for f in log._flushErrors(ZeroDivisionError)]
62
75
self.assertEquals([ZeroDivisionError], excs)
64
77
self.assertEquals(len(L1), 2)
115
128
sys.setdefaultencoding(self._origEncoding)
116
129
del sys.setdefaultencoding
118
133
class LogPublisherTestCase(LogPublisherTestCaseMixin, unittest.TestCase):
120
134
def testSingleString(self):
121
135
self.lp.msg("Hello, world.")
122
136
self.assertEquals(len(self.out), 1)
124
139
def testMultipleString(self):
125
140
# Test some stupid behavior that will be deprecated real soon.
126
141
# If you are reading this and trying to learn how the logging
128
143
self.lp.msg("Hello, ", "world.")
129
144
self.assertEquals(len(self.out), 1)
131
147
def testSingleUnicode(self):
132
148
self.lp.msg(u"Hello, \N{VULGAR FRACTION ONE HALF} world.")
133
149
self.assertEquals(len(self.out), 1)
134
150
self.assertIn('with str error Traceback', self.out[0])
135
151
self.assertIn('UnicodeEncodeError', self.out[0])
137
155
class FileObserverTestCase(LogPublisherTestCaseMixin, unittest.TestCase):
156
def test_timeFormatting(self):
158
Test the method of L{FileLogObserver} which turns a timestamp into a
159
human-readable string.
161
# There is no function in the time module which converts a UTC time
162
# tuple to a timestamp.
163
when = time.mktime((2001, 2, 3, 4, 5, 6, 7, 8, 0)) - time.timezone
165
# Pretend to be in US/Eastern for a moment
166
self.flo.getTimezoneOffset = lambda: 18000
167
self.assertEquals(self.flo.formatTime(when), '2001/02/02 23:05 -0500')
169
# Okay now we're in Eastern Europe somewhere
170
self.flo.getTimezoneOffset = lambda: -3600
171
self.assertEquals(self.flo.formatTime(when), '2001/02/03 05:05 +0100')
173
# And off in the Pacific or someplace like that
174
self.flo.getTimezoneOffset = lambda: -39600
175
self.assertEquals(self.flo.formatTime(when), '2001/02/03 15:05 +1100')
177
# One of those weird places with a half-hour offset timezone
178
self.flo.getTimezoneOffset = lambda: 5400
179
self.assertEquals(self.flo.formatTime(when), '2001/02/03 02:35 -0130')
181
# Half-hour offset in the other direction
182
self.flo.getTimezoneOffset = lambda: -5400
183
self.assertEquals(self.flo.formatTime(when), '2001/02/03 05:35 +0130')
185
# If a strftime-format string is present on the logger, it should
186
# use that instead. Note we don't assert anything about day, hour
187
# or minute because we cannot easily control what time.strftime()
188
# thinks the local timezone is.
189
self.flo.timeFormat = '%Y %m'
190
self.assertEquals(self.flo.formatTime(when), '2001 02')
138
193
def testLoggingAnObjectWithBroken__str__(self):
140
195
self.lp.msg(EvilStr())
142
197
# Logging system shouldn't need to crap itself for this trivial case
143
198
self.assertNotIn('UNFORMATTABLE', self.out[0])
145
201
def testFormattingAnObjectWithBroken__str__(self):
146
202
self.lp.msg(format='%(blat)s', blat=EvilStr())
147
203
self.assertEquals(len(self.out), 1)
148
204
self.assertIn('Invalid format string or unformattable object', self.out[0])
150
207
def testBrokenSystem__str__(self):
151
208
self.lp.msg('huh', system=EvilStr())
152
209
self.assertEquals(len(self.out), 1)
153
210
self.assertIn('Invalid format string or unformattable object', self.out[0])
155
213
def testFormattingAnObjectWithBroken__repr__Indirect(self):
156
214
self.lp.msg(format='%(blat)s', blat=[EvilRepr()])
157
215
self.assertEquals(len(self.out), 1)
158
216
self.assertIn('UNFORMATTABLE OBJECT', self.out[0])
160
219
def testSystemWithBroker__repr__Indirect(self):
161
220
self.lp.msg('huh', system=[EvilRepr()])
162
221
self.assertEquals(len(self.out), 1)
163
222
self.assertIn('UNFORMATTABLE OBJECT', self.out[0])
165
225
def testSimpleBrokenFormat(self):
166
226
self.lp.msg(format='hooj %s %s', blat=1)
167
227
self.assertEquals(len(self.out), 1)
168
228
self.assertIn('Invalid format string or unformattable object', self.out[0])
170
231
def testRidiculousFormat(self):
171
232
self.lp.msg(format=42, blat=1)
172
233
self.assertEquals(len(self.out), 1)
173
234
self.assertIn('Invalid format string or unformattable object', self.out[0])
175
237
def testEvilFormat__repr__And__str__(self):
176
238
self.lp.msg(format=EvilReprStr(), blat=1)
177
239
self.assertEquals(len(self.out), 1)