From 14f984fbcfaf60e76fe8006ef8a3323fd92b67bf Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Fri, 6 Nov 2020 13:09:01 -0800 Subject: [PATCH] improve xargs when running windows batch files --- pre_commit/xargs.py | 10 ++++++++++ tests/xargs_test.py | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/pre_commit/xargs.py b/pre_commit/xargs.py index 7538b54f6..60a057c19 100644 --- a/pre_commit/xargs.py +++ b/pre_commit/xargs.py @@ -137,6 +137,16 @@ def xargs( except parse_shebang.ExecutableNotFoundError as e: return e.to_output()[:2] + # on windows, batch files have a separate length limit than windows itself + if ( + sys.platform == 'win32' and + cmd[0].lower().endswith(('.bat', '.cmd')) + ): # pragma: win32 cover + # this is implementation details but the command gets translated into + # full/path/to/cmd.exe /c *cmd + cmd_exe = parse_shebang.find_executable('cmd.exe') + _max_length = 8192 - len(cmd_exe) - len(' /c ') + partitions = partition(cmd, varargs, target_concurrency, _max_length) def run_cmd_partition( diff --git a/tests/xargs_test.py b/tests/xargs_test.py index 4f6136ede..7e83ef590 100644 --- a/tests/xargs_test.py +++ b/tests/xargs_test.py @@ -195,3 +195,12 @@ def test_xargs_color_true_makes_tty(): ) assert retcode == 0 assert out == b'True\n' + + +@pytest.mark.xfail(os.name == 'posix', reason='nt only') +@pytest.mark.parametrize('filename', ('t.bat', 't.cmd', 'T.CMD')) +def test_xargs_with_batch_files(tmpdir, filename): + f = tmpdir.join(filename) + f.write('echo it works\n') + retcode, out = xargs.xargs((str(f),), ('x',) * 8192) + assert retcode == 0, (retcode, out)