Skip to content

Commit

Permalink
Handling of exception groups
Browse files Browse the repository at this point in the history
  • Loading branch information
cjw296 committed Jun 5, 2024
1 parent 9caee25 commit 9d63918
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: 2.1

orbs:
python: cjw296/python-ci@4.0.1
python: cjw296/python-ci@4.1

jobs:
check-package:
Expand Down
2 changes: 2 additions & 0 deletions docs/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ testfixtures.comparison

.. autofunction:: testfixtures.comparison.compare_exception

.. autofunction:: testfixtures.comparison.compare_exception_group

.. autofunction:: testfixtures.comparison.compare_with_type

.. autofunction:: testfixtures.comparison.compare_sequence
Expand Down
21 changes: 20 additions & 1 deletion testfixtures/comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import re

from testfixtures import not_there, singleton
from testfixtures.compat import PY_311_PLUS
from testfixtures.resolve import resolve
from testfixtures.utils import indent
from testfixtures.mock import parent_name, mock_call
Expand Down Expand Up @@ -489,7 +490,25 @@ def _short_repr(obj) -> str:
Path: compare_path,
datetime: compare_with_fold,
time: compare_with_fold,
}
}

if PY_311_PLUS:

def compare_exception_group(
x: BaseExceptionGroup, y: BaseExceptionGroup, context: 'CompareContext'
) -> Optional[str]:
"""
Compare the two supplied exception groups
"""

x_msg, x_excs = x.args
y_msg, y_excs = y.args
msg_different = context.different(x_msg, y_msg, 'msg')
excs_different = context.different(x_excs, y_excs, 'excs')
if msg_different or excs_different:
return 'exception group not as expected:'

_registry[BaseExceptionGroup] = compare_exception_group


def register(type_: type, comparer: Comparer):
Expand Down
26 changes: 25 additions & 1 deletion testfixtures/tests/test_should_raise.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from textwrap import dedent

import pytest

from testfixtures import Comparison as C, ShouldRaise, should_raise
from unittest import TestCase

from ..compat import PY_311_PLUS
from ..shouldraise import ShouldAssert


Expand Down Expand Up @@ -36,7 +39,6 @@ def test_wrong_text(self):
+assert False""")



class TestShouldRaise(TestCase):

def test_no_params(self):
Expand Down Expand Up @@ -301,4 +303,26 @@ def __repr__(self):
with ShouldRaise(MessageError('foo')):
raise MessageError('foo', None)

@pytest.mark.skipif(not PY_311_PLUS, reason="requires python3.11 or higher")
def test_exception_group_okay(self):
with ShouldRaise(ExceptionGroup('foo', [Exception('bar')])):
raise ExceptionGroup('foo', [Exception('bar')])

@pytest.mark.skipif(not PY_311_PLUS, reason="requires python3.11 or higher")
def test_exception_group_different(self):
with ShouldAssert(
"exception group not as expected:\n\n"
"While comparing msg: 'fob' (expected) != 'foo' (raised)\n\n"
"While comparing excs: sequence not as expected:\n\n"
"same:\n"
"[]\n\n"
"expected:\n"
"[Exception('baz')]\n\n"
"raised:\n"
"[Exception('bar')]\n\n"
"While comparing excs[0]: "
"Exception('baz') (expected) != Exception('bar') (raised)"
):
with ShouldRaise(ExceptionGroup('fob', [Exception('baz')])):
raise ExceptionGroup('foo', [Exception('bar')])

0 comments on commit 9d63918

Please sign in to comment.