|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from pre_commit_hooks.destroyed_symlinks import find_destroyed_symlinks |
| 7 | +from pre_commit_hooks.destroyed_symlinks import main |
| 8 | + |
| 9 | +TEST_SYMLINK = 'test_symlink' |
| 10 | +TEST_SYMLINK_TARGET = '/doesnt/really/matters' |
| 11 | +TEST_FILE = 'test_file' |
| 12 | +TEST_FILE_RENAMED = f'{TEST_FILE}_renamed' |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture |
| 16 | +def repo_with_destroyed_symlink(tmpdir): |
| 17 | + source_repo = tmpdir.join('src') |
| 18 | + os.makedirs(source_repo, exist_ok=True) |
| 19 | + test_repo = tmpdir.join('test') |
| 20 | + with source_repo.as_cwd(): |
| 21 | + subprocess.check_call(('git', 'init')) |
| 22 | + os.symlink(TEST_SYMLINK_TARGET, TEST_SYMLINK) |
| 23 | + with open(TEST_FILE, 'w') as f: |
| 24 | + print('some random content', file=f) |
| 25 | + subprocess.check_call(('git', 'add', '.')) |
| 26 | + subprocess.check_call( |
| 27 | + ('git', 'commit', '--no-gpg-sign', '-m', 'initial'), |
| 28 | + ) |
| 29 | + assert b'120000 ' in subprocess.check_output( |
| 30 | + ('git', 'cat-file', '-p', 'HEAD^{tree}'), |
| 31 | + ) |
| 32 | + subprocess.check_call( |
| 33 | + ('git', '-c', 'core.symlinks=false', 'clone', source_repo, test_repo), |
| 34 | + ) |
| 35 | + with test_repo.as_cwd(): |
| 36 | + subprocess.check_call( |
| 37 | + ('git', 'config', '--local', 'core.symlinks', 'true'), |
| 38 | + ) |
| 39 | + subprocess.check_call(('git', 'mv', TEST_FILE, TEST_FILE_RENAMED)) |
| 40 | + assert not os.path.islink(test_repo.join(TEST_SYMLINK)) |
| 41 | + yield test_repo |
| 42 | + |
| 43 | + |
| 44 | +def test_find_destroyed_symlinks(repo_with_destroyed_symlink): |
| 45 | + with repo_with_destroyed_symlink.as_cwd(): |
| 46 | + assert find_destroyed_symlinks([]) == [] |
| 47 | + assert main([]) == 0 |
| 48 | + |
| 49 | + subprocess.check_call(('git', 'add', TEST_SYMLINK)) |
| 50 | + assert find_destroyed_symlinks([TEST_SYMLINK]) == [TEST_SYMLINK] |
| 51 | + assert find_destroyed_symlinks([]) == [] |
| 52 | + assert main([]) == 0 |
| 53 | + assert find_destroyed_symlinks([TEST_FILE_RENAMED, TEST_FILE]) == [] |
| 54 | + ALL_STAGED = [TEST_SYMLINK, TEST_FILE_RENAMED] |
| 55 | + assert find_destroyed_symlinks(ALL_STAGED) == [TEST_SYMLINK] |
| 56 | + assert main(ALL_STAGED) != 0 |
| 57 | + |
| 58 | + with open(TEST_SYMLINK, 'a') as f: |
| 59 | + print(file=f) # add trailing newline |
| 60 | + subprocess.check_call(['git', 'add', TEST_SYMLINK]) |
| 61 | + assert find_destroyed_symlinks(ALL_STAGED) == [TEST_SYMLINK] |
| 62 | + assert main(ALL_STAGED) != 0 |
| 63 | + |
| 64 | + with open(TEST_SYMLINK, 'w') as f: |
| 65 | + print('0' * len(TEST_SYMLINK_TARGET), file=f) |
| 66 | + subprocess.check_call(('git', 'add', TEST_SYMLINK)) |
| 67 | + assert find_destroyed_symlinks(ALL_STAGED) == [] |
| 68 | + assert main(ALL_STAGED) == 0 |
| 69 | + |
| 70 | + with open(TEST_SYMLINK, 'w') as f: |
| 71 | + print('0' * (len(TEST_SYMLINK_TARGET) + 3), file=f) |
| 72 | + subprocess.check_call(('git', 'add', TEST_SYMLINK)) |
| 73 | + assert find_destroyed_symlinks(ALL_STAGED) == [] |
| 74 | + assert main(ALL_STAGED) == 0 |
0 commit comments