-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
222 additions
and
78 deletions.
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
Empty file.
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,76 @@ | ||
import warnings | ||
from contextlib import contextmanager | ||
from functools import wraps | ||
from inspect import signature | ||
from typing import ( | ||
Any, | ||
Iterator, | ||
) | ||
|
||
from syrupy.assertion import SnapshotAssertion | ||
|
||
|
||
@contextmanager | ||
def patch_pycharm_diff() -> Iterator[None]: | ||
""" | ||
Applies PyCharm diff patch to add Syrupy snapshot support. | ||
See: https://github.com/syrupy-project/syrupy/issues/675 | ||
""" | ||
|
||
try: | ||
from teamcity.diff_tools import EqualsAssertionError # type: ignore | ||
except ImportError: | ||
warnings.warn( | ||
"Failed to patch PyCharm's diff tools. Skipping patch.", | ||
stacklevel=2, | ||
) | ||
yield | ||
return | ||
|
||
old_init = EqualsAssertionError.__init__ | ||
old_init_signature = signature(old_init) | ||
|
||
@wraps(old_init) | ||
def new_init(self: "EqualsAssertionError", *args: Any, **kwargs: Any) -> None: | ||
|
||
# Extract the __init__ arguments as originally passed in order to | ||
# process them later | ||
parameters = old_init_signature.bind(self, *args, **kwargs) | ||
parameters.apply_defaults() | ||
|
||
expected = parameters.arguments["expected"] | ||
actual = parameters.arguments["actual"] | ||
real_exception = parameters.arguments["real_exception"] | ||
|
||
if isinstance(expected, SnapshotAssertion): | ||
snapshot = expected | ||
elif isinstance(actual, SnapshotAssertion): | ||
snapshot = actual | ||
else: | ||
snapshot = None | ||
|
||
old_init(self, *args, **kwargs) | ||
|
||
# No snapshot was involved in the assertion. Let the old logic do its | ||
# thing. | ||
if snapshot is None: | ||
return | ||
|
||
# Although a snapshot was involved in the assertion, it seems the error | ||
# was a result of a non-assertion exception (Ex. `assert 1/0`). | ||
# Therefore, We will not do anything here either. | ||
if real_exception is not None: | ||
return | ||
|
||
assertion_result = snapshot.executions[snapshot.num_executions - 1] | ||
if assertion_result.exception is not None: | ||
return | ||
|
||
self.expected = str(assertion_result.recalled_data) | ||
self.actual = str(assertion_result.asserted_data) | ||
|
||
try: | ||
EqualsAssertionError.__init__ = new_init | ||
yield | ||
finally: | ||
EqualsAssertionError.__init__ = old_init |
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,126 @@ | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
|
||
# EqualsAssertionError comes from: | ||
# https://github.com/JetBrains/intellij-community/blob/cd9bfbd98a7dca730fbc469156ce1ed30364afba/python/helpers/pycharm/teamcity/diff_tools.py#L53 | ||
@pytest.fixture | ||
def mock_teamcity_diff_tools(testdir: "pytest.Testdir"): | ||
teamcity_pkg = testdir.mkpydir("teamcity") | ||
diff_tools_file = teamcity_pkg / Path("diff_tools.py") | ||
diff_tools_file.write_text( | ||
""" | ||
class EqualsAssertionError: | ||
def __init__(self, expected, actual, msg=None, preformated=False, real_exception=None): # noqa: E501 | ||
self.real_exception = real_exception | ||
self.expected = expected | ||
self.actual = actual | ||
self.msg = str(msg) | ||
""", | ||
"utf-8", | ||
) | ||
|
||
|
||
@pytest.mark.filterwarnings("default") | ||
def test_logs_a_warning_if_unable_to_apply_patch(testdir): | ||
testdir.makepyfile( | ||
test_file=""" | ||
def test_case(snapshot): | ||
assert snapshot == [1, 2] | ||
""" | ||
) | ||
testdir.runpytest("-v", "--snapshot-update") | ||
testdir.makepyfile( | ||
test_file=""" | ||
def test_case(snapshot): | ||
assert snapshot == [1, 2, 3] | ||
""" | ||
) | ||
|
||
result = testdir.runpytest("-v", "--snapshot-patch-pycharm-diff") | ||
result.assert_outcomes(failed=1, passed=0, warnings=1) | ||
|
||
|
||
@pytest.mark.filterwarnings("default") | ||
def test_patches_pycharm_diff_tools_when_flag_set(testdir, mock_teamcity_diff_tools): | ||
# Generate initial snapshot | ||
testdir.makepyfile( | ||
test_file=""" | ||
def test_case(snapshot): | ||
assert snapshot == [1, 2] | ||
""" | ||
) | ||
testdir.runpytest("-v", "--snapshot-update") | ||
|
||
# Generate diff and mimic EqualsAssertionError being thrown | ||
testdir.makepyfile( | ||
test_file=""" | ||
def test_case(snapshot): | ||
try: | ||
assert snapshot == [1, 2, 3] | ||
except: | ||
from teamcity.diff_tools import EqualsAssertionError | ||
err = EqualsAssertionError(expected=snapshot, actual=[1,2,3]) | ||
print("Expected:", repr(err.expected)) | ||
print("Actual:", repr(err.actual)) | ||
raise | ||
""" | ||
) | ||
|
||
result = testdir.runpytest("-v", "--snapshot-patch-pycharm-diff") | ||
# No warnings because patch should have been successful | ||
result.assert_outcomes(failed=1, passed=0, warnings=0) | ||
|
||
result.stdout.re_match_lines( | ||
[ | ||
r"Expected: 'list([\n 1,\n 2,\n])'", | ||
# Actual is the amber-style list representation | ||
r"Actual: 'list([\n 1,\n 2,\n 3,\n])'", | ||
] | ||
) | ||
|
||
|
||
@pytest.mark.filterwarnings("default") | ||
def test_it_does_not_patch_pycharm_diff_tools_by_default( | ||
testdir, mock_teamcity_diff_tools | ||
): | ||
# Generate initial snapshot | ||
testdir.makepyfile( | ||
test_file=""" | ||
def test_case(snapshot): | ||
assert snapshot == [1, 2] | ||
""" | ||
) | ||
testdir.runpytest("-v", "--snapshot-update") | ||
|
||
# Generate diff and mimic EqualsAssertionError being thrown | ||
testdir.makepyfile( | ||
test_file=""" | ||
def test_case(snapshot): | ||
try: | ||
assert snapshot == [1, 2, 3] | ||
except: | ||
from teamcity.diff_tools import EqualsAssertionError | ||
err = EqualsAssertionError(expected=snapshot, actual=[1,2,3]) | ||
print("Expected:", repr(str(err.expected))) | ||
print("Actual:", repr(str(err.actual))) | ||
raise | ||
""" | ||
) | ||
|
||
result = testdir.runpytest("-v") | ||
# No warnings because patch should have been successful | ||
result.assert_outcomes(failed=1, passed=0, warnings=0) | ||
|
||
result.stdout.re_match_lines( | ||
[ | ||
r"Expected: 'list([\n 1,\n 2,\n])'", | ||
# Actual is the original list's repr. No newlines or amber-style list prefix | ||
r"Actual: '[1, 2, 3]'", | ||
] | ||
) |