From 85a76617c13b43c0aaf12b70b9da7d927807de61 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sun, 15 Jun 2014 18:48:43 -0700 Subject: [PATCH] Refactor fixtures in tests. --- pre_commit/clientlib/validate_base.py | 1 + pre_commit/clientlib/validate_config.py | 2 + pre_commit/clientlib/validate_manifest.py | 2 + pre_commit/color.py | 2 + pre_commit/constants.py | 3 + pre_commit/five.py | 2 + pre_commit/git.py | 2 + pre_commit/jsonschema_extensions.py | 2 + pre_commit/languages/all.py | 2 + pre_commit/languages/helpers.py | 1 + pre_commit/languages/node.py | 2 + pre_commit/languages/python.py | 2 + pre_commit/languages/script.py | 2 + pre_commit/languages/system.py | 2 + pre_commit/logging_handler.py | 2 + pre_commit/manifest.py | 2 + pre_commit/output.py | 2 + pre_commit/prefixed_command_runner.py | 2 + pre_commit/repository.py | 2 + pre_commit/runner.py | 2 + pre_commit/staged_files_only.py | 2 + pre_commit/util.py | 2 + testing/auto_namedtuple.py | 2 + testing/fixtures.py | 18 +- testing/util.py | 2 + tests/clientlib/validate_base_test.py | 2 + tests/clientlib/validate_config_test.py | 2 + tests/clientlib/validate_manifest_test.py | 2 + tests/color_test.py | 2 + tests/commands/autoupdate_test.py | 20 +-- tests/commands/run_test.py | 15 ++ tests/conftest.py | 195 ++-------------------- tests/git_test.py | 1 + tests/jsonschema_extensions_test.py | 2 + tests/languages/all_test.py | 2 + tests/languages/ruby_test.py | 2 + tests/logging_handler_test.py | 2 + tests/manifest_test.py | 11 +- tests/output_test.py | 2 + tests/prefixed_command_runner_test.py | 6 +- tests/repository_test.py | 188 ++++++++++++--------- tests/runner_test.py | 6 +- tests/util_test.py | 2 + 43 files changed, 249 insertions(+), 278 deletions(-) diff --git a/pre_commit/clientlib/validate_base.py b/pre_commit/clientlib/validate_base.py index 27f363a9..dd3aaea0 100644 --- a/pre_commit/clientlib/validate_base.py +++ b/pre_commit/clientlib/validate_base.py @@ -1,4 +1,5 @@ from __future__ import print_function +from __future__ import unicode_literals import argparse import jsonschema diff --git a/pre_commit/clientlib/validate_config.py b/pre_commit/clientlib/validate_config.py index f18a7b73..bfe482b1 100644 --- a/pre_commit/clientlib/validate_config.py +++ b/pre_commit/clientlib/validate_config.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import sys from pre_commit.clientlib.validate_base import get_run_function diff --git a/pre_commit/clientlib/validate_manifest.py b/pre_commit/clientlib/validate_manifest.py index fed5a033..2a0f188f 100644 --- a/pre_commit/clientlib/validate_manifest.py +++ b/pre_commit/clientlib/validate_manifest.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import sys from pre_commit.clientlib.validate_base import get_run_function diff --git a/pre_commit/color.py b/pre_commit/color.py index eaf7d3c3..a787821c 100644 --- a/pre_commit/color.py +++ b/pre_commit/color.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import sys RED = '\033[41m' diff --git a/pre_commit/constants.py b/pre_commit/constants.py index 9983fdbc..b89c29f8 100644 --- a/pre_commit/constants.py +++ b/pre_commit/constants.py @@ -1,3 +1,6 @@ +from __future__ import unicode_literals + + CONFIG_FILE = '.pre-commit-config.yaml' MANIFEST_FILE = 'hooks.yaml' diff --git a/pre_commit/five.py b/pre_commit/five.py index 28db863f..ce7917d0 100644 --- a/pre_commit/five.py +++ b/pre_commit/five.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + """five: six, redux""" # pylint:disable=invalid-name PY2 = (str is bytes) diff --git a/pre_commit/git.py b/pre_commit/git.py index 382daa86..75e2662c 100644 --- a/pre_commit/git.py +++ b/pre_commit/git.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import functools import logging import os diff --git a/pre_commit/jsonschema_extensions.py b/pre_commit/jsonschema_extensions.py index d287bcaf..f7608135 100644 --- a/pre_commit/jsonschema_extensions.py +++ b/pre_commit/jsonschema_extensions.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import copy import jsonschema import jsonschema.validators diff --git a/pre_commit/languages/all.py b/pre_commit/languages/all.py index 50d83af6..9506fbea 100644 --- a/pre_commit/languages/all.py +++ b/pre_commit/languages/all.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + from pre_commit.languages import node from pre_commit.languages import python from pre_commit.languages import ruby diff --git a/pre_commit/languages/helpers.py b/pre_commit/languages/helpers.py index fab8e5ae..780d928f 100644 --- a/pre_commit/languages/helpers.py +++ b/pre_commit/languages/helpers.py @@ -1,3 +1,4 @@ +from __future__ import unicode_literals def run_hook(env, hook, file_args): diff --git a/pre_commit/languages/node.py b/pre_commit/languages/node.py index 0ddc3fa0..563ece5a 100644 --- a/pre_commit/languages/node.py +++ b/pre_commit/languages/node.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import contextlib from pre_commit.languages import helpers diff --git a/pre_commit/languages/python.py b/pre_commit/languages/python.py index 0ad39c0a..e7ac7787 100644 --- a/pre_commit/languages/python.py +++ b/pre_commit/languages/python.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import contextlib from pre_commit.languages import helpers diff --git a/pre_commit/languages/script.py b/pre_commit/languages/script.py index 411e48ad..860d4bf6 100644 --- a/pre_commit/languages/script.py +++ b/pre_commit/languages/script.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + ENVIRONMENT_DIR = None diff --git a/pre_commit/languages/system.py b/pre_commit/languages/system.py index 486b965c..a75c618a 100644 --- a/pre_commit/languages/system.py +++ b/pre_commit/languages/system.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import shlex diff --git a/pre_commit/logging_handler.py b/pre_commit/logging_handler.py index 8fed2df3..c331e396 100644 --- a/pre_commit/logging_handler.py +++ b/pre_commit/logging_handler.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import logging import sys diff --git a/pre_commit/manifest.py b/pre_commit/manifest.py index 3b2363e3..e3b25dd7 100644 --- a/pre_commit/manifest.py +++ b/pre_commit/manifest.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import os.path from asottile.cached_property import cached_property diff --git a/pre_commit/output.py b/pre_commit/output.py index e3078391..1de4b322 100644 --- a/pre_commit/output.py +++ b/pre_commit/output.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import subprocess from pre_commit import color diff --git a/pre_commit/prefixed_command_runner.py b/pre_commit/prefixed_command_runner.py index 45acb7f1..bcefe8e6 100644 --- a/pre_commit/prefixed_command_runner.py +++ b/pre_commit/prefixed_command_runner.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import os import os.path import subprocess diff --git a/pre_commit/repository.py b/pre_commit/repository.py index 5799efe6..08fc397f 100644 --- a/pre_commit/repository.py +++ b/pre_commit/repository.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + from asottile.cached_property import cached_property from asottile.ordereddict import OrderedDict diff --git a/pre_commit/runner.py b/pre_commit/runner.py index 815c3abd..1768a336 100644 --- a/pre_commit/runner.py +++ b/pre_commit/runner.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import os import os.path from asottile.cached_property import cached_property diff --git a/pre_commit/staged_files_only.py b/pre_commit/staged_files_only.py index 751a84b7..6b68a5ca 100644 --- a/pre_commit/staged_files_only.py +++ b/pre_commit/staged_files_only.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import contextlib import io import logging diff --git a/pre_commit/util.py b/pre_commit/util.py index 121e2808..4c4b37df 100644 --- a/pre_commit/util.py +++ b/pre_commit/util.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import contextlib import functools import os diff --git a/testing/auto_namedtuple.py b/testing/auto_namedtuple.py index 0841094e..02e08fef 100644 --- a/testing/auto_namedtuple.py +++ b/testing/auto_namedtuple.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import collections diff --git a/testing/fixtures.py b/testing/fixtures.py index 74ad6e38..2fa5f535 100644 --- a/testing/fixtures.py +++ b/testing/fixtures.py @@ -1,8 +1,10 @@ from __future__ import absolute_import from __future__ import unicode_literals +import io import os.path from asottile.ordereddict import OrderedDict +from asottile.yaml import ordered_dump from plumbum import local import pre_commit.constants as C @@ -25,7 +27,7 @@ def git_dir(tmpdir_factory): return path -def make_hooks_repo(tmpdir_factory, repo_source): +def make_repo(tmpdir_factory, repo_source): path = git_dir(tmpdir_factory) copy_tree_to_path(get_resource_path(repo_source), path) with local.cwd(path): @@ -51,3 +53,17 @@ def make_config_from_repo(repo_path, sha=None, hooks=None, check=True): return wrapped_config[0] else: return config + + +def write_config(directory, config): + assert type(config) is OrderedDict + with io.open(os.path.join(directory, C.CONFIG_FILE), 'w') as config_file: + config_file.write(ordered_dump([config], **C.YAML_DUMP_KWARGS)) + + +def make_consuming_repo(tmpdir_factory, repo_source): + path = make_repo(tmpdir_factory, repo_source) + config = make_config_from_repo(path) + git_path = git_dir(tmpdir_factory) + write_config(git_path, config) + return git_path diff --git a/testing/util.py b/testing/util.py index 7b1bc7d1..be61169c 100644 --- a/testing/util.py +++ b/testing/util.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import jsonschema import os import os.path diff --git a/tests/clientlib/validate_base_test.py b/tests/clientlib/validate_base_test.py index 5e85b0ee..9b40e8c0 100644 --- a/tests/clientlib/validate_base_test.py +++ b/tests/clientlib/validate_base_test.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import pytest from asottile.ordereddict import OrderedDict from asottile.yaml import ordered_load diff --git a/tests/clientlib/validate_config_test.py b/tests/clientlib/validate_config_test.py index 93b8fd45..dc1ac883 100644 --- a/tests/clientlib/validate_config_test.py +++ b/tests/clientlib/validate_config_test.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import pytest from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA diff --git a/tests/clientlib/validate_manifest_test.py b/tests/clientlib/validate_manifest_test.py index 5dba2747..c45e5f8f 100644 --- a/tests/clientlib/validate_manifest_test.py +++ b/tests/clientlib/validate_manifest_test.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import pytest from pre_commit.clientlib.validate_manifest import additional_manifest_check diff --git a/tests/color_test.py b/tests/color_test.py index 0897793e..24a48378 100644 --- a/tests/color_test.py +++ b/tests/color_test.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import mock import pytest import sys diff --git a/tests/commands/autoupdate_test.py b/tests/commands/autoupdate_test.py index 3b021639..50ce031d 100644 --- a/tests/commands/autoupdate_test.py +++ b/tests/commands/autoupdate_test.py @@ -1,10 +1,8 @@ from __future__ import unicode_literals -import io import pytest import shutil from asottile.ordereddict import OrderedDict -from asottile.yaml import ordered_dump from plumbum import local import pre_commit.constants as C @@ -14,14 +12,15 @@ from pre_commit.commands.autoupdate import RepositoryCannotBeUpdatedError 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.fixtures import make_repo +from testing.fixtures import write_config from testing.util import get_head_sha from testing.util import get_resource_path @pytest.yield_fixture def up_to_date_repo(tmpdir_factory): - yield make_hooks_repo(tmpdir_factory, 'python_hooks_repo') + yield make_repo(tmpdir_factory, 'python_hooks_repo') def test_up_to_date_repo(up_to_date_repo, runner_with_mocked_store): @@ -36,8 +35,7 @@ def test_autoupdate_up_to_date_repo( ): # 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)) + write_config('.', config) before = open(C.CONFIG_FILE).read() assert '^$' not in before @@ -50,7 +48,7 @@ def test_autoupdate_up_to_date_repo( @pytest.yield_fixture def out_of_date_repo(tmpdir_factory): - path = make_hooks_repo(tmpdir_factory, 'python_hooks_repo') + path = make_repo(tmpdir_factory, 'python_hooks_repo') original_sha = get_head_sha(path) # Make a commit @@ -79,8 +77,7 @@ def test_autoupdate_out_of_date_repo( 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)) + write_config('.', config) before = open(C.CONFIG_FILE).read() runner = Runner('.') @@ -95,7 +92,7 @@ def test_autoupdate_out_of_date_repo( @pytest.yield_fixture def hook_disappearing_repo(tmpdir_factory): - path = make_hooks_repo(tmpdir_factory, 'python_hooks_repo') + path = make_repo(tmpdir_factory, 'python_hooks_repo') original_sha = get_head_sha(path) with local.cwd(path): @@ -130,8 +127,7 @@ def test_autoupdate_hook_disappearing_repo( 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)) + write_config('.', config) before = open(C.CONFIG_FILE).read() runner = Runner('.') diff --git a/tests/commands/run_test.py b/tests/commands/run_test.py index 021f88b9..1e03587b 100644 --- a/tests/commands/run_test.py +++ b/tests/commands/run_test.py @@ -11,6 +11,21 @@ from pre_commit.commands.run import _has_unmerged_paths from pre_commit.commands.run import run from pre_commit.runner import Runner from testing.auto_namedtuple import auto_namedtuple +from testing.fixtures import make_consuming_repo + + +@pytest.yield_fixture +def repo_with_passing_hook(tmpdir_factory): + git_path = make_consuming_repo(tmpdir_factory, 'script_hooks_repo') + with local.cwd(git_path): + yield git_path + + +@pytest.yield_fixture +def repo_with_failing_hook(tmpdir_factory): + git_path = make_consuming_repo(tmpdir_factory, 'failing_hook_repo') + with local.cwd(git_path): + yield git_path def stage_a_file(): diff --git a/tests/conftest.py b/tests/conftest.py index ce1214d1..9cc5f03b 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,26 +1,19 @@ from __future__ import absolute_import from __future__ import unicode_literals +import io import mock import os import os.path import pytest -import time -import yaml from plumbum import local import pre_commit.constants as C from pre_commit import five -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 pre_commit.prefixed_command_runner import PrefixedCommandRunner from pre_commit.runner import Runner from pre_commit.store import Store -from testing.fixtures import git_dir -from testing.util import copy_tree_to_path -from testing.util import get_head_sha -from testing.util import get_resource_path +from testing.fixtures import make_consuming_repo git = local['git'] @@ -48,194 +41,36 @@ def in_tmpdir(tmpdir_factory): yield path -def add_and_commit(): - git('add', '.') - git('commit', '-m', 'random commit {0}'.format(time.time())) - - @pytest.yield_fixture -def dummy_git_repo(tmpdir_factory): - path = git_dir(tmpdir_factory) +def in_merge_conflict(tmpdir_factory): + path = make_consuming_repo(tmpdir_factory, 'script_hooks_repo') with local.cwd(path): - # This is needed otherwise there is no `HEAD` local['touch']('dummy') - add_and_commit() - yield path + git('add', 'dummy') + git('add', C.CONFIG_FILE) + git('commit', '-m', 'Add config.') - -def _make_repo(repo_path, repo_source): - copy_tree_to_path(get_resource_path(repo_source), repo_path) - add_and_commit() - return repo_path - - -@pytest.yield_fixture -def python_hooks_repo(dummy_git_repo): - yield _make_repo(dummy_git_repo, 'python_hooks_repo') - - -@pytest.yield_fixture -def python3_hooks_repo(dummy_git_repo): - yield _make_repo(dummy_git_repo, 'python3_hooks_repo') - - -@pytest.yield_fixture -def node_hooks_repo(dummy_git_repo): - yield _make_repo(dummy_git_repo, 'node_hooks_repo') - - -@pytest.yield_fixture -def node_0_11_8_hooks_repo(dummy_git_repo): - yield _make_repo(dummy_git_repo, 'node_0_11_8_hooks_repo') - - -@pytest.yield_fixture -def ruby_hooks_repo(dummy_git_repo): - yield _make_repo(dummy_git_repo, 'ruby_hooks_repo') - - -@pytest.yield_fixture -def ruby_1_9_3_hooks_repo(dummy_git_repo): - yield _make_repo(dummy_git_repo, 'ruby_1_9_3_hooks_repo') - - -@pytest.yield_fixture -def consumer_repo(dummy_git_repo): - yield _make_repo(dummy_git_repo, 'consumer_repo') - - -@pytest.yield_fixture -def prints_cwd_repo(dummy_git_repo): - yield _make_repo(dummy_git_repo, 'prints_cwd_repo') - - -@pytest.yield_fixture -def script_hooks_repo(dummy_git_repo): - yield _make_repo(dummy_git_repo, 'script_hooks_repo') - - -@pytest.yield_fixture -def failing_hook_repo(dummy_git_repo): - yield _make_repo(dummy_git_repo, 'failing_hook_repo') - - -@pytest.yield_fixture -def system_hook_with_spaces_repo(dummy_git_repo): - yield _make_repo(dummy_git_repo, 'system_hook_with_spaces_repo') - - -def _make_config(path, hook_id): - config = { - 'repo': path, - 'sha': get_head_sha(path), - 'hooks': [{'id': hook_id}], - } - config_wrapped = apply_defaults([config], CONFIG_JSON_SCHEMA) - validate_config_extra(config_wrapped) - return config_wrapped[0] - - -@pytest.yield_fixture -def config_for_node_hooks_repo(node_hooks_repo): - yield _make_config(node_hooks_repo, 'foo') - - -@pytest.yield_fixture -def config_for_node_0_11_8_hooks_repo(node_0_11_8_hooks_repo): - yield _make_config(node_0_11_8_hooks_repo, 'node-11-8-hook') - - -@pytest.yield_fixture -def config_for_ruby_hooks_repo(ruby_hooks_repo): - yield _make_config(ruby_hooks_repo, 'ruby_hook') - - -@pytest.yield_fixture -def config_for_ruby_1_9_3_hooks_repo(ruby_1_9_3_hooks_repo): - yield _make_config(ruby_1_9_3_hooks_repo, 'ruby_hook') - - -@pytest.yield_fixture -def config_for_python_hooks_repo(python_hooks_repo): - yield _make_config(python_hooks_repo, 'foo') - - -@pytest.yield_fixture -def config_for_python3_hooks_repo(python3_hooks_repo): - yield _make_config(python3_hooks_repo, 'python3-hook') - - -@pytest.yield_fixture -def config_for_prints_cwd_repo(prints_cwd_repo): - yield _make_config(prints_cwd_repo, 'prints_cwd') - - -@pytest.yield_fixture -def config_for_script_hooks_repo(script_hooks_repo): - yield _make_config(script_hooks_repo, 'bash_hook') - - -@pytest.yield_fixture -def config_for_system_hook_with_spaces(system_hook_with_spaces_repo): - yield _make_config( - system_hook_with_spaces_repo, 'system-hook-with-spaces', - ) - - -def _make_repo_from_configs(*configs): - with open(C.CONFIG_FILE, 'w') as config_file: - yaml.dump( - configs, - stream=config_file, - Dumper=yaml.SafeDumper, - **C.YAML_DUMP_KWARGS - ) - - -@pytest.yield_fixture -def repo_with_passing_hook(config_for_script_hooks_repo, tmpdir_factory): - path = git_dir(tmpdir_factory) - with local.cwd(path): - _make_repo_from_configs(config_for_script_hooks_repo) - yield path - - -@pytest.yield_fixture -def repo_with_failing_hook(failing_hook_repo, tmpdir_factory): - path = git_dir(tmpdir_factory) - with local.cwd(path): - _make_repo_from_configs( - _make_config(failing_hook_repo, 'failing_hook') - ) - yield path - - -@pytest.yield_fixture -def in_merge_conflict(repo_with_passing_hook): - local['touch']('dummy') - git('add', 'dummy') - git('add', C.CONFIG_FILE) - git('commit', '-m' 'add hooks file') - git('clone', '.', 'foo') - with local.cwd('foo'): + conflict_path = tmpdir_factory.get() + git('clone', path, conflict_path) + with local.cwd(conflict_path): git('checkout', 'origin/master', '-b', 'foo') - with open('conflict_file', 'w') as conflict_file: + with io.open('conflict_file', 'w') as conflict_file: conflict_file.write('herp\nderp\n') git('add', 'conflict_file') - with open('foo_only_file', 'w') as foo_only_file: + with io.open('foo_only_file', 'w') as foo_only_file: foo_only_file.write('foo') git('add', 'foo_only_file') git('commit', '-m', 'conflict_file') git('checkout', 'origin/master', '-b', 'bar') - with open('conflict_file', 'w') as conflict_file: + with io.open('conflict_file', 'w') as conflict_file: conflict_file.write('harp\nddrp\n') git('add', 'conflict_file') - with open('bar_only_file', 'w') as bar_only_file: + with io.open('bar_only_file', 'w') as bar_only_file: bar_only_file.write('bar') git('add', 'bar_only_file') git('commit', '-m', 'conflict_file') git('merge', 'foo', retcode=None) - yield os.path.join(repo_with_passing_hook, 'foo') + yield os.path.join(conflict_path) @pytest.yield_fixture(scope='session', autouse=True) diff --git a/tests/git_test.py b/tests/git_test.py index 64b0465e..9b22359d 100644 --- a/tests/git_test.py +++ b/tests/git_test.py @@ -1,4 +1,5 @@ from __future__ import absolute_import +from __future__ import unicode_literals import os.path import pytest diff --git a/tests/jsonschema_extensions_test.py b/tests/jsonschema_extensions_test.py index d5a3b4d2..65948564 100644 --- a/tests/jsonschema_extensions_test.py +++ b/tests/jsonschema_extensions_test.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import jsonschema.exceptions import pytest diff --git a/tests/languages/all_test.py b/tests/languages/all_test.py index aeba7cc1..a66162dd 100644 --- a/tests/languages/all_test.py +++ b/tests/languages/all_test.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import pytest from pre_commit.languages.all import all_languages diff --git a/tests/languages/ruby_test.py b/tests/languages/ruby_test.py index 80e7bb72..d55b36b0 100644 --- a/tests/languages/ruby_test.py +++ b/tests/languages/ruby_test.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import os.path from pre_commit.languages.ruby import _install_rbenv diff --git a/tests/logging_handler_test.py b/tests/logging_handler_test.py index 63d9d21d..2273de9b 100644 --- a/tests/logging_handler_test.py +++ b/tests/logging_handler_test.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import mock from pre_commit import color diff --git a/tests/manifest_test.py b/tests/manifest_test.py index e419ce7a..89bc5943 100644 --- a/tests/manifest_test.py +++ b/tests/manifest_test.py @@ -1,13 +1,18 @@ +from __future__ import absolute_import +from __future__ import unicode_literals + import pytest from pre_commit.manifest import Manifest +from testing.fixtures import make_repo from testing.util import get_head_sha @pytest.yield_fixture -def manifest(store, script_hooks_repo): - head_sha = get_head_sha(script_hooks_repo) - repo_path_getter = store.get_repo_path_getter(script_hooks_repo, head_sha) +def manifest(store, tmpdir_factory): + path = make_repo(tmpdir_factory, 'script_hooks_repo') + head_sha = get_head_sha(path) + repo_path_getter = store.get_repo_path_getter(path, head_sha) yield Manifest(repo_path_getter) diff --git a/tests/output_test.py b/tests/output_test.py index d3f44c36..3daad1f6 100644 --- a/tests/output_test.py +++ b/tests/output_test.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import pytest from pre_commit import color diff --git a/tests/prefixed_command_runner_test.py b/tests/prefixed_command_runner_test.py index 2e4a586f..0d5e410e 100644 --- a/tests/prefixed_command_runner_test.py +++ b/tests/prefixed_command_runner_test.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import os import mock import pytest @@ -9,7 +11,9 @@ from pre_commit.prefixed_command_runner import PrefixedCommandRunner def test_CalledProcessError_str(): - error = CalledProcessError(1, ['git', 'status'], 0, ('stdout', 'stderr')) + error = CalledProcessError( + 1, [str('git'), str('status')], 0, (str('stdout'), str('stderr')) + ) assert str(error) == ( "Command: ['git', 'status']\n" "Return code: 1\n" diff --git a/tests/repository_test.py b/tests/repository_test.py index 5150832d..5d130e88 100644 --- a/tests/repository_test.py +++ b/tests/repository_test.py @@ -1,3 +1,7 @@ +from __future__ import absolute_import +from __future__ import unicode_literals + +import mock import os.path import pytest from plumbum import local @@ -6,101 +10,115 @@ 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 pre_commit.repository import Repository +from testing.fixtures import git_dir +from testing.fixtures import make_config_from_repo +from testing.fixtures import make_repo from testing.util import skipif_slowtests_false @pytest.mark.integration -def test_install_python_repo_in_env(config_for_python_hooks_repo, store): - repo = Repository.create(config_for_python_hooks_repo, store) +def test_install_python_repo_in_env(tmpdir_factory, store): + path = make_repo(tmpdir_factory, 'python_hooks_repo') + config = make_config_from_repo(path) + repo = Repository.create(config, store) repo.install() assert os.path.exists(os.path.join(store.directory, repo.sha, 'py_env')) -@pytest.mark.integration -def test_run_a_python_hook(config_for_python_hooks_repo, store): - repo = Repository.create(config_for_python_hooks_repo, store) - ret = repo.run_hook('foo', ['/dev/null']) +def _test_hook_repo(tmpdir_factory, store, repo_path, hook_id, args, expected): + path = make_repo(tmpdir_factory, repo_path) + config = make_config_from_repo(path) + repo = Repository.create(config, store) + ret = repo.run_hook(hook_id, args) assert ret[0] == 0 - assert ret[1] == "['/dev/null']\nHello World\n" + assert ret[1] == expected @pytest.mark.integration -def test_run_versioned_hook(config_for_python3_hooks_repo, store): - repo = Repository.create(config_for_python3_hooks_repo, store) - ret = repo.run_hook('python3-hook', ['/dev/null']) - assert ret[0] == 0 - assert ret[1] == "3.3\n['/dev/null']\nHello World\n" +def test_python_hook(tmpdir_factory, store): + _test_hook_repo( + tmpdir_factory, store, 'python_hooks_repo', + 'foo', ['/dev/null'], "['/dev/null']\nHello World\n", + ) + + +@pytest.mark.integration +def test_versioned_python_hook(tmpdir_factory, store): + _test_hook_repo( + tmpdir_factory, store, 'python3_hooks_repo', + 'python3-hook', ['/dev/null'], "3.3\n['/dev/null']\nHello World\n", + ) @skipif_slowtests_false @pytest.mark.integration -def test_run_versioned_node_hook(config_for_node_0_11_8_hooks_repo, store): - repo = Repository.create(config_for_node_0_11_8_hooks_repo, store) - ret = repo.run_hook('node-11-8-hook', ['/dev/null']) - assert ret[0] == 0 - assert ret[1] == 'v0.11.8\nHello World\n' +def test_run_a_node_hook(tmpdir_factory, store): + _test_hook_repo( + tmpdir_factory, store, 'node_hooks_repo', + 'foo', [], 'Hello World\n', + ) -@pytest.mark.herpderp @skipif_slowtests_false @pytest.mark.integration -def test_run_versioned_ruby_hook(config_for_ruby_1_9_3_hooks_repo, store): - repo = Repository.create(config_for_ruby_1_9_3_hooks_repo, store) - ret = repo.run_hook('ruby_hook', []) - assert ret[0] == 0 - assert ret[1] == '1.9.3\n484\nHello world from a ruby hook\n' +def test_run_versioned_node_hook(tmpdir_factory, store): + _test_hook_repo( + tmpdir_factory, store, 'node_0_11_8_hooks_repo', + 'node-11-8-hook', ['/dev/null'], 'v0.11.8\nHello World\n', + ) + + +@skipif_slowtests_false +@pytest.mark.integration +def test_run_a_ruby_hook(tmpdir_factory, store): + _test_hook_repo( + tmpdir_factory, store, 'ruby_hooks_repo', + 'ruby_hook', [], 'Hello world from a ruby hook\n', + ) + + +@skipif_slowtests_false +@pytest.mark.integration +def test_run_versioned_ruby_hook(tmpdir_factory, store): + _test_hook_repo( + tmpdir_factory, store, 'ruby_1_9_3_hooks_repo', + 'ruby_hook', [], '1.9.3\n484\nHello world from a ruby hook\n', + ) @pytest.mark.integration -def test_lots_of_files(config_for_python_hooks_repo, store): - repo = Repository.create(config_for_python_hooks_repo, store) - ret = repo.run_hook('foo', ['/dev/null'] * 15000) - assert ret[0] == 0 +def test_system_hook_with_spaces(tmpdir_factory, store): + _test_hook_repo( + tmpdir_factory, store, 'system_hook_with_spaces_repo', + 'system-hook-with-spaces', [], 'Hello World\n', + ) @pytest.mark.integration -def test_cwd_of_hook(config_for_prints_cwd_repo, store): +def test_run_a_script_hook(tmpdir_factory, store): + _test_hook_repo( + tmpdir_factory, store, 'script_hooks_repo', + 'bash_hook', ['bar'], 'bar\nHello World\n', + ) + + +@pytest.mark.integration +def test_cwd_of_hook(tmpdir_factory, store): # Note: this doubles as a test for `system` hooks - repo = Repository.create(config_for_prints_cwd_repo, store) - ret = repo.run_hook('prints_cwd', []) - assert ret[0] == 0 - assert ret[1] == repo.repo_url + '\n' + path = git_dir(tmpdir_factory) + with local.cwd(path): + _test_hook_repo( + tmpdir_factory, store, 'prints_cwd_repo', + 'prints_cwd', [], path + '\n', + ) @pytest.mark.integration -def test_system_hook_with_spaces(config_for_system_hook_with_spaces, store): - repo = Repository.create(config_for_system_hook_with_spaces, store) - ret = repo.run_hook('system-hook-with-spaces', []) - assert ret[0] == 0 - assert ret[1] == 'Hello World\n' - - -@skipif_slowtests_false -@pytest.mark.integration -def test_run_a_node_hook(config_for_node_hooks_repo, store): - repo = Repository.create(config_for_node_hooks_repo, store) - ret = repo.run_hook('foo', []) - assert ret[0] == 0 - assert ret[1] == 'Hello World\n' - - -@pytest.mark.herpderp -@skipif_slowtests_false -@pytest.mark.integration -def test_run_a_ruby_hook(config_for_ruby_hooks_repo, store): - repo = Repository.create(config_for_ruby_hooks_repo, store) - ret = repo.run_hook('ruby_hook', []) - assert ret[0] == 0 - assert ret[1] == 'Hello world from a ruby hook\n' - - -@pytest.mark.integration -def test_run_a_script_hook(config_for_script_hooks_repo, store): - repo = Repository.create(config_for_script_hooks_repo, store) - ret = repo.run_hook('bash_hook', ['bar']) - - assert ret[0] == 0 - assert ret[1] == 'bar\nHello World\n' +def test_lots_of_files(tmpdir_factory, store): + _test_hook_repo( + tmpdir_factory, store, 'script_hooks_repo', + 'bash_hook', ['/dev/null'] * 15000, mock.ANY, + ) @pytest.fixture @@ -129,37 +147,49 @@ def test_sha(mock_repo_config): @pytest.mark.integration -def test_languages(config_for_python_hooks_repo, store): - repo = Repository.create(config_for_python_hooks_repo, store) +def test_languages(tmpdir_factory, store): + path = make_repo(tmpdir_factory, 'python_hooks_repo') + config = make_config_from_repo(path) + repo = Repository.create(config, store) assert repo.languages == set([('python', 'default')]) -def test_reinstall(config_for_python_hooks_repo, store): - repo = Repository.create(config_for_python_hooks_repo, store) +def test_reinstall(tmpdir_factory, store): + path = make_repo(tmpdir_factory, 'python_hooks_repo') + config = make_config_from_repo(path) + repo = Repository.create(config, store) repo.require_installed() # Reinstall with same repo should not trigger another install # TODO: how to assert this? repo.require_installed() # Reinstall on another run should not trigger another install # TODO: how to assert this? - repo = Repository.create(config_for_python_hooks_repo, store) + repo = Repository.create(config, store) repo.require_installed() @pytest.mark.integration -def test_really_long_file_paths(config_for_python_hooks_repo, store): - path = 'really_long' * 10 - local['git']('init', path) - with local.cwd(path): - repo = Repository.create(config_for_python_hooks_repo, store) +def test_really_long_file_paths(tmpdir_factory, store): + base_path = tmpdir_factory.get() + really_long_path = os.path.join(base_path, 'really_long' * 10) + local['git']('init', really_long_path) + + path = make_repo(tmpdir_factory, 'python_hooks_repo') + config = make_config_from_repo(path) + + with local.cwd(really_long_path): + repo = Repository.create(config, store) repo.require_installed() @pytest.mark.integration -def test_config_overrides_repo_specifics(config_for_script_hooks_repo, store): - repo = Repository.create(config_for_script_hooks_repo, store) +def test_config_overrides_repo_specifics(tmpdir_factory, store): + path = make_repo(tmpdir_factory, 'script_hooks_repo') + config = make_config_from_repo(path) + + repo = Repository.create(config, store) assert repo.hooks['bash_hook']['files'] == '' # Set the file regex to something else - config_for_script_hooks_repo['hooks'][0]['files'] = '\\.sh$' - repo = Repository.create(config_for_script_hooks_repo, store) + config['hooks'][0]['files'] = '\\.sh$' + repo = Repository.create(config, store) assert repo.hooks['bash_hook']['files'] == '\\.sh$' diff --git a/tests/runner_test.py b/tests/runner_test.py index 7cc7f6b4..76401650 100644 --- a/tests/runner_test.py +++ b/tests/runner_test.py @@ -8,6 +8,7 @@ from plumbum import local import pre_commit.constants as C from pre_commit.runner import Runner from testing.fixtures import git_dir +from testing.fixtures import make_repo def test_init_has_no_side_effects(tmpdir): @@ -45,9 +46,10 @@ def test_config_file_path(): assert runner.config_file_path == expected_path -def test_repositories(consumer_repo, mock_out_store_directory): +def test_repositories(tmpdir_factory, mock_out_store_directory): # TODO: make this not have external deps - runner = Runner(consumer_repo) + path = make_repo(tmpdir_factory, 'consumer_repo') + runner = Runner(path) assert len(runner.repositories) == 2 assert [repo.repo_url for repo in runner.repositories] == [ 'git@github.com:pre-commit/pre-commit-hooks', diff --git a/tests/util_test.py b/tests/util_test.py index 22aea85c..0eb2cf7e 100644 --- a/tests/util_test.py +++ b/tests/util_test.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import mock import pytest import os