mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-01-14 13:00:10 -06:00
add init-templatedir --no-allow-missing-config
Add a `--no-allow-missing-config` option to the `init-templatedir` command. Enable configuration of a Git template that requires newly cloned repos to have a `pre-commit` config.
This commit is contained in:
committed by
Anthony Sottile
parent
0e851bdf75
commit
1b435f1f1f
@@ -15,10 +15,15 @@ def init_templatedir(
|
||||
store: Store,
|
||||
directory: str,
|
||||
hook_types: Sequence[str],
|
||||
skip_on_missing_config: bool = True,
|
||||
) -> int:
|
||||
install(
|
||||
config_file, store, hook_types=hook_types,
|
||||
overwrite=True, skip_on_missing_config=True, git_dir=directory,
|
||||
config_file,
|
||||
store,
|
||||
hook_types=hook_types,
|
||||
overwrite=True,
|
||||
skip_on_missing_config=skip_on_missing_config,
|
||||
git_dir=directory,
|
||||
)
|
||||
try:
|
||||
_, out, _ = cmd_output('git', 'config', 'init.templateDir')
|
||||
|
||||
@@ -245,6 +245,12 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||
init_templatedir_parser.add_argument(
|
||||
'directory', help='The directory in which to write the hook script.',
|
||||
)
|
||||
init_templatedir_parser.add_argument(
|
||||
'--no-allow-missing-config',
|
||||
action='store_false',
|
||||
dest='allow_missing_config',
|
||||
help='Assume cloned repos should have a `pre-commit` config.',
|
||||
)
|
||||
_add_hook_type_option(init_templatedir_parser)
|
||||
|
||||
install_parser = subparsers.add_parser(
|
||||
@@ -383,6 +389,7 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||
return init_templatedir(
|
||||
args.config, store, args.directory,
|
||||
hook_types=args.hook_types,
|
||||
skip_on_missing_config=args.allow_missing_config,
|
||||
)
|
||||
elif args.command == 'install-hooks':
|
||||
return install_hooks(args.config, store)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import os.path
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
|
||||
import pre_commit.constants as C
|
||||
from pre_commit.commands.init_templatedir import init_templatedir
|
||||
from pre_commit.envcontext import envcontext
|
||||
@@ -90,3 +92,49 @@ def test_init_templatedir_hookspath_set(tmpdir, tempdir_factory, store):
|
||||
C.CONFIG_FILE, store, target, hook_types=['pre-commit'],
|
||||
)
|
||||
assert target.join('hooks/pre-commit').exists()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
('skip', 'commit_retcode', 'commit_output_snippet'),
|
||||
(
|
||||
(True, 0, 'Skipping `pre-commit`.'),
|
||||
(False, 1, f'No {C.CONFIG_FILE} file was found'),
|
||||
),
|
||||
)
|
||||
def test_init_templatedir_skip_on_missing_config(
|
||||
tmpdir,
|
||||
tempdir_factory,
|
||||
store,
|
||||
cap_out,
|
||||
skip,
|
||||
commit_retcode,
|
||||
commit_output_snippet,
|
||||
):
|
||||
target = str(tmpdir.join('tmpl'))
|
||||
init_git_dir = git_dir(tempdir_factory)
|
||||
with cwd(init_git_dir):
|
||||
cmd_output('git', 'config', 'init.templateDir', target)
|
||||
init_templatedir(
|
||||
C.CONFIG_FILE,
|
||||
store,
|
||||
target,
|
||||
hook_types=['pre-commit'],
|
||||
skip_on_missing_config=skip,
|
||||
)
|
||||
|
||||
lines = cap_out.get().splitlines()
|
||||
assert len(lines) == 1
|
||||
assert lines[0].startswith('pre-commit installed at')
|
||||
|
||||
with envcontext((('GIT_TEMPLATE_DIR', target),)):
|
||||
verify_git_dir = git_dir(tempdir_factory)
|
||||
|
||||
with cwd(verify_git_dir):
|
||||
retcode, output = git_commit(
|
||||
fn=cmd_output_mocked_pre_commit_home,
|
||||
tempdir_factory=tempdir_factory,
|
||||
retcode=None,
|
||||
)
|
||||
|
||||
assert retcode == commit_retcode
|
||||
assert commit_output_snippet in output
|
||||
|
||||
@@ -159,7 +159,28 @@ def test_try_repo(mock_store_dir):
|
||||
def test_init_templatedir(mock_store_dir):
|
||||
with mock.patch.object(main, 'init_templatedir') as patch:
|
||||
main.main(('init-templatedir', 'tdir'))
|
||||
|
||||
assert patch.call_count == 1
|
||||
assert 'tdir' in patch.call_args[0]
|
||||
assert patch.call_args[1]['hook_types'] == ['pre-commit']
|
||||
assert patch.call_args[1]['skip_on_missing_config'] is True
|
||||
|
||||
|
||||
def test_init_templatedir_options(mock_store_dir):
|
||||
args = (
|
||||
'init-templatedir',
|
||||
'tdir',
|
||||
'--hook-type',
|
||||
'commit-msg',
|
||||
'--no-allow-missing-config',
|
||||
)
|
||||
with mock.patch.object(main, 'init_templatedir') as patch:
|
||||
main.main(args)
|
||||
|
||||
assert patch.call_count == 1
|
||||
assert 'tdir' in patch.call_args[0]
|
||||
assert patch.call_args[1]['hook_types'] == ['commit-msg']
|
||||
assert patch.call_args[1]['skip_on_missing_config'] is False
|
||||
|
||||
|
||||
def test_help_cmd_in_empty_directory(
|
||||
|
||||
Reference in New Issue
Block a user