|
2 | 2 | from __future__ import absolute_import |
3 | 3 | from __future__ import unicode_literals |
4 | 4 |
|
| 5 | +import sys |
| 6 | + |
5 | 7 | import mock |
6 | 8 | import pytest |
| 9 | +import six |
7 | 10 |
|
8 | 11 | from pre_commit import xargs |
9 | 12 |
|
10 | 13 |
|
11 | 14 | @pytest.fixture |
12 | | -def sys_win32_mock(): |
13 | | - return mock.Mock( |
14 | | - platform='win32', |
15 | | - getfilesystemencoding=mock.Mock(return_value='utf-8'), |
16 | | - ) |
| 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 |
17 | 20 |
|
18 | 21 |
|
19 | 22 | @pytest.fixture |
20 | | -def sys_linux_mock(): |
21 | | - return mock.Mock( |
22 | | - platform='linux', |
23 | | - getfilesystemencoding=mock.Mock(return_value='utf-8'), |
24 | | - ) |
| 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 |
25 | 35 |
|
26 | 36 |
|
27 | 37 | def test_partition_trivial(): |
@@ -53,31 +63,33 @@ def test_partition_limits(): |
53 | 63 | ) |
54 | 64 |
|
55 | 65 |
|
56 | | -def test_partition_limit_win32(sys_win32_mock): |
| 66 | +def test_partition_limit_win32_py3(win32_py3_mock): |
57 | 67 | cmd = ('ninechars',) |
58 | 68 | # counted as half because of utf-16 encode |
59 | 69 | varargs = ('😑' * 5,) |
60 | | - with mock.patch('pre_commit.xargs.sys', sys_win32_mock): |
61 | | - ret = xargs.partition(cmd, varargs, _max_length=20) |
| 70 | + ret = xargs.partition(cmd, varargs, _max_length=20) |
| 71 | + assert ret == (cmd + varargs,) |
| 72 | + |
62 | 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) |
63 | 78 | assert ret == (cmd + varargs,) |
64 | 79 |
|
65 | 80 |
|
66 | | -def test_partition_limit_linux(sys_linux_mock): |
| 81 | +def test_partition_limit_linux(linux_mock): |
67 | 82 | cmd = ('ninechars',) |
68 | 83 | varargs = ('😑' * 5,) |
69 | | - with mock.patch('pre_commit.xargs.sys', sys_linux_mock): |
70 | | - ret = xargs.partition(cmd, varargs, _max_length=30) |
71 | | - |
| 84 | + ret = xargs.partition(cmd, varargs, _max_length=30) |
72 | 85 | assert ret == (cmd + varargs,) |
73 | 86 |
|
74 | 87 |
|
75 | | -def test_argument_too_long_with_large_unicode(sys_linux_mock): |
| 88 | +def test_argument_too_long_with_large_unicode(linux_mock): |
76 | 89 | cmd = ('ninechars',) |
77 | 90 | varargs = ('😑' * 10,) # 4 bytes * 10 |
78 | | - with mock.patch('pre_commit.xargs.sys', sys_linux_mock): |
79 | | - with pytest.raises(xargs.ArgumentTooLongError): |
80 | | - xargs.partition(cmd, varargs, _max_length=20) |
| 91 | + with pytest.raises(xargs.ArgumentTooLongError): |
| 92 | + xargs.partition(cmd, varargs, _max_length=20) |
81 | 93 |
|
82 | 94 |
|
83 | 95 | def test_argument_too_long(): |
|
0 commit comments