From e4cf5f321b9d084e61b6165b3951264b22e899e2 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Mon, 31 Dec 2018 11:15:22 -0800 Subject: [PATCH] just use normal dicts in tests --- pre_commit/clientlib.py | 3 +- testing/fixtures.py | 37 +++---- tests/clientlib_test.py | 16 +-- tests/commands/autoupdate_test.py | 5 +- tests/commands/run_test.py | 159 +++++++++++++----------------- tests/conftest.py | 24 ++--- tests/repository_test.py | 27 +++-- 7 files changed, 114 insertions(+), 157 deletions(-) diff --git a/pre_commit/clientlib.py b/pre_commit/clientlib.py index 44599ea6..07423c34 100644 --- a/pre_commit/clientlib.py +++ b/pre_commit/clientlib.py @@ -2,7 +2,6 @@ from __future__ import absolute_import from __future__ import unicode_literals import argparse -import collections import functools import cfgv @@ -170,7 +169,7 @@ 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)]) + return {'repos': data} else: return data diff --git a/testing/fixtures.py b/testing/fixtures.py index 91c095a8..74fe517b 100644 --- a/testing/fixtures.py +++ b/testing/fixtures.py @@ -5,7 +5,6 @@ import contextlib import io import os.path import shutil -from collections import OrderedDict from aspy.yaml import ordered_dump from aspy.yaml import ordered_load @@ -83,30 +82,24 @@ def modify_config(path='.', commit=True): def config_with_local_hooks(): - return OrderedDict(( - ('repo', 'local'), - ( - 'hooks', [OrderedDict(( - ('id', 'do_not_commit'), - ('name', 'Block if "DO NOT COMMIT" is found'), - ('entry', 'DO NOT COMMIT'), - ('language', 'pygrep'), - ('files', '^(.*)$'), - ))], - ), - )) + return { + 'repo': 'local', + 'hooks': [{ + 'id': 'do_not_commit', + 'name': 'Block if "DO NOT COMMIT" is found', + 'entry': 'DO NOT COMMIT', + 'language': 'pygrep', + }], + } def make_config_from_repo(repo_path, rev=None, hooks=None, check=True): manifest = load_manifest(os.path.join(repo_path, C.MANIFEST_FILE)) - config = OrderedDict(( - ('repo', 'file://{}'.format(repo_path)), - ('rev', rev or git.head_rev(repo_path)), - ( - 'hooks', - hooks or [OrderedDict((('id', hook['id']),)) for hook in manifest], - ), - )) + config = { + 'repo': 'file://{}'.format(repo_path), + 'rev': rev or git.head_rev(repo_path), + 'hooks': hooks or [{'id': hook['id']} for hook in manifest], + } if check: 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): if type(config) is not list and 'repos' not in config: - assert type(config) is OrderedDict + assert isinstance(config, dict), config config = {'repos': [config]} with io.open(os.path.join(directory, config_file), 'w') as outfile: outfile.write(ordered_dump(config, **C.YAML_DUMP_KWARGS)) diff --git a/tests/clientlib_test.py b/tests/clientlib_test.py index fcd34dc0..c9908a25 100644 --- a/tests/clientlib_test.py +++ b/tests/clientlib_test.py @@ -11,6 +11,7 @@ from pre_commit.clientlib import MANIFEST_SCHEMA from pre_commit.clientlib import MigrateShaToRev from pre_commit.clientlib import validate_config_main from pre_commit.clientlib import validate_manifest_main +from testing.fixtures import config_with_local_hooks from testing.util import get_resource_path @@ -92,18 +93,9 @@ def test_config_valid(config_obj, expected): assert ret is expected -def test_config_with_local_hooks_definition_fails(): - config_obj = {'repos': [{ - 'repo': 'local', - 'rev': 'foo', - 'hooks': [{ - 'id': 'do_not_commit', - 'name': 'Block if "DO NOT COMMIT" is found', - 'entry': 'DO NOT COMMIT', - 'language': 'pcre', - 'files': '^(.*)$', - }], - }]} +def test_local_hooks_with_rev_fails(): + config_obj = {'repos': [config_with_local_hooks()]} + config_obj['repos'][0]['rev'] = 'foo' with pytest.raises(cfgv.ValidationError): cfgv.validate(config_obj, CONFIG_SCHEMA) diff --git a/tests/commands/autoupdate_test.py b/tests/commands/autoupdate_test.py index e4d3cc88..08926172 100644 --- a/tests/commands/autoupdate_test.py +++ b/tests/commands/autoupdate_test.py @@ -3,7 +3,6 @@ from __future__ import unicode_literals import os.path import pipes import shutil -from collections import OrderedDict import pytest @@ -290,7 +289,7 @@ def test_hook_disppearing_repo_raises(hook_disappearing_repo, store): config = make_config_from_repo( hook_disappearing_repo.path, rev=hook_disappearing_repo.original_rev, - hooks=[OrderedDict((('id', 'foo'),))], + hooks=[{'id': 'foo'}], ) with pytest.raises(RepositoryCannotBeUpdatedError): _update_repo(config, store, tags_only=False) @@ -302,7 +301,7 @@ def test_autoupdate_hook_disappearing_repo( config = make_config_from_repo( hook_disappearing_repo.path, rev=hook_disappearing_repo.original_rev, - hooks=[OrderedDict((('id', 'foo'),))], + hooks=[{'id': 'foo'}], check=False, ) write_config('.', config) diff --git a/tests/commands/run_test.py b/tests/commands/run_test.py index 0345ea7d..84ab1b2c 100644 --- a/tests/commands/run_test.py +++ b/tests/commands/run_test.py @@ -5,7 +5,6 @@ import io import os.path import subprocess import sys -from collections import OrderedDict import pytest @@ -521,21 +520,19 @@ def test_lots_of_files(store, tempdir_factory): def test_stages(cap_out, store, repo_with_passing_hook): - config = OrderedDict(( - ('repo', 'local'), - ( - 'hooks', tuple( - { - 'id': 'do-not-commit-{}'.format(i), - 'name': 'hook {}'.format(i), - 'entry': 'DO NOT COMMIT', - 'language': 'pygrep', - 'stages': [stage], - } - for i, stage in enumerate(('commit', 'push', 'manual'), 1) - ), - ), - )) + config = { + 'repo': 'local', + 'hooks': [ + { + 'id': 'do-not-commit-{}'.format(i), + 'name': 'hook {}'.format(i), + 'entry': 'DO NOT COMMIT', + 'language': 'pygrep', + 'stages': [stage], + } + for i, stage in enumerate(('commit', 'push', 'manual'), 1) + ], + } add_config_to_repo(repo_with_passing_hook, config) 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): - config = OrderedDict(( - ('repo', 'local'), - ( - 'hooks', ( - OrderedDict(( - ('id', 'flake8'), - ('name', 'flake8'), - ('entry', "'{}' -m flake8".format(sys.executable)), - ('language', 'system'), - ('files', r'\.py$'), - )), OrderedDict(( - ('id', 'do_not_commit'), - ('name', 'Block if "DO NOT COMMIT" is found'), - ('entry', 'DO NOT COMMIT'), - ('language', 'pygrep'), - ('files', '^(.*)$'), - )), - ), - ), - )) + config = { + 'repo': 'local', + 'hooks': [ + { + 'id': 'flake8', + 'name': 'flake8', + 'entry': "'{}' -m flake8".format(sys.executable), + 'language': 'system', + 'files': r'\.py$', + }, + { + 'id': 'do_not_commit', + 'name': 'Block if "DO NOT COMMIT" is found', + 'entry': 'DO NOT COMMIT', + 'language': 'pygrep', + }, + ], + } add_config_to_repo(repo_with_passing_hook, config) 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): - config = OrderedDict(( - ('repo', 'local'), - ( - 'hooks', [OrderedDict(( - ('id', 'no-todo'), - ('name', 'No TODO'), - ('entry', 'sh -c "! grep -iI todo $@" --'), - ('language', 'system'), - ('files', ''), - ))], - ), - )) + config = { + 'repo': 'local', + 'hooks': [{ + 'id': 'no-todo', + 'name': 'No TODO', + 'entry': 'sh -c "! grep -iI todo $@" --', + 'language': 'system', + }], + } add_config_to_repo(repo_with_passing_hook, config) 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): - config = OrderedDict(( - ('repo', 'local'), - ( - 'hooks', [OrderedDict(( - ('id', 'pcre-hook'), - ('name', 'pcre-hook'), - ('language', 'pcre'), - ('entry', '.'), - ))], - ), - )) + config = { + 'repo': 'local', + 'hooks': [{ + 'id': 'pcre-hook', + 'name': 'pcre-hook', + 'language': 'pcre', + 'entry': '.', + }], + } add_config_to_repo(repo_with_passing_hook, config) _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): - config = OrderedDict(( - ('repo', 'meta'), - ( - 'hooks', ( - OrderedDict(( - ('id', 'check-useless-excludes'), - )), - ), - ), - )) + config = { + 'repo': 'meta', + 'hooks': [{'id': 'check-useless-excludes'}], + } add_config_to_repo(repo_with_passing_hook, config) _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): - config = OrderedDict(( - ('repo', 'local'), - ( - 'hooks', ( - OrderedDict(( - ('id', 'flake8'), - ('name', 'flake8'), - ('entry', "'{}' -m flake8".format(sys.executable)), - ('language', 'system'), - ('stages', ['commit']), - )), OrderedDict(( - ('id', 'do_not_commit'), - ('name', 'Block if "DO NOT COMMIT" is found'), - ('entry', 'DO NOT COMMIT'), - ('language', 'pygrep'), - )), - ), - ), - )) + config = { + 'repo': 'local', + 'hooks': [ + { + 'id': 'flake8', + 'name': 'flake8', + 'entry': "'{}' -m flake8".format(sys.executable), + 'language': 'system', + 'stages': ['commit'], + }, + { + 'id': 'do_not_commit', + 'name': 'Block if "DO NOT COMMIT" is found', + 'entry': 'DO NOT COMMIT', + 'language': 'pygrep', + }, + ], + } add_config_to_repo(repo_with_passing_hook, config) stage_a_file() ret, printed = _do_run( diff --git a/tests/conftest.py b/tests/conftest.py index f72af094..7479a7b7 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,7 +1,6 @@ from __future__ import absolute_import from __future__ import unicode_literals -import collections import functools import io import logging @@ -120,19 +119,16 @@ def in_conflicting_submodule(tempdir_factory): @pytest.fixture def commit_msg_repo(tempdir_factory): path = git_dir(tempdir_factory) - config = collections.OrderedDict(( - ('repo', 'local'), - ( - 'hooks', - [collections.OrderedDict(( - ('id', 'must-have-signoff'), - ('name', 'Must have "Signed off by:"'), - ('entry', 'grep -q "Signed off by:"'), - ('language', 'system'), - ('stages', ['commit-msg']), - ))], - ), - )) + config = { + 'repo': 'local', + 'hooks': [{ + 'id': 'must-have-signoff', + 'name': 'Must have "Signed off by:"', + 'entry': 'grep -q "Signed off by:"', + 'language': 'system', + 'stages': ['commit-msg'], + }], + } write_config(path, config) with cwd(path): cmd_output('git', 'add', '.') diff --git a/tests/repository_test.py b/tests/repository_test.py index 2092802b..0286423b 100644 --- a/tests/repository_test.py +++ b/tests/repository_test.py @@ -1,7 +1,6 @@ from __future__ import absolute_import from __future__ import unicode_literals -import collections import os.path import re 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=()): - config = collections.OrderedDict(( - ('repo', 'local'), - ( - 'hooks', [ - collections.OrderedDict(( - ('id', 'grep-hook'), - ('name', 'grep-hook'), - ('language', language), - ('entry', entry), - ('args', args), - ('types', ['text']), - )), - ], - ), - )) + config = { + 'repo': 'local', + 'hooks': [{ + 'id': 'grep-hook', + 'name': 'grep-hook', + 'language': language, + 'entry': entry, + 'args': args, + 'types': ['text'], + }], + } return _get_hook(config, store, 'grep-hook')