Merge pull request #900 from pre-commit/simplify_dict

just use normal dicts in tests
This commit is contained in:
Anthony Sottile 2018-12-31 11:43:36 -08:00 committed by GitHub
commit 5f9a667470
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 114 additions and 157 deletions

View file

@ -2,7 +2,6 @@ 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
import cfgv import cfgv
@ -170,7 +169,7 @@ def ordered_load_normalize_legacy_config(contents):
data = ordered_load(contents) data = ordered_load(contents)
if isinstance(data, list): if isinstance(data, list):
# TODO: Once happy, issue a deprecation warning and instructions # TODO: Once happy, issue a deprecation warning and instructions
return collections.OrderedDict([('repos', data)]) return {'repos': data}
else: else:
return data return data

View file

@ -5,7 +5,6 @@ import contextlib
import io import io
import os.path import os.path
import shutil import shutil
from collections import OrderedDict
from aspy.yaml import ordered_dump from aspy.yaml import ordered_dump
from aspy.yaml import ordered_load from aspy.yaml import ordered_load
@ -83,30 +82,24 @@ def modify_config(path='.', commit=True):
def config_with_local_hooks(): def config_with_local_hooks():
return OrderedDict(( return {
('repo', 'local'), 'repo': 'local',
( 'hooks': [{
'hooks', [OrderedDict(( 'id': 'do_not_commit',
('id', 'do_not_commit'), 'name': 'Block if "DO NOT COMMIT" is found',
('name', 'Block if "DO NOT COMMIT" is found'), 'entry': 'DO NOT COMMIT',
('entry', 'DO NOT COMMIT'), 'language': 'pygrep',
('language', 'pygrep'), }],
('files', '^(.*)$'), }
))],
),
))
def make_config_from_repo(repo_path, rev=None, hooks=None, check=True): def make_config_from_repo(repo_path, rev=None, hooks=None, check=True):
manifest = load_manifest(os.path.join(repo_path, C.MANIFEST_FILE)) manifest = load_manifest(os.path.join(repo_path, C.MANIFEST_FILE))
config = OrderedDict(( config = {
('repo', 'file://{}'.format(repo_path)), 'repo': 'file://{}'.format(repo_path),
('rev', rev or git.head_rev(repo_path)), 'rev': rev or git.head_rev(repo_path),
( 'hooks': hooks or [{'id': hook['id']} for hook in manifest],
'hooks', }
hooks or [OrderedDict((('id', hook['id']),)) for hook in manifest],
),
))
if check: if check:
wrapped = validate({'repos': [config]}, CONFIG_SCHEMA) wrapped = validate({'repos': [config]}, CONFIG_SCHEMA)
@ -126,7 +119,7 @@ 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 and 'repos' not in config: if type(config) is not list and 'repos' not in config:
assert type(config) is OrderedDict assert isinstance(config, dict), config
config = {'repos': [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

@ -11,6 +11,7 @@ from pre_commit.clientlib import MANIFEST_SCHEMA
from pre_commit.clientlib import MigrateShaToRev from pre_commit.clientlib import MigrateShaToRev
from pre_commit.clientlib import validate_config_main from pre_commit.clientlib import validate_config_main
from pre_commit.clientlib import validate_manifest_main from pre_commit.clientlib import validate_manifest_main
from testing.fixtures import config_with_local_hooks
from testing.util import get_resource_path from testing.util import get_resource_path
@ -92,18 +93,9 @@ def test_config_valid(config_obj, expected):
assert ret is expected assert ret is expected
def test_config_with_local_hooks_definition_fails(): def test_local_hooks_with_rev_fails():
config_obj = {'repos': [{ config_obj = {'repos': [config_with_local_hooks()]}
'repo': 'local', config_obj['repos'][0]['rev'] = 'foo'
'rev': 'foo',
'hooks': [{
'id': 'do_not_commit',
'name': 'Block if "DO NOT COMMIT" is found',
'entry': 'DO NOT COMMIT',
'language': 'pcre',
'files': '^(.*)$',
}],
}]}
with pytest.raises(cfgv.ValidationError): with pytest.raises(cfgv.ValidationError):
cfgv.validate(config_obj, CONFIG_SCHEMA) cfgv.validate(config_obj, CONFIG_SCHEMA)

View file

@ -3,7 +3,6 @@ from __future__ import unicode_literals
import os.path import os.path
import pipes import pipes
import shutil import shutil
from collections import OrderedDict
import pytest import pytest
@ -290,7 +289,7 @@ def test_hook_disppearing_repo_raises(hook_disappearing_repo, store):
config = make_config_from_repo( config = make_config_from_repo(
hook_disappearing_repo.path, hook_disappearing_repo.path,
rev=hook_disappearing_repo.original_rev, rev=hook_disappearing_repo.original_rev,
hooks=[OrderedDict((('id', 'foo'),))], hooks=[{'id': 'foo'}],
) )
with pytest.raises(RepositoryCannotBeUpdatedError): with pytest.raises(RepositoryCannotBeUpdatedError):
_update_repo(config, store, tags_only=False) _update_repo(config, store, tags_only=False)
@ -302,7 +301,7 @@ def test_autoupdate_hook_disappearing_repo(
config = make_config_from_repo( config = make_config_from_repo(
hook_disappearing_repo.path, hook_disappearing_repo.path,
rev=hook_disappearing_repo.original_rev, rev=hook_disappearing_repo.original_rev,
hooks=[OrderedDict((('id', 'foo'),))], hooks=[{'id': 'foo'}],
check=False, check=False,
) )
write_config('.', config) write_config('.', config)

View file

@ -5,7 +5,6 @@ import io
import os.path import os.path
import subprocess import subprocess
import sys import sys
from collections import OrderedDict
import pytest import pytest
@ -521,21 +520,19 @@ def test_lots_of_files(store, tempdir_factory):
def test_stages(cap_out, store, repo_with_passing_hook): def test_stages(cap_out, store, repo_with_passing_hook):
config = OrderedDict(( config = {
('repo', 'local'), 'repo': 'local',
( 'hooks': [
'hooks', tuple( {
{ 'id': 'do-not-commit-{}'.format(i),
'id': 'do-not-commit-{}'.format(i), 'name': 'hook {}'.format(i),
'name': 'hook {}'.format(i), 'entry': 'DO NOT COMMIT',
'entry': 'DO NOT COMMIT', 'language': 'pygrep',
'language': 'pygrep', 'stages': [stage],
'stages': [stage], }
} for i, stage in enumerate(('commit', 'push', 'manual'), 1)
for i, stage in enumerate(('commit', 'push', 'manual'), 1) ],
), }
),
))
add_config_to_repo(repo_with_passing_hook, config) add_config_to_repo(repo_with_passing_hook, config)
stage_a_file() stage_a_file()
@ -570,26 +567,24 @@ def test_commit_msg_hook(cap_out, store, commit_msg_repo):
def test_local_hook_passes(cap_out, store, repo_with_passing_hook): def test_local_hook_passes(cap_out, store, repo_with_passing_hook):
config = OrderedDict(( config = {
('repo', 'local'), 'repo': 'local',
( 'hooks': [
'hooks', ( {
OrderedDict(( 'id': 'flake8',
('id', 'flake8'), 'name': 'flake8',
('name', 'flake8'), 'entry': "'{}' -m flake8".format(sys.executable),
('entry', "'{}' -m flake8".format(sys.executable)), 'language': 'system',
('language', 'system'), 'files': r'\.py$',
('files', r'\.py$'), },
)), OrderedDict(( {
('id', 'do_not_commit'), 'id': 'do_not_commit',
('name', 'Block if "DO NOT COMMIT" is found'), 'name': 'Block if "DO NOT COMMIT" is found',
('entry', 'DO NOT COMMIT'), 'entry': 'DO NOT COMMIT',
('language', 'pygrep'), 'language': 'pygrep',
('files', '^(.*)$'), },
)), ],
), }
),
))
add_config_to_repo(repo_with_passing_hook, config) add_config_to_repo(repo_with_passing_hook, config)
with io.open('dummy.py', 'w') as staged_file: with io.open('dummy.py', 'w') as staged_file:
@ -608,18 +603,15 @@ def test_local_hook_passes(cap_out, store, repo_with_passing_hook):
def test_local_hook_fails(cap_out, store, repo_with_passing_hook): def test_local_hook_fails(cap_out, store, repo_with_passing_hook):
config = OrderedDict(( config = {
('repo', 'local'), 'repo': 'local',
( 'hooks': [{
'hooks', [OrderedDict(( 'id': 'no-todo',
('id', 'no-todo'), 'name': 'No TODO',
('name', 'No TODO'), 'entry': 'sh -c "! grep -iI todo $@" --',
('entry', 'sh -c "! grep -iI todo $@" --'), 'language': 'system',
('language', 'system'), }],
('files', ''), }
))],
),
))
add_config_to_repo(repo_with_passing_hook, config) add_config_to_repo(repo_with_passing_hook, config)
with io.open('dummy.py', 'w') as staged_file: with io.open('dummy.py', 'w') as staged_file:
@ -638,17 +630,15 @@ def test_local_hook_fails(cap_out, store, repo_with_passing_hook):
def test_pcre_deprecation_warning(cap_out, store, repo_with_passing_hook): def test_pcre_deprecation_warning(cap_out, store, repo_with_passing_hook):
config = OrderedDict(( config = {
('repo', 'local'), 'repo': 'local',
( 'hooks': [{
'hooks', [OrderedDict(( 'id': 'pcre-hook',
('id', 'pcre-hook'), 'name': 'pcre-hook',
('name', 'pcre-hook'), 'language': 'pcre',
('language', 'pcre'), 'entry': '.',
('entry', '.'), }],
))], }
),
))
add_config_to_repo(repo_with_passing_hook, config) add_config_to_repo(repo_with_passing_hook, config)
_test_run( _test_run(
@ -666,16 +656,10 @@ def test_pcre_deprecation_warning(cap_out, store, repo_with_passing_hook):
def test_meta_hook_passes(cap_out, store, repo_with_passing_hook): def test_meta_hook_passes(cap_out, store, repo_with_passing_hook):
config = OrderedDict(( config = {
('repo', 'meta'), 'repo': 'meta',
( 'hooks': [{'id': 'check-useless-excludes'}],
'hooks', ( }
OrderedDict((
('id', 'check-useless-excludes'),
)),
),
),
))
add_config_to_repo(repo_with_passing_hook, config) add_config_to_repo(repo_with_passing_hook, config)
_test_run( _test_run(
@ -810,25 +794,24 @@ def test_include_exclude_exclude_removes_files(some_filenames):
def test_args_hook_only(cap_out, store, repo_with_passing_hook): def test_args_hook_only(cap_out, store, repo_with_passing_hook):
config = OrderedDict(( config = {
('repo', 'local'), 'repo': 'local',
( 'hooks': [
'hooks', ( {
OrderedDict(( 'id': 'flake8',
('id', 'flake8'), 'name': 'flake8',
('name', 'flake8'), 'entry': "'{}' -m flake8".format(sys.executable),
('entry', "'{}' -m flake8".format(sys.executable)), 'language': 'system',
('language', 'system'), 'stages': ['commit'],
('stages', ['commit']), },
)), OrderedDict(( {
('id', 'do_not_commit'), 'id': 'do_not_commit',
('name', 'Block if "DO NOT COMMIT" is found'), 'name': 'Block if "DO NOT COMMIT" is found',
('entry', 'DO NOT COMMIT'), 'entry': 'DO NOT COMMIT',
('language', 'pygrep'), 'language': 'pygrep',
)), },
), ],
), }
))
add_config_to_repo(repo_with_passing_hook, config) add_config_to_repo(repo_with_passing_hook, config)
stage_a_file() stage_a_file()
ret, printed = _do_run( ret, printed = _do_run(

View file

@ -1,7 +1,6 @@
from __future__ import absolute_import from __future__ import absolute_import
from __future__ import unicode_literals from __future__ import unicode_literals
import collections
import functools import functools
import io import io
import logging import logging
@ -120,19 +119,16 @@ def in_conflicting_submodule(tempdir_factory):
@pytest.fixture @pytest.fixture
def commit_msg_repo(tempdir_factory): def commit_msg_repo(tempdir_factory):
path = git_dir(tempdir_factory) path = git_dir(tempdir_factory)
config = collections.OrderedDict(( config = {
('repo', 'local'), 'repo': 'local',
( 'hooks': [{
'hooks', 'id': 'must-have-signoff',
[collections.OrderedDict(( 'name': 'Must have "Signed off by:"',
('id', 'must-have-signoff'), 'entry': 'grep -q "Signed off by:"',
('name', 'Must have "Signed off by:"'), 'language': 'system',
('entry', 'grep -q "Signed off by:"'), 'stages': ['commit-msg'],
('language', 'system'), }],
('stages', ['commit-msg']), }
))],
),
))
write_config(path, config) write_config(path, config)
with cwd(path): with cwd(path):
cmd_output('git', 'add', '.') cmd_output('git', 'add', '.')

View file

@ -1,7 +1,6 @@
from __future__ import absolute_import from __future__ import absolute_import
from __future__ import unicode_literals from __future__ import unicode_literals
import collections
import os.path import os.path
import re import re
import shutil import shutil
@ -341,21 +340,17 @@ def test_run_hook_with_curly_braced_arguments(tempdir_factory, store):
def _make_grep_repo(language, entry, store, args=()): def _make_grep_repo(language, entry, store, args=()):
config = collections.OrderedDict(( config = {
('repo', 'local'), 'repo': 'local',
( 'hooks': [{
'hooks', [ 'id': 'grep-hook',
collections.OrderedDict(( 'name': 'grep-hook',
('id', 'grep-hook'), 'language': language,
('name', 'grep-hook'), 'entry': entry,
('language', language), 'args': args,
('entry', entry), 'types': ['text'],
('args', args), }],
('types', ['text']), }
)),
],
),
))
return _get_hook(config, store, 'grep-hook') return _get_hook(config, store, 'grep-hook')