1010import pytest
1111import six
1212
13+ from pre_commit import parse_shebang
1314from pre_commit import xargs
1415
1516
17+ @pytest .mark .parametrize (
18+ ('env' , 'expected' ),
19+ (
20+ ({}, 0 ),
21+ ({b'x' : b'1' }, 12 ),
22+ ({b'x' : b'12' }, 13 ),
23+ ({b'x' : b'1' , b'y' : b'2' }, 24 ),
24+ ),
25+ )
26+ def test_environ_size (env , expected ):
27+ # normalize integer sizing
28+ assert xargs ._environ_size (_env = env ) == expected
29+
30+
1631@pytest .fixture
1732def win32_py2_mock ():
1833 with mock .patch .object (sys , 'getfilesystemencoding' , return_value = 'utf-8' ):
@@ -56,7 +71,7 @@ def test_partition_limits():
5671 '.' * 6 ,
5772 ),
5873 1 ,
59- _max_length = 20 ,
74+ _max_length = 21 ,
6075 )
6176 assert ret == (
6277 ('ninechars' , '.' * 5 , '.' * 4 ),
@@ -70,21 +85,21 @@ def test_partition_limit_win32_py3(win32_py3_mock):
7085 cmd = ('ninechars' ,)
7186 # counted as half because of utf-16 encode
7287 varargs = ('😑' * 5 ,)
73- ret = xargs .partition (cmd , varargs , 1 , _max_length = 20 )
88+ ret = xargs .partition (cmd , varargs , 1 , _max_length = 21 )
7489 assert ret == (cmd + varargs ,)
7590
7691
7792def test_partition_limit_win32_py2 (win32_py2_mock ):
7893 cmd = ('ninechars' ,)
7994 varargs = ('😑' * 5 ,) # 4 bytes * 5
80- ret = xargs .partition (cmd , varargs , 1 , _max_length = 30 )
95+ ret = xargs .partition (cmd , varargs , 1 , _max_length = 31 )
8196 assert ret == (cmd + varargs ,)
8297
8398
8499def test_partition_limit_linux (linux_mock ):
85100 cmd = ('ninechars' ,)
86101 varargs = ('😑' * 5 ,)
87- ret = xargs .partition (cmd , varargs , 1 , _max_length = 30 )
102+ ret = xargs .partition (cmd , varargs , 1 , _max_length = 31 )
88103 assert ret == (cmd + varargs ,)
89104
90105
@@ -134,9 +149,9 @@ def test_xargs_smoke():
134149 assert err == b''
135150
136151
137- exit_cmd = ( 'bash' , '-c' , 'exit $1' , '--' )
152+ exit_cmd = parse_shebang . normalize_cmd (( 'bash' , '-c' , 'exit $1' , '--' ) )
138153# Abuse max_length to control the exit code
139- max_length = len (' ' .join (exit_cmd )) + 2
154+ max_length = len (' ' .join (exit_cmd )) + 3
140155
141156
142157def test_xargs_negate ():
@@ -165,14 +180,14 @@ def test_xargs_retcode_normal():
165180
166181
167182def test_xargs_concurrency ():
168- bash_cmd = ( 'bash' , '-c' )
183+ bash_cmd = parse_shebang . normalize_cmd (( 'bash' , '-c' ) )
169184 print_pid = ('sleep 0.5 && echo $$' ,)
170185
171186 start = time .time ()
172187 ret , stdout , _ = xargs .xargs (
173188 bash_cmd , print_pid * 5 ,
174189 target_concurrency = 5 ,
175- _max_length = len (' ' .join (bash_cmd + print_pid )),
190+ _max_length = len (' ' .join (bash_cmd + print_pid )) + 1 ,
176191 )
177192 elapsed = time .time () - start
178193 assert ret == 0
0 commit comments