Skip to content

Commit 7c7ec36

Browse files
committed
#2447 Split resource exception handling tests in single and nested
1 parent 92129c5 commit 7c7ec36

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

tests/test_context.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,41 @@ class TestException(Exception):
478478
)
479479
raise TestException()
480480

481+
482+
def test_with_resource_nested_exception() -> None:
483+
class TestContext(AbstractContextManager[list[int]]):
484+
_handle_exception: bool
485+
_base_val: int
486+
val: list[int]
487+
488+
def __init__(self, base_val: int = 1, *, handle_exception: bool = True) -> None:
489+
self._handle_exception = handle_exception
490+
self._base_val = base_val
491+
492+
def __enter__(self) -> list[int]:
493+
self.val = [self._base_val]
494+
return self.val
495+
496+
def __exit__(
497+
self,
498+
exc_type: type[BaseException] | None,
499+
exc_value: BaseException | None,
500+
traceback: TracebackType | None,
501+
) -> bool | None:
502+
if not exc_type:
503+
self.val[0] = self._base_val - 1
504+
return None
505+
506+
self.val[0] = self._base_val + 1
507+
return self._handle_exception
508+
509+
class TestException(Exception):
510+
pass
511+
512+
ctx = click.Context(click.Command("test"))
513+
base_val = 1
481514
base_val_nested = 11
515+
482516
with ctx.scope():
483517
rv = ctx.with_resource(TestContext(base_val=base_val))
484518
rv_nested = ctx.with_resource(TestContext(base_val=base_val_nested))

0 commit comments

Comments
 (0)