From c70fecea0d21b36af1b22f195bddb0ac36d7c233 Mon Sep 17 00:00:00 2001 From: Emmanuel Ogbizi-Ugbe Date: Fri, 30 Oct 2020 00:35:43 -0400 Subject: [PATCH 1/2] fix: assertion exception prints error --- src/syrupy/assertion.py | 16 ++++++++++++++++ .../extensions/amber/test_amber_matchers.py | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/syrupy/assertion.py b/src/syrupy/assertion.py index db2bddb2..1f77709c 100644 --- a/src/syrupy/assertion.py +++ b/src/syrupy/assertion.py @@ -1,3 +1,4 @@ +import traceback from gettext import gettext from typing import ( TYPE_CHECKING, @@ -34,6 +35,7 @@ class AssertionResult: created: bool = attr.ib() updated: bool = attr.ib() success: bool = attr.ib() + exception: Optional[Exception] = attr.ib() @property def final_data(self) -> Optional["SerializedData"]: @@ -112,6 +114,15 @@ def _serialize(self, data: "SerializableData") -> "SerializedData": def get_assert_diff(self) -> List[str]: assertion_result = self._execution_results[self.num_executions - 1] + if assertion_result.exception: + return [ + line + for lines in traceback.format_exception_only( + assertion_result.exception.__class__, + assertion_result.exception, + ) + for line in lines.splitlines() + ] snapshot_data = assertion_result.recalled_data serialized_data = assertion_result.asserted_data or "" diff: List[str] = [] @@ -162,6 +173,7 @@ def _assert(self, data: "SerializableData") -> bool: serialized_data: Optional["SerializedData"] = None matches = False assertion_success = False + assertion_exception = None try: snapshot_data = self._recall_data(index=self.num_executions) serialized_data = self._serialize(data) @@ -173,6 +185,9 @@ def _assert(self, data: "SerializableData") -> bool: ) assertion_success = True return assertion_success + except Exception as e: + assertion_exception = e + return False finally: snapshot_created = snapshot_data is None and assertion_success snapshot_updated = matches is False and assertion_success @@ -184,6 +199,7 @@ def _assert(self, data: "SerializableData") -> bool: success=assertion_success, created=snapshot_created, updated=snapshot_updated, + exception=assertion_exception, ) self._executions += 1 self._post_assert() diff --git a/tests/syrupy/extensions/amber/test_amber_matchers.py b/tests/syrupy/extensions/amber/test_amber_matchers.py index 93fd2780..303bcc99 100644 --- a/tests/syrupy/extensions/amber/test_amber_matchers.py +++ b/tests/syrupy/extensions/amber/test_amber_matchers.py @@ -43,7 +43,7 @@ def test_raises_unexpected_type(snapshot): "some_uuid": uuid.uuid4(), } assert actual == snapshot(matcher=path_type(**kwargs, strict=False)) - with pytest.raises(PathTypeError, match="does not match any of the expected"): + with pytest.raises(AssertionError, match="does not match any of the expected"): assert actual == snapshot(matcher=path_type(**kwargs)) From eb8513ea6890e9f68f1ed6e3dfe0a61232e25702 Mon Sep 17 00:00:00 2001 From: Emmanuel Ogbizi-Ugbe Date: Fri, 30 Oct 2020 00:46:26 -0400 Subject: [PATCH 2/2] chore: place exception on top the printed trace --- src/syrupy/assertion.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/syrupy/assertion.py b/src/syrupy/assertion.py index 1f77709c..9da99968 100644 --- a/src/syrupy/assertion.py +++ b/src/syrupy/assertion.py @@ -115,14 +115,17 @@ def _serialize(self, data: "SerializableData") -> "SerializedData": def get_assert_diff(self) -> List[str]: assertion_result = self._execution_results[self.num_executions - 1] if assertion_result.exception: - return [ + lines = [ line - for lines in traceback.format_exception_only( + for lines in traceback.format_exception( assertion_result.exception.__class__, assertion_result.exception, + assertion_result.exception.__traceback__, ) for line in lines.splitlines() ] + # Rotate to place exception with message at first line + return lines[-1:] + lines[:-1] snapshot_data = assertion_result.recalled_data serialized_data = assertion_result.asserted_data or "" diff: List[str] = []