Make pre_commit also support pre-push hook

This commit is contained in:
dongweiming 2015-01-11 22:40:35 +08:00 committed by Anthony Sottile
parent d2b11a0c50
commit b707cbba06
10 changed files with 227 additions and 42 deletions

View file

@ -10,7 +10,7 @@ import subprocess
import sys
import mock
from pre_commit.commands.install_uninstall import get_hook_path
from pre_commit.commands.install_uninstall import IDENTIFYING_HASH
from pre_commit.commands.install_uninstall import install
from pre_commit.commands.install_uninstall import is_our_pre_commit
@ -22,6 +22,7 @@ from pre_commit.runner import Runner
from pre_commit.util import cmd_output
from pre_commit.util import cwd
from pre_commit.util import resource_filename
from testing.fixtures import git_dir
from testing.fixtures import make_consuming_repo
@ -31,7 +32,7 @@ def test_is_not_our_pre_commit():
def test_is_our_pre_commit():
assert is_our_pre_commit(resource_filename('pre-commit-hook'))
assert is_our_pre_commit(resource_filename('hook-tmpl'))
def test_is_not_previous_pre_commit():
@ -39,7 +40,7 @@ def test_is_not_previous_pre_commit():
def test_is_also_not_previous_pre_commit():
assert not is_previous_pre_commit(resource_filename('pre-commit-hook'))
assert not is_previous_pre_commit(resource_filename('hook-tmpl'))
def test_is_previous_pre_commit(in_tmpdir):
@ -56,14 +57,28 @@ def test_install_pre_commit(tmpdir_factory):
assert ret == 0
assert os.path.exists(runner.pre_commit_path)
pre_commit_contents = io.open(runner.pre_commit_path).read()
pre_commit_script = resource_filename('pre-commit-hook')
pre_commit_script = resource_filename('hook-tmpl')
expected_contents = io.open(pre_commit_script).read().format(
sys_executable=sys.executable,
hook_type='pre-commit',
pre_push=''
)
assert pre_commit_contents == expected_contents
stat_result = os.stat(runner.pre_commit_path)
assert stat_result.st_mode & (stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
ret = install(runner, hook_type='pre-push')
assert ret == 0
assert os.path.exists(runner.pre_push_path)
pre_push_contents = io.open(runner.pre_push_path).read()
pre_push_template_contents = io.open(runner.pre_push_template).read()
expected_contents = io.open(pre_commit_script).read().format(
sys_executable=sys.executable,
hook_type='pre-push',
pre_push=pre_push_template_contents
)
assert pre_push_contents == expected_contents
def test_uninstall_does_not_blow_up_when_not_there(tmpdir_factory):
path = git_dir(tmpdir_factory)
@ -322,7 +337,7 @@ def test_replace_old_commit_script(tmpdir_factory):
# Install a script that looks like our old script
pre_commit_contents = io.open(
resource_filename('pre-commit-hook'),
resource_filename('hook-tmpl'),
).read()
new_contents = pre_commit_contents.replace(
IDENTIFYING_HASH, PREVIOUS_IDENTIFYING_HASHES[-1],
@ -391,3 +406,17 @@ def test_installed_from_venv(tmpdir_factory):
)
assert ret == 0
assert NORMAL_PRE_COMMIT_RUN.match(output)
def test_get_hook_path(tmpdir_factory):
path = git_dir(tmpdir_factory)
with cwd(path):
runner = Runner(path)
expected_paths = (os.path.join(path, '.git/hooks/pre-commit'),
os.path.join(path, '.git/hooks/pre-commit.legacy')
)
assert expected_paths == get_hook_path(runner, 'pre-commit')
expected_paths = (os.path.join(path, '.git/hooks/pre-push'),
os.path.join(path, '.git/hooks/pre-push.legacy')
)
assert expected_paths == get_hook_path(runner, 'pre-push')

View file

@ -12,6 +12,7 @@ import pytest
from pre_commit.commands.install_uninstall import install
from pre_commit.commands.run import _get_skips
from pre_commit.commands.run import _has_unmerged_paths
from pre_commit.commands.run import get_changed_files
from pre_commit.commands.run import run
from pre_commit.runner import Runner
from pre_commit.util import cmd_output
@ -50,6 +51,8 @@ def _get_opts(
verbose=False,
hook=None,
no_stash=False,
origin='',
source='',
):
# These are mutually exclusive
assert not (all_files and files)
@ -60,6 +63,8 @@ def _get_opts(
verbose=verbose,
hook=hook,
no_stash=no_stash,
origin=origin,
source=source,
)
@ -126,6 +131,28 @@ def test_run(
_test_run(repo_with_passing_hook, options, outputs, expected_ret, stage)
@pytest.mark.parametrize(
('origin', 'source', 'expect_stash'),
(
('master', 'master', False),
('master', '', True),
('', 'master', True),
)
)
def test_origin_source_define(
repo_with_passing_hook, origin, source, expect_stash,
mock_out_store_directory):
args = _get_opts(origin=origin, source=source)
ret, printed = _do_run(repo_with_passing_hook, args)
warning_msg = '--origin and --source depend on each other.'
if expect_stash:
assert ret == 1
assert warning_msg in printed
else:
assert ret == 0
assert warning_msg not in printed
@pytest.mark.parametrize(
('no_stash', 'all_files', 'expect_stash'),
(
@ -267,3 +294,10 @@ def test_stdout_write_bug_py26(
assert 'UnicodeEncodeError' not in stdout
# Doesn't actually happen, but a reasonable assertion
assert 'UnicodeDecodeError' not in stdout
def test_get_changed_files():
files = list(get_changed_files('78c682a1d13ba20e7cb735313b9314a74365cd3a',
'3387edbb1288a580b37fe25225aa0b856b18ad1a'
))
assert files == ['CHANGELOG.md', 'setup.py']

View file

@ -7,6 +7,7 @@ import os.path
import pre_commit.constants as C
from pre_commit.runner import Runner
from pre_commit.util import cwd
from pre_commit.util import resource_filename
from testing.fixtures import git_dir
from testing.fixtures import make_consuming_repo
@ -58,6 +59,34 @@ def test_pre_commit_path():
assert runner.pre_commit_path == expected_path
def test_pre_push_path():
runner = Runner('foo/bar')
expected_path = os.path.join('foo/bar', '.git/hooks/pre-push')
assert runner.pre_push_path == expected_path
def test_pre_commit_legacy_path():
runner = Runner('foo/bar')
expected_path = os.path.join('foo/bar', '.git/hooks/pre-commit.legacy')
assert runner.pre_commit_legacy_path == expected_path
def test_pre_push_legacy_path():
runner = Runner('foo/bar')
expected_path = os.path.join('foo/bar', '.git/hooks/pre-push.legacy')
assert runner.pre_push_legacy_path == expected_path
def test_pre_template():
runner = Runner('foo/bar')
assert runner.pre_template == resource_filename('hook-tmpl')
def test_pre_push_template():
runner = Runner('foo/bar')
assert runner.pre_push_template == resource_filename('pre-push-tmpl')
def test_cmd_runner(mock_out_store_directory):
runner = Runner('foo/bar')
ret = runner.cmd_runner