Enable map configurations (config v2).

This commit is contained in:
Anthony Sottile 2017-09-05 14:04:08 -07:00
parent ef8347cf2d
commit 3e76cdaf25
11 changed files with 70 additions and 52 deletions

View file

@ -1,3 +1,4 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks.git - repo: https://github.com/pre-commit/pre-commit-hooks.git
sha: v0.9.1 sha: v0.9.1
hooks: hooks:

View file

@ -2,6 +2,7 @@ from __future__ import absolute_import
from __future__ import unicode_literals from __future__ import unicode_literals
import argparse import argparse
import collections
import functools import functools
from aspy.yaml import ordered_load from aspy.yaml import ordered_load
@ -125,7 +126,11 @@ CONFIG_REPO_DICT = schema.Map(
ensure_absent=True, ensure_absent=True,
), ),
) )
CONFIG_SCHEMA = schema.Array(CONFIG_REPO_DICT) CONFIG_SCHEMA = schema.Map(
'Config', None,
schema.RequiredRecurse('repos', schema.Array(CONFIG_REPO_DICT)),
)
def is_local_repo(repo_entry): def is_local_repo(repo_entry):
@ -136,10 +141,19 @@ class InvalidConfigError(FatalError):
pass pass
def ordered_load_normalize_legacy_config(contents):
data = ordered_load(contents)
if isinstance(data, list):
# TODO: Once happy, issue a deprecation warning and instructions
return collections.OrderedDict([('repos', data)])
else:
return data
load_config = functools.partial( load_config = functools.partial(
schema.load_from_filename, schema.load_from_filename,
schema=CONFIG_SCHEMA, schema=CONFIG_SCHEMA,
load_strategy=ordered_load, load_strategy=ordered_load_normalize_legacy_config,
exc_tp=InvalidConfigError, exc_tp=InvalidConfigError,
) )

View file

@ -109,7 +109,7 @@ def autoupdate(runner, tags_only):
input_configs = load_config(runner.config_file_path) input_configs = load_config(runner.config_file_path)
for repo_config in input_configs: for repo_config in input_configs['repos']:
if is_local_repo(repo_config): if is_local_repo(repo_config):
output_configs.append(repo_config) output_configs.append(repo_config)
continue continue

View file

@ -10,8 +10,9 @@ from __future__ import unicode_literals
SAMPLE_CONFIG = '''\ SAMPLE_CONFIG = '''\
# See http://pre-commit.com for more information # See http://pre-commit.com for more information
# See http://pre-commit.com/hooks.html for more hooks # See http://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks - repo: https://github.com/pre-commit/pre-commit-hooks
sha: v0.9.1 sha: v0.9.2
hooks: hooks:
- id: trailing-whitespace - id: trailing-whitespace
- id: end-of-file-fixer - id: end-of-file-fixer

View file

@ -40,11 +40,11 @@ class Runner(object):
@cached_property @cached_property
def repositories(self): def repositories(self):
"""Returns a tuple of the configured repositories.""" """Returns a tuple of the configured repositories."""
config = load_config(self.config_file_path) repos = load_config(self.config_file_path)['repos']
repositories = tuple(Repository.create(x, self.store) for x in config) repos = tuple(Repository.create(x, self.store) for x in repos)
for repository in repositories: for repo in repos:
repository.require_installed() repo.require_installed()
return repositories return repos
def get_hook_path(self, hook_type): def get_hook_path(self, hook_type):
return os.path.join(self.git_dir, 'hooks', hook_type) return os.path.join(self.git_dir, 'hooks', hook_type)

View file

@ -142,9 +142,13 @@ class Map(collections.namedtuple('Map', ('object_name', 'id_key', 'items'))):
raise ValidationError('Expected a {} map but got a {}'.format( raise ValidationError('Expected a {} map but got a {}'.format(
self.object_name, type(v).__name__, self.object_name, type(v).__name__,
)) ))
with validate_context('At {}({}={!r})'.format( if self.id_key is None:
context = 'At {}()'.format(self.object_name)
else:
context = 'At {}({}={!r})'.format(
self.object_name, self.id_key, v.get(self.id_key, MISSING), self.object_name, self.id_key, v.get(self.id_key, MISSING),
)): )
with validate_context(context):
for item in self.items: for item in self.items:
item.check(v) item.check(v)

View file

@ -92,8 +92,9 @@ def make_config_from_repo(repo_path, sha=None, hooks=None, check=True):
)) ))
if check: if check:
wrapped = validate([config], CONFIG_SCHEMA) wrapped = validate({'repos': [config]}, CONFIG_SCHEMA)
config, = apply_defaults(wrapped, CONFIG_SCHEMA) wrapped = apply_defaults(wrapped, CONFIG_SCHEMA)
config, = wrapped['repos']
return config return config
else: else:
return config return config
@ -106,9 +107,9 @@ def read_config(directory, config_file=C.CONFIG_FILE):
def write_config(directory, config, config_file=C.CONFIG_FILE): def write_config(directory, config, config_file=C.CONFIG_FILE):
if type(config) is not list: if type(config) is not list and 'repos' not in config:
assert type(config) is OrderedDict assert type(config) is OrderedDict
config = [config] config = {'repos': [config]}
with io.open(os.path.join(directory, config_file), 'w') as outfile: with io.open(os.path.join(directory, config_file), 'w') as outfile:
outfile.write(ordered_dump(config, **C.YAML_DUMP_KWARGS)) outfile.write(ordered_dump(config, **C.YAML_DUMP_KWARGS))

View file

@ -60,15 +60,15 @@ def test_validate_config_main(args, expected_output):
('config_obj', 'expected'), ( ('config_obj', 'expected'), (
([], False), ([], False),
( (
[{ {'repos': [{
'repo': 'git@github.com:pre-commit/pre-commit-hooks', 'repo': 'git@github.com:pre-commit/pre-commit-hooks',
'sha': 'cd74dc150c142c3be70b24eaf0b02cae9d235f37', 'sha': 'cd74dc150c142c3be70b24eaf0b02cae9d235f37',
'hooks': [{'id': 'pyflakes', 'files': '\\.py$'}], 'hooks': [{'id': 'pyflakes', 'files': '\\.py$'}],
}], }]},
True, True,
), ),
( (
[{ {'repos': [{
'repo': 'git@github.com:pre-commit/pre-commit-hooks', 'repo': 'git@github.com:pre-commit/pre-commit-hooks',
'sha': 'cd74dc150c142c3be70b24eaf0b02cae9d235f37', 'sha': 'cd74dc150c142c3be70b24eaf0b02cae9d235f37',
'hooks': [ 'hooks': [
@ -78,11 +78,11 @@ def test_validate_config_main(args, expected_output):
'args': ['foo', 'bar', 'baz'], 'args': ['foo', 'bar', 'baz'],
}, },
], ],
}], }]},
True, True,
), ),
( (
[{ {'repos': [{
'repo': 'git@github.com:pre-commit/pre-commit-hooks', 'repo': 'git@github.com:pre-commit/pre-commit-hooks',
'sha': 'cd74dc150c142c3be70b24eaf0b02cae9d235f37', 'sha': 'cd74dc150c142c3be70b24eaf0b02cae9d235f37',
'hooks': [ 'hooks': [
@ -94,7 +94,7 @@ def test_validate_config_main(args, expected_output):
'args': ['foo', 'bar', 'baz'], 'args': ['foo', 'bar', 'baz'],
}, },
], ],
}], }]},
False, False,
), ),
), ),
@ -104,9 +104,8 @@ def test_config_valid(config_obj, expected):
assert ret is expected assert ret is expected
@pytest.mark.parametrize( def test_config_with_local_hooks_definition_fails():
'config_obj', ( config_obj = {'repos': [{
[{
'repo': 'local', 'repo': 'local',
'sha': 'foo', 'sha': 'foo',
'hooks': [{ 'hooks': [{
@ -116,17 +115,14 @@ def test_config_valid(config_obj, expected):
'language': 'pcre', 'language': 'pcre',
'files': '^(.*)$', 'files': '^(.*)$',
}], }],
}], }]}
),
)
def test_config_with_local_hooks_definition_fails(config_obj):
with pytest.raises(schema.ValidationError): with pytest.raises(schema.ValidationError):
schema.validate(config_obj, CONFIG_SCHEMA) schema.validate(config_obj, CONFIG_SCHEMA)
@pytest.mark.parametrize( @pytest.mark.parametrize(
'config_obj', ( 'config_obj', (
[{ {'repos': [{
'repo': 'local', 'repo': 'local',
'hooks': [{ 'hooks': [{
'id': 'arg-per-line', 'id': 'arg-per-line',
@ -136,8 +132,8 @@ def test_config_with_local_hooks_definition_fails(config_obj):
'files': '', 'files': '',
'args': ['hello', 'world'], 'args': ['hello', 'world'],
}], }],
}], }]},
[{ {'repos': [{
'repo': 'local', 'repo': 'local',
'hooks': [{ 'hooks': [{
'id': 'arg-per-line', 'id': 'arg-per-line',
@ -147,7 +143,7 @@ def test_config_with_local_hooks_definition_fails(config_obj):
'files': '', 'files': '',
'args': ['hello', 'world'], 'args': ['hello', 'world'],
}], }],
}], }]},
), ),
) )
def test_config_with_local_hooks_definition_passes(config_obj): def test_config_with_local_hooks_definition_passes(config_obj):

View file

@ -274,7 +274,7 @@ def test_autoupdate_local_hooks(tempdir_factory):
assert autoupdate(runner, tags_only=False) == 0 assert autoupdate(runner, tags_only=False) == 0
new_config_writen = load_config(runner.config_file_path) new_config_writen = load_config(runner.config_file_path)
assert len(new_config_writen) == 1 assert len(new_config_writen) == 1
assert new_config_writen[0] == config assert new_config_writen['repos'][0] == config
def test_autoupdate_local_hooks_with_out_of_date_repo( def test_autoupdate_local_hooks_with_out_of_date_repo(
@ -289,5 +289,5 @@ def test_autoupdate_local_hooks_with_out_of_date_repo(
runner = Runner('.', C.CONFIG_FILE) runner = Runner('.', C.CONFIG_FILE)
assert autoupdate(runner, tags_only=False) == 0 assert autoupdate(runner, tags_only=False) == 0
new_config_writen = load_config(runner.config_file_path) new_config_writen = load_config(runner.config_file_path)
assert len(new_config_writen) == 2 assert len(new_config_writen['repos']) == 2
assert new_config_writen[0] == local_config assert new_config_writen['repos'][0] == local_config

View file

@ -272,7 +272,7 @@ def test_always_run(
cap_out, repo_with_passing_hook, mock_out_store_directory, cap_out, repo_with_passing_hook, mock_out_store_directory,
): ):
with modify_config() as config: with modify_config() as config:
config[0]['hooks'][0]['always_run'] = True config['repos'][0]['hooks'][0]['always_run'] = True
_test_run( _test_run(
cap_out, cap_out,
repo_with_passing_hook, repo_with_passing_hook,
@ -288,7 +288,7 @@ def test_always_run_alt_config(
): ):
repo_root = '.' repo_root = '.'
config = read_config(repo_root) config = read_config(repo_root)
config[0]['hooks'][0]['always_run'] = True config['repos'][0]['hooks'][0]['always_run'] = True
alt_config_file = 'alternate_config.yaml' alt_config_file = 'alternate_config.yaml'
add_config_to_repo(repo_root, config, config_file=alt_config_file) add_config_to_repo(repo_root, config, config_file=alt_config_file)
@ -428,7 +428,7 @@ def test_multiple_hooks_same_id(
with cwd(repo_with_passing_hook): with cwd(repo_with_passing_hook):
# Add bash hook on there again # Add bash hook on there again
with modify_config() as config: with modify_config() as config:
config[0]['hooks'].append({'id': 'bash_hook'}) config['repos'][0]['hooks'].append({'id': 'bash_hook'})
stage_a_file() stage_a_file()
ret, output = _do_run(cap_out, repo_with_passing_hook, _get_opts()) ret, output = _do_run(cap_out, repo_with_passing_hook, _get_opts())
@ -455,7 +455,7 @@ def test_stdout_write_bug_py26(
): ):
with cwd(repo_with_failing_hook): with cwd(repo_with_failing_hook):
with modify_config() as config: with modify_config() as config:
config[0]['hooks'][0]['args'] = [''] config['repos'][0]['hooks'][0]['args'] = ['']
stage_a_file() stage_a_file()
install(Runner(repo_with_failing_hook, C.CONFIG_FILE)) install(Runner(repo_with_failing_hook, C.CONFIG_FILE))
@ -505,7 +505,7 @@ def test_lots_of_files(mock_out_store_directory, tempdir_factory):
with cwd(git_path): with cwd(git_path):
# Override files so we run against them # Override files so we run against them
with modify_config() as config: with modify_config() as config:
config[0]['hooks'][0]['files'] = '' config['repos'][0]['hooks'][0]['files'] = ''
# Write a crap ton of files # Write a crap ton of files
for i in range(400): for i in range(400):
@ -660,7 +660,7 @@ def test_local_hook_fails(
def modified_config_repo(repo_with_passing_hook): def modified_config_repo(repo_with_passing_hook):
with modify_config(repo_with_passing_hook, commit=False) as config: with modify_config(repo_with_passing_hook, commit=False) as config:
# Some minor modification # Some minor modification
config[0]['hooks'][0]['files'] = '' config['repos'][0]['hooks'][0]['files'] = ''
yield repo_with_passing_hook yield repo_with_passing_hook
@ -721,8 +721,8 @@ def test_pass_filenames(
expected_out, expected_out,
): ):
with modify_config() as config: with modify_config() as config:
config[0]['hooks'][0]['pass_filenames'] = pass_filenames config['repos'][0]['hooks'][0]['pass_filenames'] = pass_filenames
config[0]['hooks'][0]['args'] = hook_args config['repos'][0]['hooks'][0]['args'] = hook_args
stage_a_file() stage_a_file()
ret, printed = _do_run( ret, printed = _do_run(
cap_out, repo_with_passing_hook, _get_opts(verbose=True), cap_out, repo_with_passing_hook, _get_opts(verbose=True),

View file

@ -11,8 +11,9 @@ def test_sample_config(capsys):
assert out == '''\ assert out == '''\
# See http://pre-commit.com for more information # See http://pre-commit.com for more information
# See http://pre-commit.com/hooks.html for more hooks # See http://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks - repo: https://github.com/pre-commit/pre-commit-hooks
sha: v0.9.1 sha: v0.9.2
hooks: hooks:
- id: trailing-whitespace - id: trailing-whitespace
- id: end-of-file-fixer - id: end-of-file-fixer