Implement pre-commit autoupdate --freeze

This commit is contained in:
Anthony Sottile 2019-12-28 12:25:30 -08:00
parent 32ce682238
commit 8a3c740f9e
5 changed files with 314 additions and 260 deletions

View file

@ -6,9 +6,10 @@ import pytest
import pre_commit.constants as C
from pre_commit import git
from pre_commit.commands.autoupdate import _update_repo
from pre_commit.commands.autoupdate import _check_hooks_still_exist_at_rev
from pre_commit.commands.autoupdate import autoupdate
from pre_commit.commands.autoupdate import RepositoryCannotBeUpdatedError
from pre_commit.commands.autoupdate import RevInfo
from pre_commit.util import cmd_output
from testing.auto_namedtuple import auto_namedtuple
from testing.fixtures import add_config_to_repo
@ -22,30 +23,114 @@ from testing.util import git_commit
@pytest.fixture
def up_to_date_repo(tempdir_factory):
def up_to_date(tempdir_factory):
yield make_repo(tempdir_factory, 'python_hooks_repo')
def test_up_to_date_repo(up_to_date_repo, store):
config = make_config_from_repo(up_to_date_repo)
input_rev = config['rev']
ret = _update_repo(config, store, tags_only=False)
assert ret['rev'] == input_rev
@pytest.fixture
def out_of_date(tempdir_factory):
path = make_repo(tempdir_factory, 'python_hooks_repo')
original_rev = git.head_rev(path)
git_commit(cwd=path)
head_rev = git.head_rev(path)
yield auto_namedtuple(
path=path, original_rev=original_rev, head_rev=head_rev,
)
def test_autoupdate_up_to_date_repo(up_to_date_repo, in_tmpdir, store):
# Write out the config
config = make_config_from_repo(up_to_date_repo, check=False)
write_config('.', config)
@pytest.fixture
def tagged(out_of_date):
cmd_output('git', 'tag', 'v1.2.3', cwd=out_of_date.path)
yield out_of_date
with open(C.CONFIG_FILE) as f:
before = f.read()
assert '^$' not in before
ret = autoupdate(C.CONFIG_FILE, store, tags_only=False)
with open(C.CONFIG_FILE) as f:
after = f.read()
assert ret == 0
assert before == after
@pytest.fixture
def hook_disappearing(tempdir_factory):
path = make_repo(tempdir_factory, 'python_hooks_repo')
original_rev = git.head_rev(path)
with modify_manifest(path) as manifest:
manifest[0]['id'] = 'bar'
yield auto_namedtuple(path=path, original_rev=original_rev)
def test_rev_info_from_config():
info = RevInfo.from_config({'repo': 'repo/path', 'rev': 'v1.2.3'})
assert info == RevInfo('repo/path', 'v1.2.3', None)
def test_rev_info_update_up_to_date_repo(up_to_date):
config = make_config_from_repo(up_to_date)
info = RevInfo.from_config(config)
new_info = info.update(tags_only=False, freeze=False)
assert info == new_info
def test_rev_info_update_out_of_date_repo(out_of_date):
config = make_config_from_repo(
out_of_date.path, rev=out_of_date.original_rev,
)
info = RevInfo.from_config(config)
new_info = info.update(tags_only=False, freeze=False)
assert new_info.rev == out_of_date.head_rev
def test_rev_info_update_non_master_default_branch(out_of_date):
# change the default branch to be not-master
cmd_output('git', '-C', out_of_date.path, 'branch', '-m', 'dev')
test_rev_info_update_out_of_date_repo(out_of_date)
def test_rev_info_update_tags_even_if_not_tags_only(tagged):
config = make_config_from_repo(tagged.path, rev=tagged.original_rev)
info = RevInfo.from_config(config)
new_info = info.update(tags_only=False, freeze=False)
assert new_info.rev == 'v1.2.3'
def test_rev_info_update_tags_only_does_not_pick_tip(tagged):
git_commit(cwd=tagged.path)
config = make_config_from_repo(tagged.path, rev=tagged.original_rev)
info = RevInfo.from_config(config)
new_info = info.update(tags_only=True, freeze=False)
assert new_info.rev == 'v1.2.3'
def test_rev_info_update_freeze_tag(tagged):
git_commit(cwd=tagged.path)
config = make_config_from_repo(tagged.path, rev=tagged.original_rev)
info = RevInfo.from_config(config)
new_info = info.update(tags_only=True, freeze=True)
assert new_info.rev == tagged.head_rev
assert new_info.frozen == 'v1.2.3'
def test_rev_info_update_does_not_freeze_if_already_sha(out_of_date):
config = make_config_from_repo(
out_of_date.path, rev=out_of_date.original_rev,
)
info = RevInfo.from_config(config)
new_info = info.update(tags_only=True, freeze=True)
assert new_info.rev == out_of_date.head_rev
assert new_info.frozen is None
def test_autoupdate_up_to_date_repo(up_to_date, tmpdir, store):
contents = (
'repos:\n'
'- repo: {}\n'
' rev: {}\n'
' hooks:\n'
' - id: foo\n'
).format(up_to_date, git.head_rev(up_to_date))
cfg = tmpdir.join(C.CONFIG_FILE)
cfg.write(contents)
assert autoupdate(str(cfg), store, freeze=False, tags_only=False) == 0
assert cfg.read() == contents
def test_autoupdate_old_revision_broken(tempdir_factory, in_tmpdir, store):
@ -68,98 +153,101 @@ def test_autoupdate_old_revision_broken(tempdir_factory, in_tmpdir, store):
write_config('.', config)
with open(C.CONFIG_FILE) as f:
before = f.read()
ret = autoupdate(C.CONFIG_FILE, store, tags_only=False)
assert autoupdate(C.CONFIG_FILE, store, freeze=False, tags_only=False) == 0
with open(C.CONFIG_FILE) as f:
after = f.read()
assert ret == 0
assert before != after
assert update_rev in after
@pytest.fixture
def out_of_date_repo(tempdir_factory):
path = make_repo(tempdir_factory, 'python_hooks_repo')
original_rev = git.head_rev(path)
git_commit(cwd=path)
head_rev = git.head_rev(path)
yield auto_namedtuple(
path=path, original_rev=original_rev, head_rev=head_rev,
def test_autoupdate_out_of_date_repo(out_of_date, tmpdir, store):
fmt = (
'repos:\n'
'- repo: {}\n'
' rev: {}\n'
' hooks:\n'
' - id: foo\n'
)
cfg = tmpdir.join(C.CONFIG_FILE)
cfg.write(fmt.format(out_of_date.path, out_of_date.original_rev))
assert autoupdate(str(cfg), store, freeze=False, tags_only=False) == 0
assert cfg.read() == fmt.format(out_of_date.path, out_of_date.head_rev)
def test_out_of_date_repo(out_of_date_repo, store):
config = make_config_from_repo(
out_of_date_repo.path, rev=out_of_date_repo.original_rev,
def test_autoupdate_only_one_to_update(up_to_date, out_of_date, tmpdir, store):
fmt = (
'repos:\n'
'- repo: {}\n'
' rev: {}\n'
' hooks:\n'
' - id: foo\n'
'- repo: {}\n'
' rev: {}\n'
' hooks:\n'
' - id: foo\n'
)
ret = _update_repo(config, store, tags_only=False)
assert ret['rev'] != out_of_date_repo.original_rev
assert ret['rev'] == out_of_date_repo.head_rev
def test_autoupdate_out_of_date_repo(out_of_date_repo, in_tmpdir, store):
# Write out the config
config = make_config_from_repo(
out_of_date_repo.path, rev=out_of_date_repo.original_rev, check=False,
cfg = tmpdir.join(C.CONFIG_FILE)
before = fmt.format(
up_to_date, git.head_rev(up_to_date),
out_of_date.path, out_of_date.original_rev,
)
write_config('.', config)
cfg.write(before)
with open(C.CONFIG_FILE) as f:
before = f.read()
ret = autoupdate(C.CONFIG_FILE, store, tags_only=False)
with open(C.CONFIG_FILE) as f:
after = f.read()
assert ret == 0
assert before != after
# Make sure we don't add defaults
assert 'exclude' not in after
assert out_of_date_repo.head_rev in after
assert autoupdate(str(cfg), store, freeze=False, tags_only=False) == 0
assert cfg.read() == fmt.format(
up_to_date, git.head_rev(up_to_date),
out_of_date.path, out_of_date.head_rev,
)
def test_autoupdate_out_of_date_repo_with_correct_repo_name(
out_of_date_repo, in_tmpdir, store,
out_of_date, in_tmpdir, store,
):
stale_config = make_config_from_repo(
out_of_date_repo.path, rev=out_of_date_repo.original_rev, check=False,
out_of_date.path, rev=out_of_date.original_rev, check=False,
)
local_config = sample_local_config()
config = {'repos': [stale_config, local_config]}
# Write out the config
write_config('.', config)
with open(C.CONFIG_FILE) as f:
before = f.read()
repo_name = 'file://{}'.format(out_of_date_repo.path)
ret = autoupdate(C.CONFIG_FILE, store, tags_only=False, repos=(repo_name,))
repo_name = 'file://{}'.format(out_of_date.path)
ret = autoupdate(
C.CONFIG_FILE, store, freeze=False, tags_only=False,
repos=(repo_name,),
)
with open(C.CONFIG_FILE) as f:
after = f.read()
assert ret == 0
assert before != after
assert out_of_date_repo.head_rev in after
assert out_of_date.head_rev in after
assert 'local' in after
def test_autoupdate_out_of_date_repo_with_wrong_repo_name(
out_of_date_repo, in_tmpdir, store,
out_of_date, in_tmpdir, store,
):
# Write out the config
config = make_config_from_repo(
out_of_date_repo.path, rev=out_of_date_repo.original_rev, check=False,
out_of_date.path, rev=out_of_date.original_rev, check=False,
)
write_config('.', config)
with open(C.CONFIG_FILE) as f:
before = f.read()
# It will not update it, because the name doesn't match
ret = autoupdate(C.CONFIG_FILE, store, tags_only=False, repos=('dne',))
ret = autoupdate(
C.CONFIG_FILE, store, freeze=False, tags_only=False,
repos=('dne',),
)
with open(C.CONFIG_FILE) as f:
after = f.read()
assert ret == 0
assert before == after
def test_does_not_reformat(in_tmpdir, out_of_date_repo, store):
def test_does_not_reformat(tmpdir, out_of_date, store):
fmt = (
'repos:\n'
'- repo: {}\n'
@ -169,20 +257,15 @@ def test_does_not_reformat(in_tmpdir, out_of_date_repo, store):
' # These args are because reasons!\n'
' args: [foo, bar, baz]\n'
)
config = fmt.format(out_of_date_repo.path, out_of_date_repo.original_rev)
with open(C.CONFIG_FILE, 'w') as f:
f.write(config)
cfg = tmpdir.join(C.CONFIG_FILE)
cfg.write(fmt.format(out_of_date.path, out_of_date.original_rev))
autoupdate(C.CONFIG_FILE, store, tags_only=False)
with open(C.CONFIG_FILE) as f:
after = f.read()
expected = fmt.format(out_of_date_repo.path, out_of_date_repo.head_rev)
assert after == expected
assert autoupdate(str(cfg), store, freeze=False, tags_only=False) == 0
expected = fmt.format(out_of_date.path, out_of_date.head_rev)
assert cfg.read() == expected
def test_loses_formatting_when_not_detectable(
out_of_date_repo, store, in_tmpdir,
):
def test_loses_formatting_when_not_detectable(out_of_date, store, tmpdir):
"""A best-effort attempt is made at updating rev without rewriting
formatting. When the original formatting cannot be detected, this
is abandoned.
@ -197,149 +280,119 @@ def test_loses_formatting_when_not_detectable(
' ],\n'
' }}\n'
']\n'.format(
pipes.quote(out_of_date_repo.path), out_of_date_repo.original_rev,
pipes.quote(out_of_date.path), out_of_date.original_rev,
)
)
with open(C.CONFIG_FILE, 'w') as f:
f.write(config)
cfg = tmpdir.join(C.CONFIG_FILE)
cfg.write(config)
autoupdate(C.CONFIG_FILE, store, tags_only=False)
with open(C.CONFIG_FILE) as f:
after = f.read()
assert autoupdate(str(cfg), store, freeze=False, tags_only=False) == 0
expected = (
'repos:\n'
'- repo: {}\n'
' rev: {}\n'
' hooks:\n'
' - id: foo\n'
).format(out_of_date_repo.path, out_of_date_repo.head_rev)
assert after == expected
).format(out_of_date.path, out_of_date.head_rev)
assert cfg.read() == expected
@pytest.fixture
def tagged_repo(out_of_date_repo):
cmd_output('git', 'tag', 'v1.2.3', cwd=out_of_date_repo.path)
yield out_of_date_repo
def test_autoupdate_tagged_repo(tagged_repo, in_tmpdir, store):
config = make_config_from_repo(
tagged_repo.path, rev=tagged_repo.original_rev,
)
def test_autoupdate_tagged_repo(tagged, in_tmpdir, store):
config = make_config_from_repo(tagged.path, rev=tagged.original_rev)
write_config('.', config)
ret = autoupdate(C.CONFIG_FILE, store, tags_only=False)
assert ret == 0
assert autoupdate(C.CONFIG_FILE, store, freeze=False, tags_only=False) == 0
with open(C.CONFIG_FILE) as f:
assert 'v1.2.3' in f.read()
@pytest.fixture
def tagged_repo_with_more_commits(tagged_repo):
git_commit(cwd=tagged_repo.path)
yield tagged_repo
def test_autoupdate_tags_only(tagged_repo_with_more_commits, in_tmpdir, store):
config = make_config_from_repo(
tagged_repo_with_more_commits.path,
rev=tagged_repo_with_more_commits.original_rev,
)
def test_autoupdate_freeze(tagged, in_tmpdir, store):
config = make_config_from_repo(tagged.path, rev=tagged.original_rev)
write_config('.', config)
ret = autoupdate(C.CONFIG_FILE, store, tags_only=True)
assert ret == 0
assert autoupdate(C.CONFIG_FILE, store, freeze=True, tags_only=False) == 0
with open(C.CONFIG_FILE) as f:
expected = 'rev: {} # v1.2.3'.format(tagged.head_rev)
assert expected in f.read()
def test_autoupdate_tags_only(tagged, in_tmpdir, store):
# add some commits after the tag
git_commit(cwd=tagged.path)
config = make_config_from_repo(tagged.path, rev=tagged.original_rev)
write_config('.', config)
assert autoupdate(C.CONFIG_FILE, store, freeze=False, tags_only=True) == 0
with open(C.CONFIG_FILE) as f:
assert 'v1.2.3' in f.read()
def test_autoupdate_latest_no_config(out_of_date_repo, in_tmpdir, store):
def test_autoupdate_latest_no_config(out_of_date, in_tmpdir, store):
config = make_config_from_repo(
out_of_date_repo.path, rev=out_of_date_repo.original_rev,
out_of_date.path, rev=out_of_date.original_rev,
)
write_config('.', config)
cmd_output('git', 'rm', '-r', ':/', cwd=out_of_date_repo.path)
git_commit(cwd=out_of_date_repo.path)
cmd_output('git', 'rm', '-r', ':/', cwd=out_of_date.path)
git_commit(cwd=out_of_date.path)
ret = autoupdate(C.CONFIG_FILE, store, tags_only=False)
assert ret == 1
assert autoupdate(C.CONFIG_FILE, store, freeze=False, tags_only=False) == 1
with open(C.CONFIG_FILE) as f:
assert out_of_date_repo.original_rev in f.read()
assert out_of_date.original_rev in f.read()
@pytest.fixture
def hook_disappearing_repo(tempdir_factory):
path = make_repo(tempdir_factory, 'python_hooks_repo')
original_rev = git.head_rev(path)
with modify_manifest(path) as manifest:
manifest[0]['id'] = 'bar'
yield auto_namedtuple(path=path, original_rev=original_rev)
def test_hook_disppearing_repo_raises(hook_disappearing_repo, store):
def test_hook_disppearing_repo_raises(hook_disappearing, store):
config = make_config_from_repo(
hook_disappearing_repo.path,
rev=hook_disappearing_repo.original_rev,
hook_disappearing.path,
rev=hook_disappearing.original_rev,
hooks=[{'id': 'foo'}],
)
info = RevInfo.from_config(config).update(tags_only=False, freeze=False)
with pytest.raises(RepositoryCannotBeUpdatedError):
_update_repo(config, store, tags_only=False)
_check_hooks_still_exist_at_rev(config, info, store)
def test_autoupdate_hook_disappearing_repo(
hook_disappearing_repo, in_tmpdir, store,
):
config = make_config_from_repo(
hook_disappearing_repo.path,
rev=hook_disappearing_repo.original_rev,
hooks=[{'id': 'foo'}],
check=False,
)
write_config('.', config)
def test_autoupdate_hook_disappearing_repo(hook_disappearing, tmpdir, store):
contents = (
'repos:\n'
'- repo: {}\n'
' rev: {}\n'
' hooks:\n'
' - id: foo\n'
).format(hook_disappearing.path, hook_disappearing.original_rev)
cfg = tmpdir.join(C.CONFIG_FILE)
cfg.write(contents)
with open(C.CONFIG_FILE) as f:
before = f.read()
ret = autoupdate(C.CONFIG_FILE, store, tags_only=False)
with open(C.CONFIG_FILE) as f:
after = f.read()
assert ret == 1
assert before == after
def test_autoupdate_non_master_default_branch(up_to_date_repo, store):
# change the default branch to be not-master
cmd_output('git', '-C', up_to_date_repo, 'branch', '-m', 'dev')
test_up_to_date_repo(up_to_date_repo, store)
assert autoupdate(str(cfg), store, freeze=False, tags_only=False) == 1
assert cfg.read() == contents
def test_autoupdate_local_hooks(in_git_dir, store):
config = sample_local_config()
add_config_to_repo('.', config)
assert autoupdate(C.CONFIG_FILE, store, tags_only=False) == 0
assert autoupdate(C.CONFIG_FILE, store, freeze=False, tags_only=False) == 0
new_config_writen = read_config('.')
assert len(new_config_writen['repos']) == 1
assert new_config_writen['repos'][0] == config
def test_autoupdate_local_hooks_with_out_of_date_repo(
out_of_date_repo, in_tmpdir, store,
out_of_date, in_tmpdir, store,
):
stale_config = make_config_from_repo(
out_of_date_repo.path, rev=out_of_date_repo.original_rev, check=False,
out_of_date.path, rev=out_of_date.original_rev, check=False,
)
local_config = sample_local_config()
config = {'repos': [local_config, stale_config]}
write_config('.', config)
assert autoupdate(C.CONFIG_FILE, store, tags_only=False) == 0
assert autoupdate(C.CONFIG_FILE, store, freeze=False, tags_only=False) == 0
new_config_writen = read_config('.')
assert len(new_config_writen['repos']) == 2
assert new_config_writen['repos'][0] == local_config
def test_autoupdate_meta_hooks(tmpdir, capsys, store):
def test_autoupdate_meta_hooks(tmpdir, store):
cfg = tmpdir.join(C.CONFIG_FILE)
cfg.write(
'repos:\n'
@ -347,9 +400,7 @@ def test_autoupdate_meta_hooks(tmpdir, capsys, store):
' hooks:\n'
' - id: check-useless-excludes\n',
)
with tmpdir.as_cwd():
ret = autoupdate(C.CONFIG_FILE, store, tags_only=True)
assert ret == 0
assert autoupdate(str(cfg), store, freeze=False, tags_only=True) == 0
assert cfg.read() == (
'repos:\n'
'- repo: meta\n'
@ -368,9 +419,7 @@ def test_updates_old_format_to_new_format(tmpdir, capsys, store):
' entry: ./bin/foo.sh\n'
' language: script\n',
)
with tmpdir.as_cwd():
ret = autoupdate(C.CONFIG_FILE, store, tags_only=True)
assert ret == 0
assert autoupdate(str(cfg), store, freeze=False, tags_only=True) == 0
contents = cfg.read()
assert contents == (
'repos:\n'