mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Adding support for locally-defined hooks
This commit is contained in:
parent
d97ea30c4b
commit
b68261c720
8 changed files with 241 additions and 40 deletions
|
|
@ -1,5 +1,6 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import jsonschema
|
||||
import pytest
|
||||
|
||||
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
|
||||
|
|
@ -25,7 +26,7 @@ def test_run(input, expected_output):
|
|||
assert run(input) == expected_output
|
||||
|
||||
|
||||
@pytest.mark.parametrize(('manifest_obj', 'expected'), (
|
||||
@pytest.mark.parametrize(('config_obj', 'expected'), (
|
||||
([], False),
|
||||
(
|
||||
[{
|
||||
|
|
@ -66,8 +67,8 @@ def test_run(input, expected_output):
|
|||
False,
|
||||
),
|
||||
))
|
||||
def test_is_valid_according_to_schema(manifest_obj, expected):
|
||||
ret = is_valid_according_to_schema(manifest_obj, CONFIG_JSON_SCHEMA)
|
||||
def test_is_valid_according_to_schema(config_obj, expected):
|
||||
ret = is_valid_according_to_schema(config_obj, CONFIG_JSON_SCHEMA)
|
||||
assert ret is expected
|
||||
|
||||
|
||||
|
|
@ -121,3 +122,55 @@ def test_config_with_ok_exclude_regex_passes():
|
|||
CONFIG_JSON_SCHEMA,
|
||||
)
|
||||
validate_config_extra(config)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('config_obj', (
|
||||
[{
|
||||
'repo': 'local',
|
||||
'sha': 'foo',
|
||||
'hooks': [{
|
||||
'id': 'do_not_commit',
|
||||
'name': 'Block if "DO NOT COMMIT" is found',
|
||||
'entry': 'DO NOT COMMIT',
|
||||
'language': 'pcre',
|
||||
'files': '^(.*)$',
|
||||
}],
|
||||
}],
|
||||
))
|
||||
def test_config_with_local_hooks_definition_fails(config_obj):
|
||||
with pytest.raises((
|
||||
jsonschema.exceptions.ValidationError, InvalidConfigError
|
||||
)):
|
||||
jsonschema.validate(config_obj, CONFIG_JSON_SCHEMA)
|
||||
config = apply_defaults(config_obj, CONFIG_JSON_SCHEMA)
|
||||
validate_config_extra(config)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('config_obj', (
|
||||
[{
|
||||
'repo': 'local',
|
||||
'hooks': [{
|
||||
'id': 'arg-per-line',
|
||||
'name': 'Args per line hook',
|
||||
'entry': 'bin/hook.sh',
|
||||
'language': 'script',
|
||||
'files': '',
|
||||
'args': ['hello', 'world'],
|
||||
}],
|
||||
}],
|
||||
[{
|
||||
'repo': 'local',
|
||||
'hooks': [{
|
||||
'id': 'arg-per-line',
|
||||
'name': 'Args per line hook',
|
||||
'entry': 'bin/hook.sh',
|
||||
'language': 'script',
|
||||
'files': '',
|
||||
'args': ['hello', 'world'],
|
||||
}]
|
||||
}],
|
||||
))
|
||||
def test_config_with_local_hooks_definition_passes(config_obj):
|
||||
jsonschema.validate(config_obj, CONFIG_JSON_SCHEMA)
|
||||
config = apply_defaults(config_obj, CONFIG_JSON_SCHEMA)
|
||||
validate_config_extra(config)
|
||||
|
|
|
|||
|
|
@ -14,10 +14,12 @@ from pre_commit.commands.run import _get_skips
|
|||
from pre_commit.commands.run import _has_unmerged_paths
|
||||
from pre_commit.commands.run import get_changed_files
|
||||
from pre_commit.commands.run import run
|
||||
from pre_commit.ordereddict import OrderedDict
|
||||
from pre_commit.runner import Runner
|
||||
from pre_commit.util import cmd_output
|
||||
from pre_commit.util import cwd
|
||||
from testing.auto_namedtuple import auto_namedtuple
|
||||
from testing.fixtures import add_config_to_repo
|
||||
from testing.fixtures import make_consuming_repo
|
||||
|
||||
|
||||
|
|
@ -81,7 +83,7 @@ def _test_run(repo, options, expected_outputs, expected_ret, stage):
|
|||
stage_a_file()
|
||||
args = _get_opts(**options)
|
||||
ret, printed = _do_run(repo, args)
|
||||
assert ret == expected_ret
|
||||
assert ret == expected_ret, (ret, expected_ret, printed)
|
||||
for expected_output_part in expected_outputs:
|
||||
assert expected_output_part in printed
|
||||
|
||||
|
|
@ -313,9 +315,7 @@ def test_lots_of_files(mock_out_store_directory, tmpdir_factory):
|
|||
git_path = make_consuming_repo(tmpdir_factory, 'python_hooks_repo')
|
||||
with cwd(git_path):
|
||||
# Override files so we run against them
|
||||
with io.open(
|
||||
'.pre-commit-config.yaml', 'a+',
|
||||
) as config_file:
|
||||
with io.open('.pre-commit-config.yaml', 'a+') as config_file:
|
||||
config_file.write(' files: ""\n')
|
||||
|
||||
# Write a crap ton of files
|
||||
|
|
@ -334,3 +334,66 @@ def test_lots_of_files(mock_out_store_directory, tmpdir_factory):
|
|||
stderr=subprocess.STDOUT,
|
||||
env=env,
|
||||
)
|
||||
|
||||
|
||||
def test_local_hook_passes(
|
||||
repo_with_passing_hook, mock_out_store_directory,
|
||||
):
|
||||
config = OrderedDict((
|
||||
('repo', 'local'),
|
||||
('hooks', (OrderedDict((
|
||||
('id', 'pylint'),
|
||||
('name', 'PyLint'),
|
||||
('entry', 'python -m pylint.__main__'),
|
||||
('language', 'system'),
|
||||
('files', r'\.py$'),
|
||||
)), OrderedDict((
|
||||
('id', 'do_not_commit'),
|
||||
('name', 'Block if "DO NOT COMMIT" is found'),
|
||||
('entry', 'DO NOT COMMIT'),
|
||||
('language', 'pcre'),
|
||||
('files', '^(.*)$'),
|
||||
))))
|
||||
))
|
||||
add_config_to_repo(repo_with_passing_hook, config)
|
||||
|
||||
with io.open('dummy.py', 'w') as staged_file:
|
||||
staged_file.write('"""TODO: something"""\n')
|
||||
cmd_output('git', 'add', 'dummy.py')
|
||||
|
||||
_test_run(
|
||||
repo_with_passing_hook,
|
||||
options={},
|
||||
expected_outputs=[''],
|
||||
expected_ret=0,
|
||||
stage=False
|
||||
)
|
||||
|
||||
|
||||
def test_local_hook_fails(
|
||||
repo_with_passing_hook, mock_out_store_directory,
|
||||
):
|
||||
config = OrderedDict((
|
||||
('repo', 'local'),
|
||||
('hooks', [OrderedDict((
|
||||
('id', 'no-todo'),
|
||||
('name', 'No TODO'),
|
||||
('entry', 'grep -iI todo'),
|
||||
('expected_return_value', 1),
|
||||
('language', 'system'),
|
||||
('files', ''),
|
||||
))])
|
||||
))
|
||||
add_config_to_repo(repo_with_passing_hook, config)
|
||||
|
||||
with io.open('dummy.py', 'w') as staged_file:
|
||||
staged_file.write('"""TODO: something"""\n')
|
||||
cmd_output('git', 'add', 'dummy.py')
|
||||
|
||||
_test_run(
|
||||
repo_with_passing_hook,
|
||||
options={},
|
||||
expected_outputs=[''],
|
||||
expected_ret=1,
|
||||
stage=False
|
||||
)
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
|
|||
from pre_commit.clientlib.validate_config import validate_config_extra
|
||||
from pre_commit.jsonschema_extensions import apply_defaults
|
||||
from pre_commit.languages.python import PythonEnv
|
||||
from pre_commit.ordereddict import OrderedDict
|
||||
from pre_commit.repository import Repository
|
||||
from pre_commit.util import cmd_output
|
||||
from pre_commit.util import cwd
|
||||
|
|
@ -377,3 +378,22 @@ def test_tags_on_repositories(in_tmpdir, tmpdir_factory, store):
|
|||
ret = repo_2.run_hook(repo_2.hooks[0][1], ['bar'])
|
||||
assert ret[0] == 0
|
||||
assert ret[1] == 'bar\nHello World\n'
|
||||
|
||||
|
||||
def test_local_repository():
|
||||
config = OrderedDict((
|
||||
('repo', 'local'),
|
||||
('hooks', [OrderedDict((
|
||||
('id', 'do_not_commit'),
|
||||
('name', 'Block if "DO NOT COMMIT" is found'),
|
||||
('entry', 'DO NOT COMMIT'),
|
||||
('language', 'pcre'),
|
||||
('files', '^(.*)$'),
|
||||
))])
|
||||
))
|
||||
local_repo = Repository.create(config, 'dummy')
|
||||
with pytest.raises(NotImplementedError):
|
||||
local_repo.sha
|
||||
with pytest.raises(NotImplementedError):
|
||||
local_repo.manifest
|
||||
assert len(local_repo.hooks) == 1
|
||||
|
|
|
|||
|
|
@ -5,8 +5,10 @@ import os
|
|||
import os.path
|
||||
|
||||
import pre_commit.constants as C
|
||||
from pre_commit.ordereddict import OrderedDict
|
||||
from pre_commit.runner import Runner
|
||||
from pre_commit.util import cwd
|
||||
from testing.fixtures import add_config_to_repo
|
||||
from testing.fixtures import git_dir
|
||||
from testing.fixtures import make_consuming_repo
|
||||
|
||||
|
|
@ -52,6 +54,31 @@ def test_repositories(tmpdir_factory, mock_out_store_directory):
|
|||
assert len(runner.repositories) == 1
|
||||
|
||||
|
||||
def test_local_hooks(tmpdir_factory, mock_out_store_directory):
|
||||
config = OrderedDict((
|
||||
('repo', 'local'),
|
||||
('hooks', (OrderedDict((
|
||||
('id', 'arg-per-line'),
|
||||
('name', 'Args per line hook'),
|
||||
('entry', 'bin/hook.sh'),
|
||||
('language', 'script'),
|
||||
('files', ''),
|
||||
('args', ['hello', 'world']),
|
||||
)), OrderedDict((
|
||||
('id', 'do_not_commit'),
|
||||
('name', 'Block if "DO NOT COMMIT" is found'),
|
||||
('entry', 'DO NOT COMMIT'),
|
||||
('language', 'pcre'),
|
||||
('files', '^(.*)$'),
|
||||
))))
|
||||
))
|
||||
git_path = git_dir(tmpdir_factory)
|
||||
add_config_to_repo(git_path, config)
|
||||
runner = Runner(git_path)
|
||||
assert len(runner.repositories) == 1
|
||||
assert len(runner.repositories[0].hooks) == 2
|
||||
|
||||
|
||||
def test_pre_commit_path():
|
||||
runner = Runner(os.path.join('foo', 'bar'))
|
||||
expected_path = os.path.join('foo', 'bar', '.git', 'hooks', 'pre-commit')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue