Repository: JonathonReinhart/scuba (python)

Issue:
utils.format_cmdline misses last line
It appears that utils.format_cmdline fails to yield the last accumulated line.

This also means that a better test could be written, which splits the result back out, and compares it to the list of input arguments. 

Pull request:
Fix missing final line in format_cmdline()
This fixes #41.

Patch:
diff --git a/scuba/__main__.py b/scuba/__main__.py
index 57f8671..4a54fae 100755
--- a/scuba/__main__.py
+++ b/scuba/__main__.py
@@ -217,7 +217,7 @@ def main(argv=None):
 
     if g_verbose or scuba_args.dry_run:
         appmsg('Docker command line:')
-        print(format_cmdline(run_args))
+        print('$ ' + format_cmdline(run_args))
 
     if scuba_args.dry_run:
         appmsg('Exiting for dry run. Temporary files not removed:')
diff --git a/scuba/utils.py b/scuba/utils.py
index f4d742b..0bd2e0e 100644
--- a/scuba/utils.py
+++ b/scuba/utils.py
@@ -3,13 +3,33 @@ try:
 except ImportError:
     from pipes import quote as shell_quote
 
+
 def format_cmdline(args, maxwidth=80):
+    '''Format args into a shell-quoted command line.
+
+    The result will be wrapped to maxwidth characters where possible,
+    not breaking a single long argument.
+    '''
+
+    # Leave room for the space and backslash at the end of each line
+    maxwidth -= 2
+
     def lines():
         line = ''
         for a in (shell_quote(a) for a in args):
-            if len(line) + len(a) > maxwidth:
+            # If adding this argument will make the line too long,
+            # yield the current line, and start a new one.
+            if len(line) + len(a) + 1 > maxwidth:
                 yield line
                 line = ''
-            line += ' ' + a
 
-    return '$' + ' \\\n'.join(lines())
+            # Append this argument to the current line, separating
+            # it by a space from the existing arguments.
+            if line:
+                line += ' ' + a
+            else:
+                line = a
+
+        yield line
+
+    return ' \\\n'.join(lines())


Test patch:
diff --git a/tests/test_utils.py b/tests/test_utils.py
new file mode 100644
index 0000000..ff9ad97
--- /dev/null
+++ b/tests/test_utils.py
@@ -0,0 +1,49 @@
+from __future__ import print_function
+
+from nose.tools import *
+from unittest import TestCase
+
+import logging
+import shlex
+from itertools import chain
+
+from .utils import *
+
+import scuba.utils
+
+
+class TestUtils(TestCase):
+
+    def _parse_cmdline(self, cmdline):
+        # Strip the formatting and whitespace
+        lines = [l.rstrip('\\').strip() for l in cmdline.splitlines()]
+
+        # Split each line, and return a flattened list of arguments
+        return chain.from_iterable(map(shlex.split, lines))
+
+    def _test_format_cmdline(self, args):
+
+        # Call the unit-under-test to get the formatted command line
+        result = scuba.utils.format_cmdline(args)
+
+        # Parse the result back out to a list of arguments
+        out_args = self._parse_cmdline(result)
+
+        # Verify that they match
+        assert_seq_equal(out_args, args)
+
+
+    def test_basic(self):
+        '''format_cmdline works as expected'''
+
+        self._test_format_cmdline([
+            'something',
+            '-a',
+            '-b',
+            '--long', 'option text',
+            '-s', 'hort',
+            'a very long argument here that will end up on its own line because it is so wide and nothing else will fit at the default width',
+            'and now',
+            'some', 'more', 'stuff',
+            'and even more stuff',
+        ])