generated from tophat/new-project-kit
-
Notifications
You must be signed in to change notification settings - Fork 37
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
fix: check current session's pending-write queue when recalling snapshots (e.g. diffing) #927
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dbf395a
fix: check current session's pending-write queue when recalling snaps…
huonw 0fb10fd
Make PyTestLocation hashable
huonw 0f694ca
Explicitly set methodname to None for doctests
huonw c577ba6
Queue writes with a dict for O(1) look-ups
huonw 55c4995
Use type aliases
huonw 9a3c9f0
return both keys from _snapshot_write_queue_key
huonw 817de52
Use a defaultdict
huonw a86f5d8
Update comments
huonw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ | |
from syrupy.constants import PYTEST_NODE_SEP | ||
|
||
|
||
@dataclass | ||
@dataclass(frozen=True) | ||
class PyTestLocation: | ||
item: "pytest.Item" | ||
nodename: Optional[str] = field(init=False) | ||
|
@@ -23,27 +23,42 @@ class PyTestLocation: | |
filepath: str = field(init=False) | ||
|
||
def __post_init__(self) -> None: | ||
# NB. we're in a frozen dataclass, but need to transform the values that the caller | ||
# supplied... we do so by (ab)using object.__setattr__ to forcibly set the attributes. (See | ||
# rejected PEP-0712 for an example of a better way to handle this.) | ||
# | ||
# This is safe because this all happens during initialization: `self` hasn't been hashed | ||
# (or, e.g., stored in a dict), so the mutation won't be noticed. | ||
if self.is_doctest: | ||
return self.__attrs_post_init_doc__() | ||
self.__attrs_post_init_def__() | ||
|
||
def __attrs_post_init_def__(self) -> None: | ||
node_path: Path = getattr(self.item, "path") # noqa: B009 | ||
self.filepath = str(node_path.absolute()) | ||
# See __post_init__ for discussion of object.__setattr__ | ||
object.__setattr__(self, "filepath", str(node_path.absolute())) | ||
obj = getattr(self.item, "obj") # noqa: B009 | ||
self.modulename = obj.__module__ | ||
self.methodname = obj.__name__ | ||
self.nodename = getattr(self.item, "name", None) | ||
self.testname = self.nodename or self.methodname | ||
object.__setattr__(self, "modulename", obj.__module__) | ||
object.__setattr__(self, "methodname", obj.__name__) | ||
object.__setattr__(self, "nodename", getattr(self.item, "name", None)) | ||
object.__setattr__(self, "testname", self.nodename or self.methodname) | ||
|
||
def __attrs_post_init_doc__(self) -> None: | ||
doctest = getattr(self.item, "dtest") # noqa: B009 | ||
self.filepath = doctest.filename | ||
# See __post_init__ for discussion of object.__setattr__ | ||
object.__setattr__(self, "filepath", doctest.filename) | ||
test_relfile, test_node = self.nodeid.split(PYTEST_NODE_SEP) | ||
test_relpath = Path(test_relfile) | ||
self.modulename = ".".join([*test_relpath.parent.parts, test_relpath.stem]) | ||
self.nodename = test_node.replace(f"{self.modulename}.", "") | ||
self.testname = self.nodename or self.methodname | ||
object.__setattr__( | ||
self, | ||
"modulename", | ||
".".join([*test_relpath.parent.parts, test_relpath.stem]), | ||
) | ||
object.__setattr__(self, "methodname", None) | ||
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. Setting this attribute is newly added: it previously wasn't being set on this codepath, and attempting to hash/compare the location values (to put into the queue dictionary) was blowing up when accessing it. It seems like it was previously not read at all for doc tests? |
||
object.__setattr__( | ||
self, "nodename", test_node.replace(f"{self.modulename}.", "") | ||
) | ||
object.__setattr__(self, "testname", self.nodename or self.methodname) | ||
|
||
@property | ||
def classname(self) -> Optional[str]: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import pytest | ||
|
||
_TEST = """ | ||
def test_foo(snapshot): | ||
assert {**base} == snapshot(name="a") | ||
assert {**base, **extra} == snapshot(name="b", diff="a") | ||
""" | ||
|
||
|
||
def _make_file(testdir, base, extra): | ||
testdir.makepyfile( | ||
test_file="\n\n".join([f"base = {base!r}", f"extra = {extra!r}", _TEST]) | ||
) | ||
|
||
|
||
def _run_test(testdir, base, extra, expected_update_lines): | ||
_make_file(testdir, base=base, extra=extra) | ||
|
||
# Run with --snapshot-update, to generate/update snapshots: | ||
result = testdir.runpytest( | ||
"-v", | ||
"--snapshot-update", | ||
) | ||
result.stdout.re_match_lines((expected_update_lines,)) | ||
assert result.ret == 0 | ||
|
||
# Run without --snapshot-update, to validate the snapshots are actually up-to-date | ||
result = testdir.runpytest("-v") | ||
result.stdout.re_match_lines((r"2 snapshots passed\.",)) | ||
assert result.ret == 0 | ||
|
||
|
||
def test_diff_lifecycle(testdir) -> pytest.Testdir: | ||
# first: create both snapshots completely from scratch | ||
_run_test( | ||
testdir, | ||
base={"A": 1}, | ||
extra={"X": 10}, | ||
expected_update_lines=r"2 snapshots generated\.", | ||
) | ||
|
||
# second: edit the base data, to change the data for both snapshots (only changes the serialized output for the base snapshot `a`). | ||
_run_test( | ||
testdir, | ||
base={"A": 1, "B": 2}, | ||
extra={"X": 10}, | ||
expected_update_lines=r"1 snapshot passed. 1 snapshot updated\.", | ||
) | ||
|
||
# third: edit just the extra data (only changes the serialized output for the diff snapshot `b`) | ||
_run_test( | ||
testdir, | ||
base={"A": 1, "B": 2}, | ||
extra={"X": 10, "Y": 20}, | ||
expected_update_lines=r"1 snapshot passed. 1 snapshot updated\.", | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I note that #606 added this
session_id
parameter, which I imagine was meant to help with circumstances like this, but it seems like it ends up ignored/unused in practice?cache_key
: https://github.com/tophat/syrupy/blob/ef8189c68460593fead9c484405b755e272c8cca/src/syrupy/extensions/amber/__init__.py#L57-L66session_id
: https://github.com/tophat/syrupy/blob/ef8189c68460593fead9c484405b755e272c8cca/src/syrupy/extensions/single_file.py#L92-L103And, in practice, I think it may be quite hard to use, since the relevant cached data is held in memory in the session itself (i.e. the extension
_read_snapshot_data_from_location
method calls don't really have access to it at all).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.
The session_id is used as the cache_key argument in Amber's
__cacheable_read_snapshot
method. Although it's not used in the function, it's used by the lru_cache decorator (which caches based on the kwargs I believe). So it essentially invalidates the cache