Patchwork [2,of,3,hglib] util: make cmdbuilder() robust for faulty parsing of early options

login
register
mail settings
Submitter Yuya Nishihara
Date Nov. 11, 2017, 11:41 a.m.
Message ID <cd73c3ee469e22216fbb.1510400491@mimosa>
Download mbox | patch
Permalink /patch/25477/
State Accepted
Headers show

Comments

Yuya Nishihara - Nov. 11, 2017, 11:41 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1510397568 -32400
#      Sat Nov 11 19:52:48 2017 +0900
# Node ID cd73c3ee469e22216fbb9116701eed7059c1833c
# Parent  519dfa937de37d1441e2a39da76d700c7099b0c7
util: make cmdbuilder() robust for faulty parsing of early options

Also fixed handling of positional arguments.

Patch

diff --git a/hglib/util.py b/hglib/util.py
--- a/hglib/util.py
+++ b/hglib/util.py
@@ -94,43 +94,50 @@  def cmdbuilder(name, *args, **kwargs):
     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
 
diff --git a/tests/test-log.py b/tests/test-log.py
--- a/tests/test-log.py
+++ b/tests/test-log.py
@@ -20,6 +20,13 @@  class test_log(common.basetest):
 
         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')