diff --git a/src/syrupy/extensions/amber/serializer.py b/src/syrupy/extensions/amber/serializer.py index b5414b11..802b460b 100644 --- a/src/syrupy/extensions/amber/serializer.py +++ b/src/syrupy/extensions/amber/serializer.py @@ -1,5 +1,6 @@ import collections import inspect +import textwrap from collections import OrderedDict from collections.abc import Callable, Generator, Iterable from types import ( @@ -184,6 +185,9 @@ def __read_file_with_markers( snapshot_data = "" elif test_name is not None and line.startswith(cls._indent): snapshot_data += line[indent_len:] + elif test_name is not None and line.strip() == "": + # Remove trailing whitespace (but not newline character) from empty line. + snapshot_data += textwrap.dedent(line) except FileNotFoundError: pass else: diff --git a/tests/syrupy/extensions/amber/__snapshots__/test_amber_newlines.ambr b/tests/syrupy/extensions/amber/__snapshots__/test_amber_newlines.ambr index dc9ec053..233c9546 100644 --- a/tests/syrupy/extensions/amber/__snapshots__/test_amber_newlines.ambr +++ b/tests/syrupy/extensions/amber/__snapshots__/test_amber_newlines.ambr @@ -4,6 +4,14 @@ Line2 Line3 + +# --- +# name: test_multiline_repr_with_no_indentation + Line1 + Line2 + + Line3 + # --- # name: test_trailing_2_newlines_in_repr ReprWithNewline diff --git a/tests/syrupy/extensions/amber/test_amber_newlines.py b/tests/syrupy/extensions/amber/test_amber_newlines.py index 4eeff97d..7b7841eb 100644 --- a/tests/syrupy/extensions/amber/test_amber_newlines.py +++ b/tests/syrupy/extensions/amber/test_amber_newlines.py @@ -26,9 +26,16 @@ def __repr__(self) -> str: "Line1", "Line2\n", # extra newline "Line3 ", # with an extra space + "", # newline at end ] ) def test_multiline_repr(snapshot): assert MultilineRepr() == snapshot + + +def test_multiline_repr_with_no_indentation(snapshot): + # snapshot file has been manually edited + # remove newlines from this test if you update the snapshot + assert MultilineRepr() == snapshot