Skip to content

Commit

Permalink
fix ignoring packages that are imported after freezing
Browse files Browse the repository at this point in the history
  • Loading branch information
pegler committed Nov 19, 2021
1 parent 8994558 commit 8e81aa3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
17 changes: 14 additions & 3 deletions freezegun/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,11 @@ def __sub__(self, other):

@classmethod
def today(cls):
result = cls._date_to_freeze() + cls._tz_offset()
if _should_use_real_time():
result = real_date.today() + cls._tz_offset()
else:
result = cls._date_to_freeze() + cls._tz_offset()

return date_to_fakedate(result)

@staticmethod
Expand Down Expand Up @@ -383,7 +387,11 @@ def timestamp(self):

@classmethod
def now(cls, tz=None):
now = cls._time_to_freeze() or real_datetime.now()
if _should_use_real_time():
now = real_datetime.now()
else:
now = cls._time_to_freeze() or real_datetime.now()

if tz:
result = tz.fromutc(now.replace(tzinfo=tz)) + cls._tz_offset()
else:
Expand All @@ -407,7 +415,10 @@ def today(cls):

@classmethod
def utcnow(cls):
result = cls._time_to_freeze() or real_datetime.utcnow()
if _should_use_real_time():
result = real_datetime.utcnow()
else:
result = cls._time_to_freeze() or real_datetime.utcnow()
return datetime_to_fakedatetime(result)

@staticmethod
Expand Down
15 changes: 15 additions & 0 deletions tests/another_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,18 @@ def get_fake_gmtime():

def get_fake_strftime():
return fake_strftime


# Getters


def get_datetime_now():
return datetime.now()


def get_datetime_utcnow():
return datetime.utcnow()


def get_date_today():
return date.today()
25 changes: 24 additions & 1 deletion tests/test_class_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,30 @@ def test_import_after_start():
assert another_module.get_fake_localtime() is fake_localtime
assert another_module.get_fake_gmtime() is fake_gmtime
assert another_module.get_fake_strftime() is fake_strftime
del sys.modules['tests.another_module']

del sys.modules["tests.another_module"]
del sys.modules["tests"]
del another_module


def test_ignore_import_after_start():
with freeze_time("2012-01-14", ignore=["tests.another_module"]):
assert "tests.another_module" not in sys.modules.keys()
from tests import another_module

utcnow = another_module.get_datetime_utcnow()
assert utcnow != FakeDatetime(2012, 1, 14)

now = another_module.get_datetime_now()
assert now != FakeDatetime(2012, 1, 14)

today = another_module.get_date_today()
assert today != FakeDate(2012, 1, 14)

del sys.modules["tests.another_module"]
del sys.modules["tests"]
del another_module


def test_none_as_initial():
with freeze_time() as ft:
Expand Down

0 comments on commit 8e81aa3

Please sign in to comment.