-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Support passing arbitrary arguments/context to custom extensions (Issue #700) #814
base: main
Are you sure you want to change the base?
Changes from 5 commits
343ae08
581a115
ac53835
829921a
c0238da
07a51cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,6 +63,7 @@ class SnapshotAssertion: | |
include: Optional["PropertyFilter"] = None | ||
exclude: Optional["PropertyFilter"] = None | ||
matcher: Optional["PropertyMatcher"] = None | ||
extra_args: Dict = field(default_factory=dict) | ||
|
||
_exclude: Optional["PropertyFilter"] = field( | ||
init=False, | ||
|
@@ -105,6 +106,7 @@ def __post_init__(self) -> None: | |
self._include = self.include | ||
self._exclude = self.exclude | ||
self._matcher = self.matcher | ||
self._extra_args = self.extra_args | ||
|
||
def __init_extension( | ||
self, extension_class: Type["AbstractSyrupyExtension"] | ||
|
@@ -178,6 +180,7 @@ def with_defaults( | |
include: Optional["PropertyFilter"] = None, | ||
matcher: Optional["PropertyMatcher"] = None, | ||
extension_class: Optional[Type["AbstractSyrupyExtension"]] = None, | ||
extra_args: Optional[Dict] = None | ||
) -> "SnapshotAssertion": | ||
""" | ||
Create new snapshot assertion fixture with provided values. This preserves | ||
|
@@ -191,6 +194,7 @@ def with_defaults( | |
test_location=self.test_location, | ||
extension_class=extension_class or self.extension_class, | ||
session=self.session, | ||
extra_args=extra_args or self.extra_args | ||
) | ||
|
||
def use_extension( | ||
|
@@ -264,6 +268,7 @@ def __call__( | |
extension_class: Optional[Type["AbstractSyrupyExtension"]] = None, | ||
matcher: Optional["PropertyMatcher"] = None, | ||
name: Optional["SnapshotIndex"] = None, | ||
extra_args: Optional[Dict] = None, | ||
) -> "SnapshotAssertion": | ||
""" | ||
Modifies assertion instance options | ||
|
@@ -280,6 +285,8 @@ def __call__( | |
self.__with_prop("_custom_index", name) | ||
if diff is not None: | ||
self.__with_prop("_snapshot_diff", diff) | ||
if extra_args: | ||
self._extra_args = extra_args | ||
return self | ||
|
||
def __repr__(self) -> str: | ||
|
@@ -300,6 +307,11 @@ def _assert(self, data: "SerializableData") -> bool: | |
matches = False | ||
assertion_success = False | ||
assertion_exception = None | ||
matcher_options = None | ||
for key,value in self._extra_args.items(): | ||
if key == "matcher_options": | ||
matcher_options = value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This enforces a certain schema in the extra_args object which if we want to support arbitrary args we shouldn't do. Instead, we should propagate the "extra_args" to all relevant methods. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed this. I also forwaded |
||
print("matcher_options", matcher_options) | ||
atharva-2001 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
try: | ||
snapshot_data, tainted = self._recall_data(index=self.index) | ||
serialized_data = self._serialize(data) | ||
|
@@ -316,7 +328,7 @@ def _assert(self, data: "SerializableData") -> bool: | |
not tainted | ||
and snapshot_data is not None | ||
and self.extension.matches( | ||
serialized_data=serialized_data, snapshot_data=snapshot_data | ||
serialized_data=serialized_data, snapshot_data=snapshot_data, **matcher_options | ||
) | ||
) | ||
assertion_success = matches | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
self.__with_prop
. It does seam auto-cleanup