| 1 |
import contextlib |
| 2 |
import datetime |
| 3 |
import sys |
| 4 |
import types |
| 5 |
import warnings |
| 6 |
from typing import Iterator |
| 7 |
|
| 8 |
from freezegun import freeze_time |
| 9 |
|
| 10 |
|
| 11 |
class ModuleWithWarning: |
| 12 |
""" |
| 13 |
A module that triggers warnings on attribute access. |
| 14 |
|
| 15 |
This does not happen with regular modules, there has to be a bit of lazy |
| 16 |
module magic going on in order for this to happen. |
| 17 |
|
| 18 |
Examples of modules that uses this pattern in real projects can be found at: |
| 19 |
|
| 20 |
py.code - the compiler package import causes a warning to be emitted: |
| 21 |
https://github.com/pytest-dev/py/blob/67987e26aadddbbe7d1ec76c16ea9be346ae9811/py/__init__.py |
| 22 |
https://github.com/pytest-dev/py/blob/67987e26aadddbbe7d1ec76c16ea9be346ae9811/py/_code/_assertionold.py#L3 |
| 23 |
|
| 24 |
celery.task - the sets module is listed in __all__ in celery.task and freeze_time accesses it: |
| 25 |
https://github.com/celery/celery/blob/46c92025cdec07a4a30ad44901cf66cb27346638/celery/task/__init__.py |
| 26 |
https://github.com/celery/celery/blob/46c92025cdec07a4a30ad44901cf66cb27346638/celery/task/sets.py |
| 27 |
""" |
| 28 |
__name__ = 'module_with_warning' |
| 29 |
__dict__ = {} |
| 30 |
warning_triggered = False |
| 31 |
counter = 0 |
| 32 |
|
| 33 |
@property |
| 34 |
def attribute_that_emits_a_warning(self) -> None: |
| 35 |
|
| 36 |
self.__class__.counter += 1 |
| 37 |
warnings.warn(f'this is test warning #{self.__class__.counter}') |
| 38 |
self.warning_triggered = True |
| 39 |
|
| 40 |
|
| 41 |
@contextlib.contextmanager |
| 42 |
def assert_module_with_emitted_warning() -> Iterator[None]: |
| 43 |
"""Install a module that triggers warnings into sys.modules and ensure the |
| 44 |
warning was triggered in the with-block. """ |
| 45 |
module = sys.modules['module_with_warning'] = ModuleWithWarning() |
| 46 |
|
| 47 |
try: |
| 48 |
yield |
| 49 |
finally: |
| 50 |
del sys.modules['module_with_warning'] |
| 51 |
|
| 52 |
assert module.warning_triggered |
| 53 |
|
| 54 |
|
| 55 |
@contextlib.contextmanager |
| 56 |
def assert_no_warnings() -> Iterator[None]: |
| 57 |
"""A context manager that makes sure no warnings was emitted.""" |
| 58 |
with warnings.catch_warnings(record=True) as caught_warnings: |
| 59 |
warnings.filterwarnings('always') |
| 60 |
yield |
| 61 |
assert not caught_warnings |
| 62 |
|
| 63 |
|
| 64 |
def test_ignore_warnings_in_start() -> None: |
| 65 |
"""Make sure that modules being introspected in start() does not emit warnings.""" |
| 66 |
with assert_module_with_emitted_warning(): |
| 67 |
freezer = freeze_time(datetime.datetime(2016, 10, 27, 9, 56)) |
| 68 |
|
| 69 |
try: |
| 70 |
with assert_no_warnings(): |
| 71 |
freezer.start() |
| 72 |
|
| 73 |
finally: |
| 74 |
freezer.stop() |
| 75 |
|
| 76 |
|
| 77 |
def test_ignore_warnings_in_stop() -> None: |
| 78 |
"""Make sure that modules that was loaded after start() does not trigger |
| 79 |
warnings in stop()""" |
| 80 |
freezer = freeze_time(datetime.datetime(2016, 10, 27, 9, 56)) |
| 81 |
freezer.start() |
| 82 |
|
| 83 |
with assert_module_with_emitted_warning(): |
| 84 |
with assert_no_warnings(): |
| 85 |
freezer.stop() |