Merge pull request #460 from pre-commit/install_hooks_command

Add an install-hooks command (similar to install --install-hooks).  Resolves #456
This commit is contained in:
Anthony Sottile 2017-01-04 08:53:18 -08:00 committed by GitHub
commit fa57970c29
4 changed files with 51 additions and 10 deletions

View file

@ -82,12 +82,16 @@ def install(runner, overwrite=False, hooks=False, hook_type='pre-commit'):
# If they requested we install all of the hooks, do so. # If they requested we install all of the hooks, do so.
if hooks: if hooks:
for repository in runner.repositories: install_hooks(runner)
repository.require_installed()
return 0 return 0
def install_hooks(runner):
for repository in runner.repositories:
repository.require_installed()
def uninstall(runner, hook_type='pre-commit'): def uninstall(runner, hook_type='pre-commit'):
"""Uninstall the pre-commit hooks.""" """Uninstall the pre-commit hooks."""
hook_path = runner.get_hook_path(hook_type) hook_path = runner.get_hook_path(hook_type)

View file

@ -12,6 +12,7 @@ from pre_commit import git
from pre_commit.commands.autoupdate import autoupdate from pre_commit.commands.autoupdate import autoupdate
from pre_commit.commands.clean import clean from pre_commit.commands.clean import clean
from pre_commit.commands.install_uninstall import install from pre_commit.commands.install_uninstall import install
from pre_commit.commands.install_uninstall import install_hooks
from pre_commit.commands.install_uninstall import uninstall from pre_commit.commands.install_uninstall import uninstall
from pre_commit.commands.run import run from pre_commit.commands.run import run
from pre_commit.error_handler import error_handler from pre_commit.error_handler import error_handler
@ -78,6 +79,17 @@ def main(argv=None):
default='pre-commit', default='pre-commit',
) )
install_hooks_parser = subparsers.add_parser(
'install-hooks',
help=(
'Install hook environemnts for all environemnts in the config '
'file. You may find `pre-commit install --install-hooks` more '
'useful.'
),
)
_add_color_option(install_hooks_parser)
_add_config_option(install_hooks_parser)
uninstall_parser = subparsers.add_parser( uninstall_parser = subparsers.add_parser(
'uninstall', help='Uninstall the pre-commit script.', 'uninstall', help='Uninstall the pre-commit script.',
) )
@ -171,6 +183,8 @@ def main(argv=None):
runner, overwrite=args.overwrite, hooks=args.install_hooks, runner, overwrite=args.overwrite, hooks=args.install_hooks,
hook_type=args.hook_type, hook_type=args.hook_type,
) )
elif args.command == 'install-hooks':
return install_hooks(runner)
elif args.command == 'uninstall': elif args.command == 'uninstall':
return uninstall(runner, hook_type=args.hook_type) return uninstall(runner, hook_type=args.hook_type)
elif args.command == 'clean': elif args.command == 'clean':

View file

@ -13,6 +13,7 @@ import mock
import pre_commit.constants as C import pre_commit.constants as C
from pre_commit.commands.install_uninstall import IDENTIFYING_HASH from pre_commit.commands.install_uninstall import IDENTIFYING_HASH
from pre_commit.commands.install_uninstall import install from pre_commit.commands.install_uninstall import install
from pre_commit.commands.install_uninstall import install_hooks
from pre_commit.commands.install_uninstall import is_our_pre_commit from pre_commit.commands.install_uninstall import is_our_pre_commit
from pre_commit.commands.install_uninstall import is_previous_pre_commit from pre_commit.commands.install_uninstall import is_previous_pre_commit
from pre_commit.commands.install_uninstall import PREVIOUS_IDENTIFYING_HASHES from pre_commit.commands.install_uninstall import PREVIOUS_IDENTIFYING_HASHES
@ -460,6 +461,20 @@ def test_installs_hooks_with_hooks_True(
assert PRE_INSTALLED.match(output) assert PRE_INSTALLED.match(output)
def test_install_hooks_command(tempdir_factory, mock_out_store_directory):
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
with cwd(path):
runner = Runner(path, C.CONFIG_FILE)
install(runner)
install_hooks(runner)
ret, output = _get_commit_output(
tempdir_factory, pre_commit_home=mock_out_store_directory,
)
assert ret == 0
assert PRE_INSTALLED.match(output)
def test_installed_from_venv(tempdir_factory): def test_installed_from_venv(tempdir_factory):
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo') path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
with cwd(path): with cwd(path):

View file

@ -15,17 +15,19 @@ from testing.auto_namedtuple import auto_namedtuple
@pytest.yield_fixture @pytest.yield_fixture
def mock_commands(): def mock_commands():
with mock.patch.object(main, 'autoupdate') as autoupdate_mock: with mock.patch.object(main, 'autoupdate') as autoupdate_mock:
with mock.patch.object(main, 'clean') as clean_mock: with mock.patch.object(main, 'install_hooks') as install_hooks_mock:
with mock.patch.object(main, 'install') as install_mock: with mock.patch.object(main, 'install') as install_mock:
with mock.patch.object(main, 'uninstall') as uninstall_mock: with mock.patch.object(main, 'uninstall') as uninstall_mock:
with mock.patch.object(main, 'run') as run_mock: with mock.patch.object(main, 'run') as run_mock:
yield auto_namedtuple( with mock.patch.object(main, 'clean') as clean_mock:
autoupdate_mock=autoupdate_mock, yield auto_namedtuple(
clean_mock=clean_mock, autoupdate_mock=autoupdate_mock,
install_mock=install_mock, clean_mock=clean_mock,
uninstall_mock=uninstall_mock, install_mock=install_mock,
run_mock=run_mock, install_hooks_mock=install_hooks_mock,
) uninstall_mock=uninstall_mock,
run_mock=run_mock,
)
class CalledExit(Exception): class CalledExit(Exception):
@ -121,6 +123,12 @@ def test_run_command(mock_commands):
assert_only_one_mock_called(mock_commands) assert_only_one_mock_called(mock_commands)
def test_install_hooks_command(mock_commands):
main.main(('install-hooks',))
assert mock_commands.install_hooks_mock.call_count == 1
assert_only_one_mock_called(mock_commands)
def test_no_commands_run_command(mock_commands): def test_no_commands_run_command(mock_commands):
main.main([]) main.main([])
assert mock_commands.run_mock.call_count == 1 assert mock_commands.run_mock.call_count == 1