1010import stat
1111from plumbum import local
1212
13+ from pre_commit .commands .install_uninstall import IDENTIFYING_HASH
14+ from pre_commit .commands .install_uninstall import PREVIOUS_IDENTIFYING_HASHES
1315from pre_commit .commands .install_uninstall import install
1416from pre_commit .commands .install_uninstall import is_our_pre_commit
17+ from pre_commit .commands .install_uninstall import is_previous_pre_commit
1518from pre_commit .commands .install_uninstall import make_executable
1619from pre_commit .commands .install_uninstall import uninstall
1720from pre_commit .runner import Runner
@@ -31,6 +34,25 @@ def test_is_our_pre_commit():
3134 ) is True
3235
3336
37+ def test_is_not_previous_pre_commit ():
38+ assert is_previous_pre_commit ('setup.py' ) is False
39+
40+
41+ def test_is_also_not_previous_pre_commit ():
42+ assert is_previous_pre_commit (
43+ pkg_resources .resource_filename (
44+ 'pre_commit' , 'resources/pre-commit-hook' ,
45+ )
46+ ) is False
47+
48+
49+ def test_is_previous_pre_commit (in_tmpdir ):
50+ with io .open ('foo' , 'w' ) as foo_file :
51+ foo_file .write (PREVIOUS_IDENTIFYING_HASHES [0 ])
52+
53+ assert is_previous_pre_commit ('foo' )
54+
55+
3456def test_install_pre_commit (tmpdir_factory ):
3557 path = git_dir (tmpdir_factory )
3658 runner = Runner (path )
@@ -276,3 +298,43 @@ def test_uninstall_restores_legacy_hooks(tmpdir_factory):
276298 ret , output = _get_commit_output (tmpdir_factory , touch_file = 'baz' )
277299 assert ret == 0
278300 assert EXISTING_COMMIT_RUN .match (output )
301+
302+
303+ def test_replace_old_commit_script (tmpdir_factory ):
304+ path = make_consuming_repo (tmpdir_factory , 'script_hooks_repo' )
305+ with local .cwd (path ):
306+ runner = Runner (path )
307+
308+ # Install a script that looks like our old script
309+ pre_commit_contents = io .open (
310+ pkg_resources .resource_filename (
311+ 'pre_commit' , 'resources/pre-commit-hook' ,
312+ )
313+ ).read ()
314+ new_contents = pre_commit_contents .replace (
315+ IDENTIFYING_HASH , PREVIOUS_IDENTIFYING_HASHES [- 1 ],
316+ )
317+
318+ with io .open (runner .pre_commit_path , 'w' ) as pre_commit_file :
319+ pre_commit_file .write (new_contents )
320+ make_executable (runner .pre_commit_path )
321+
322+ # Install normally
323+ assert install (runner ) == 0
324+
325+ ret , output = _get_commit_output (tmpdir_factory )
326+ assert ret == 0
327+ assert NORMAL_PRE_COMMIT_RUN .match (output )
328+
329+
330+ def test_uninstall_doesnt_remove_not_our_hooks (tmpdir_factory ):
331+ path = git_dir (tmpdir_factory )
332+ with local .cwd (path ):
333+ runner = Runner (path )
334+ with io .open (runner .pre_commit_path , 'w' ) as pre_commit_file :
335+ pre_commit_file .write ('#!/usr/bin/env bash\n echo 1\n ' )
336+ make_executable (runner .pre_commit_path )
337+
338+ assert uninstall (runner ) == 0
339+
340+ assert os .path .exists (runner .pre_commit_path )
0 commit comments