changeset 193:32e8d51ec16c

util: make cmdbuilder() robust for faulty parsing of early options Also fixed handling of positional arguments.
author Yuya Nishihara <yuya@tcha.org>
date Sat, 11 Nov 2017 19:52:48 +0900
parents 7a84a8656679
children 22767a1e61ac
files hglib/util.py tests/test-log.py
diffstat 2 files changed, 24 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/hglib/util.py	Sat Nov 11 20:21:26 2017 +0900
+++ b/hglib/util.py	Sat Nov 11 19:52:48 2017 +0900
@@ -94,43 +94,50 @@
     True
     >>> cmdbuilder(b('cmd'), long=True) == [b('cmd'), b('--long')]
     True
-    >>> cmdbuilder(b('cmd'), str=b('s')) == [b('cmd'), b('--str'), b('s')]
+    >>> cmdbuilder(b('cmd'), s=b('hort')) == [b('cmd'), b('-short')]
+    True
+    >>> cmdbuilder(b('cmd'), str=b('s')) == [b('cmd'), b('--str=s')]
     True
     >>> cmdbuilder(b('cmd'), d_ash=True) == [b('cmd'), b('--d-ash')]
     True
     >>> cmdbuilder(b('cmd'), _=True) == [b('cmd'), b('-')]
     True
-    >>> expect = [b('cmd'), b('--list'), b('1'), b('--list'), b('2')]
+    >>> cmdbuilder(b('cmd'), l=[1, 2]) == [b('cmd'), b('-l1'), b('-l2')]
+    True
+    >>> expect = [b('cmd'), b('--list=1'), b('--list=2')]
     >>> cmdbuilder(b('cmd'), list=[1, 2]) == expect
     True
     >>> cmdbuilder(b('cmd'), None) == [b('cmd')]
     True
+    >>> cmdbuilder(b('cmd'), b('-a')) == [b('cmd'), b('--'), b('-a')]
+    True
     """
     cmd = [name]
     for arg, val in kwargs.items():
         if val is None:
             continue
 
-        arg = arg.encode('latin-1').replace(b('_'), b('-'))
+        arg = pfx = arg.encode('latin-1').replace(b('_'), b('-'))
         if arg != b('-'):
             if len(arg) == 1:
-                arg = b('-') + arg
+                arg = pfx = b('-') + arg
             else:
                 arg = b('--') + arg
+                pfx = arg + b('=')
         if isinstance(val, bool):
             if val:
                 cmd.append(arg)
         elif isinstance(val, list):
             for v in val:
-                cmd.append(arg)
-                cmd.append(_cmdval(v))
+                cmd.append(pfx + _cmdval(v))
         else:
-            cmd.append(arg)
-            cmd.append(_cmdval(val))
+            cmd.append(pfx + _cmdval(val))
 
+    args = [a for a in args if a is not None]
+    if args:
+        cmd.append(b('--'))
     for a in args:
-        if a is not None:
-            cmd.append(a)
+        cmd.append(a)
 
     return cmd
 
--- a/tests/test-log.py	Sat Nov 11 20:21:26 2017 +0900
+++ b/tests/test-log.py	Sat Nov 11 19:52:48 2017 +0900
@@ -20,6 +20,13 @@
 
         self.assertEquals(self.client.log(), self.client.log(hidden=True))
 
+    def test_dash_in_filename(self):
+        self.append('-a', '-a')
+        self.client.commit(b('first'), addremove=True)
+        revs = self.client.log(files=[b('-a')])
+        self.assertTrue(len(revs) == 1)
+        self.assertEquals(revs[0].rev, b('0'))
+
     # def test_errors(self):
     #     self.assertRaisesRegexp(CommandError, 'abort: unknown revision',
     #                             self.client.log, 'foo')