PageRenderTime 25ms CodeModel.GetById 2ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/core/test_debugger.py

https://bitbucket.org/prologic/circuits/
Python | 303 lines | 276 code | 22 blank | 5 comment | 3 complexity | ae71736a3bba66e548c90152d41a1c97 MD5 | raw file
  1# Module:   debugger
  2# Date:     5th November 2008
  3# Author:   James Mills, prologic at shortcircuit dot net dot au
  4
  5"""Debugger Tests"""
  6
  7import sys
  8
  9import pytest
 10
 11try:
 12    from StringIO import StringIO
 13except ImportError:
 14    from io import StringIO  # NOQA
 15
 16from circuits import Debugger
 17from circuits.core import Event, Component
 18
 19
 20class test(Event):
 21    """test Event"""
 22
 23
 24class App(Component):
 25
 26    def test(self, raiseException=False):
 27        if raiseException:
 28            raise Exception()
 29
 30
 31class Logger(object):
 32
 33    error_msg = None
 34    debug_msg = None
 35
 36    def error(self, msg):
 37        self.error_msg = msg
 38
 39    def debug(self, msg):
 40        self.debug_msg = msg
 41
 42
 43def test_main():
 44    app = App()
 45    stderr = StringIO()
 46    debugger = Debugger(file=stderr)
 47    debugger.register(app)
 48    while app:
 49        app.flush()
 50    stderr.seek(0)
 51    stderr.truncate()
 52
 53    assert debugger._events
 54
 55    e = Event()
 56    app.fire(e)
 57    app.flush()
 58    stderr.seek(0)
 59    s = stderr.read().strip()
 60    assert s == str(e)
 61    stderr.seek(0)
 62    stderr.truncate()
 63
 64    debugger._events = False
 65    assert not debugger._events
 66
 67    e = Event()
 68    app.fire(e)
 69
 70    stderr.seek(0)
 71    s = stderr.read().strip()
 72    assert s == ""
 73    stderr.seek(0)
 74    stderr.truncate()
 75
 76
 77def test_file(tmpdir):
 78    logfile = str(tmpdir.ensure("debug.log"))
 79    stderr = open(logfile, "w+")
 80
 81    app = App()
 82    debugger = Debugger(file=stderr)
 83    debugger.register(app)
 84    while app:
 85        app.flush()
 86    stderr.seek(0)
 87    stderr.truncate()
 88
 89    assert debugger._events
 90
 91    e = Event()
 92    app.fire(e)
 93    app.flush()
 94
 95    stderr.seek(0)
 96    s = stderr.read().strip()
 97    assert s == str(e)
 98    stderr.seek(0)
 99    stderr.truncate()
100
101    debugger._events = False
102    assert not debugger._events
103
104    e = Event()
105    app.fire(e)
106
107    stderr.seek(0)
108    s = stderr.read().strip()
109    assert s == ""
110    stderr.seek(0)
111    stderr.truncate()
112
113
114def test_filename(tmpdir):
115    if "__pypy__" in sys.modules:
116        pytest.skip("Broken on pypy")
117
118    logfile = str(tmpdir.ensure("debug.log"))
119    stderr = open(logfile, "r+")
120
121    app = App()
122    debugger = Debugger(file=logfile)
123    debugger.register(app)
124    while app:
125        app.flush()
126    stderr.seek(0)
127    stderr.truncate()
128
129    assert debugger._events
130
131    e = Event()
132    app.fire(e)
133    app.flush()
134
135    stderr.seek(0)
136    s = stderr.read().strip()
137    assert s == str(e)
138    stderr.seek(0)
139    stderr.truncate()
140
141    debugger._events = False
142    assert not debugger._events
143
144    e = Event()
145    app.fire(e)
146
147    stderr.seek(0)
148    s = stderr.read().strip()
149    assert s == ""
150    stderr.seek(0)
151    stderr.truncate()
152
153
154def test_exceptions():
155    app = App()
156    stderr = StringIO()
157    debugger = Debugger(file=stderr)
158    debugger.register(app)
159    while app:
160        app.flush()
161    stderr.seek(0)
162    stderr.truncate()
163
164    assert debugger._events
165    assert debugger._errors
166
167    e = test(raiseException=True)
168    app.fire(e)
169    app.flush()
170
171    stderr.seek(0)
172    s = stderr.read().strip()
173    assert s == str(e)
174    stderr.seek(0)
175    stderr.truncate()
176
177    app.flush()
178    stderr.seek(0)
179    s = stderr.read().strip()
180    assert s.startswith("<exception[*]")
181    stderr.seek(0)
182    stderr.truncate()
183
184    debugger._events = False
185    debugger._errors = False
186
187    assert not debugger._events
188    assert not debugger._errors
189
190    e = test(raiseException=True)
191    app.fire(e)
192    app.flush()
193
194    stderr.seek(0)
195    s = stderr.read().strip()
196    assert s == ""
197    stderr.seek(0)
198    stderr.truncate()
199
200    app.flush()
201    stderr.seek(0)
202    s = stderr.read().strip()
203    assert s == ""
204
205
206def test_IgnoreEvents():
207    app = App()
208    stderr = StringIO()
209    debugger = Debugger(file=stderr)
210    debugger.register(app)
211    while app:
212        app.flush()
213    stderr.seek(0)
214    stderr.truncate()
215
216    assert debugger._events
217
218    debugger.IgnoreEvents.extend(["test"])
219
220    e = Event()
221    app.fire(e)
222    app.flush()
223
224    stderr.seek(0)
225    s = stderr.read().strip()
226    assert s == str(e)
227    stderr.seek(0)
228    stderr.truncate()
229
230    e = test()
231    app.fire(e)
232    app.flush()
233
234    stderr.seek(0)
235    s = stderr.read().strip()
236    assert s == ""
237    stderr.seek(0)
238    stderr.truncate()
239
240
241def test_IgnoreChannels():
242    app = App()
243    stderr = StringIO()
244    debugger = Debugger(file=stderr)
245    debugger.register(app)
246    while app:
247        app.flush()
248    stderr.seek(0)
249    stderr.truncate()
250
251    assert debugger._events
252    debugger.IgnoreChannels.extend([("*", "test")])
253
254    e = Event()
255    app.fire(e)
256    app.flush()
257
258    stderr.seek(0)
259    s = stderr.read().strip()
260    assert s == str(e)
261    stderr.seek(0)
262    stderr.truncate()
263
264    e = test()
265    app.fire(e)
266    app.flush()
267
268    stderr.seek(0)
269    s = stderr.read().strip()
270    assert s == ""
271    stderr.seek(0)
272    stderr.truncate()
273
274
275def test_Logger_debug():
276    app = App()
277    logger = Logger()
278    debugger = Debugger(logger=logger)
279    debugger.register(app)
280    while app:
281        app.flush()
282
283    e = Event()
284    app.fire(e)
285    app.flush()
286
287    assert logger.debug_msg == repr(e)
288
289
290def test_Logger_error():
291    app = App()
292    logger = Logger()
293    debugger = Debugger(logger=logger)
294    debugger.register(app)
295    while app:
296        app.flush()
297
298    e = test(raiseException=True)
299    app.fire(e)
300    while app:
301        app.flush()
302
303    assert logger.error_msg.startswith("ERROR <handler[*.test] (App.test)> (")