mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Refactor meta hooks
This commit is contained in:
parent
a0a8fc15ff
commit
9db827ef9d
8 changed files with 329 additions and 164 deletions
0
tests/meta_hooks/__init__.py
Normal file
0
tests/meta_hooks/__init__.py
Normal file
130
tests/meta_hooks/hook_matches_any_test.py
Normal file
130
tests/meta_hooks/hook_matches_any_test.py
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
from collections import OrderedDict
|
||||
|
||||
from pre_commit.meta_hooks import check_files_matches_any
|
||||
from pre_commit.util import cwd
|
||||
from testing.fixtures import add_config_to_repo
|
||||
from testing.fixtures import git_dir
|
||||
|
||||
|
||||
def test_hook_excludes_everything(
|
||||
capsys, tempdir_factory, mock_out_store_directory,
|
||||
):
|
||||
config = OrderedDict((
|
||||
('repo', 'meta'),
|
||||
(
|
||||
'hooks', (
|
||||
OrderedDict((
|
||||
('id', 'check-useless-excludes'),
|
||||
('exclude', '.pre-commit-config.yaml'),
|
||||
)),
|
||||
),
|
||||
),
|
||||
))
|
||||
|
||||
repo = git_dir(tempdir_factory)
|
||||
add_config_to_repo(repo, config)
|
||||
|
||||
with cwd(repo):
|
||||
assert check_files_matches_any.main(argv=[]) == 1
|
||||
|
||||
out, _ = capsys.readouterr()
|
||||
assert 'check-useless-excludes does not apply to this repository' in out
|
||||
|
||||
|
||||
def test_hook_includes_nothing(
|
||||
capsys, tempdir_factory, mock_out_store_directory,
|
||||
):
|
||||
config = OrderedDict((
|
||||
('repo', 'meta'),
|
||||
(
|
||||
'hooks', (
|
||||
OrderedDict((
|
||||
('id', 'check-useless-excludes'),
|
||||
('files', 'foo'),
|
||||
)),
|
||||
),
|
||||
),
|
||||
))
|
||||
|
||||
repo = git_dir(tempdir_factory)
|
||||
add_config_to_repo(repo, config)
|
||||
|
||||
with cwd(repo):
|
||||
assert check_files_matches_any.main(argv=[]) == 1
|
||||
|
||||
out, _ = capsys.readouterr()
|
||||
assert 'check-useless-excludes does not apply to this repository' in out
|
||||
|
||||
|
||||
def test_hook_types_not_matched(
|
||||
capsys, tempdir_factory, mock_out_store_directory,
|
||||
):
|
||||
config = OrderedDict((
|
||||
('repo', 'meta'),
|
||||
(
|
||||
'hooks', (
|
||||
OrderedDict((
|
||||
('id', 'check-useless-excludes'),
|
||||
('types', ['python']),
|
||||
)),
|
||||
),
|
||||
),
|
||||
))
|
||||
|
||||
repo = git_dir(tempdir_factory)
|
||||
add_config_to_repo(repo, config)
|
||||
|
||||
with cwd(repo):
|
||||
assert check_files_matches_any.main(argv=[]) == 1
|
||||
|
||||
out, _ = capsys.readouterr()
|
||||
assert 'check-useless-excludes does not apply to this repository' in out
|
||||
|
||||
|
||||
def test_hook_types_excludes_everything(
|
||||
capsys, tempdir_factory, mock_out_store_directory,
|
||||
):
|
||||
config = OrderedDict((
|
||||
('repo', 'meta'),
|
||||
(
|
||||
'hooks', (
|
||||
OrderedDict((
|
||||
('id', 'check-useless-excludes'),
|
||||
('exclude_types', ['yaml']),
|
||||
)),
|
||||
),
|
||||
),
|
||||
))
|
||||
|
||||
repo = git_dir(tempdir_factory)
|
||||
add_config_to_repo(repo, config)
|
||||
|
||||
with cwd(repo):
|
||||
assert check_files_matches_any.main(argv=[]) == 1
|
||||
|
||||
out, _ = capsys.readouterr()
|
||||
assert 'check-useless-excludes does not apply to this repository' in out
|
||||
|
||||
|
||||
def test_valid_includes(
|
||||
capsys, tempdir_factory, mock_out_store_directory,
|
||||
):
|
||||
config = OrderedDict((
|
||||
('repo', 'meta'),
|
||||
(
|
||||
'hooks', (
|
||||
OrderedDict((
|
||||
('id', 'check-useless-excludes'),
|
||||
)),
|
||||
),
|
||||
),
|
||||
))
|
||||
|
||||
repo = git_dir(tempdir_factory)
|
||||
add_config_to_repo(repo, config)
|
||||
|
||||
with cwd(repo):
|
||||
assert check_files_matches_any.main(argv=[]) == 0
|
||||
|
||||
out, _ = capsys.readouterr()
|
||||
assert out == ''
|
||||
107
tests/meta_hooks/useless_excludes_test.py
Normal file
107
tests/meta_hooks/useless_excludes_test.py
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
from collections import OrderedDict
|
||||
|
||||
from pre_commit.meta_hooks import check_useless_excludes
|
||||
from pre_commit.util import cwd
|
||||
from testing.fixtures import add_config_to_repo
|
||||
from testing.fixtures import git_dir
|
||||
|
||||
|
||||
def test_useless_exclude_global(capsys, tempdir_factory):
|
||||
config = OrderedDict((
|
||||
('exclude', 'foo'),
|
||||
(
|
||||
'repos', [
|
||||
OrderedDict((
|
||||
('repo', 'meta'),
|
||||
(
|
||||
'hooks', (
|
||||
OrderedDict((
|
||||
('id', 'check-useless-excludes'),
|
||||
)),
|
||||
),
|
||||
),
|
||||
)),
|
||||
],
|
||||
),
|
||||
))
|
||||
|
||||
repo = git_dir(tempdir_factory)
|
||||
add_config_to_repo(repo, config)
|
||||
|
||||
with cwd(repo):
|
||||
assert check_useless_excludes.main(argv=[]) == 1
|
||||
|
||||
out, _ = capsys.readouterr()
|
||||
assert "The global exclude pattern 'foo' does not match any files" in out
|
||||
|
||||
|
||||
def test_useless_exclude_for_hook(capsys, tempdir_factory):
|
||||
config = OrderedDict((
|
||||
('repo', 'meta'),
|
||||
(
|
||||
'hooks', (
|
||||
OrderedDict((
|
||||
('id', 'check-useless-excludes'),
|
||||
('exclude', 'foo'),
|
||||
)),
|
||||
),
|
||||
),
|
||||
))
|
||||
|
||||
repo = git_dir(tempdir_factory)
|
||||
add_config_to_repo(repo, config)
|
||||
|
||||
with cwd(repo):
|
||||
assert check_useless_excludes.main(argv=[]) == 1
|
||||
|
||||
out, _ = capsys.readouterr()
|
||||
expected = (
|
||||
"The exclude pattern 'foo' for check-useless-excludes "
|
||||
"does not match any files"
|
||||
)
|
||||
assert expected in out
|
||||
|
||||
|
||||
def test_no_excludes(capsys, tempdir_factory):
|
||||
config = OrderedDict((
|
||||
('repo', 'meta'),
|
||||
(
|
||||
'hooks', (
|
||||
OrderedDict((
|
||||
('id', 'check-useless-excludes'),
|
||||
)),
|
||||
),
|
||||
),
|
||||
))
|
||||
|
||||
repo = git_dir(tempdir_factory)
|
||||
add_config_to_repo(repo, config)
|
||||
|
||||
with cwd(repo):
|
||||
assert check_useless_excludes.main(argv=[]) == 0
|
||||
|
||||
out, _ = capsys.readouterr()
|
||||
assert out == ''
|
||||
|
||||
|
||||
def test_valid_exclude(capsys, tempdir_factory):
|
||||
config = OrderedDict((
|
||||
('repo', 'meta'),
|
||||
(
|
||||
'hooks', (
|
||||
OrderedDict((
|
||||
('id', 'check-useless-excludes'),
|
||||
('exclude', '.pre-commit-config.yaml'),
|
||||
)),
|
||||
),
|
||||
),
|
||||
))
|
||||
|
||||
repo = git_dir(tempdir_factory)
|
||||
add_config_to_repo(repo, config)
|
||||
|
||||
with cwd(repo):
|
||||
assert check_useless_excludes.main(argv=[]) == 0
|
||||
|
||||
out, _ = capsys.readouterr()
|
||||
assert out == ''
|
||||
Loading…
Add table
Add a link
Reference in a new issue