Skip to content

Commit 173ce83

Browse files
committed
Make hook-tmpl resilient to future changes
1 parent 41dcaff commit 173ce83

File tree

4 files changed

+27
-51
lines changed

4 files changed

+27
-51
lines changed

pre_commit/commands/install_uninstall.py

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,21 @@
1212

1313

1414
# This is used to identify the hook file we install
15-
PREVIOUS_IDENTIFYING_HASHES = (
15+
PRIOR_HASHES = (
1616
'4d9958c90bc262f47553e2c073f14cfe',
1717
'd8ee923c46731b42cd95cc869add4062',
1818
'49fd668cb42069aa1b6048464be5d395',
1919
'79f09a650522a87b0da915d0d983b2de',
2020
'e358c9dae00eac5d06b38dfdb1e33a8c',
2121
)
22+
CURRENT_HASH = '138fd403232d2ddd5efb44317e38bf03'
2223

2324

24-
IDENTIFYING_HASH = '138fd403232d2ddd5efb44317e38bf03'
25-
26-
27-
def is_our_pre_commit(filename):
28-
if not os.path.exists(filename):
29-
return False
30-
return IDENTIFYING_HASH in io.open(filename).read()
31-
32-
33-
def is_previous_pre_commit(filename):
25+
def is_our_script(filename):
3426
if not os.path.exists(filename):
3527
return False
3628
contents = io.open(filename).read()
37-
return any(hash in contents for hash in PREVIOUS_IDENTIFYING_HASHES)
29+
return any(h in contents for h in (CURRENT_HASH,) + PRIOR_HASHES)
3830

3931

4032
def install(runner, overwrite=False, hooks=False, hook_type='pre-commit'):
@@ -45,11 +37,7 @@ def install(runner, overwrite=False, hooks=False, hook_type='pre-commit'):
4537
mkdirp(os.path.dirname(hook_path))
4638

4739
# If we have an existing hook, move it to pre-commit.legacy
48-
if (
49-
os.path.lexists(hook_path) and
50-
not is_our_pre_commit(hook_path) and
51-
not is_previous_pre_commit(hook_path)
52-
):
40+
if os.path.lexists(hook_path) and not is_our_script(hook_path):
5341
os.rename(hook_path, legacy_path)
5442

5543
# If we specify overwrite, we simply delete the legacy file
@@ -97,12 +85,7 @@ def uninstall(runner, hook_type='pre-commit'):
9785
hook_path = runner.get_hook_path(hook_type)
9886
legacy_path = hook_path + '.legacy'
9987
# If our file doesn't exist or it isn't ours, gtfo.
100-
if (
101-
not os.path.exists(hook_path) or (
102-
not is_our_pre_commit(hook_path) and
103-
not is_previous_pre_commit(hook_path)
104-
)
105-
):
88+
if not os.path.exists(hook_path) or not is_our_script(hook_path):
10689
return 0
10790

10891
os.remove(hook_path)

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ coverage
44
flake8
55
mock
66
pytest
7+
pytest-env
78

89
# setuptools breaks pypy3 with extraneous output
910
setuptools<18.5

tests/commands/install_uninstall_test.py

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@
1111
import mock
1212

1313
import pre_commit.constants as C
14-
from pre_commit.commands.install_uninstall import IDENTIFYING_HASH
14+
from pre_commit.commands.install_uninstall import CURRENT_HASH
1515
from pre_commit.commands.install_uninstall import install
1616
from pre_commit.commands.install_uninstall import install_hooks
17-
from pre_commit.commands.install_uninstall import is_our_pre_commit
18-
from pre_commit.commands.install_uninstall import is_previous_pre_commit
19-
from pre_commit.commands.install_uninstall import PREVIOUS_IDENTIFYING_HASHES
17+
from pre_commit.commands.install_uninstall import is_our_script
18+
from pre_commit.commands.install_uninstall import PRIOR_HASHES
2019
from pre_commit.commands.install_uninstall import uninstall
2120
from pre_commit.runner import Runner
2221
from pre_commit.util import cmd_output
@@ -30,27 +29,18 @@
3029
from testing.util import xfailif_no_symlink
3130

3231

33-
def test_is_not_our_pre_commit():
34-
assert is_our_pre_commit('setup.py') is False
32+
def test_is_not_script():
33+
assert is_our_script('setup.py') is False
3534

3635

37-
def test_is_our_pre_commit():
38-
assert is_our_pre_commit(resource_filename('hook-tmpl'))
36+
def test_is_script():
37+
assert is_our_script(resource_filename('hook-tmpl'))
3938

4039

41-
def test_is_not_previous_pre_commit():
42-
assert is_previous_pre_commit('setup.py') is False
43-
44-
45-
def test_is_also_not_previous_pre_commit():
46-
assert not is_previous_pre_commit(resource_filename('hook-tmpl'))
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')
40+
def test_is_previous_pre_commit(tmpdir):
41+
f = tmpdir.join('foo')
42+
f.write(PRIOR_HASHES[0] + '\n')
43+
assert is_our_script(f.strpath)
5444

5545

5646
def test_install_pre_commit(tempdir_factory):
@@ -408,7 +398,7 @@ def test_replace_old_commit_script(tempdir_factory):
408398
resource_filename('hook-tmpl'),
409399
).read()
410400
new_contents = pre_commit_contents.replace(
411-
IDENTIFYING_HASH, PREVIOUS_IDENTIFYING_HASHES[-1],
401+
CURRENT_HASH, PRIOR_HASHES[-1],
412402
)
413403

414404
mkdirp(os.path.dirname(runner.pre_commit_path))

tox.ini

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ envlist = py27,py34,py35,pypy
66
[testenv]
77
deps = -rrequirements-dev.txt
88
passenv = GOROOT HOME HOMEPATH PROGRAMDATA TERM
9-
setenv =
10-
VIRTUALENV_NO_DOWNLOAD = 1
11-
GIT_AUTHOR_NAME = "test"
12-
GIT_COMMITTER_NAME = "test"
13-
GIT_AUTHOR_EMAIL = "test@example.com"
14-
GIT_COMMITTER_EMAIL = "test@example.com"
159
commands =
1610
coverage erase
1711
coverage run -m pytest {posargs:tests}
@@ -25,3 +19,11 @@ commands =
2519

2620
[pep8]
2721
ignore = E265,E309,E501
22+
23+
[pytest]
24+
env =
25+
GIT_AUTHOR_NAME=test
26+
GIT_COMMITTER_NAME=test
27+
GIT_AUTHOR_EMAIL=test@example.com
28+
GIT_COMMITTER_EMAIL=test@example.com
29+
VIRTUALENV_NO_DOWNLOAD=1

0 commit comments

Comments
 (0)