Update xargs.partition with platform information

Change how xargs.partition computes the command length (including
arguments) depending on the plataform.
More specifically, 'win32' uses the amount of characters while posix
system uses the byte count.
This commit is contained in:
George Y. Kussumoto 2018-10-05 11:54:31 -03:00
parent c0b1f2ff25
commit fa4c03da65
2 changed files with 65 additions and 8 deletions

View file

@ -1,11 +1,29 @@
from __future__ import absolute_import
from __future__ import unicode_literals
from unittest import mock
import pytest
from pre_commit import xargs
@pytest.fixture
def sys_win32_mock():
return mock.Mock(
platform='win32',
getdefaultencoding=mock.Mock(return_value='utf-8'),
)
@pytest.fixture
def sys_linux_mock():
return mock.Mock(
platform='linux',
getdefaultencoding=mock.Mock(return_value='utf-8'),
)
def test_partition_trivial():
assert xargs.partition(('cmd',), ()) == (('cmd',),)
@ -35,6 +53,32 @@ def test_partition_limits():
)
def test_partition_limit_win32(sys_win32_mock):
cmd = ('ninechars',)
varargs = ('😑' * 10,)
with mock.patch('pre_commit.xargs.sys', sys_win32_mock):
ret = xargs.partition(cmd, varargs, _max_length=20)
assert ret == (cmd + varargs,)
def test_partition_limit_linux(sys_linux_mock):
cmd = ('ninechars',)
varargs = ('😑' * 5,)
with mock.patch('pre_commit.xargs.sys', sys_linux_mock):
ret = xargs.partition(cmd, varargs, _max_length=30)
assert ret == (cmd + varargs,)
def test_argument_too_long_with_large_unicode(sys_linux_mock):
cmd = ('ninechars',)
varargs = ('😑' * 10,) # 4 bytes * 10
with mock.patch('pre_commit.xargs.sys', sys_linux_mock):
with pytest.raises(xargs.ArgumentTooLongError):
xargs.partition(cmd, varargs, _max_length=20)
def test_argument_too_long():
with pytest.raises(xargs.ArgumentTooLongError):
xargs.partition(('a' * 5,), ('a' * 5,), _max_length=10)