|
| 1 | +# -*- coding: utf-8 -*- |
1 | 2 | from __future__ import absolute_import |
2 | 3 | from __future__ import unicode_literals |
3 | 4 |
|
| 5 | +import sys |
| 6 | + |
| 7 | +import mock |
4 | 8 | import pytest |
| 9 | +import six |
5 | 10 |
|
6 | 11 | from pre_commit import xargs |
7 | 12 |
|
8 | 13 |
|
| 14 | +@pytest.fixture |
| 15 | +def win32_py2_mock(): |
| 16 | + with mock.patch.object(sys, 'getfilesystemencoding', return_value='utf-8'): |
| 17 | + with mock.patch.object(sys, 'platform', 'win32'): |
| 18 | + with mock.patch.object(six, 'PY2', True): |
| 19 | + yield |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def win32_py3_mock(): |
| 24 | + with mock.patch.object(sys, 'getfilesystemencoding', return_value='utf-8'): |
| 25 | + with mock.patch.object(sys, 'platform', 'win32'): |
| 26 | + with mock.patch.object(six, 'PY2', False): |
| 27 | + yield |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture |
| 31 | +def linux_mock(): |
| 32 | + with mock.patch.object(sys, 'getfilesystemencoding', return_value='utf-8'): |
| 33 | + with mock.patch.object(sys, 'platform', 'linux'): |
| 34 | + yield |
| 35 | + |
| 36 | + |
9 | 37 | def test_partition_trivial(): |
10 | 38 | assert xargs.partition(('cmd',), ()) == (('cmd',),) |
11 | 39 |
|
@@ -35,6 +63,35 @@ def test_partition_limits(): |
35 | 63 | ) |
36 | 64 |
|
37 | 65 |
|
| 66 | +def test_partition_limit_win32_py3(win32_py3_mock): |
| 67 | + cmd = ('ninechars',) |
| 68 | + # counted as half because of utf-16 encode |
| 69 | + varargs = ('π' * 5,) |
| 70 | + ret = xargs.partition(cmd, varargs, _max_length=20) |
| 71 | + assert ret == (cmd + varargs,) |
| 72 | + |
| 73 | + |
| 74 | +def test_partition_limit_win32_py2(win32_py2_mock): |
| 75 | + cmd = ('ninechars',) |
| 76 | + varargs = ('π' * 5,) # 4 bytes * 5 |
| 77 | + ret = xargs.partition(cmd, varargs, _max_length=30) |
| 78 | + assert ret == (cmd + varargs,) |
| 79 | + |
| 80 | + |
| 81 | +def test_partition_limit_linux(linux_mock): |
| 82 | + cmd = ('ninechars',) |
| 83 | + varargs = ('π' * 5,) |
| 84 | + ret = xargs.partition(cmd, varargs, _max_length=30) |
| 85 | + assert ret == (cmd + varargs,) |
| 86 | + |
| 87 | + |
| 88 | +def test_argument_too_long_with_large_unicode(linux_mock): |
| 89 | + cmd = ('ninechars',) |
| 90 | + varargs = ('π' * 10,) # 4 bytes * 10 |
| 91 | + with pytest.raises(xargs.ArgumentTooLongError): |
| 92 | + xargs.partition(cmd, varargs, _max_length=20) |
| 93 | + |
| 94 | + |
38 | 95 | def test_argument_too_long(): |
39 | 96 | with pytest.raises(xargs.ArgumentTooLongError): |
40 | 97 | xargs.partition(('a' * 5,), ('a' * 5,), _max_length=10) |
|
0 commit comments