Skip to content

Commit

Permalink
Merge pull request #370 from lukaszb/feature/configure-default-ignore…
Browse files Browse the repository at this point in the history
…-list

Allow to configure default ignore list
  • Loading branch information
boxed authored Oct 7, 2020
2 parents b9e15e2 + 1d8326f commit fe3a032
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 15 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ Patches and Suggestions
- `Dan Elkis <github.com/rinslow>`_
- `Bastien Vallet <github.com/djailla>`_
- `Julian Mehnle <github.com/jmehnle>`_
- `Lukasz Balcerzak <https://github.com/lukaszb>`_
52 changes: 52 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,55 @@ On Debian systems:
.. code-block:: bash
$ sudo apt-get install python-freezegun
Ignore packages
---------------

Sometimes it's desired to ignore FreezeGun behaviour for particular packages (i.e. libraries).
It's possible to ignore them for a single invocation:


.. code-block:: python
from freezegun import freeze_time
with freeze_time('2020-10-06', ignore=['threading']):
# ...
By default FreezeGun ignores following packages:

.. code-block:: python
[
'nose.plugins',
'six.moves',
'django.utils.six.moves',
'google.gax',
'threading',
'Queue',
'selenium',
'_pytest.terminal.',
'_pytest.runner.',
'gi',
]
It's possible to set your own default ignore list:

.. code-block:: python
import freezegun
freezegun.configure(default_ignore_list=['threading', 'tensorflow'])
Please note this will override default ignore list. If you want to extend existing defaults
please use:

.. code-block:: python
import freezegun
freezegun.configure(extend_ignore_list=['tensorflow'])
3 changes: 2 additions & 1 deletion freezegun/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""
from .api import freeze_time
from .config import configure

__title__ = 'freezegun'
__version__ = '1.0.0'
Expand All @@ -15,4 +16,4 @@
__copyright__ = 'Copyright 2012 Steve Pulec'


__all__ = ["freeze_time"]
__all__ = ["freeze_time", "configure"]
26 changes: 13 additions & 13 deletions freezegun/api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from . import config
import dateutil
import datetime
import functools
Expand Down Expand Up @@ -809,19 +810,18 @@ def freeze_time(time_to_freeze=None, tz_offset=0, ignore=None, tick=False, as_ar
if ignore is None:
ignore = []
ignore = ignore[:]
ignore.extend(['nose.plugins',
'six.moves',
'django.utils.six.moves',
'google.gax',
'threading',
'Queue',
'selenium',
'_pytest.terminal.',
'_pytest.runner.',
'gi',
])

return _freeze_time(time_to_freeze, tz_offset, ignore, tick, as_arg, as_kwarg, auto_tick_seconds)
if config.settings.default_ignore_list:
ignore.extend(config.settings.default_ignore_list)

return _freeze_time(
time_to_freeze_str=time_to_freeze,
tz_offset=tz_offset,
ignore=ignore,
tick=tick,
as_arg=as_arg,
as_kwarg=as_kwarg,
auto_tick_seconds=auto_tick_seconds,
)


# Setup adapters for sqlite
Expand Down
40 changes: 40 additions & 0 deletions freezegun/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@


DEFAULT_IGNORE_LIST = [
'nose.plugins',
'six.moves',
'django.utils.six.moves',
'google.gax',
'threading',
'Queue',
'selenium',
'_pytest.terminal.',
'_pytest.runner.',
'gi',
]


class Settings:
def __init__(self, default_ignore_list=None):
self.default_ignore_list = default_ignore_list or DEFAULT_IGNORE_LIST[:]


settings = Settings()


class ConfigurationError(Exception):
pass


def configure(default_ignore_list=None, extend_ignore_list=None):
if default_ignore_list is not None and extend_ignore_list is not None:
raise ConfigurationError("Either default_ignore_list or extend_ignore_list might be given, not both")
if default_ignore_list:
settings.default_ignore_list = default_ignore_list
if extend_ignore_list:
settings.default_ignore_list = [*settings.default_ignore_list, *extend_ignore_list]


def reset_config():
global settings
settings = Settings()
65 changes: 65 additions & 0 deletions tests/test_configure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from unittest import mock
import freezegun
import freezegun.config


def setup_function():
freezegun.config.reset_config()


def teardown_function():
freezegun.config.reset_config()


def test_default_ignore_list_is_overridden():
freezegun.configure(default_ignore_list=['threading', 'tensorflow'])

with mock.patch("freezegun.api._freeze_time.__init__", return_value=None) as _freeze_time_init_mock:

freezegun.freeze_time("2020-10-06")

expected_ignore_list = [
'threading',
'tensorflow',
]

_freeze_time_init_mock.assert_called_once_with(
time_to_freeze_str="2020-10-06",
tz_offset=0,
ignore=expected_ignore_list,
tick=False,
as_arg=False,
as_kwarg='',
auto_tick_seconds=0,
)

def test_extend_default_ignore_list():
freezegun.configure(extend_ignore_list=['tensorflow'])

with mock.patch("freezegun.api._freeze_time.__init__", return_value=None) as _freeze_time_init_mock:

freezegun.freeze_time("2020-10-06")

expected_ignore_list = [
'nose.plugins',
'six.moves',
'django.utils.six.moves',
'google.gax',
'threading',
'Queue',
'selenium',
'_pytest.terminal.',
'_pytest.runner.',
'gi',
'tensorflow',
]

_freeze_time_init_mock.assert_called_once_with(
time_to_freeze_str="2020-10-06",
tz_offset=0,
ignore=expected_ignore_list,
tick=False,
as_arg=False,
as_kwarg='',
auto_tick_seconds=0,
)
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
envlist = py35, py36, py37, py38, pypy3

[testenv]
commands = pytest --cov
commands = pytest --cov {posargs}
deps = -rrequirements.txt

0 comments on commit fe3a032

Please sign in to comment.