Added the useful thinking bit of the autoupdate and tests.

This commit is contained in:
Anthony Sottile
2014-03-23 21:35:35 -07:00
parent d8f8f5e1f4
commit 49da1ba72a
5 changed files with 144 additions and 8 deletions

View File

@@ -4,6 +4,10 @@ from __future__ import print_function
import os
import pkg_resources
import stat
from plumbum import local
from pre_commit.ordereddict import OrderedDict
from pre_commit.repository import Repository
def install(runner):
@@ -29,3 +33,46 @@ def uninstall(runner):
os.remove(runner.pre_commit_path)
print('pre-commit uninstalled')
return 0
class RepositoryCannotBeUpdatedError(RuntimeError): pass
def _update_repository(repo_config):
"""Updates a repository to the tip of `master`. If the repository cannot
be updated because a hook that is configured does not exist in `master`,
this raises a RepositoryCannotBeUpdatedError
Args:
repo_config - A config for a repository
"""
repo = Repository(repo_config)
with repo.in_checkout():
local['git']['fetch']()
head_sha = local['git']['rev-parse', 'origin/master']().strip()
# Don't bother trying to update if our sha is the same
if head_sha == repo_config['sha']:
return repo_config
# Construct a new config with the head sha
new_config = OrderedDict(repo_config)
new_config['sha'] = head_sha
new_repo = Repository(new_config)
# See if any of our hooks were deleted with the new commits
hooks = set(repo.hooks.keys())
hooks_missing = hooks - (hooks & set(new_repo.manifest.keys()))
if hooks_missing:
raise RepositoryCannotBeUpdatedError(
'Cannot update because the tip of master is missing these hooks:\n'
'{0}'.format(', '.join(sorted(hooks_missing)))
)
return new_config
def autoupdate(runner):
"""Auto-update the pre-commit config to the latest versions of repos."""
pass

View File

@@ -100,14 +100,7 @@ def run(argv):
subparsers.add_parser('uninstall', help='Uninstall the pre-commit script.')
execute_hook = subparsers.add_parser(
'execute-hook', help='Run a single hook.'
)
execute_hook.add_argument('hook', help='The hook-id to run.')
execute_hook.add_argument(
'--all-files', '-a', action='store_true', default=False,
help='Run on all the files in the repo.',
)
subparsers.add_parser('autoupdate', help='Auto-update hooks config.')
run = subparsers.add_parser('run', help='Run hooks.')
run.add_argument('hook', nargs='?', help='A single hook-id to run'),
@@ -130,6 +123,8 @@ def run(argv):
return commands.install(runner)
elif args.command == 'uninstall':
return commands.uninstall(runner)
elif args.command == 'autoupdate':
return commands.autoupdate(runner)
elif args.command == 'run':
if args.hook:
return run_single_hook(runner, args.hook, all_files=args.all_files)

View File

@@ -0,0 +1,11 @@
import collections
def auto_namedtuple(classname='auto_namedtuple', **kwargs):
"""Returns an automatic namedtuple object.
Args:
classname - The class name for the returned object.
**kwargs - Properties to give the returned object.
"""
return (collections.namedtuple(classname, kwargs.keys())(**kwargs))

View File

@@ -0,0 +1,4 @@
- id: bar
name: Bar
entry: bar
language: python

View File

@@ -1,12 +1,24 @@
import jsonschema
import os
import os.path
import pkg_resources
import pytest
import shutil
import stat
from plumbum import local
from pre_commit import git
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
from pre_commit.clientlib.validate_config import validate_config_extra
from pre_commit.commands import install
from pre_commit.commands import RepositoryCannotBeUpdatedError
from pre_commit.commands import uninstall
from pre_commit.commands import _update_repository
from pre_commit.ordereddict import OrderedDict
from pre_commit.runner import Runner
from testing.auto_namedtuple import auto_namedtuple
from testing.util import get_resource_path
def test_install_pre_commit(empty_git_dir):
@@ -35,3 +47,70 @@ def test_uninstall(empty_git_dir):
assert os.path.exists(runner.pre_commit_path)
uninstall(runner)
assert not os.path.exists(runner.pre_commit_path)
@pytest.yield_fixture
def up_to_date_repo(python_hooks_repo):
config = OrderedDict((
('repo', python_hooks_repo),
('sha', git.get_head_sha(python_hooks_repo)),
('hooks', [{'id': 'foo', 'files': ''}]),
))
jsonschema.validate([config], CONFIG_JSON_SCHEMA)
validate_config_extra([config])
yield auto_namedtuple(
repo_config=config,
python_hooks_repo=python_hooks_repo,
)
def test_up_to_date_repo(up_to_date_repo):
input_sha = up_to_date_repo.repo_config['sha']
ret = _update_repository(up_to_date_repo.repo_config)
assert ret['sha'] == input_sha
@pytest.yield_fixture
def out_of_date_repo(python_hooks_repo):
config = OrderedDict((
('repo', python_hooks_repo),
('sha', git.get_head_sha(python_hooks_repo)),
('hooks', [{'id': 'foo', 'files': ''}]),
))
jsonschema.validate([config], CONFIG_JSON_SCHEMA)
validate_config_extra([config])
local['git']['commit', '--allow-empty', '-m', 'foo']()
head_sha = git.get_head_sha(python_hooks_repo)
yield auto_namedtuple(
repo_config=config,
head_sha=head_sha,
python_hooks_repo=python_hooks_repo,
)
def test_out_of_date_repo(out_of_date_repo):
ret = _update_repository(out_of_date_repo.repo_config)
assert ret['sha'] == out_of_date_repo.head_sha
@pytest.yield_fixture
def hook_disappearing_repo(python_hooks_repo):
config = OrderedDict((
('repo', python_hooks_repo),
('sha', git.get_head_sha(python_hooks_repo)),
('hooks', [{'id': 'foo', 'files': ''}]),
))
jsonschema.validate([config], CONFIG_JSON_SCHEMA)
validate_config_extra([config])
shutil.copy(get_resource_path('manifest_without_foo.yaml'), 'manifest.yaml')
local['git']['add', '.']()
local['git']['commit', '-m', 'Remove foo']()
yield auto_namedtuple(
repo_config=config,
python_hooks_repo=python_hooks_repo,
)
def test_hook_disppearing_repo_raises(hook_disappearing_repo):
with pytest.raises(RepositoryCannotBeUpdatedError):
_update_repository(hook_disappearing_repo.repo_config)