Fix wrapping file paths in Writer.comment
authorAlex Vallée <avallee@google.com>
Fri, 23 Oct 2015 20:36:28 +0000 (16:36 -0400)
committerAlex Vallée <avallee@google.com>
Fri, 23 Oct 2015 20:36:28 +0000 (16:36 -0400)
Long file names, especially with hyphens will get incorrectly wrapped by
the comment method. Pass has_path=True to prevent this type of wrapping.

This is mainly so that longer path names can show up in comments on
their on line without breaking them up.

misc/ninja_syntax.py
misc/ninja_syntax_test.py

index 8673518..091e054 100644 (file)
@@ -21,8 +21,11 @@ class Writer(object):
     def newline(self):
         self.output.write('\n')
 
-    def comment(self, text):
-        for line in textwrap.wrap(text, self.width - 2):
+    def comment(self, text, has_path=False):
+        args = {}
+        if has_path:
+          args['break_long_words'] = args['break_on_hyphens'] = False
+        for line in textwrap.wrap(text, self.width - 2, **args):
             self.output.write('# ' + line + '\n')
 
     def variable(self, key, value, indent=0):
index 36b2e7b..46ce382 100755 (executable)
@@ -45,6 +45,17 @@ class TestLineWordWrap(unittest.TestCase):
                                       INDENT + 'y']) + '\n',
                          self.out.getvalue())
 
+    def test_comment_wrap(self):
+        # We should wrap the comments
+        self.n.comment('Hello there')
+        self.assertEqual('# Hello\n# there\n', self.out.getvalue())
+
+    def test_comment_wrap_filename(self):
+        # Filenames shoud not be wrapped
+        self.n.comment('Hello /usr/local/build-tools/bin', has_path=True)
+        self.assertEqual('# Hello\n# /usr/local/build-tools/bin\n',
+                         self.out.getvalue())
+
     def test_short_words_indented(self):
         # Test that indent is taking into acount when breaking subsequent lines.
         # The second line should not be '    to tree', as that's longer than the