Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pendulum.test to properly unwind after an exception #445

Merged
merged 1 commit into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pendulum/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ def _sign(x):
@contextmanager
def test(mock):
set_test_now(mock)

yield

set_test_now()
try:
yield
finally:
set_test_now()


def set_test_now(test_now=None):
Expand Down
17 changes: 17 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,20 @@ def test_week_ends_at_invalid_value():

with pytest.raises(ValueError):
pendulum.week_ends_at(11)


def test_with_test():
t = pendulum.datetime(2000, 1, 1)

with pendulum.test(t):
assert pendulum.now() == t

assert pendulum.now() != t

# Also make sure that it restores things after an exception
with pytest.raises(RuntimeError):
with pendulum.test(t):
assert pendulum.now() == t
raise RuntimeError

assert pendulum.now() != t