mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-01-18 06:55:25 -06:00
Merge pull request #1145 from pre-commit/multiple_hooks_install
Allow --hook-type to be specified multiple times
This commit is contained in:
@@ -8,10 +8,10 @@ from pre_commit.util import cmd_output
|
||||
logger = logging.getLogger('pre_commit')
|
||||
|
||||
|
||||
def init_templatedir(config_file, store, directory, hook_type):
|
||||
def init_templatedir(config_file, store, directory, hook_types):
|
||||
install(
|
||||
config_file, store, overwrite=True, hook_type=hook_type,
|
||||
skip_on_missing_config=True, git_dir=directory,
|
||||
config_file, store, hook_types=hook_types,
|
||||
overwrite=True, skip_on_missing_config=True, git_dir=directory,
|
||||
)
|
||||
try:
|
||||
_, out, _ = cmd_output('git', 'config', 'init.templateDir')
|
||||
|
||||
@@ -67,19 +67,10 @@ def shebang():
|
||||
return '#!/usr/bin/env {}'.format(py)
|
||||
|
||||
|
||||
def install(
|
||||
config_file, store,
|
||||
overwrite=False, hooks=False, hook_type='pre-commit',
|
||||
skip_on_missing_config=False, git_dir=None,
|
||||
def _install_hook_script(
|
||||
config_file, hook_type,
|
||||
overwrite=False, skip_on_missing_config=False, git_dir=None,
|
||||
):
|
||||
"""Install the pre-commit hooks."""
|
||||
if cmd_output('git', 'config', 'core.hooksPath', retcode=None)[1].strip():
|
||||
logger.error(
|
||||
'Cowardly refusing to install hooks with `core.hooksPath` set.\n'
|
||||
'hint: `git config --unset-all core.hooksPath`',
|
||||
)
|
||||
return 1
|
||||
|
||||
hook_path, legacy_path = _hook_paths(hook_type, git_dir=git_dir)
|
||||
|
||||
mkdirp(os.path.dirname(hook_path))
|
||||
@@ -120,7 +111,27 @@ def install(
|
||||
|
||||
output.write_line('pre-commit installed at {}'.format(hook_path))
|
||||
|
||||
# If they requested we install all of the hooks, do so.
|
||||
|
||||
def install(
|
||||
config_file, store, hook_types,
|
||||
overwrite=False, hooks=False,
|
||||
skip_on_missing_config=False, git_dir=None,
|
||||
):
|
||||
if cmd_output('git', 'config', 'core.hooksPath', retcode=None)[1].strip():
|
||||
logger.error(
|
||||
'Cowardly refusing to install hooks with `core.hooksPath` set.\n'
|
||||
'hint: `git config --unset-all core.hooksPath`',
|
||||
)
|
||||
return 1
|
||||
|
||||
for hook_type in hook_types:
|
||||
_install_hook_script(
|
||||
config_file, hook_type,
|
||||
overwrite=overwrite,
|
||||
skip_on_missing_config=skip_on_missing_config,
|
||||
git_dir=git_dir,
|
||||
)
|
||||
|
||||
if hooks:
|
||||
install_hooks(config_file, store)
|
||||
|
||||
@@ -131,13 +142,12 @@ def install_hooks(config_file, store):
|
||||
install_hook_envs(all_hooks(load_config(config_file), store), store)
|
||||
|
||||
|
||||
def uninstall(hook_type='pre-commit'):
|
||||
"""Uninstall the pre-commit hooks."""
|
||||
def _uninstall_hook_script(hook_type): # type: (str) -> None
|
||||
hook_path, legacy_path = _hook_paths(hook_type)
|
||||
|
||||
# If our file doesn't exist or it isn't ours, gtfo.
|
||||
if not os.path.exists(hook_path) or not is_our_script(hook_path):
|
||||
return 0
|
||||
return
|
||||
|
||||
os.remove(hook_path)
|
||||
output.write_line('{} uninstalled'.format(hook_type))
|
||||
@@ -146,4 +156,8 @@ def uninstall(hook_type='pre-commit'):
|
||||
os.rename(legacy_path, hook_path)
|
||||
output.write_line('Restored previous hooks to {}'.format(hook_path))
|
||||
|
||||
|
||||
def uninstall(hook_types):
|
||||
for hook_type in hook_types:
|
||||
_uninstall_hook_script(hook_type)
|
||||
return 0
|
||||
|
||||
@@ -60,7 +60,8 @@ def _add_hook_type_option(parser):
|
||||
'-t', '--hook-type', choices=(
|
||||
'pre-commit', 'pre-push', 'prepare-commit-msg', 'commit-msg',
|
||||
),
|
||||
default='pre-commit',
|
||||
action='append',
|
||||
dest='hook_types',
|
||||
)
|
||||
|
||||
|
||||
@@ -120,6 +121,11 @@ def _adjust_args_and_chdir(args):
|
||||
args.files = [os.path.relpath(filename) for filename in args.files]
|
||||
if args.command == 'try-repo' and os.path.exists(args.repo):
|
||||
args.repo = os.path.relpath(args.repo)
|
||||
if (
|
||||
args.command in {'install', 'uninstall', 'init-templatedir'} and
|
||||
not args.hook_types
|
||||
):
|
||||
args.hook_types = ['pre-commit']
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
@@ -299,14 +305,14 @@ def main(argv=None):
|
||||
elif args.command == 'install':
|
||||
return install(
|
||||
args.config, store,
|
||||
hook_types=args.hook_types,
|
||||
overwrite=args.overwrite, hooks=args.install_hooks,
|
||||
hook_type=args.hook_type,
|
||||
skip_on_missing_config=args.allow_missing_config,
|
||||
)
|
||||
elif args.command == 'init-templatedir':
|
||||
return init_templatedir(
|
||||
args.config, store,
|
||||
args.directory, hook_type=args.hook_type,
|
||||
args.config, store, args.directory,
|
||||
hook_types=args.hook_types,
|
||||
)
|
||||
elif args.command == 'install-hooks':
|
||||
return install_hooks(args.config, store)
|
||||
@@ -319,7 +325,7 @@ def main(argv=None):
|
||||
elif args.command == 'try-repo':
|
||||
return try_repo(args)
|
||||
elif args.command == 'uninstall':
|
||||
return uninstall(hook_type=args.hook_type)
|
||||
return uninstall(hook_types=args.hook_types)
|
||||
else:
|
||||
raise NotImplementedError(
|
||||
'Command {} not implemented.'.format(args.command),
|
||||
|
||||
@@ -16,7 +16,7 @@ from testing.util import git_commit
|
||||
|
||||
def test_init_templatedir(tmpdir, tempdir_factory, store, cap_out):
|
||||
target = str(tmpdir.join('tmpl'))
|
||||
init_templatedir(C.CONFIG_FILE, store, target, hook_type='pre-commit')
|
||||
init_templatedir(C.CONFIG_FILE, store, target, hook_types=['pre-commit'])
|
||||
lines = cap_out.get().splitlines()
|
||||
assert lines[0].startswith('pre-commit installed at ')
|
||||
assert lines[1] == (
|
||||
@@ -45,7 +45,9 @@ def test_init_templatedir_already_set(tmpdir, tempdir_factory, store, cap_out):
|
||||
tmp_git_dir = git_dir(tempdir_factory)
|
||||
with cwd(tmp_git_dir):
|
||||
cmd_output('git', 'config', 'init.templateDir', target)
|
||||
init_templatedir(C.CONFIG_FILE, store, target, hook_type='pre-commit')
|
||||
init_templatedir(
|
||||
C.CONFIG_FILE, store, target, hook_types=['pre-commit'],
|
||||
)
|
||||
|
||||
lines = cap_out.get().splitlines()
|
||||
assert len(lines) == 1
|
||||
@@ -57,7 +59,9 @@ def test_init_templatedir_not_set(tmpdir, store, cap_out):
|
||||
with envcontext([('HOME', str(tmpdir))]):
|
||||
with tmpdir.join('tmpl').ensure_dir().as_cwd():
|
||||
# we have not set init.templateDir so this should produce a warning
|
||||
init_templatedir(C.CONFIG_FILE, store, '.', hook_type='pre-commit')
|
||||
init_templatedir(
|
||||
C.CONFIG_FILE, store, '.', hook_types=['pre-commit'],
|
||||
)
|
||||
|
||||
lines = cap_out.get().splitlines()
|
||||
assert len(lines) == 3
|
||||
@@ -73,7 +77,7 @@ def test_init_templatedir_expanduser(tmpdir, tempdir_factory, store, cap_out):
|
||||
cmd_output('git', 'config', 'init.templateDir', '~/templatedir')
|
||||
with mock.patch.object(os.path, 'expanduser', return_value=target):
|
||||
init_templatedir(
|
||||
C.CONFIG_FILE, store, target, hook_type='pre-commit',
|
||||
C.CONFIG_FILE, store, target, hook_types=['pre-commit'],
|
||||
)
|
||||
|
||||
lines = cap_out.get().splitlines()
|
||||
|
||||
@@ -67,10 +67,10 @@ def test_shebang_posix_on_path(tmpdir):
|
||||
|
||||
|
||||
def test_install_pre_commit(in_git_dir, store):
|
||||
assert not install(C.CONFIG_FILE, store)
|
||||
assert not install(C.CONFIG_FILE, store, hook_types=['pre-commit'])
|
||||
assert os.access(in_git_dir.join('.git/hooks/pre-commit').strpath, os.X_OK)
|
||||
|
||||
assert not install(C.CONFIG_FILE, store, hook_type='pre-push')
|
||||
assert not install(C.CONFIG_FILE, store, hook_types=['pre-push'])
|
||||
assert os.access(in_git_dir.join('.git/hooks/pre-push').strpath, os.X_OK)
|
||||
|
||||
|
||||
@@ -78,32 +78,41 @@ def test_install_hooks_directory_not_present(in_git_dir, store):
|
||||
# Simulate some git clients which don't make .git/hooks #234
|
||||
if in_git_dir.join('.git/hooks').exists(): # pragma: no cover (odd git)
|
||||
in_git_dir.join('.git/hooks').remove()
|
||||
install(C.CONFIG_FILE, store)
|
||||
install(C.CONFIG_FILE, store, hook_types=['pre-commit'])
|
||||
assert in_git_dir.join('.git/hooks/pre-commit').exists()
|
||||
|
||||
|
||||
def test_install_multiple_hooks_at_once(in_git_dir, store):
|
||||
install(C.CONFIG_FILE, store, hook_types=['pre-commit', 'pre-push'])
|
||||
assert in_git_dir.join('.git/hooks/pre-commit').exists()
|
||||
assert in_git_dir.join('.git/hooks/pre-push').exists()
|
||||
uninstall(hook_types=['pre-commit', 'pre-push'])
|
||||
assert not in_git_dir.join('.git/hooks/pre-commit').exists()
|
||||
assert not in_git_dir.join('.git/hooks/pre-push').exists()
|
||||
|
||||
|
||||
def test_install_refuses_core_hookspath(in_git_dir, store):
|
||||
cmd_output('git', 'config', '--local', 'core.hooksPath', 'hooks')
|
||||
assert install(C.CONFIG_FILE, store)
|
||||
assert install(C.CONFIG_FILE, store, hook_types=['pre-commit'])
|
||||
|
||||
|
||||
@xfailif_no_symlink # pragma: windows no cover
|
||||
def test_install_hooks_dead_symlink(in_git_dir, store):
|
||||
hook = in_git_dir.join('.git/hooks').ensure_dir().join('pre-commit')
|
||||
os.symlink('/fake/baz', hook.strpath)
|
||||
install(C.CONFIG_FILE, store)
|
||||
install(C.CONFIG_FILE, store, hook_types=['pre-commit'])
|
||||
assert hook.exists()
|
||||
|
||||
|
||||
def test_uninstall_does_not_blow_up_when_not_there(in_git_dir):
|
||||
assert uninstall() == 0
|
||||
assert uninstall(hook_types=['pre-commit']) == 0
|
||||
|
||||
|
||||
def test_uninstall(in_git_dir, store):
|
||||
assert not in_git_dir.join('.git/hooks/pre-commit').exists()
|
||||
install(C.CONFIG_FILE, store)
|
||||
install(C.CONFIG_FILE, store, hook_types=['pre-commit'])
|
||||
assert in_git_dir.join('.git/hooks/pre-commit').exists()
|
||||
uninstall()
|
||||
uninstall(hook_types=['pre-commit'])
|
||||
assert not in_git_dir.join('.git/hooks/pre-commit').exists()
|
||||
|
||||
|
||||
@@ -142,7 +151,7 @@ NORMAL_PRE_COMMIT_RUN = re.compile(
|
||||
def test_install_pre_commit_and_run(tempdir_factory, store):
|
||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||
with cwd(path):
|
||||
assert install(C.CONFIG_FILE, store) == 0
|
||||
assert install(C.CONFIG_FILE, store, hook_types=['pre-commit']) == 0
|
||||
|
||||
ret, output = _get_commit_output(tempdir_factory)
|
||||
assert ret == 0
|
||||
@@ -152,9 +161,9 @@ def test_install_pre_commit_and_run(tempdir_factory, store):
|
||||
def test_install_pre_commit_and_run_custom_path(tempdir_factory, store):
|
||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||
with cwd(path):
|
||||
cmd_output('git', 'mv', C.CONFIG_FILE, 'custom-config.yaml')
|
||||
cmd_output('git', 'mv', C.CONFIG_FILE, 'custom.yaml')
|
||||
git_commit(cwd=path)
|
||||
assert install('custom-config.yaml', store) == 0
|
||||
assert install('custom.yaml', store, hook_types=['pre-commit']) == 0
|
||||
|
||||
ret, output = _get_commit_output(tempdir_factory)
|
||||
assert ret == 0
|
||||
@@ -169,7 +178,7 @@ def test_install_in_submodule_and_run(tempdir_factory, store):
|
||||
|
||||
sub_pth = os.path.join(parent_path, 'sub')
|
||||
with cwd(sub_pth):
|
||||
assert install(C.CONFIG_FILE, store) == 0
|
||||
assert install(C.CONFIG_FILE, store, hook_types=['pre-commit']) == 0
|
||||
ret, output = _get_commit_output(tempdir_factory)
|
||||
assert ret == 0
|
||||
assert NORMAL_PRE_COMMIT_RUN.match(output)
|
||||
@@ -182,7 +191,7 @@ def test_install_in_worktree_and_run(tempdir_factory, store):
|
||||
cmd_output('git', '-C', src_path, 'worktree', 'add', path, '-b', 'master')
|
||||
|
||||
with cwd(path):
|
||||
assert install(C.CONFIG_FILE, store) == 0
|
||||
assert install(C.CONFIG_FILE, store, hook_types=['pre-commit']) == 0
|
||||
ret, output = _get_commit_output(tempdir_factory)
|
||||
assert ret == 0
|
||||
assert NORMAL_PRE_COMMIT_RUN.match(output)
|
||||
@@ -199,7 +208,7 @@ def test_commit_am(tempdir_factory, store):
|
||||
with io.open('unstaged', 'w') as foo_file:
|
||||
foo_file.write('Oh hai')
|
||||
|
||||
assert install(C.CONFIG_FILE, store) == 0
|
||||
assert install(C.CONFIG_FILE, store, hook_types=['pre-commit']) == 0
|
||||
|
||||
ret, output = _get_commit_output(tempdir_factory)
|
||||
assert ret == 0
|
||||
@@ -208,7 +217,7 @@ def test_commit_am(tempdir_factory, store):
|
||||
def test_unicode_merge_commit_message(tempdir_factory, store):
|
||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||
with cwd(path):
|
||||
assert install(C.CONFIG_FILE, store) == 0
|
||||
assert install(C.CONFIG_FILE, store, hook_types=['pre-commit']) == 0
|
||||
cmd_output('git', 'checkout', 'master', '-b', 'foo')
|
||||
git_commit('-n', cwd=path)
|
||||
cmd_output('git', 'checkout', 'master')
|
||||
@@ -225,8 +234,8 @@ def test_unicode_merge_commit_message(tempdir_factory, store):
|
||||
def test_install_idempotent(tempdir_factory, store):
|
||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||
with cwd(path):
|
||||
assert install(C.CONFIG_FILE, store) == 0
|
||||
assert install(C.CONFIG_FILE, store) == 0
|
||||
assert install(C.CONFIG_FILE, store, hook_types=['pre-commit']) == 0
|
||||
assert install(C.CONFIG_FILE, store, hook_types=['pre-commit']) == 0
|
||||
|
||||
ret, output = _get_commit_output(tempdir_factory)
|
||||
assert ret == 0
|
||||
@@ -252,7 +261,7 @@ def test_environment_not_sourced(tempdir_factory, store):
|
||||
with cwd(path):
|
||||
# Patch the executable to simulate rming virtualenv
|
||||
with mock.patch.object(sys, 'executable', '/does-not-exist'):
|
||||
assert install(C.CONFIG_FILE, store) == 0
|
||||
assert not install(C.CONFIG_FILE, store, hook_types=['pre-commit'])
|
||||
|
||||
# Use a specific homedir to ignore --user installs
|
||||
homedir = tempdir_factory.get()
|
||||
@@ -290,7 +299,7 @@ FAILING_PRE_COMMIT_RUN = re.compile(
|
||||
def test_failing_hooks_returns_nonzero(tempdir_factory, store):
|
||||
path = make_consuming_repo(tempdir_factory, 'failing_hook_repo')
|
||||
with cwd(path):
|
||||
assert install(C.CONFIG_FILE, store) == 0
|
||||
assert install(C.CONFIG_FILE, store, hook_types=['pre-commit']) == 0
|
||||
|
||||
ret, output = _get_commit_output(tempdir_factory)
|
||||
assert ret == 1
|
||||
@@ -323,7 +332,7 @@ def test_install_existing_hooks_no_overwrite(tempdir_factory, store):
|
||||
assert EXISTING_COMMIT_RUN.match(output)
|
||||
|
||||
# Now install pre-commit (no-overwrite)
|
||||
assert install(C.CONFIG_FILE, store) == 0
|
||||
assert install(C.CONFIG_FILE, store, hook_types=['pre-commit']) == 0
|
||||
|
||||
# We should run both the legacy and pre-commit hooks
|
||||
ret, output = _get_commit_output(tempdir_factory)
|
||||
@@ -336,10 +345,10 @@ def test_legacy_overwriting_legacy_hook(tempdir_factory, store):
|
||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||
with cwd(path):
|
||||
_write_legacy_hook(path)
|
||||
assert install(C.CONFIG_FILE, store) == 0
|
||||
assert install(C.CONFIG_FILE, store, hook_types=['pre-commit']) == 0
|
||||
_write_legacy_hook(path)
|
||||
# this previously crashed on windows. See #1010
|
||||
assert install(C.CONFIG_FILE, store) == 0
|
||||
assert install(C.CONFIG_FILE, store, hook_types=['pre-commit']) == 0
|
||||
|
||||
|
||||
def test_install_existing_hook_no_overwrite_idempotent(tempdir_factory, store):
|
||||
@@ -348,8 +357,8 @@ def test_install_existing_hook_no_overwrite_idempotent(tempdir_factory, store):
|
||||
_write_legacy_hook(path)
|
||||
|
||||
# Install twice
|
||||
assert install(C.CONFIG_FILE, store) == 0
|
||||
assert install(C.CONFIG_FILE, store) == 0
|
||||
assert install(C.CONFIG_FILE, store, hook_types=['pre-commit']) == 0
|
||||
assert install(C.CONFIG_FILE, store, hook_types=['pre-commit']) == 0
|
||||
|
||||
# We should run both the legacy and pre-commit hooks
|
||||
ret, output = _get_commit_output(tempdir_factory)
|
||||
@@ -374,7 +383,7 @@ def test_failing_existing_hook_returns_1(tempdir_factory, store):
|
||||
f.write('#!/usr/bin/env bash\necho "fail!"\nexit 1\n')
|
||||
make_executable(f.name)
|
||||
|
||||
assert install(C.CONFIG_FILE, store) == 0
|
||||
assert install(C.CONFIG_FILE, store, hook_types=['pre-commit']) == 0
|
||||
|
||||
# We should get a failure from the legacy hook
|
||||
ret, output = _get_commit_output(tempdir_factory)
|
||||
@@ -385,7 +394,9 @@ def test_failing_existing_hook_returns_1(tempdir_factory, store):
|
||||
def test_install_overwrite_no_existing_hooks(tempdir_factory, store):
|
||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||
with cwd(path):
|
||||
assert install(C.CONFIG_FILE, store, overwrite=True) == 0
|
||||
assert not install(
|
||||
C.CONFIG_FILE, store, hook_types=['pre-commit'], overwrite=True,
|
||||
)
|
||||
|
||||
ret, output = _get_commit_output(tempdir_factory)
|
||||
assert ret == 0
|
||||
@@ -396,7 +407,9 @@ def test_install_overwrite(tempdir_factory, store):
|
||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||
with cwd(path):
|
||||
_write_legacy_hook(path)
|
||||
assert install(C.CONFIG_FILE, store, overwrite=True) == 0
|
||||
assert not install(
|
||||
C.CONFIG_FILE, store, hook_types=['pre-commit'], overwrite=True,
|
||||
)
|
||||
|
||||
ret, output = _get_commit_output(tempdir_factory)
|
||||
assert ret == 0
|
||||
@@ -409,8 +422,8 @@ def test_uninstall_restores_legacy_hooks(tempdir_factory, store):
|
||||
_write_legacy_hook(path)
|
||||
|
||||
# Now install and uninstall pre-commit
|
||||
assert install(C.CONFIG_FILE, store) == 0
|
||||
assert uninstall() == 0
|
||||
assert install(C.CONFIG_FILE, store, hook_types=['pre-commit']) == 0
|
||||
assert uninstall(hook_types=['pre-commit']) == 0
|
||||
|
||||
# Make sure we installed the "old" hook correctly
|
||||
ret, output = _get_commit_output(tempdir_factory, touch_file='baz')
|
||||
@@ -433,7 +446,7 @@ def test_replace_old_commit_script(tempdir_factory, store):
|
||||
make_executable(f.name)
|
||||
|
||||
# Install normally
|
||||
assert install(C.CONFIG_FILE, store) == 0
|
||||
assert install(C.CONFIG_FILE, store, hook_types=['pre-commit']) == 0
|
||||
|
||||
ret, output = _get_commit_output(tempdir_factory)
|
||||
assert ret == 0
|
||||
@@ -445,7 +458,7 @@ def test_uninstall_doesnt_remove_not_our_hooks(in_git_dir):
|
||||
pre_commit.write('#!/usr/bin/env bash\necho 1\n')
|
||||
make_executable(pre_commit.strpath)
|
||||
|
||||
assert uninstall() == 0
|
||||
assert uninstall(hook_types=['pre-commit']) == 0
|
||||
|
||||
assert pre_commit.exists()
|
||||
|
||||
@@ -461,7 +474,7 @@ PRE_INSTALLED = re.compile(
|
||||
def test_installs_hooks_with_hooks_True(tempdir_factory, store):
|
||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||
with cwd(path):
|
||||
install(C.CONFIG_FILE, store, hooks=True)
|
||||
install(C.CONFIG_FILE, store, hook_types=['pre-commit'], hooks=True)
|
||||
ret, output = _get_commit_output(
|
||||
tempdir_factory, pre_commit_home=store.directory,
|
||||
)
|
||||
@@ -473,7 +486,7 @@ def test_installs_hooks_with_hooks_True(tempdir_factory, store):
|
||||
def test_install_hooks_command(tempdir_factory, store):
|
||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||
with cwd(path):
|
||||
install(C.CONFIG_FILE, store)
|
||||
install(C.CONFIG_FILE, store, hook_types=['pre-commit'])
|
||||
install_hooks(C.CONFIG_FILE, store)
|
||||
ret, output = _get_commit_output(
|
||||
tempdir_factory, pre_commit_home=store.directory,
|
||||
@@ -486,7 +499,7 @@ def test_install_hooks_command(tempdir_factory, store):
|
||||
def test_installed_from_venv(tempdir_factory, store):
|
||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||
with cwd(path):
|
||||
install(C.CONFIG_FILE, store)
|
||||
install(C.CONFIG_FILE, store, hook_types=['pre-commit'])
|
||||
# No environment so pre-commit is not on the path when running!
|
||||
# Should still pick up the python from when we installed
|
||||
ret, output = _get_commit_output(
|
||||
@@ -525,7 +538,7 @@ def test_pre_push_integration_failing(tempdir_factory, store):
|
||||
path = tempdir_factory.get()
|
||||
cmd_output('git', 'clone', upstream, path)
|
||||
with cwd(path):
|
||||
install(C.CONFIG_FILE, store, hook_type='pre-push')
|
||||
install(C.CONFIG_FILE, store, hook_types=['pre-push'])
|
||||
# commit succeeds because pre-commit is only installed for pre-push
|
||||
assert _get_commit_output(tempdir_factory)[0] == 0
|
||||
assert _get_commit_output(tempdir_factory, touch_file='zzz')[0] == 0
|
||||
@@ -543,7 +556,7 @@ def test_pre_push_integration_accepted(tempdir_factory, store):
|
||||
path = tempdir_factory.get()
|
||||
cmd_output('git', 'clone', upstream, path)
|
||||
with cwd(path):
|
||||
install(C.CONFIG_FILE, store, hook_type='pre-push')
|
||||
install(C.CONFIG_FILE, store, hook_types=['pre-push'])
|
||||
assert _get_commit_output(tempdir_factory)[0] == 0
|
||||
|
||||
retc, output = _get_push_output(tempdir_factory)
|
||||
@@ -563,7 +576,7 @@ def test_pre_push_force_push_without_fetch(tempdir_factory, store):
|
||||
assert _get_push_output(tempdir_factory)[0] == 0
|
||||
|
||||
with cwd(path2):
|
||||
install(C.CONFIG_FILE, store, hook_type='pre-push')
|
||||
install(C.CONFIG_FILE, store, hook_types=['pre-push'])
|
||||
assert _get_commit_output(tempdir_factory, msg='force!')[0] == 0
|
||||
|
||||
retc, output = _get_push_output(tempdir_factory, opts=('--force',))
|
||||
@@ -578,7 +591,7 @@ def test_pre_push_new_upstream(tempdir_factory, store):
|
||||
path = tempdir_factory.get()
|
||||
cmd_output('git', 'clone', upstream, path)
|
||||
with cwd(path):
|
||||
install(C.CONFIG_FILE, store, hook_type='pre-push')
|
||||
install(C.CONFIG_FILE, store, hook_types=['pre-push'])
|
||||
assert _get_commit_output(tempdir_factory)[0] == 0
|
||||
|
||||
cmd_output('git', 'remote', 'rename', 'origin', 'upstream')
|
||||
@@ -594,7 +607,7 @@ def test_pre_push_integration_empty_push(tempdir_factory, store):
|
||||
path = tempdir_factory.get()
|
||||
cmd_output('git', 'clone', upstream, path)
|
||||
with cwd(path):
|
||||
install(C.CONFIG_FILE, store, hook_type='pre-push')
|
||||
install(C.CONFIG_FILE, store, hook_types=['pre-push'])
|
||||
_get_push_output(tempdir_factory)
|
||||
retc, output = _get_push_output(tempdir_factory)
|
||||
assert output == 'Everything up-to-date\n'
|
||||
@@ -617,7 +630,7 @@ def test_pre_push_legacy(tempdir_factory, store):
|
||||
)
|
||||
make_executable(f.name)
|
||||
|
||||
install(C.CONFIG_FILE, store, hook_type='pre-push')
|
||||
install(C.CONFIG_FILE, store, hook_types=['pre-push'])
|
||||
assert _get_commit_output(tempdir_factory)[0] == 0
|
||||
|
||||
retc, output = _get_push_output(tempdir_factory)
|
||||
@@ -631,7 +644,7 @@ def test_pre_push_legacy(tempdir_factory, store):
|
||||
def test_commit_msg_integration_failing(
|
||||
commit_msg_repo, tempdir_factory, store,
|
||||
):
|
||||
install(C.CONFIG_FILE, store, hook_type='commit-msg')
|
||||
install(C.CONFIG_FILE, store, hook_types=['commit-msg'])
|
||||
retc, out = _get_commit_output(tempdir_factory)
|
||||
assert retc == 1
|
||||
assert out.startswith('Must have "Signed off by:"...')
|
||||
@@ -641,7 +654,7 @@ def test_commit_msg_integration_failing(
|
||||
def test_commit_msg_integration_passing(
|
||||
commit_msg_repo, tempdir_factory, store,
|
||||
):
|
||||
install(C.CONFIG_FILE, store, hook_type='commit-msg')
|
||||
install(C.CONFIG_FILE, store, hook_types=['commit-msg'])
|
||||
msg = 'Hi\nSigned off by: me, lol'
|
||||
retc, out = _get_commit_output(tempdir_factory, msg=msg)
|
||||
assert retc == 0
|
||||
@@ -662,7 +675,7 @@ def test_commit_msg_legacy(commit_msg_repo, tempdir_factory, store):
|
||||
)
|
||||
make_executable(hook_path)
|
||||
|
||||
install(C.CONFIG_FILE, store, hook_type='commit-msg')
|
||||
install(C.CONFIG_FILE, store, hook_types=['commit-msg'])
|
||||
|
||||
msg = 'Hi\nSigned off by: asottile'
|
||||
retc, out = _get_commit_output(tempdir_factory, msg=msg)
|
||||
@@ -675,7 +688,7 @@ def test_commit_msg_legacy(commit_msg_repo, tempdir_factory, store):
|
||||
def test_prepare_commit_msg_integration_failing(
|
||||
failing_prepare_commit_msg_repo, tempdir_factory, store,
|
||||
):
|
||||
install(C.CONFIG_FILE, store, hook_type='prepare-commit-msg')
|
||||
install(C.CONFIG_FILE, store, hook_types=['prepare-commit-msg'])
|
||||
retc, out = _get_commit_output(tempdir_factory)
|
||||
assert retc == 1
|
||||
assert out.startswith('Add "Signed off by:"...')
|
||||
@@ -685,7 +698,7 @@ def test_prepare_commit_msg_integration_failing(
|
||||
def test_prepare_commit_msg_integration_passing(
|
||||
prepare_commit_msg_repo, tempdir_factory, store,
|
||||
):
|
||||
install(C.CONFIG_FILE, store, hook_type='prepare-commit-msg')
|
||||
install(C.CONFIG_FILE, store, hook_types=['prepare-commit-msg'])
|
||||
msg = 'Hi'
|
||||
retc, out = _get_commit_output(tempdir_factory, msg=msg)
|
||||
assert retc == 0
|
||||
@@ -715,7 +728,7 @@ def test_prepare_commit_msg_legacy(
|
||||
)
|
||||
make_executable(hook_path)
|
||||
|
||||
install(C.CONFIG_FILE, store, hook_type='prepare-commit-msg')
|
||||
install(C.CONFIG_FILE, store, hook_types=['prepare-commit-msg'])
|
||||
|
||||
msg = 'Hi'
|
||||
retc, out = _get_commit_output(tempdir_factory, msg=msg)
|
||||
@@ -735,7 +748,8 @@ def test_install_disallow_missing_config(tempdir_factory, store):
|
||||
with cwd(path):
|
||||
remove_config_from_repo(path)
|
||||
ret = install(
|
||||
C.CONFIG_FILE, store, overwrite=True, skip_on_missing_config=False,
|
||||
C.CONFIG_FILE, store, hook_types=['pre-commit'],
|
||||
overwrite=True, skip_on_missing_config=False,
|
||||
)
|
||||
assert ret == 0
|
||||
|
||||
@@ -748,7 +762,8 @@ def test_install_allow_missing_config(tempdir_factory, store):
|
||||
with cwd(path):
|
||||
remove_config_from_repo(path)
|
||||
ret = install(
|
||||
C.CONFIG_FILE, store, overwrite=True, skip_on_missing_config=True,
|
||||
C.CONFIG_FILE, store, hook_types=['pre-commit'],
|
||||
overwrite=True, skip_on_missing_config=True,
|
||||
)
|
||||
assert ret == 0
|
||||
|
||||
@@ -766,7 +781,8 @@ def test_install_temporarily_allow_mising_config(tempdir_factory, store):
|
||||
with cwd(path):
|
||||
remove_config_from_repo(path)
|
||||
ret = install(
|
||||
C.CONFIG_FILE, store, overwrite=True, skip_on_missing_config=False,
|
||||
C.CONFIG_FILE, store, hook_types=['pre-commit'],
|
||||
overwrite=True, skip_on_missing_config=False,
|
||||
)
|
||||
assert ret == 0
|
||||
|
||||
|
||||
@@ -525,7 +525,7 @@ def test_stdout_write_bug_py26(repo_with_failing_hook, store, tempdir_factory):
|
||||
config['repos'][0]['hooks'][0]['args'] = ['☃']
|
||||
stage_a_file()
|
||||
|
||||
install(C.CONFIG_FILE, store)
|
||||
install(C.CONFIG_FILE, store, hook_types=['pre-commit'])
|
||||
|
||||
# Have to use subprocess because pytest monkeypatches sys.stdout
|
||||
_, stdout, _ = git_commit(
|
||||
@@ -555,7 +555,7 @@ def test_lots_of_files(store, tempdir_factory):
|
||||
open(filename, 'w').close()
|
||||
|
||||
cmd_output('git', 'add', '.')
|
||||
install(C.CONFIG_FILE, store)
|
||||
install(C.CONFIG_FILE, store, hook_types=['pre-commit'])
|
||||
|
||||
git_commit(
|
||||
fn=cmd_output_mocked_pre_commit_home,
|
||||
|
||||
Reference in New Issue
Block a user