mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Refactor filename collection for hooks
This commit is contained in:
parent
3cc5aa023e
commit
6af60158ec
4 changed files with 80 additions and 106 deletions
|
|
@ -12,6 +12,7 @@ import pytest
|
|||
import pre_commit.constants as C
|
||||
from pre_commit.commands.install_uninstall import install
|
||||
from pre_commit.commands.run import _compute_cols
|
||||
from pre_commit.commands.run import _filter_by_include_exclude
|
||||
from pre_commit.commands.run import _get_skips
|
||||
from pre_commit.commands.run import _has_unmerged_paths
|
||||
from pre_commit.commands.run import run
|
||||
|
|
@ -25,6 +26,7 @@ from testing.fixtures import make_consuming_repo
|
|||
from testing.fixtures import modify_config
|
||||
from testing.fixtures import read_config
|
||||
from testing.util import cmd_output_mocked_pre_commit_home
|
||||
from testing.util import xfailif_no_symlink
|
||||
|
||||
|
||||
@pytest.yield_fixture
|
||||
|
|
@ -744,3 +746,45 @@ def test_fail_fast(
|
|||
ret, printed = _do_run(cap_out, repo_with_failing_hook, _get_opts())
|
||||
# it should have only run one hook
|
||||
assert printed.count(b'Failing hook') == 1
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def some_filenames():
|
||||
return (
|
||||
'.pre-commit-hooks.yaml',
|
||||
'pre_commit/main.py',
|
||||
'pre_commit/git.py',
|
||||
'im_a_file_that_doesnt_exist.py',
|
||||
)
|
||||
|
||||
|
||||
def test_include_exclude_base_case(some_filenames):
|
||||
ret = _filter_by_include_exclude(some_filenames, '', '^$')
|
||||
assert ret == {
|
||||
'.pre-commit-hooks.yaml',
|
||||
'pre_commit/main.py',
|
||||
'pre_commit/git.py',
|
||||
}
|
||||
|
||||
|
||||
@xfailif_no_symlink
|
||||
def test_matches_broken_symlink(tmpdir): # pramga: no cover (non-windows)
|
||||
with tmpdir.as_cwd():
|
||||
os.symlink('does-not-exist', 'link')
|
||||
ret = _filter_by_include_exclude({'link'}, '', '^$')
|
||||
assert ret == {'link'}
|
||||
|
||||
|
||||
def test_include_exclude_total_match(some_filenames):
|
||||
ret = _filter_by_include_exclude(some_filenames, r'^.*\.py$', '^$')
|
||||
assert ret == {'pre_commit/main.py', 'pre_commit/git.py'}
|
||||
|
||||
|
||||
def test_include_exclude_does_search_instead_of_match(some_filenames):
|
||||
ret = _filter_by_include_exclude(some_filenames, r'\.yaml$', '^$')
|
||||
assert ret == {'.pre-commit-hooks.yaml'}
|
||||
|
||||
|
||||
def test_include_exclude_exclude_removes_files(some_filenames):
|
||||
ret = _filter_by_include_exclude(some_filenames, '', r'\.py$')
|
||||
assert ret == {'.pre-commit-hooks.yaml'}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,6 @@ from pre_commit.errors import FatalError
|
|||
from pre_commit.util import cmd_output
|
||||
from pre_commit.util import cwd
|
||||
from testing.fixtures import git_dir
|
||||
from testing.util import xfailif_no_symlink
|
||||
|
||||
|
||||
def test_get_root_at_root(tempdir_factory):
|
||||
|
|
@ -66,56 +65,6 @@ def test_cherry_pick_conflict(in_merge_conflict):
|
|||
assert git.is_in_merge_conflict() is False
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def get_files_matching_func():
|
||||
def get_filenames():
|
||||
return (
|
||||
'.pre-commit-hooks.yaml',
|
||||
'pre_commit/main.py',
|
||||
'pre_commit/git.py',
|
||||
'im_a_file_that_doesnt_exist.py',
|
||||
)
|
||||
|
||||
return git.get_files_matching(get_filenames)
|
||||
|
||||
|
||||
def test_get_files_matching_base(get_files_matching_func):
|
||||
ret = get_files_matching_func('', '^$')
|
||||
assert ret == {
|
||||
'.pre-commit-hooks.yaml',
|
||||
'pre_commit/main.py',
|
||||
'pre_commit/git.py',
|
||||
}
|
||||
|
||||
|
||||
@xfailif_no_symlink
|
||||
def test_matches_broken_symlink(tmpdir): # pragma: no cover (non-windwos)
|
||||
with tmpdir.as_cwd():
|
||||
os.symlink('does-not-exist', 'link')
|
||||
func = git.get_files_matching(lambda: ('link',))
|
||||
assert func('', '^$') == {'link'}
|
||||
|
||||
|
||||
def test_get_files_matching_total_match(get_files_matching_func):
|
||||
ret = get_files_matching_func('^.*\\.py$', '^$')
|
||||
assert ret == {'pre_commit/main.py', 'pre_commit/git.py'}
|
||||
|
||||
|
||||
def test_does_search_instead_of_match(get_files_matching_func):
|
||||
ret = get_files_matching_func('\\.yaml$', '^$')
|
||||
assert ret == {'.pre-commit-hooks.yaml'}
|
||||
|
||||
|
||||
def test_does_not_include_deleted_fileS(get_files_matching_func):
|
||||
ret = get_files_matching_func('exist.py', '^$')
|
||||
assert ret == set()
|
||||
|
||||
|
||||
def test_exclude_removes_files(get_files_matching_func):
|
||||
ret = get_files_matching_func('', '\\.py$')
|
||||
assert ret == {'.pre-commit-hooks.yaml'}
|
||||
|
||||
|
||||
def resolve_conflict():
|
||||
with open('conflict_file', 'w') as conflicted_file:
|
||||
conflicted_file.write('herp\nderp\n')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue