Merge pull request #901 from pre-commit/no_asdict

Use Hook api in languages
This commit is contained in:
Anthony Sottile
2018-12-31 13:27:32 -08:00
committed by GitHub
18 changed files with 41 additions and 38 deletions

View File

@@ -39,13 +39,12 @@ from pre_commit.languages import system
# 'default'.
# """
#
# def run_hook(prefix, hook, file_args):
# def run_hook(hook, file_args):
# """Runs a hook and returns the returncode and output of running that
# hook.
#
# Args:
# prefix - `Prefix` bound to the repository.
# hook - Hook dictionary
# hook - `Hook`
# file_args - The files to be run
#
# Returns:

View File

@@ -85,15 +85,15 @@ def docker_cmd():
)
def run_hook(prefix, hook, file_args): # pragma: windows no cover
def run_hook(hook, file_args): # pragma: windows no cover
assert_docker_available()
# Rebuild the docker image in case it has gone missing, as many people do
# automated cleanup of docker images.
build_docker_image(prefix, pull=False)
build_docker_image(hook.prefix, pull=False)
hook_cmd = helpers.to_cmd(hook)
entry_exe, cmd_rest = hook_cmd[0], hook_cmd[1:]
entry_tag = ('--entrypoint', entry_exe, docker_tag(prefix))
entry_tag = ('--entrypoint', entry_exe, docker_tag(hook.prefix))
cmd = docker_cmd() + entry_tag + cmd_rest
return helpers.run_xargs(hook, cmd, file_args)

View File

@@ -12,7 +12,7 @@ healthy = helpers.basic_healthy
install_environment = helpers.no_install
def run_hook(prefix, hook, file_args): # pragma: windows no cover
def run_hook(hook, file_args): # pragma: windows no cover
assert_docker_available()
cmd = docker_cmd() + helpers.to_cmd(hook)
return helpers.run_xargs(hook, cmd, file_args)

View File

@@ -9,7 +9,7 @@ healthy = helpers.basic_healthy
install_environment = helpers.no_install
def run_hook(prefix, hook, file_args):
out = hook['entry'].encode('UTF-8') + b'\n\n'
def run_hook(hook, file_args):
out = hook.entry.encode('UTF-8') + b'\n\n'
out += b'\n'.join(f.encode('UTF-8') for f in file_args) + b'\n'
return 1, out, b''

View File

@@ -78,6 +78,6 @@ def install_environment(prefix, version, additional_dependencies):
rmtree(pkgdir)
def run_hook(prefix, hook, file_args):
with in_env(prefix):
def run_hook(hook, file_args):
with in_env(hook.prefix):
return helpers.run_xargs(hook, helpers.to_cmd(hook), file_args)

View File

@@ -26,7 +26,7 @@ def environment_dir(ENVIRONMENT_DIR, language_version):
def to_cmd(hook):
return tuple(shlex.split(hook['entry'])) + tuple(hook['args'])
return tuple(shlex.split(hook.entry)) + tuple(hook.args)
def assert_version_default(binary, version):
@@ -57,7 +57,7 @@ def no_install(prefix, version, additional_dependencies):
def target_concurrency(hook):
if hook['require_serial'] or 'PRE_COMMIT_NO_CONCURRENCY' in os.environ:
if hook.require_serial or 'PRE_COMMIT_NO_CONCURRENCY' in os.environ:
return 1
else:
# Travis appears to have a bunch of CPUs, but we can't use them all.

View File

@@ -68,6 +68,6 @@ def install_environment(prefix, version, additional_dependencies):
)
def run_hook(prefix, hook, file_args):
with in_env(prefix, hook['language_version']):
def run_hook(hook, file_args):
with in_env(hook.prefix, hook.language_version):
return helpers.run_xargs(hook, helpers.to_cmd(hook), file_args)

View File

@@ -13,9 +13,9 @@ healthy = helpers.basic_healthy
install_environment = helpers.no_install
def run_hook(prefix, hook, file_args):
def run_hook(hook, file_args):
# For PCRE the entry is the regular expression to match
cmd = (GREP, '-H', '-n', '-P') + tuple(hook['args']) + (hook['entry'],)
cmd = (GREP, '-H', '-n', '-P') + tuple(hook.args) + (hook.entry,)
# Grep usually returns 0 for matches, and nonzero for non-matches so we
# negate it here.

View File

@@ -44,9 +44,8 @@ def _process_filename_at_once(pattern, filename):
return retv
def run_hook(prefix, hook, file_args):
exe = (sys.executable, '-m', __name__)
exe += tuple(hook['args']) + (hook['entry'],)
def run_hook(hook, file_args):
exe = (sys.executable, '-m', __name__) + tuple(hook.args) + (hook.entry,)
return xargs(exe, file_args)

View File

@@ -124,8 +124,8 @@ def py_interface(_dir, _make_venv):
)
return retcode == 0
def run_hook(prefix, hook, file_args):
with in_env(prefix, hook['language_version']):
def run_hook(hook, file_args):
with in_env(hook.prefix, hook.language_version):
return helpers.run_xargs(hook, helpers.to_cmd(hook), file_args)
def install_environment(prefix, version, additional_dependencies):

View File

@@ -123,6 +123,6 @@ def install_environment(
)
def run_hook(prefix, hook, file_args): # pragma: windows no cover
with in_env(prefix, hook['language_version']):
def run_hook(hook, file_args): # pragma: windows no cover
with in_env(hook.prefix, hook.language_version):
return helpers.run_xargs(hook, helpers.to_cmd(hook), file_args)

View File

@@ -88,6 +88,6 @@ def install_environment(prefix, version, additional_dependencies):
)
def run_hook(prefix, hook, file_args):
with in_env(prefix):
def run_hook(hook, file_args):
with in_env(hook.prefix):
return helpers.run_xargs(hook, helpers.to_cmd(hook), file_args)

View File

@@ -9,7 +9,7 @@ healthy = helpers.basic_healthy
install_environment = helpers.no_install
def run_hook(prefix, hook, file_args):
def run_hook(hook, file_args):
cmd = helpers.to_cmd(hook)
cmd = (prefix.path(cmd[0]),) + cmd[1:]
cmd = (hook.prefix.path(cmd[0]),) + cmd[1:]
return helpers.run_xargs(hook, cmd, file_args)

View File

@@ -50,6 +50,6 @@ def install_environment(
)
def run_hook(prefix, hook, file_args): # pragma: windows no cover
with in_env(prefix):
def run_hook(hook, file_args): # pragma: windows no cover
with in_env(hook.prefix):
return helpers.run_xargs(hook, helpers.to_cmd(hook), file_args)

View File

@@ -9,5 +9,5 @@ healthy = helpers.basic_healthy
install_environment = helpers.no_install
def run_hook(prefix, hook, file_args):
def run_hook(hook, file_args):
return helpers.run_xargs(hook, helpers.to_cmd(hook), file_args)

View File

@@ -100,7 +100,7 @@ class Hook(collections.namedtuple('Hook', ('src', 'prefix') + _KEYS)):
def run(self, file_args):
lang = languages[self.language]
return lang.run_hook(self.prefix, self._asdict(), file_args)
return lang.run_hook(self, file_args)
@classmethod
def create(cls, src, prefix, dct):

View File

@@ -39,7 +39,7 @@ def test_ENVIRONMENT_DIR(language):
@pytest.mark.parametrize('language', all_languages)
def test_run_hook_argpsec(language):
expected_argspec = ArgSpec(args=['prefix', 'hook', 'file_args'])
expected_argspec = ArgSpec(args=['hook', 'file_args'])
argspec = getargspec(languages[language].run_hook)
assert argspec == expected_argspec

View File

@@ -11,6 +11,7 @@ import pytest
from pre_commit.languages import helpers
from pre_commit.prefix import Prefix
from pre_commit.util import CalledProcessError
from testing.auto_namedtuple import auto_namedtuple
def test_basic_get_default_version():
@@ -33,27 +34,31 @@ def test_failed_setup_command_does_not_unicode_error():
helpers.run_setup_cmd(Prefix('.'), (sys.executable, '-c', script))
SERIAL_FALSE = auto_namedtuple(require_serial=False)
SERIAL_TRUE = auto_namedtuple(require_serial=True)
def test_target_concurrency_normal():
with mock.patch.object(multiprocessing, 'cpu_count', return_value=123):
with mock.patch.dict(os.environ, {}, clear=True):
assert helpers.target_concurrency({'require_serial': False}) == 123
assert helpers.target_concurrency(SERIAL_FALSE) == 123
def test_target_concurrency_cpu_count_require_serial_true():
with mock.patch.dict(os.environ, {}, clear=True):
assert helpers.target_concurrency({'require_serial': True}) == 1
assert helpers.target_concurrency(SERIAL_TRUE) == 1
def test_target_concurrency_testing_env_var():
with mock.patch.dict(
os.environ, {'PRE_COMMIT_NO_CONCURRENCY': '1'}, clear=True,
):
assert helpers.target_concurrency({'require_serial': False}) == 1
assert helpers.target_concurrency(SERIAL_FALSE) == 1
def test_target_concurrency_on_travis():
with mock.patch.dict(os.environ, {'TRAVIS': '1'}, clear=True):
assert helpers.target_concurrency({'require_serial': False}) == 2
assert helpers.target_concurrency(SERIAL_FALSE) == 2
def test_target_concurrency_cpu_count_not_implemented():
@@ -61,7 +66,7 @@ def test_target_concurrency_cpu_count_not_implemented():
multiprocessing, 'cpu_count', side_effect=NotImplementedError,
):
with mock.patch.dict(os.environ, {}, clear=True):
assert helpers.target_concurrency({'require_serial': False}) == 1
assert helpers.target_concurrency(SERIAL_FALSE) == 1
def test_shuffled_is_deterministic():