Used in standard hook tests to collect any logged events. Should be used in a with block to ensure that it has no impact after the test completes.
| 13 | |
| 14 | |
| 15 | class TestHook: |
| 16 | """Used in standard hook tests to collect any logged events. |
| 17 | |
| 18 | Should be used in a with block to ensure that it has no impact |
| 19 | after the test completes. |
| 20 | """ |
| 21 | |
| 22 | def __init__(self, raise_on_events=None, exc_type=RuntimeError): |
| 23 | self.raise_on_events = raise_on_events or () |
| 24 | self.exc_type = exc_type |
| 25 | self.seen = [] |
| 26 | self.closed = False |
| 27 | |
| 28 | def __enter__(self, *a): |
| 29 | sys.addaudithook(self) |
| 30 | return self |
| 31 | |
| 32 | def __exit__(self, *a): |
| 33 | self.close() |
| 34 | |
| 35 | def close(self): |
| 36 | self.closed = True |
| 37 | |
| 38 | @property |
| 39 | def seen_events(self): |
| 40 | return [i[0] for i in self.seen] |
| 41 | |
| 42 | def __call__(self, event, args): |
| 43 | if self.closed: |
| 44 | return |
| 45 | self.seen.append((event, args)) |
| 46 | if event in self.raise_on_events: |
| 47 | raise self.exc_type("saw event " + event) |
| 48 | |
| 49 | |
| 50 | # Simple helpers, since we are not in unittest here |
no outgoing calls
searching dependent graphs…