Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve xargs when running windows batch files #1686

Merged
merged 1 commit into from Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions pre_commit/xargs.py
Expand Up @@ -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(
Expand Down
9 changes: 9 additions & 0 deletions tests/xargs_test.py
Expand Up @@ -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)