()
| 536 | |
| 537 | |
| 538 | def test_with_resource_exception() -> None: |
| 539 | class TestContext(AbstractContextManager[list[int]]): |
| 540 | _handle_exception: bool |
| 541 | _base_val: int |
| 542 | val: list[int] |
| 543 | |
| 544 | def __init__(self, base_val: int = 1, *, handle_exception: bool = True) -> None: |
| 545 | self._handle_exception = handle_exception |
| 546 | self._base_val = base_val |
| 547 | |
| 548 | def __enter__(self) -> list[int]: |
| 549 | self.val = [self._base_val] |
| 550 | return self.val |
| 551 | |
| 552 | def __exit__( |
| 553 | self, |
| 554 | exc_type: type[BaseException] | None, |
| 555 | exc_value: BaseException | None, |
| 556 | traceback: TracebackType | None, |
| 557 | ) -> bool | None: |
| 558 | if not exc_type: |
| 559 | self.val[0] = self._base_val - 1 |
| 560 | return None |
| 561 | |
| 562 | self.val[0] = self._base_val + 1 |
| 563 | return self._handle_exception |
| 564 | |
| 565 | class TestException(Exception): |
| 566 | pass |
| 567 | |
| 568 | ctx = click.Context(click.Command("test")) |
| 569 | |
| 570 | base_val = 1 |
| 571 | |
| 572 | with ctx.scope(): |
| 573 | rv = ctx.with_resource(TestContext(base_val=base_val)) |
| 574 | assert rv[0] == base_val |
| 575 | |
| 576 | assert rv == [base_val - 1] |
| 577 | |
| 578 | with ctx.scope(): |
| 579 | rv = ctx.with_resource(TestContext(base_val=base_val)) |
| 580 | raise TestException() |
| 581 | |
| 582 | assert rv == [base_val + 1] |
| 583 | |
| 584 | with pytest.raises(TestException): |
| 585 | with ctx.scope(): |
| 586 | rv = ctx.with_resource( |
| 587 | TestContext(base_val=base_val, handle_exception=False) |
| 588 | ) |
| 589 | raise TestException() |
| 590 | |
| 591 | |
| 592 | def test_with_resource_nested_exception() -> None: |
nothing calls this directly
no test coverage detected