]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
fix alembic.util.messaging.msg to properly wrap at terminal width
authorSaif Hakim <saif@benchling.com>
Fri, 5 Jan 2024 15:38:36 +0000 (10:38 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 13 Jan 2024 15:32:28 +0000 (10:32 -0500)
Fixed bug in alembic command stdout where long messages were not properly
wrapping at the terminal width.   Pull request courtesy Saif Hakim.

Fixes: #1384
Closes: #1385
Pull-request: https://github.com/sqlalchemy/alembic/pull/1385
Pull-request-sha: ff59fa59861487cee1943090acc970d9a64f7e96

Change-Id: Ia584d840ca1a9a01e224b92dc4cdcae880ea13aa

alembic/util/messaging.py
docs/build/unreleased/1384.rst [new file with mode: 0644]
tests/test_command.py
tests/test_messaging.py [new file with mode: 0644]

index 5f14d597554e281ee098afbbb0f2864cec0fd2e8..6618fa7faae4ce996622aeeb0d2aad5fc2ed51ac 100644 (file)
@@ -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()
 
diff --git a/docs/build/unreleased/1384.rst b/docs/build/unreleased/1384.rst
new file mode 100644 (file)
index 0000000..91e6ea2
--- /dev/null
@@ -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.   Pull request courtesy Saif Hakim.
index 48e7af3af8011cec1a15082d2a9e9dc0423f9369..c665f95556deed81c09b8ff9c405c620191e4f53 100644 (file)
@@ -323,12 +323,6 @@ class CurrentTest(_BufMixin, TestBase):
         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 (file)
index 0000000..cfd450a
--- /dev/null
@@ -0,0 +1,30 @@
+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")
+        eq_(
+            str(buf.getvalue()).splitlines(),
+            [
+                "  AAAAAAAA",  # initial indent 10 chars before wrapping
+                "  AAAAAAAA",  # subsequent indent 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",
+        )