Skip to content

Commit

Permalink
Merge pull request #3720 from aknrdureegaesr/test_helper_improvement
Browse files Browse the repository at this point in the history
Fix chdir leaking on error out of cd-contextmanager of tests/helper.py.
  • Loading branch information
Kwpolska authored Nov 14, 2023
2 parents 54056f8 + 192de5e commit 8689cf3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
8 changes: 5 additions & 3 deletions tests/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
@contextmanager
def cd(path):
old_dir = os.getcwd()
os.chdir(path)
yield
os.chdir(old_dir)
try:
os.chdir(path)
yield
finally:
os.chdir(old_dir)


class FakeSite:
Expand Down
22 changes: 22 additions & 0 deletions tests/test_test_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import os

from .helper import cd


class SomeTestError(Exception):
"""An arbitrary error to be thrown by the test."""
pass


def test_test_helper():
"""Check that the cd test helper duly resets the directory even in spite of an error."""
old_dir = os.getcwd()
exception_seen = False
try:
with cd(".."):
raise SomeTestError("Just raising an exception, as failing tests sometimes do.")
except SomeTestError:
now_dir = os.getcwd()
assert old_dir == now_dir
exception_seen = True
assert exception_seen

0 comments on commit 8689cf3

Please sign in to comment.