From 72b61a81f946f35d123adf0712b9d20fbadb6010 Mon Sep 17 00:00:00 2001 From: Dmitriy Kunitskiy Date: Mon, 20 Jul 2015 14:51:25 -0700 Subject: [PATCH] fix for issue 246 --- pre_commit/commands/install_uninstall.py | 6 +++++- testing/util.py | 5 +++++ tests/commands/install_uninstall_test.py | 10 ++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/pre_commit/commands/install_uninstall.py b/pre_commit/commands/install_uninstall.py index 47c9484c..d7a03c09 100644 --- a/pre_commit/commands/install_uninstall.py +++ b/pre_commit/commands/install_uninstall.py @@ -27,10 +27,14 @@ IDENTIFYING_HASH = '79f09a650522a87b0da915d0d983b2de' def is_our_pre_commit(filename): + if not os.path.exists(filename): + return False return IDENTIFYING_HASH in io.open(filename).read() def is_previous_pre_commit(filename): + if not os.path.exists(filename): + return False contents = io.open(filename).read() return any(hash in contents for hash in PREVIOUS_IDENTIFYING_HASHES) @@ -53,7 +57,7 @@ def install(runner, overwrite=False, hooks=False, hook_type='pre-commit'): # If we have an existing hook, move it to pre-commit.legacy if ( - os.path.exists(hook_path) and + os.path.lexists(hook_path) and not is_our_pre_commit(hook_path) and not is_previous_pre_commit(hook_path) ): diff --git a/testing/util.py b/testing/util.py index d1b8e988..20a01a0f 100644 --- a/testing/util.py +++ b/testing/util.py @@ -73,3 +73,8 @@ xfailif_no_pcre_support = pytest.mark.xfail( not platform_supports_pcre(), reason='grep -P is not supported on this platform', ) + +xfailif_no_symlink = pytest.mark.xfail( + not hasattr(os, 'symlink'), + reason='Symlink is not supported on this platform', +) diff --git a/tests/commands/install_uninstall_test.py b/tests/commands/install_uninstall_test.py index ca82c061..85a241f7 100644 --- a/tests/commands/install_uninstall_test.py +++ b/tests/commands/install_uninstall_test.py @@ -24,6 +24,7 @@ from pre_commit.util import cwd from pre_commit.util import resource_filename from testing.fixtures import git_dir from testing.fixtures import make_consuming_repo +from testing.util import xfailif_no_symlink def test_is_not_our_pre_commit(): @@ -88,6 +89,15 @@ def test_install_hooks_directory_not_present(tmpdir_factory): assert os.path.exists(runner.pre_commit_path) +@xfailif_no_symlink +def test_install_hooks_dead_symlink(tmpdir_factory): + path = git_dir(tmpdir_factory) + os.symlink('/fake/baz', os.path.join(path, '.git', 'hooks', 'pre-commit')) + runner = Runner(path) + install(runner) + assert os.path.exists(runner.pre_commit_path) + + def test_uninstall_does_not_blow_up_when_not_there(tmpdir_factory): path = git_dir(tmpdir_factory) runner = Runner(path)