mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-05 03:26:53 +00:00
New hook 'destroyed-symlinks' to detect symlinks which are changed to regular files with a content of a path which that symlink was pointing to; move zsplit to util
This commit is contained in:
parent
14e9f0e512
commit
1e87d59a2d
9 changed files with 204 additions and 18 deletions
|
|
@ -102,16 +102,6 @@ def test_check_git_filemode_failing(tmpdir):
|
|||
assert check_executables_have_shebangs._check_git_filemode(files) == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize('out', ('\0f1\0f2\0', '\0f1\0f2', 'f1\0f2\0'))
|
||||
def test_check_zsplits_correctly(out):
|
||||
assert check_executables_have_shebangs.zsplit(out) == ['f1', 'f2']
|
||||
|
||||
|
||||
@pytest.mark.parametrize('out', ('\0\0', '\0', ''))
|
||||
def test_check_zsplit_returns_empty(out):
|
||||
assert check_executables_have_shebangs.zsplit(out) == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
('content', 'mode', 'expected'),
|
||||
(
|
||||
|
|
|
|||
74
tests/destroyed_symlinks_test.py
Normal file
74
tests/destroyed_symlinks_test.py
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
import os
|
||||
import subprocess
|
||||
|
||||
import pytest
|
||||
|
||||
from pre_commit_hooks.destroyed_symlinks import find_destroyed_symlinks
|
||||
from pre_commit_hooks.destroyed_symlinks import main
|
||||
|
||||
TEST_SYMLINK = 'test_symlink'
|
||||
TEST_SYMLINK_TARGET = '/doesnt/really/matters'
|
||||
TEST_FILE = 'test_file'
|
||||
TEST_FILE_RENAMED = f'{TEST_FILE}_renamed'
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def repo_with_destroyed_symlink(tmpdir):
|
||||
source_repo = tmpdir.join('src')
|
||||
os.makedirs(source_repo, exist_ok=True)
|
||||
test_repo = tmpdir.join('test')
|
||||
with source_repo.as_cwd():
|
||||
subprocess.check_call(('git', 'init'))
|
||||
os.symlink(TEST_SYMLINK_TARGET, TEST_SYMLINK)
|
||||
with open(TEST_FILE, 'w') as f:
|
||||
print('some random content', file=f)
|
||||
subprocess.check_call(('git', 'add', '.'))
|
||||
subprocess.check_call(
|
||||
('git', 'commit', '--no-gpg-sign', '-m', 'initial'),
|
||||
)
|
||||
assert b'120000 ' in subprocess.check_output(
|
||||
('git', 'cat-file', '-p', 'HEAD^{tree}'),
|
||||
)
|
||||
subprocess.check_call(
|
||||
('git', '-c', 'core.symlinks=false', 'clone', source_repo, test_repo),
|
||||
)
|
||||
with test_repo.as_cwd():
|
||||
subprocess.check_call(
|
||||
('git', 'config', '--local', 'core.symlinks', 'true'),
|
||||
)
|
||||
subprocess.check_call(('git', 'mv', TEST_FILE, TEST_FILE_RENAMED))
|
||||
assert not os.path.islink(test_repo.join(TEST_SYMLINK))
|
||||
yield test_repo
|
||||
|
||||
|
||||
def test_find_destroyed_symlinks(repo_with_destroyed_symlink):
|
||||
with repo_with_destroyed_symlink.as_cwd():
|
||||
assert find_destroyed_symlinks([]) == []
|
||||
assert main([]) == 0
|
||||
|
||||
subprocess.check_call(('git', 'add', TEST_SYMLINK))
|
||||
assert find_destroyed_symlinks([TEST_SYMLINK]) == [TEST_SYMLINK]
|
||||
assert find_destroyed_symlinks([]) == []
|
||||
assert main([]) == 0
|
||||
assert find_destroyed_symlinks([TEST_FILE_RENAMED, TEST_FILE]) == []
|
||||
ALL_STAGED = [TEST_SYMLINK, TEST_FILE_RENAMED]
|
||||
assert find_destroyed_symlinks(ALL_STAGED) == [TEST_SYMLINK]
|
||||
assert main(ALL_STAGED) != 0
|
||||
|
||||
with open(TEST_SYMLINK, 'a') as f:
|
||||
print(file=f) # add trailing newline
|
||||
subprocess.check_call(['git', 'add', TEST_SYMLINK])
|
||||
assert find_destroyed_symlinks(ALL_STAGED) == [TEST_SYMLINK]
|
||||
assert main(ALL_STAGED) != 0
|
||||
|
||||
with open(TEST_SYMLINK, 'w') as f:
|
||||
print('0' * len(TEST_SYMLINK_TARGET), file=f)
|
||||
subprocess.check_call(('git', 'add', TEST_SYMLINK))
|
||||
assert find_destroyed_symlinks(ALL_STAGED) == []
|
||||
assert main(ALL_STAGED) == 0
|
||||
|
||||
with open(TEST_SYMLINK, 'w') as f:
|
||||
print('0' * (len(TEST_SYMLINK_TARGET) + 3), file=f)
|
||||
subprocess.check_call(('git', 'add', TEST_SYMLINK))
|
||||
assert find_destroyed_symlinks(ALL_STAGED) == []
|
||||
assert main(ALL_STAGED) == 0
|
||||
|
|
@ -2,6 +2,7 @@ import pytest
|
|||
|
||||
from pre_commit_hooks.util import CalledProcessError
|
||||
from pre_commit_hooks.util import cmd_output
|
||||
from pre_commit_hooks.util import zsplit
|
||||
|
||||
|
||||
def test_raises_on_error():
|
||||
|
|
@ -12,3 +13,13 @@ def test_raises_on_error():
|
|||
def test_output():
|
||||
ret = cmd_output('sh', '-c', 'echo hi')
|
||||
assert ret == 'hi\n'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('out', ('\0f1\0f2\0', '\0f1\0f2', 'f1\0f2\0'))
|
||||
def test_check_zsplits_str_correctly(out):
|
||||
assert zsplit(out) == ['f1', 'f2']
|
||||
|
||||
|
||||
@pytest.mark.parametrize('out', ('\0\0', '\0', ''))
|
||||
def test_check_zsplit_returns_empty(out):
|
||||
assert zsplit(out) == []
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue