Skip to content
Myr

#2447 Added exception forwarding to context tests

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Michele Bianchi · committed by Michele Bianchi · parent 555fa9b · Browse files · 1 file+93 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+93 added−0 removed

  • tests:+93 added−0 removed

1 file+93 added−0 removed

modified:tests/test_context.py+93 added
1import logging2from contextlib import AbstractContextManager3from contextlib import contextmanager4from types import TracebackType56import pytest78import click
415 unmodified lines
424425 assert rv == [0]426427428def test_with_resource_exception() -> None:429 class TestContext(AbstractContextManager[list[int]]):430 _handle_exception: bool431 _base_val: int432 val: list[int]433434 def __init__(self, base_val: int = 1, *, handle_exception: bool = True) -> None:435 self._handle_exception = handle_exception436 self._base_val = base_val437438 def __enter__(self) -> list[int]:439 self.val = [self._base_val]440 return self.val441442 def __exit__(443 self,444 exc_type: type[BaseException] | None,445 exc_value: BaseException | None,446 traceback: TracebackType | None,447 ) -> bool | None:448 if not exc_type:449 self.val[0] = self._base_val - 1450 return None451452 self.val[0] = self._base_val + 1453 return self._handle_exception454455 class TestException(Exception):456 pass457458 ctx = click.Context(click.Command("test"))459460 base_val = 1461462 with ctx.scope():463 rv = ctx.with_resource(TestContext(base_val=base_val))464 assert rv[0] == base_val465466 assert rv == [base_val - 1]467468 with ctx.scope():469 rv = ctx.with_resource(TestContext(base_val=base_val))470 raise TestException()471472 assert rv == [base_val + 1]473474 with pytest.raises(TestException):475 with ctx.scope():476 rv = ctx.with_resource(477 TestContext(base_val=base_val, handle_exception=False)478 )479 raise TestException()480481 base_val_nested = 11482 with ctx.scope():483 rv = ctx.with_resource(TestContext(base_val=base_val))484 rv_nested = ctx.with_resource(TestContext(base_val=base_val_nested))485 assert rv[0] == base_val486 assert rv_nested[0] == base_val_nested487488 assert rv == [base_val - 1]489 assert rv_nested == [base_val_nested - 1]490491 with ctx.scope():492 rv = ctx.with_resource(TestContext(base_val=base_val))493 rv_nested = ctx.with_resource(TestContext(base_val=base_val_nested))494 raise TestException()495496 # If one of the context "eats" the exceptions they will not be forwarded to other497 # parts. This is due to how ExitStack unwinding works498 assert rv_nested == [base_val_nested + 1]499 assert rv == [base_val - 1]500501 with ctx.scope():502 rv = ctx.with_resource(TestContext(base_val=base_val))503 rv_nested = ctx.with_resource(504 TestContext(base_val=base_val_nested, handle_exception=False)505 )506 raise TestException()507508 assert rv_nested == [base_val_nested + 1]509 assert rv == [base_val + 1]510511 with pytest.raises(TestException):512 rv = ctx.with_resource(TestContext(base_val=base_val, handle_exception=False))513 rv_nested = ctx.with_resource(514 TestContext(base_val=base_val_nested, handle_exception=False)515 )516 raise TestException()517518519def test_make_pass_decorator_args(runner):520 """521 Test to check that make_pass_decorator doesn't consume arguments based on522 invocation order.
116 unmodified lines