Fix double legacy install on windows

This commit is contained in:
Anthony Sottile 2019-04-27 15:10:01 -07:00
parent b8300268bf
commit af2c6de9ae
2 changed files with 12 additions and 1 deletions

View file

@ -5,6 +5,7 @@ import io
import itertools import itertools
import logging import logging
import os.path import os.path
import shutil
import sys import sys
from pre_commit import git from pre_commit import git
@ -84,7 +85,7 @@ def install(
# If we have an existing hook, move it to pre-commit.legacy # If we have an existing hook, move it to pre-commit.legacy
if os.path.lexists(hook_path) and not is_our_script(hook_path): if os.path.lexists(hook_path) and not is_our_script(hook_path):
os.rename(hook_path, legacy_path) shutil.move(hook_path, legacy_path)
# If we specify overwrite, we simply delete the legacy file # If we specify overwrite, we simply delete the legacy file
if overwrite and os.path.exists(legacy_path): if overwrite and os.path.exists(legacy_path):

View file

@ -325,6 +325,16 @@ def test_install_existing_hooks_no_overwrite(tempdir_factory, store):
assert NORMAL_PRE_COMMIT_RUN.match(output[len('legacy hook\n'):]) assert NORMAL_PRE_COMMIT_RUN.match(output[len('legacy hook\n'):])
def test_legacy_overwriting_legacy_hook(tempdir_factory, store):
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
with cwd(path):
_write_legacy_hook(path)
assert install(C.CONFIG_FILE, store) == 0
_write_legacy_hook(path)
# this previously crashed on windows. See #1010
assert install(C.CONFIG_FILE, store) == 0
def test_install_existing_hook_no_overwrite_idempotent(tempdir_factory, store): def test_install_existing_hook_no_overwrite_idempotent(tempdir_factory, store):
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo') path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
with cwd(path): with cwd(path):