From a8f6841eeefc2c0a3ff4cee574c1a45164f60030 Mon Sep 17 00:00:00 2001 From: Antonio Ossa Guerra Date: Fri, 11 Nov 2022 09:47:08 -0300 Subject: [PATCH] Minor improvements on code readability Adding comments on a non-intuitive behavior ar `_format_str_once`, and improving the `test_one_empty_line` test readability by using a pre-defined function that implements the same assertions Signed-off-by: Antonio Ossa Guerra --- src/black/__init__.py | 2 ++ tests/test_black.py | 5 +---- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/black/__init__.py b/src/black/__init__.py index db434584a2f..c411e88b995 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -1107,6 +1107,8 @@ def _format_str_once(src_contents: str, *, mode: Mode) -> str: for block in dst_blocks: dst_contents.extend(block.all_lines()) if mode.preview and not dst_contents: + # Use decode_bytes to retrieve the correct source newline (CRLF or LF), + # and check if normalized_content has more than one line normalized_content, _, newline = decode_bytes(src_contents.encode("utf-8")) if "\n" in normalized_content: return newline diff --git a/tests/test_black.py b/tests/test_black.py index ef2ddaf1c8e..7b46d41ccd4 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -159,10 +159,7 @@ def test_one_empty_line(self) -> None: mode = black.Mode(preview=True) for nl in ["\n", "\r\n"]: source = expected = nl - actual = fs(source, mode=mode) - self.assertFormatEqual(expected, actual) - black.assert_equivalent(source, actual) - black.assert_stable(source, actual, mode) + assert_format(source, expected, mode=mode) def test_one_empty_line_ff(self) -> None: mode = black.Mode(preview=True)