X Tutup
Skip to content

Commit 14f984f

Browse files
committed
improve xargs when running windows batch files
1 parent c972205 commit 14f984f

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

pre_commit/xargs.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,16 @@ def xargs(
137137
except parse_shebang.ExecutableNotFoundError as e:
138138
return e.to_output()[:2]
139139

140+
# on windows, batch files have a separate length limit than windows itself
141+
if (
142+
sys.platform == 'win32' and
143+
cmd[0].lower().endswith(('.bat', '.cmd'))
144+
): # pragma: win32 cover
145+
# this is implementation details but the command gets translated into
146+
# full/path/to/cmd.exe /c *cmd
147+
cmd_exe = parse_shebang.find_executable('cmd.exe')
148+
_max_length = 8192 - len(cmd_exe) - len(' /c ')
149+
140150
partitions = partition(cmd, varargs, target_concurrency, _max_length)
141151

142152
def run_cmd_partition(

tests/xargs_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,12 @@ def test_xargs_color_true_makes_tty():
195195
)
196196
assert retcode == 0
197197
assert out == b'True\n'
198+
199+
200+
@pytest.mark.xfail(os.name == 'posix', reason='nt only')
201+
@pytest.mark.parametrize('filename', ('t.bat', 't.cmd', 'T.CMD'))
202+
def test_xargs_with_batch_files(tmpdir, filename):
203+
f = tmpdir.join(filename)
204+
f.write('echo it works\n')
205+
retcode, out = xargs.xargs((str(f),), ('x',) * 8192)
206+
assert retcode == 0, (retcode, out)

0 commit comments

Comments
 (0)
X Tutup