1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
|
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, plugin_args):
testdir.makepyfile(
test_file="""
def test_case(snapshot):
assert snapshot == [1, 2]
"""
)
testdir.runpytest("-v", "--snapshot-update", *plugin_args)
testdir.makepyfile(
test_file="""
def test_case(snapshot):
assert snapshot == [1, 2, 3]
"""
)
result = testdir.runpytest("-v", "--snapshot-patch-pycharm-diff", *plugin_args)
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, plugin_args
):
# Generate initial snapshot
testdir.makepyfile(
test_file="""
def test_case(snapshot):
assert snapshot == [1, 2]
"""
)
testdir.runpytest("-v", "--snapshot-update", *plugin_args)
# 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", *plugin_args)
# 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_patches_pycharm_diff_tools_when_flag_set_and_snapshot_on_right(
testdir, mock_teamcity_diff_tools, plugin_args
):
# Generate initial snapshot
testdir.makepyfile(
test_file="""
def test_case(snapshot):
assert [1, 2] == snapshot
"""
)
testdir.runpytest("-v", "--snapshot-update", *plugin_args)
# Generate diff and mimic EqualsAssertionError being thrown
testdir.makepyfile(
test_file="""
def test_case(snapshot):
try:
assert [1, 2, 3] == snapshot
except:
from teamcity.diff_tools import EqualsAssertionError
err = EqualsAssertionError(expected=[1,2,3], actual=snapshot)
print("Expected:", repr(err.expected))
print("Actual:", repr(err.actual))
raise
"""
)
result = testdir.runpytest("-v", "--snapshot-patch-pycharm-diff", *plugin_args)
# 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, plugin_args
):
# Generate initial snapshot
testdir.makepyfile(
test_file="""
def test_case(snapshot):
assert snapshot == [1, 2]
"""
)
testdir.runpytest("-v", "--snapshot-update", *plugin_args)
# 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", *plugin_args)
# 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]'",
)
)
@pytest.mark.filterwarnings("default")
def test_it_has_no_impact_on_non_syrupy_assertions(
testdir, mock_teamcity_diff_tools, plugin_args
):
# Generate diff and mimic EqualsAssertionError being thrown
testdir.makepyfile(
test_file="""
def test_case():
try:
assert [1, 3] == [1, 2, 3]
except:
from teamcity.diff_tools import EqualsAssertionError
err = EqualsAssertionError(expected=[1,3], actual=[1,2,3])
print("Expected:", repr(str(err.expected)))
print("Actual:", repr(str(err.actual)))
raise
"""
)
result = testdir.runpytest("-v", *plugin_args)
# No warnings because patch should have been successful
result.assert_outcomes(failed=1, passed=0, warnings=0)
result.stdout.re_match_lines(
(
r"Expected: '[1, 3]'",
r"Actual: '[1, 2, 3]'",
)
)
@pytest.mark.filterwarnings("default")
def test_has_no_impact_on_real_exceptions_that_are_not_assertion_errors(
testdir, mock_teamcity_diff_tools, plugin_args
):
# Generate diff and mimic EqualsAssertionError being thrown
testdir.makepyfile(
test_file="""
def test_case():
try:
assert [1, 3] == (1/0)
except Exception as err:
from teamcity.diff_tools import EqualsAssertionError
err = EqualsAssertionError(
expected=[1,3],
actual=[1,2,3],
real_exception=err
)
print("Expected:", repr(str(err.expected)))
print("Actual:", repr(str(err.actual)))
raise
"""
)
result = testdir.runpytest("-v", *plugin_args)
# No warnings because patch should have been successful
result.assert_outcomes(failed=1, passed=0, warnings=0)
result.stdout.re_match_lines(
(
r"Expected: '[1, 3]'",
r"Actual: '[1, 2, 3]'",
)
)
|