resource_filename instead of trying to remember the right invocation to pkg_resources.

This commit is contained in:
Anthony Sottile 2014-06-23 13:18:40 -07:00
parent c7b605fee2
commit 8fee06b53e
3 changed files with 16 additions and 21 deletions

View file

@ -4,9 +4,10 @@ from __future__ import unicode_literals
import io import io
import os import os
import os.path import os.path
import pkg_resources
import stat import stat
from pre_commit.util import resource_filename
# This is used to identify the hook file we install # This is used to identify the hook file we install
PREVIOUS_IDENTIFYING_HASHES = [ PREVIOUS_IDENTIFYING_HASHES = [
@ -36,9 +37,7 @@ def make_executable(filename):
def install(runner, overwrite=False): def install(runner, overwrite=False):
"""Install the pre-commit hooks.""" """Install the pre-commit hooks."""
pre_commit_file = pkg_resources.resource_filename( pre_commit_file = resource_filename('pre-commit-hook')
'pre_commit', 'resources/pre-commit-hook',
)
# 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 ( if (

View file

@ -5,6 +5,7 @@ import functools
import hashlib import hashlib
import os import os
import os.path import os.path
import pkg_resources
import shutil import shutil
import sys import sys
import tarfile import tarfile
@ -89,3 +90,10 @@ def tmpdir():
yield tempdir yield tempdir
finally: finally:
shutil.rmtree(tempdir) shutil.rmtree(tempdir)
def resource_filename(filename):
return pkg_resources.resource_filename(
'pre_commit',
os.path.join('resources', filename),
)

View file

@ -5,7 +5,6 @@ import io
import os import os
import os.path import os.path
import re import re
import pkg_resources
import subprocess import subprocess
import stat import stat
from plumbum import local from plumbum import local
@ -18,6 +17,7 @@ from pre_commit.commands.install_uninstall import is_previous_pre_commit
from pre_commit.commands.install_uninstall import make_executable from pre_commit.commands.install_uninstall import make_executable
from pre_commit.commands.install_uninstall import uninstall from pre_commit.commands.install_uninstall import uninstall
from pre_commit.runner import Runner from pre_commit.runner import Runner
from pre_commit.util import resource_filename
from testing.fixtures import git_dir from testing.fixtures import git_dir
from testing.fixtures import make_consuming_repo from testing.fixtures import make_consuming_repo
@ -27,11 +27,7 @@ def test_is_not_our_pre_commit():
def test_is_our_pre_commit(): def test_is_our_pre_commit():
assert is_our_pre_commit( assert is_our_pre_commit(resource_filename('pre-commit-hook'))
pkg_resources.resource_filename(
'pre_commit', 'resources/pre-commit-hook',
)
) is True
def test_is_not_previous_pre_commit(): def test_is_not_previous_pre_commit():
@ -39,11 +35,7 @@ def test_is_not_previous_pre_commit():
def test_is_also_not_previous_pre_commit(): def test_is_also_not_previous_pre_commit():
assert is_previous_pre_commit( assert not is_previous_pre_commit(resource_filename('pre-commit-hook'))
pkg_resources.resource_filename(
'pre_commit', 'resources/pre-commit-hook',
)
) is False
def test_is_previous_pre_commit(in_tmpdir): def test_is_previous_pre_commit(in_tmpdir):
@ -60,9 +52,7 @@ def test_install_pre_commit(tmpdir_factory):
assert ret == 0 assert ret == 0
assert os.path.exists(runner.pre_commit_path) assert os.path.exists(runner.pre_commit_path)
pre_commit_contents = io.open(runner.pre_commit_path).read() pre_commit_contents = io.open(runner.pre_commit_path).read()
pre_commit_script = pkg_resources.resource_filename( pre_commit_script = resource_filename('pre-commit-hook')
'pre_commit', 'resources/pre-commit-hook',
)
expected_contents = io.open(pre_commit_script).read() expected_contents = io.open(pre_commit_script).read()
assert pre_commit_contents == expected_contents assert pre_commit_contents == expected_contents
stat_result = os.stat(runner.pre_commit_path) stat_result = os.stat(runner.pre_commit_path)
@ -317,9 +307,7 @@ def test_replace_old_commit_script(tmpdir_factory):
# Install a script that looks like our old script # Install a script that looks like our old script
pre_commit_contents = io.open( pre_commit_contents = io.open(
pkg_resources.resource_filename( resource_filename('pre-commit-hook'),
'pre_commit', 'resources/pre-commit-hook',
)
).read() ).read()
new_contents = pre_commit_contents.replace( new_contents = pre_commit_contents.replace(
IDENTIFYING_HASH, PREVIOUS_IDENTIFYING_HASHES[-1], IDENTIFYING_HASH, PREVIOUS_IDENTIFYING_HASHES[-1],