From 3baefd57e20c820d1c64ab1fb7bfa8589f03a862 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sun, 15 Jun 2014 17:00:35 -0700 Subject: [PATCH] Convert autoupdate_test to use new fixture functions. --- pre_commit/constants.py | 2 + testing/fixtures.py | 39 ++++++++ tests/commands/autoupdate_test.py | 148 +++++++++++++----------------- 3 files changed, 106 insertions(+), 83 deletions(-) diff --git a/pre_commit/constants.py b/pre_commit/constants.py index 4df764bf..9983fdbc 100644 --- a/pre_commit/constants.py +++ b/pre_commit/constants.py @@ -4,5 +4,7 @@ MANIFEST_FILE = 'hooks.yaml' YAML_DUMP_KWARGS = { 'default_flow_style': False, + # Use unicode + 'encoding': None, 'indent': 4, } diff --git a/testing/fixtures.py b/testing/fixtures.py index f0dfcb85..74ad6e38 100644 --- a/testing/fixtures.py +++ b/testing/fixtures.py @@ -1,8 +1,19 @@ from __future__ import absolute_import from __future__ import unicode_literals +import os.path +from asottile.ordereddict import OrderedDict 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'] @@ -12,3 +23,31 @@ def git_dir(tmpdir_factory): with local.cwd(path): git('init') 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 diff --git a/tests/commands/autoupdate_test.py b/tests/commands/autoupdate_test.py index 03da32a3..3b021639 100644 --- a/tests/commands/autoupdate_test.py +++ b/tests/commands/autoupdate_test.py @@ -1,7 +1,6 @@ from __future__ import unicode_literals -import os -import os.path +import io import pytest import shutil from asottile.ordereddict import OrderedDict @@ -9,56 +8,40 @@ from asottile.yaml import ordered_dump from plumbum import local 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 autoupdate 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 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_resource_path @pytest.yield_fixture -def up_to_date_repo(python_hooks_repo): - config = OrderedDict(( - ('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 up_to_date_repo(tmpdir_factory): + yield make_hooks_repo(tmpdir_factory, 'python_hooks_repo') def test_up_to_date_repo(up_to_date_repo, runner_with_mocked_store): - input_sha = up_to_date_repo.repo_config['sha'] - ret = _update_repository( - up_to_date_repo.repo_config, runner_with_mocked_store, - ) + config = make_config_from_repo(up_to_date_repo) + input_sha = config['sha'] + ret = _update_repository(config, runner_with_mocked_store) 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() assert '^$' not in before - runner = Runner(up_to_date_repo.python_hooks_repo) + runner = Runner('.') ret = autoupdate(runner) after = open(C.CONFIG_FILE).read() 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 -def out_of_date_repo(python_hooks_repo): - config = OrderedDict(( - ('repo', python_hooks_repo), - ('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) +def out_of_date_repo(tmpdir_factory): + path = make_hooks_repo(tmpdir_factory, 'python_hooks_repo') + original_sha = get_head_sha(path) - with open(os.path.join(python_hooks_repo, C.CONFIG_FILE), 'w') as file_obj: - file_obj.write( - ordered_dump([config], **C.YAML_DUMP_KWARGS) - ) + # Make a commit + with local.cwd(path): + local['git']['commit', '--allow-empty', '-m', 'foo']() + head_sha = get_head_sha(path) yield auto_namedtuple( - repo_config=config, - head_sha=head_sha, - python_hooks_repo=python_hooks_repo, + path=path, original_sha=original_sha, head_sha=head_sha, ) def test_out_of_date_repo(out_of_date_repo, runner_with_mocked_store): - ret = _update_repository( - out_of_date_repo.repo_config, runner_with_mocked_store, + config = make_config_from_repo( + 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 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() - runner = Runner(out_of_date_repo.python_hooks_repo) + runner = Runner('.') ret = autoupdate(runner) after = open(C.CONFIG_FILE).read() assert ret == 0 @@ -112,47 +94,47 @@ def test_autoupdate_out_of_date_repo( @pytest.yield_fixture -def hook_disappearing_repo(python_hooks_repo): - config = OrderedDict(( - ('repo', python_hooks_repo), - ('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']() +def hook_disappearing_repo(tmpdir_factory): + path = make_hooks_repo(tmpdir_factory, 'python_hooks_repo') + original_sha = get_head_sha(path) - with open(os.path.join(python_hooks_repo, C.CONFIG_FILE), 'w') as file_obj: - file_obj.write( - ordered_dump([config], **C.YAML_DUMP_KWARGS) + with local.cwd(path): + shutil.copy( + get_resource_path('manifest_without_foo.yaml'), + C.MANIFEST_FILE, ) + local['git']('add', '.') + local['git']('commit', '-m', 'Remove foo') - yield auto_namedtuple( - repo_config=config, - python_hooks_repo=python_hooks_repo, - ) + yield auto_namedtuple(path=path, original_sha=original_sha) def test_hook_disppearing_repo_raises( 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): - _update_repository( - hook_disappearing_repo.repo_config, runner_with_mocked_store, - ) + _update_repository(config, runner_with_mocked_store) 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() - runner = Runner(hook_disappearing_repo.python_hooks_repo) + runner = Runner('.') ret = autoupdate(runner) after = open(C.CONFIG_FILE).read() assert ret == 1