Skip to content

Commit

Permalink
check also directories in check_case_conflict
Browse files Browse the repository at this point in the history
fix #70
  • Loading branch information
slsyy committed Mar 19, 2021
1 parent 4b89101 commit e567150
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pre_commit_hooks/check_case_conflict.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import argparse
from typing import Iterable
from typing import Iterator
from typing import Optional
from typing import Sequence
from typing import Set
Expand All @@ -12,9 +13,23 @@ def lower_set(iterable: Iterable[str]) -> Set[str]:
return {x.lower() for x in iterable}


def parents(file: str) -> Iterator[str]:
while True:
file = file[:file.rfind('/')]
if not file:
return
yield file


def directories_for(files: Set[str]) -> Set[str]:
return {parent for file in files for parent in parents(file)}


def find_conflicting_filenames(filenames: Sequence[str]) -> int:
repo_files = set(cmd_output('git', 'ls-files').splitlines())
repo_files |= directories_for(repo_files)
relevant_files = set(filenames) | added_files()
relevant_files |= directories_for(relevant_files)
repo_files -= relevant_files
retv = 0

Expand Down
60 changes: 60 additions & 0 deletions tests/check_case_conflict_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
import sys

import pytest

from pre_commit_hooks.check_case_conflict import find_conflicting_filenames
from pre_commit_hooks.check_case_conflict import main
from pre_commit_hooks.check_case_conflict import parents
from pre_commit_hooks.util import cmd_output

skip_win32 = pytest.mark.skipif(
sys.platform == 'win32',
reason='case conflicts between directories and files',
)


def test_parents():
assert set(parents('a')) == set()
assert set(parents('a/b')) == {'a'}
assert set(parents('a/b/c')) == {'a/b', 'a'}
assert set(parents('a/b/c/d')) == {'a/b/c', 'a/b', 'a'}


def test_nothing_added(temp_git_dir):
with temp_git_dir.as_cwd():
Expand All @@ -26,6 +43,36 @@ def test_adding_something_with_conflict(temp_git_dir):
assert find_conflicting_filenames(['f.py', 'F.py']) == 1


@skip_win32 # pragma: win32 no cover
def test_adding_files_with_conflicting_directories(temp_git_dir):
with temp_git_dir.as_cwd():
temp_git_dir.mkdir('dir').join('x').write('foo')
temp_git_dir.mkdir('DIR').join('y').write('foo')
cmd_output('git', 'add', '-A')

assert find_conflicting_filenames([]) == 1


@skip_win32 # pragma: win32 no cover
def test_adding_files_with_conflicting_deep_directories(temp_git_dir):
with temp_git_dir.as_cwd():
temp_git_dir.mkdir('x').mkdir('y').join('z').write('foo')
temp_git_dir.join('X').write('foo')
cmd_output('git', 'add', '-A')

assert find_conflicting_filenames([]) == 1


@skip_win32 # pragma: win32 no cover
def test_adding_file_with_conflicting_directory(temp_git_dir):
with temp_git_dir.as_cwd():
temp_git_dir.mkdir('dir').join('x').write('foo')
temp_git_dir.join('DIR').write('foo')
cmd_output('git', 'add', '-A')

assert find_conflicting_filenames([]) == 1


def test_added_file_not_in_pre_commits_list(temp_git_dir):
with temp_git_dir.as_cwd():
temp_git_dir.join('f.py').write("print('hello world')")
Expand All @@ -46,6 +93,19 @@ def test_file_conflicts_with_committed_file(temp_git_dir):
assert find_conflicting_filenames(['F.py']) == 1


@skip_win32 # pragma: win32 no cover
def test_file_conflicts_with_committed_dir(temp_git_dir):
with temp_git_dir.as_cwd():
temp_git_dir.mkdir('dir').join('x').write('foo')
cmd_output('git', 'add', '-A')
cmd_output('git', 'commit', '--no-gpg-sign', '-n', '-m', 'Add f.py')

temp_git_dir.join('DIR').write('foo')
cmd_output('git', 'add', '-A')

assert find_conflicting_filenames([]) == 1


def test_integration(temp_git_dir):
with temp_git_dir.as_cwd():
assert main(argv=[]) == 0
Expand Down

0 comments on commit e567150

Please sign in to comment.