MCPcopy
hub / github.com/pallets/click / test_with_resource_exception

Function test_with_resource_exception

tests/test_context.py:538–589  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

536
537
538def 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
592def test_with_resource_nested_exception() -> None:

Callers

nothing calls this directly

Calls 4

scopeMethod · 0.95
with_resourceMethod · 0.95
TestContextClass · 0.85
TestExceptionClass · 0.85

Tested by

no test coverage detected