Fix xargs.partition: win32 new string length computation

This commit is contained in:
George Y. Kussumoto 2018-10-09 22:54:41 -03:00
parent 2560280d21
commit c9e297ddb6
2 changed files with 5 additions and 5 deletions

View file

@ -17,9 +17,9 @@ def _command_length(*cmd):
full_cmd = ' '.join(cmd) full_cmd = ' '.join(cmd)
# win32 uses the amount of characters, more details at: # win32 uses the amount of characters, more details at:
# https://blogs.msdn.microsoft.com/oldnewthing/20031210-00/?p=41553/ # https://github.com/pre-commit/pre-commit/pull/839
if sys.platform == 'win32': if sys.platform == 'win32':
return len(full_cmd) return len(full_cmd.encode('utf-16le')) // 2
return len(full_cmd.encode(sys.getfilesystemencoding())) return len(full_cmd.encode(sys.getfilesystemencoding()))

View file

@ -55,7 +55,7 @@ def test_partition_limits():
def test_partition_limit_win32(sys_win32_mock): def test_partition_limit_win32(sys_win32_mock):
cmd = ('ninechars',) cmd = ('ninechars',)
varargs = (u'😑' * 10,) varargs = ('😑' * 10,)
with mock.patch('pre_commit.xargs.sys', sys_win32_mock): with mock.patch('pre_commit.xargs.sys', sys_win32_mock):
ret = xargs.partition(cmd, varargs, _max_length=20) ret = xargs.partition(cmd, varargs, _max_length=20)
@ -64,7 +64,7 @@ def test_partition_limit_win32(sys_win32_mock):
def test_partition_limit_linux(sys_linux_mock): def test_partition_limit_linux(sys_linux_mock):
cmd = ('ninechars',) cmd = ('ninechars',)
varargs = (u'😑' * 5,) varargs = ('😑' * 5,)
with mock.patch('pre_commit.xargs.sys', sys_linux_mock): with mock.patch('pre_commit.xargs.sys', sys_linux_mock):
ret = xargs.partition(cmd, varargs, _max_length=30) ret = xargs.partition(cmd, varargs, _max_length=30)
@ -73,7 +73,7 @@ def test_partition_limit_linux(sys_linux_mock):
def test_argument_too_long_with_large_unicode(sys_linux_mock): def test_argument_too_long_with_large_unicode(sys_linux_mock):
cmd = ('ninechars',) cmd = ('ninechars',)
varargs = (u'😑' * 10,) # 4 bytes * 10 varargs = ('😑' * 10,) # 4 bytes * 10
with mock.patch('pre_commit.xargs.sys', sys_linux_mock): with mock.patch('pre_commit.xargs.sys', sys_linux_mock):
with pytest.raises(xargs.ArgumentTooLongError): with pytest.raises(xargs.ArgumentTooLongError):
xargs.partition(cmd, varargs, _max_length=20) xargs.partition(cmd, varargs, _max_length=20)