New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Types #551

Merged
merged 4 commits into from Jul 3, 2017
Jump to file or symbol
Failed to load files and symbols.
+46 鈭13
Diff settings

Always

Just for now

Viewing a subset of changes. View all
Prev

Implement exclude_types

  • Loading branch information...
asottile committed Jul 3, 2017
commit 05a108efe196552292004e4dc7ff9e19b9d73267
View
@@ -49,7 +49,13 @@ def _make_argparser(filenames_help):
'files', schema.check_and(schema.check_string, schema.check_regex),
'',
),
schema.Optional(
'exclude',
schema.check_and(schema.check_string, schema.check_regex),
'^$',
),
schema.Optional('types', schema.check_array(check_type_tag), ['file']),
schema.Optional('exclude_types', schema.check_array(check_type_tag), []),
schema.Optional(
'additional_dependencies', schema.check_array(schema.check_string), [],
@@ -58,11 +64,6 @@ def _make_argparser(filenames_help):
schema.Optional('always_run', schema.check_bool, False),
schema.Optional('pass_filenames', schema.check_bool, True),
schema.Optional('description', schema.check_string, ''),
schema.Optional(
'exclude',
schema.check_and(schema.check_string, schema.check_regex),
'^$',
),
schema.Optional('language_version', schema.check_string, 'default'),
schema.Optional('log_file', schema.check_string, ''),
schema.Optional('minimum_pre_commit_version', schema.check_string, '0'),
View
@@ -43,12 +43,14 @@ def get_changed_files(new, old):
)[1].splitlines()
def filter_filenames_by_types(filenames, types):
types = frozenset(types)
return tuple(
filename for filename in filenames
if tags_from_path(filename) >= types
)
def filter_filenames_by_types(filenames, types, exclude_types):
types, exclude_types = frozenset(types), frozenset(exclude_types)
ret = []
for filename in filenames:
tags = tags_from_path(filename)
if tags >= types and not tags & exclude_types:
ret.append(filename)
return tuple(ret)
def get_filenames(args, include_expr, exclude_expr):
@@ -73,7 +75,9 @@ def get_filenames(args, include_expr, exclude_expr):
def _run_single_hook(hook, repo, args, skips, cols):
filenames = get_filenames(args, hook['files'], hook['exclude'])
filenames = filter_filenames_by_types(filenames, hook['types'])
filenames = filter_filenames_by_types(
filenames, hook['types'], hook['exclude_types'],
)
if hook['id'] in skips:
output.write(get_hook_message(
_hook_msg_start(hook, args.verbose),
@@ -0,0 +1,6 @@
- id: python-files
name: Python files
entry: bin/hook.sh
language: script
types: [python]
exclude_types: [python3]
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
echo $@
exit 1
View
@@ -20,6 +20,7 @@
from pre_commit.runner import Runner
from pre_commit.util import cmd_output
from pre_commit.util import cwd
from pre_commit.util import make_executable
from testing.auto_namedtuple import auto_namedtuple
from testing.fixtures import add_config_to_repo
from testing.fixtures import make_consuming_repo
@@ -43,7 +44,7 @@ def repo_with_failing_hook(tempdir_factory):
def stage_a_file(filename='foo.py'):
cmd_output('touch', filename)
open(filename, 'a').close()
cmd_output('git', 'add', filename)
@@ -166,6 +167,22 @@ def test_types_hook_repository(
assert b'bar.notpy' not in printed

This comment has been minimized.

@Lucas-C

Lucas-C Jul 2, 2017

Contributor

The new types_repo/bin/hook.sh file is not tested ?

@Lucas-C

Lucas-C Jul 2, 2017

Contributor

The new types_repo/bin/hook.sh file is not tested ?

This comment has been minimized.

@asottile

asottile Jul 2, 2017

Member

All hook.sh does is print the filenames and invariantly fail -- I think this tests that?

@asottile

asottile Jul 2, 2017

Member

All hook.sh does is print the filenames and invariantly fail -- I think this tests that?

This comment has been minimized.

@Lucas-C

Lucas-C Jul 2, 2017

Contributor

Yes sure. Actually I thought hook.sh role was to test shebang detection.
My mistake

@Lucas-C

Lucas-C Jul 2, 2017

Contributor

Yes sure. Actually I thought hook.sh role was to test shebang detection.
My mistake

def test_exclude_types_hook_repository(
cap_out, tempdir_factory, mock_out_store_directory,
):
git_path = make_consuming_repo(tempdir_factory, 'exclude_types_repo')
with cwd(git_path):
with io.open('exe', 'w') as exe:
exe.write('#!/usr/bin/env python3\n')
make_executable('exe')
cmd_output('git', 'add', 'exe')
stage_a_file('bar.py')
ret, printed = _do_run(cap_out, git_path, _get_opts())
assert ret == 1
assert b'bar.py' in printed
assert b'exe' not in printed
def test_show_diff_on_failure(
capfd, cap_out, tempdir_factory, mock_out_store_directory,
):
View
@@ -35,6 +35,7 @@ def test_manifest_contents(manifest):
'pass_filenames': True,
'stages': [],
'types': ['file'],
'exclude_types': [],
}]
@@ -56,6 +57,7 @@ def test_hooks(manifest):
'pass_filenames': True,
'stages': [],
'types': ['file'],
'exclude_types': [],
}
ProTip! Use n and p to navigate between commits in a pull request.