Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix alembic.util.messaging.msg to properly wrap at terminal width #1385

Closed
wants to merge 3 commits into from
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
12 changes: 9 additions & 3 deletions alembic/util/messaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,17 @@ def msg(
write_outstream(sys.stdout, "\n")
else:
# left indent output lines
lines = textwrap.wrap(msg, TERMWIDTH)
indent = " "
lines = textwrap.wrap(
msg,
TERMWIDTH,
initial_indent=indent,
subsequent_indent=indent,
)
if len(lines) > 1:
for line in lines[0:-1]:
write_outstream(sys.stdout, " ", line, "\n")
write_outstream(sys.stdout, " ", lines[-1], ("\n" if newline else ""))
write_outstream(sys.stdout, line, "\n")
write_outstream(sys.stdout, lines[-1], ("\n" if newline else ""))
if flush:
sys.stdout.flush()

Expand Down
6 changes: 6 additions & 0 deletions docs/build/unreleased/1384.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.. change::
:tags: bug, commands
:tickets: 1384

Fixed bug in alembic command stdout where long messages were not properly
wrapping at the terminal width.
6 changes: 0 additions & 6 deletions tests/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,6 @@ def test_plain_current(self):
with self._assert_lines(["a3"]):
command.current(self.cfg)

def test_current_obfuscate_password(self):
eq_(
util.obfuscate_url_pw("postgresql://scott:tiger@localhost/test"),
"postgresql://scott:***@localhost/test",
)

def test_two_heads(self):
command.stamp(self.cfg, ())
command.stamp(self.cfg, (self.a1.revision, self.b1.revision))
Expand Down
27 changes: 27 additions & 0 deletions tests/test_messaging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from io import StringIO

from alembic.testing import eq_
from alembic.testing import mock
from alembic.testing.fixtures import TestBase
from alembic.util.messaging import msg
from alembic.util.messaging import obfuscate_url_pw


class MessagingTest(TestBase):
def test_msg_wraps(self):
buf = StringIO()
with mock.patch("sys.stdout", buf), mock.patch(
"alembic.util.messaging.TERMWIDTH", 10
):
msg("AAAAAAAAAAAAAAAAA")
assert str(buf.getvalue()).splitlines() == [
" AAAAAAAA", # initial indent with 10 chars before wrapping
" AAAAAAAA", # subsequent indent with 10 chars before wrapping
" A", # subsequent indent with remainining chars
]

def test_current_obfuscate_password(self):
eq_(
obfuscate_url_pw("postgresql://scott:tiger@localhost/test"),
"postgresql://scott:***@localhost/test",
)