Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/click/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,11 @@ def echo(
out = message

if nl:
out = out or ""
if isinstance(out, str):
out += "\n"
if isinstance(out, (bytes, bytearray)):
out = out + b"\n"
else:
out += b"\n"
out = out or ""
out += "\n"

if not out:
file.flush()
Expand Down
16 changes: 16 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from decimal import Decimal
from fractions import Fraction
from functools import partial
from io import BytesIO
from io import StringIO
from unittest.mock import patch

Expand Down Expand Up @@ -92,6 +93,21 @@ def cli():
assert outstreams[0].getvalue() == b"\x1b[31mx\x1b[39m"


@pytest.mark.parametrize("message_type", [bytes, bytearray])
def test_echo_empty_bytes(message_type, runner):
with runner.isolation() as outstreams:
click.echo(message_type())
assert outstreams[0].getvalue().replace(b"\r\n", b"\n") == b"\n"

f = BytesIO()
click.echo(message_type(), file=f)
assert f.getvalue() == b"\n"

f = BytesIO()
click.echo(message_type(), nl=False, file=f)
assert f.getvalue() == b""


def test_echo_custom_file():
f = StringIO()
click.echo("hello", file=f)
Expand Down
Loading