Skip to content

Commit

Permalink
fix: assertion exception shows error at correct location (#402)
Browse files Browse the repository at this point in the history
* fix: assertion exception prints error

* chore: place exception on top the printed trace
  • Loading branch information
iamogbz committed Oct 30, 2020
1 parent 61a670f commit d46bba4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/syrupy/assertion.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import traceback
from gettext import gettext
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -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"]:
Expand Down Expand Up @@ -112,6 +114,18 @@ 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:
lines = [
line
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] = []
Expand Down Expand Up @@ -162,6 +176,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)
Expand All @@ -173,6 +188,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
Expand All @@ -184,6 +202,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()
Expand Down
2 changes: 1 addition & 1 deletion tests/syrupy/extensions/amber/test_amber_matchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))


Expand Down

0 comments on commit d46bba4

Please sign in to comment.