mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-04-14 17:41:45 +04:00
Convert autoupdate_test to use new fixture functions.
This commit is contained in:
parent
047a933554
commit
3baefd57e2
3 changed files with 106 additions and 83 deletions
|
|
@ -4,5 +4,7 @@ MANIFEST_FILE = 'hooks.yaml'
|
||||||
|
|
||||||
YAML_DUMP_KWARGS = {
|
YAML_DUMP_KWARGS = {
|
||||||
'default_flow_style': False,
|
'default_flow_style': False,
|
||||||
|
# Use unicode
|
||||||
|
'encoding': None,
|
||||||
'indent': 4,
|
'indent': 4,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,19 @@
|
||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
import os.path
|
||||||
|
from asottile.ordereddict import OrderedDict
|
||||||
from plumbum import local
|
from plumbum import local
|
||||||
|
|
||||||
|
import pre_commit.constants as C
|
||||||
|
from pre_commit.clientlib.validate_manifest import load_manifest
|
||||||
|
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
|
||||||
|
from pre_commit.clientlib.validate_config import validate_config_extra
|
||||||
|
from pre_commit.jsonschema_extensions import apply_defaults
|
||||||
|
from testing.util import copy_tree_to_path
|
||||||
|
from testing.util import get_head_sha
|
||||||
|
from testing.util import get_resource_path
|
||||||
|
|
||||||
|
|
||||||
git = local['git']
|
git = local['git']
|
||||||
|
|
||||||
|
|
@ -12,3 +23,31 @@ def git_dir(tmpdir_factory):
|
||||||
with local.cwd(path):
|
with local.cwd(path):
|
||||||
git('init')
|
git('init')
|
||||||
return path
|
return path
|
||||||
|
|
||||||
|
|
||||||
|
def make_hooks_repo(tmpdir_factory, repo_source):
|
||||||
|
path = git_dir(tmpdir_factory)
|
||||||
|
copy_tree_to_path(get_resource_path(repo_source), path)
|
||||||
|
with local.cwd(path):
|
||||||
|
git('add', '.')
|
||||||
|
git('commit', '-m', 'Add hooks')
|
||||||
|
return path
|
||||||
|
|
||||||
|
|
||||||
|
def make_config_from_repo(repo_path, sha=None, hooks=None, check=True):
|
||||||
|
manifest = load_manifest(os.path.join(repo_path, C.MANIFEST_FILE))
|
||||||
|
config = OrderedDict((
|
||||||
|
('repo', repo_path),
|
||||||
|
('sha', sha or get_head_sha(repo_path)),
|
||||||
|
(
|
||||||
|
'hooks',
|
||||||
|
hooks or [OrderedDict((('id', hook['id']),)) for hook in manifest],
|
||||||
|
),
|
||||||
|
))
|
||||||
|
|
||||||
|
if check:
|
||||||
|
wrapped_config = apply_defaults([config], CONFIG_JSON_SCHEMA)
|
||||||
|
validate_config_extra(wrapped_config)
|
||||||
|
return wrapped_config[0]
|
||||||
|
else:
|
||||||
|
return config
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import os
|
import io
|
||||||
import os.path
|
|
||||||
import pytest
|
import pytest
|
||||||
import shutil
|
import shutil
|
||||||
from asottile.ordereddict import OrderedDict
|
from asottile.ordereddict import OrderedDict
|
||||||
|
|
@ -9,56 +8,40 @@ from asottile.yaml import ordered_dump
|
||||||
from plumbum import local
|
from plumbum import local
|
||||||
|
|
||||||
import pre_commit.constants as C
|
import pre_commit.constants as C
|
||||||
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
|
|
||||||
from pre_commit.clientlib.validate_config import validate_config_extra
|
|
||||||
from pre_commit.commands.autoupdate import _update_repository
|
from pre_commit.commands.autoupdate import _update_repository
|
||||||
from pre_commit.commands.autoupdate import autoupdate
|
from pre_commit.commands.autoupdate import autoupdate
|
||||||
from pre_commit.commands.autoupdate import RepositoryCannotBeUpdatedError
|
from pre_commit.commands.autoupdate import RepositoryCannotBeUpdatedError
|
||||||
from pre_commit.jsonschema_extensions import apply_defaults
|
|
||||||
from pre_commit.jsonschema_extensions import remove_defaults
|
|
||||||
from pre_commit.runner import Runner
|
from pre_commit.runner import Runner
|
||||||
from testing.auto_namedtuple import auto_namedtuple
|
from testing.auto_namedtuple import auto_namedtuple
|
||||||
|
from testing.fixtures import make_config_from_repo
|
||||||
|
from testing.fixtures import make_hooks_repo
|
||||||
from testing.util import get_head_sha
|
from testing.util import get_head_sha
|
||||||
from testing.util import get_resource_path
|
from testing.util import get_resource_path
|
||||||
|
|
||||||
|
|
||||||
@pytest.yield_fixture
|
@pytest.yield_fixture
|
||||||
def up_to_date_repo(python_hooks_repo):
|
def up_to_date_repo(tmpdir_factory):
|
||||||
config = OrderedDict((
|
yield make_hooks_repo(tmpdir_factory, 'python_hooks_repo')
|
||||||
('repo', python_hooks_repo),
|
|
||||||
('sha', get_head_sha(python_hooks_repo)),
|
|
||||||
('hooks', [OrderedDict((('id', 'foo'),))]),
|
|
||||||
))
|
|
||||||
wrapped_config = apply_defaults([config], CONFIG_JSON_SCHEMA)
|
|
||||||
validate_config_extra(wrapped_config)
|
|
||||||
config = wrapped_config[0]
|
|
||||||
|
|
||||||
with open(os.path.join(python_hooks_repo, C.CONFIG_FILE), 'w') as file_obj:
|
|
||||||
file_obj.write(
|
|
||||||
ordered_dump(
|
|
||||||
remove_defaults([config], CONFIG_JSON_SCHEMA),
|
|
||||||
**C.YAML_DUMP_KWARGS
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
yield auto_namedtuple(
|
|
||||||
repo_config=config,
|
|
||||||
python_hooks_repo=python_hooks_repo,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_up_to_date_repo(up_to_date_repo, runner_with_mocked_store):
|
def test_up_to_date_repo(up_to_date_repo, runner_with_mocked_store):
|
||||||
input_sha = up_to_date_repo.repo_config['sha']
|
config = make_config_from_repo(up_to_date_repo)
|
||||||
ret = _update_repository(
|
input_sha = config['sha']
|
||||||
up_to_date_repo.repo_config, runner_with_mocked_store,
|
ret = _update_repository(config, runner_with_mocked_store)
|
||||||
)
|
|
||||||
assert ret['sha'] == input_sha
|
assert ret['sha'] == input_sha
|
||||||
|
|
||||||
|
|
||||||
def test_autoupdate_up_to_date_repo(up_to_date_repo, mock_out_store_directory):
|
def test_autoupdate_up_to_date_repo(
|
||||||
|
up_to_date_repo, in_tmpdir, mock_out_store_directory,
|
||||||
|
):
|
||||||
|
# Write out the config
|
||||||
|
config = make_config_from_repo(up_to_date_repo, check=False)
|
||||||
|
with io.open(C.CONFIG_FILE, 'w') as config_file:
|
||||||
|
config_file.write(ordered_dump([config], **C.YAML_DUMP_KWARGS))
|
||||||
|
|
||||||
before = open(C.CONFIG_FILE).read()
|
before = open(C.CONFIG_FILE).read()
|
||||||
assert '^$' not in before
|
assert '^$' not in before
|
||||||
runner = Runner(up_to_date_repo.python_hooks_repo)
|
runner = Runner('.')
|
||||||
ret = autoupdate(runner)
|
ret = autoupdate(runner)
|
||||||
after = open(C.CONFIG_FILE).read()
|
after = open(C.CONFIG_FILE).read()
|
||||||
assert ret == 0
|
assert ret == 0
|
||||||
|
|
@ -66,42 +49,41 @@ def test_autoupdate_up_to_date_repo(up_to_date_repo, mock_out_store_directory):
|
||||||
|
|
||||||
|
|
||||||
@pytest.yield_fixture
|
@pytest.yield_fixture
|
||||||
def out_of_date_repo(python_hooks_repo):
|
def out_of_date_repo(tmpdir_factory):
|
||||||
config = OrderedDict((
|
path = make_hooks_repo(tmpdir_factory, 'python_hooks_repo')
|
||||||
('repo', python_hooks_repo),
|
original_sha = get_head_sha(path)
|
||||||
('sha', get_head_sha(python_hooks_repo)),
|
|
||||||
('hooks', [OrderedDict((('id', 'foo'), ('files', '')))]),
|
|
||||||
))
|
|
||||||
config_wrapped = apply_defaults([config], CONFIG_JSON_SCHEMA)
|
|
||||||
validate_config_extra(config_wrapped)
|
|
||||||
config = config_wrapped[0]
|
|
||||||
local['git']['commit', '--allow-empty', '-m', 'foo']()
|
|
||||||
head_sha = get_head_sha(python_hooks_repo)
|
|
||||||
|
|
||||||
with open(os.path.join(python_hooks_repo, C.CONFIG_FILE), 'w') as file_obj:
|
# Make a commit
|
||||||
file_obj.write(
|
with local.cwd(path):
|
||||||
ordered_dump([config], **C.YAML_DUMP_KWARGS)
|
local['git']['commit', '--allow-empty', '-m', 'foo']()
|
||||||
)
|
head_sha = get_head_sha(path)
|
||||||
|
|
||||||
yield auto_namedtuple(
|
yield auto_namedtuple(
|
||||||
repo_config=config,
|
path=path, original_sha=original_sha, head_sha=head_sha,
|
||||||
head_sha=head_sha,
|
|
||||||
python_hooks_repo=python_hooks_repo,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_out_of_date_repo(out_of_date_repo, runner_with_mocked_store):
|
def test_out_of_date_repo(out_of_date_repo, runner_with_mocked_store):
|
||||||
ret = _update_repository(
|
config = make_config_from_repo(
|
||||||
out_of_date_repo.repo_config, runner_with_mocked_store,
|
out_of_date_repo.path, sha=out_of_date_repo.original_sha,
|
||||||
)
|
)
|
||||||
|
ret = _update_repository(config, runner_with_mocked_store)
|
||||||
|
assert ret['sha'] != out_of_date_repo.original_sha
|
||||||
assert ret['sha'] == out_of_date_repo.head_sha
|
assert ret['sha'] == out_of_date_repo.head_sha
|
||||||
|
|
||||||
|
|
||||||
def test_autoupdate_out_of_date_repo(
|
def test_autoupdate_out_of_date_repo(
|
||||||
out_of_date_repo, mock_out_store_directory
|
out_of_date_repo, in_tmpdir, mock_out_store_directory
|
||||||
):
|
):
|
||||||
|
# Write out the config
|
||||||
|
config = make_config_from_repo(
|
||||||
|
out_of_date_repo.path, sha=out_of_date_repo.original_sha, check=False,
|
||||||
|
)
|
||||||
|
with io.open(C.CONFIG_FILE, 'w') as config_file:
|
||||||
|
config_file.write(ordered_dump([config], **C.YAML_DUMP_KWARGS))
|
||||||
|
|
||||||
before = open(C.CONFIG_FILE).read()
|
before = open(C.CONFIG_FILE).read()
|
||||||
runner = Runner(out_of_date_repo.python_hooks_repo)
|
runner = Runner('.')
|
||||||
ret = autoupdate(runner)
|
ret = autoupdate(runner)
|
||||||
after = open(C.CONFIG_FILE).read()
|
after = open(C.CONFIG_FILE).read()
|
||||||
assert ret == 0
|
assert ret == 0
|
||||||
|
|
@ -112,47 +94,47 @@ def test_autoupdate_out_of_date_repo(
|
||||||
|
|
||||||
|
|
||||||
@pytest.yield_fixture
|
@pytest.yield_fixture
|
||||||
def hook_disappearing_repo(python_hooks_repo):
|
def hook_disappearing_repo(tmpdir_factory):
|
||||||
config = OrderedDict((
|
path = make_hooks_repo(tmpdir_factory, 'python_hooks_repo')
|
||||||
('repo', python_hooks_repo),
|
original_sha = get_head_sha(path)
|
||||||
('sha', get_head_sha(python_hooks_repo)),
|
|
||||||
('hooks', [OrderedDict((('id', 'foo'),))]),
|
|
||||||
))
|
|
||||||
config_wrapped = apply_defaults([config], CONFIG_JSON_SCHEMA)
|
|
||||||
validate_config_extra(config_wrapped)
|
|
||||||
config = config_wrapped[0]
|
|
||||||
shutil.copy(
|
|
||||||
get_resource_path('manifest_without_foo.yaml'),
|
|
||||||
C.MANIFEST_FILE,
|
|
||||||
)
|
|
||||||
local['git']['add', '.']()
|
|
||||||
local['git']['commit', '-m', 'Remove foo']()
|
|
||||||
|
|
||||||
with open(os.path.join(python_hooks_repo, C.CONFIG_FILE), 'w') as file_obj:
|
with local.cwd(path):
|
||||||
file_obj.write(
|
shutil.copy(
|
||||||
ordered_dump([config], **C.YAML_DUMP_KWARGS)
|
get_resource_path('manifest_without_foo.yaml'),
|
||||||
|
C.MANIFEST_FILE,
|
||||||
)
|
)
|
||||||
|
local['git']('add', '.')
|
||||||
|
local['git']('commit', '-m', 'Remove foo')
|
||||||
|
|
||||||
yield auto_namedtuple(
|
yield auto_namedtuple(path=path, original_sha=original_sha)
|
||||||
repo_config=config,
|
|
||||||
python_hooks_repo=python_hooks_repo,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_hook_disppearing_repo_raises(
|
def test_hook_disppearing_repo_raises(
|
||||||
hook_disappearing_repo, runner_with_mocked_store
|
hook_disappearing_repo, runner_with_mocked_store
|
||||||
):
|
):
|
||||||
|
config = make_config_from_repo(
|
||||||
|
hook_disappearing_repo.path,
|
||||||
|
sha=hook_disappearing_repo.original_sha,
|
||||||
|
hooks=[OrderedDict((('id', 'foo'),))],
|
||||||
|
)
|
||||||
with pytest.raises(RepositoryCannotBeUpdatedError):
|
with pytest.raises(RepositoryCannotBeUpdatedError):
|
||||||
_update_repository(
|
_update_repository(config, runner_with_mocked_store)
|
||||||
hook_disappearing_repo.repo_config, runner_with_mocked_store,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_autoupdate_hook_disappearing_repo(
|
def test_autoupdate_hook_disappearing_repo(
|
||||||
hook_disappearing_repo, mock_out_store_directory
|
hook_disappearing_repo, in_tmpdir, mock_out_store_directory
|
||||||
):
|
):
|
||||||
|
config = make_config_from_repo(
|
||||||
|
hook_disappearing_repo.path,
|
||||||
|
sha=hook_disappearing_repo.original_sha,
|
||||||
|
hooks=[OrderedDict((('id', 'foo'),))],
|
||||||
|
check=False,
|
||||||
|
)
|
||||||
|
with io.open(C.CONFIG_FILE, 'w') as config_file:
|
||||||
|
config_file.write(ordered_dump([config], **C.YAML_DUMP_KWARGS))
|
||||||
|
|
||||||
before = open(C.CONFIG_FILE).read()
|
before = open(C.CONFIG_FILE).read()
|
||||||
runner = Runner(hook_disappearing_repo.python_hooks_repo)
|
runner = Runner('.')
|
||||||
ret = autoupdate(runner)
|
ret = autoupdate(runner)
|
||||||
after = open(C.CONFIG_FILE).read()
|
after = open(C.CONFIG_FILE).read()
|
||||||
assert ret == 1
|
assert ret == 1
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue