Implement a simplified xargs in python

This commit is contained in:
Anthony Sottile 2016-03-21 19:30:47 -07:00
parent a5b56bd9e3
commit b7d395410b
13 changed files with 130 additions and 62 deletions

View file

@ -1,16 +0,0 @@
from __future__ import absolute_import
from __future__ import unicode_literals
from pre_commit.languages.helpers import file_args_to_stdin
def test_file_args_to_stdin_empty():
assert file_args_to_stdin([]) == ''
def test_file_args_to_stdin_some():
assert file_args_to_stdin(['foo', 'bar']) == 'foo\0bar\0'
def test_file_args_to_stdin_tuple():
assert file_args_to_stdin(('foo', 'bar')) == 'foo\0bar\0'

View file

@ -234,13 +234,13 @@ def test_pcre_hook_matching(tempdir_factory, store):
_test_hook_repo(
tempdir_factory, store, 'pcre_hooks_repo',
'regex-with-quotes', ['herp', 'derp'], b"herp:2:herpfoo'bard\n",
expected_return_code=123,
expected_return_code=1,
)
_test_hook_repo(
tempdir_factory, store, 'pcre_hooks_repo',
'other-regex', ['herp', 'derp'], b'derp:1:[INFO] information yo\n',
expected_return_code=123,
expected_return_code=1,
)
@ -255,7 +255,7 @@ def test_pcre_hook_case_insensitive_option(tempdir_factory, store):
_test_hook_repo(
tempdir_factory, store, 'pcre_hooks_repo',
'regex-with-grep-args', ['herp'], b'herp:1:FoOoOoObar\n',
expected_return_code=123,
expected_return_code=1,
)
@ -264,7 +264,7 @@ def test_pcre_hook_case_insensitive_option(tempdir_factory, store):
def test_pcre_many_files(tempdir_factory, store):
# This is intended to simulate lots of passing files and one failing file
# to make sure it still fails. This is not the case when naively using
# a system hook with `grep -H -n '...'` and expected_return_code=123.
# a system hook with `grep -H -n '...'` and expected_return_code=1.
path = git_dir(tempdir_factory)
with cwd(path):
with io.open('herp', 'w') as herp:
@ -275,7 +275,7 @@ def test_pcre_many_files(tempdir_factory, store):
'other-regex',
['/dev/null'] * 15000 + ['herp'],
b'herp:1:[INFO] info\n',
expected_return_code=123,
expected_return_code=1,
)
@ -355,7 +355,7 @@ def test_additional_python_dependencies_installed(tempdir_factory, store):
config = make_config_from_repo(path)
config['hooks'][0]['additional_dependencies'] = ['mccabe']
repo = Repository.create(config, store)
repo.run_hook(repo.hooks[0][1], [])
repo.require_installed()
with python.in_env(repo.cmd_runner, 'default'):
output = cmd_output('pip', 'freeze', '-l')[1]
assert 'mccabe' in output
@ -367,11 +367,11 @@ def test_additional_dependencies_roll_forward(tempdir_factory, store):
config = make_config_from_repo(path)
# Run the repo once without additional_dependencies
repo = Repository.create(config, store)
repo.run_hook(repo.hooks[0][1], [])
repo.require_installed()
# Now run it with additional_dependencies
config['hooks'][0]['additional_dependencies'] = ['mccabe']
repo = Repository.create(config, store)
repo.run_hook(repo.hooks[0][1], [])
repo.require_installed()
# We should see our additional dependency installed
with python.in_env(repo.cmd_runner, 'default'):
output = cmd_output('pip', 'freeze', '-l')[1]
@ -388,7 +388,7 @@ def test_additional_ruby_dependencies_installed(
config = make_config_from_repo(path)
config['hooks'][0]['additional_dependencies'] = ['thread_safe']
repo = Repository.create(config, store)
repo.run_hook(repo.hooks[0][1], [])
repo.require_installed()
with ruby.in_env(repo.cmd_runner, 'default'):
output = cmd_output('gem', 'list', '--local')[1]
assert 'thread_safe' in output
@ -405,7 +405,7 @@ def test_additional_node_dependencies_installed(
# Careful to choose a small package that's not depped by npm
config['hooks'][0]['additional_dependencies'] = ['lodash']
repo = Repository.create(config, store)
repo.run_hook(repo.hooks[0][1], [])
repo.require_installed()
with node.in_env(repo.cmd_runner, 'default'):
cmd_output('npm', 'config', 'set', 'global', 'true')
output = cmd_output('npm', 'ls')[1]

47
tests/xargs_test.py Normal file
View file

@ -0,0 +1,47 @@
from __future__ import absolute_import
from __future__ import unicode_literals
import pytest
from pre_commit import xargs
def test_partition_trivial():
assert xargs.partition(('cmd',), ()) == (('cmd',),)
def test_partition_simple():
assert xargs.partition(('cmd',), ('foo',)) == (('cmd', 'foo'),)
def test_partition_limits():
ret = xargs.partition(
('ninechars',), (
# Just match the end (with spaces)
'.' * 5, '.' * 4,
# Just match the end (single arg)
'.' * 10,
# Goes over the end
'.' * 5,
'.' * 6,
),
_max_length=20,
)
assert ret == (
('ninechars', '.' * 5, '.' * 4),
('ninechars', '.' * 10),
('ninechars', '.' * 5),
('ninechars', '.' * 6),
)
def test_argument_too_long():
with pytest.raises(xargs.ArgumentTooLongError):
xargs.partition(('a' * 5,), ('a' * 5,), _max_length=10)
def test_xargs_smoke():
ret, out, err = xargs.xargs(('echo',), ('hello', 'world'))
assert ret == 0
assert out == b'hello world\n'
assert err == b''