From ce19b5249280bacf0b20ceaab81221eefac9c483 Mon Sep 17 00:00:00 2001 From: Saif Hakim Date: Thu, 4 Jan 2024 19:54:48 -0800 Subject: [PATCH 1/3] fix alembic.util.messaging.msg to properly wrap at terminal width Instead of applying indent manually after using textwrap, simply use the `textwrap.wrap`'s `initial_indent` / `subsequent_indent` that will taken into account when wrapping to the specified `width` Fixes: #1384 --- alembic/util/messaging.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/alembic/util/messaging.py b/alembic/util/messaging.py index 5f14d597..6618fa7f 100644 --- a/alembic/util/messaging.py +++ b/alembic/util/messaging.py @@ -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() From 3ce8e83c725373a7a73ca933ca13b98dbad63b8f Mon Sep 17 00:00:00 2001 From: Saif Hakim Date: Thu, 4 Jan 2024 20:34:00 -0800 Subject: [PATCH 2/3] add tests --- tests/test_command.py | 6 ------ tests/test_messaging.py | 27 +++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 tests/test_messaging.py diff --git a/tests/test_command.py b/tests/test_command.py index 48e7af3a..c665f955 100644 --- a/tests/test_command.py +++ b/tests/test_command.py @@ -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)) diff --git a/tests/test_messaging.py b/tests/test_messaging.py new file mode 100644 index 00000000..f202b22a --- /dev/null +++ b/tests/test_messaging.py @@ -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", + ) From ff59fa59861487cee1943090acc970d9a64f7e96 Mon Sep 17 00:00:00 2001 From: Saif Hakim Date: Fri, 5 Jan 2024 07:06:20 -0800 Subject: [PATCH 3/3] add release notes --- docs/build/unreleased/1384.rst | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 docs/build/unreleased/1384.rst diff --git a/docs/build/unreleased/1384.rst b/docs/build/unreleased/1384.rst new file mode 100644 index 00000000..a1af7292 --- /dev/null +++ b/docs/build/unreleased/1384.rst @@ -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.