Skip to content

Commit 8fee06b

Browse files
committed
resource_filename instead of trying to remember the right invocation to pkg_resources.
1 parent c7b605f commit 8fee06b

3 files changed

Lines changed: 16 additions & 21 deletions

File tree

pre_commit/commands/install_uninstall.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
import io
55
import os
66
import os.path
7-
import pkg_resources
87
import stat
98

9+
from pre_commit.util import resource_filename
10+
1011

1112
# This is used to identify the hook file we install
1213
PREVIOUS_IDENTIFYING_HASHES = [
@@ -36,9 +37,7 @@ def make_executable(filename):
3637

3738
def install(runner, overwrite=False):
3839
"""Install the pre-commit hooks."""
39-
pre_commit_file = pkg_resources.resource_filename(
40-
'pre_commit', 'resources/pre-commit-hook',
41-
)
40+
pre_commit_file = resource_filename('pre-commit-hook')
4241

4342
# If we have an existing hook, move it to pre-commit.legacy
4443
if (

pre_commit/util.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import hashlib
66
import os
77
import os.path
8+
import pkg_resources
89
import shutil
910
import sys
1011
import tarfile
@@ -89,3 +90,10 @@ def tmpdir():
8990
yield tempdir
9091
finally:
9192
shutil.rmtree(tempdir)
93+
94+
95+
def resource_filename(filename):
96+
return pkg_resources.resource_filename(
97+
'pre_commit',
98+
os.path.join('resources', filename),
99+
)

tests/commands/install_uninstall_test.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import os
66
import os.path
77
import re
8-
import pkg_resources
98
import subprocess
109
import stat
1110
from plumbum import local
@@ -18,6 +17,7 @@
1817
from pre_commit.commands.install_uninstall import make_executable
1918
from pre_commit.commands.install_uninstall import uninstall
2019
from pre_commit.runner import Runner
20+
from pre_commit.util import resource_filename
2121
from testing.fixtures import git_dir
2222
from testing.fixtures import make_consuming_repo
2323

@@ -27,23 +27,15 @@ def test_is_not_our_pre_commit():
2727

2828

2929
def test_is_our_pre_commit():
30-
assert is_our_pre_commit(
31-
pkg_resources.resource_filename(
32-
'pre_commit', 'resources/pre-commit-hook',
33-
)
34-
) is True
30+
assert is_our_pre_commit(resource_filename('pre-commit-hook'))
3531

3632

3733
def test_is_not_previous_pre_commit():
3834
assert is_previous_pre_commit('setup.py') is False
3935

4036

4137
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
38+
assert not is_previous_pre_commit(resource_filename('pre-commit-hook'))
4739

4840

4941
def test_is_previous_pre_commit(in_tmpdir):
@@ -60,9 +52,7 @@ def test_install_pre_commit(tmpdir_factory):
6052
assert ret == 0
6153
assert os.path.exists(runner.pre_commit_path)
6254
pre_commit_contents = io.open(runner.pre_commit_path).read()
63-
pre_commit_script = pkg_resources.resource_filename(
64-
'pre_commit', 'resources/pre-commit-hook',
65-
)
55+
pre_commit_script = resource_filename('pre-commit-hook')
6656
expected_contents = io.open(pre_commit_script).read()
6757
assert pre_commit_contents == expected_contents
6858
stat_result = os.stat(runner.pre_commit_path)
@@ -317,9 +307,7 @@ def test_replace_old_commit_script(tmpdir_factory):
317307

318308
# Install a script that looks like our old script
319309
pre_commit_contents = io.open(
320-
pkg_resources.resource_filename(
321-
'pre_commit', 'resources/pre-commit-hook',
322-
)
310+
resource_filename('pre-commit-hook'),
323311
).read()
324312
new_contents = pre_commit_contents.replace(
325313
IDENTIFYING_HASH, PREVIOUS_IDENTIFYING_HASHES[-1],

0 commit comments

Comments
 (0)