Skip to content

Commit

Permalink
Minor improvements on code readability
Browse files Browse the repository at this point in the history
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 <aaossa@uc.cl>
  • Loading branch information
aaossa committed Nov 11, 2022
1 parent c842d58 commit a8f6841
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/black/__init__.py
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions tests/test_black.py
Expand Up @@ -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)
Expand Down

0 comments on commit a8f6841

Please sign in to comment.