Add --allow-missing-config option to install

When no '.pre-commit-config.yaml' file exists while `pre-commit` hooks
are enabled, `pre-commit` returns an error and the action is aborted.
This is a very common scenario when pre-commit is added later on a
project and the user wants to work on a previous branch where the
configuration file does not exist.

This commits allow the user to optionally install the `pre-commit` hooks
with an option to allow a missing configuration and trigger only the
legacy pre-commit hooks (if any) when it is missing.
This commit is contained in:
Filippos Giannakos 2017-02-21 12:40:33 +02:00
parent 41dcaff3fb
commit 2f4199850d
5 changed files with 84 additions and 2 deletions

View file

@ -26,6 +26,7 @@ from pre_commit.util import mkdirp
from pre_commit.util import resource_filename
from testing.fixtures import git_dir
from testing.fixtures import make_consuming_repo
from testing.fixtures import remove_config_from_repo
from testing.util import cmd_output_mocked_pre_commit_home
from testing.util import xfailif_no_symlink
@ -64,7 +65,8 @@ def test_install_pre_commit(tempdir_factory):
expected_contents = io.open(pre_commit_script).read().format(
sys_executable=sys.executable,
hook_type='pre-commit',
pre_push=''
pre_push='',
skip_on_missing_conf='false'
)
assert pre_commit_contents == expected_contents
assert os.access(runner.pre_commit_path, os.X_OK)
@ -79,6 +81,7 @@ def test_install_pre_commit(tempdir_factory):
sys_executable=sys.executable,
hook_type='pre-push',
pre_push=pre_push_template_contents,
skip_on_missing_conf='false'
)
assert pre_push_contents == expected_contents
@ -552,3 +555,46 @@ def test_pre_push_integration_empty_push(tempdir_factory):
retc, output = _get_push_output(tempdir_factory)
assert output == 'Everything up-to-date\n'
assert retc == 0
def test_install_disallow_mising_config(tempdir_factory):
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
with cwd(path):
runner = Runner(path, C.CONFIG_FILE)
remove_config_from_repo(path)
assert install(runner, overwrite=True, skip_on_missing_conf=False) == 0
ret, output = _get_commit_output(tempdir_factory)
assert ret == 1
def test_install_allow_mising_config(tempdir_factory):
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
with cwd(path):
runner = Runner(path, C.CONFIG_FILE)
remove_config_from_repo(path)
assert install(runner, overwrite=True, skip_on_missing_conf=True) == 0
ret, output = _get_commit_output(tempdir_factory)
assert ret == 0
assert '`.pre-commit-config.yaml` config file not found. '\
'Skipping `pre-commit`.' in output
def test_install_temporarily_allow_mising_config(tempdir_factory):
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
with cwd(path):
runner = Runner(path, C.CONFIG_FILE)
remove_config_from_repo(path)
assert install(runner, overwrite=True, skip_on_missing_conf=False) == 0
extra_env = {'PRE_COMMIT_ALLOW_NO_CONFIG': '1'}
env = os.environ.copy()
env.update(extra_env)
ret, output = _get_commit_output(tempdir_factory, env=env)
assert ret == 0
assert '`.pre-commit-config.yaml` config file not found. '\
'Skipping `pre-commit`.' in output