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/commands/autoupdate.py b/pre_commit/commands/autoupdate.py index 545a3d0b..5c6d1fe9 100644 --- a/pre_commit/commands/autoupdate.py +++ b/pre_commit/commands/autoupdate.py @@ -30,8 +30,8 @@ def _update_repository(repo_config, runner): repo = Repository.create(repo_config, runner.store) with local.cwd(repo.repo_path_getter.repo_path): - local['git']['fetch']() - head_sha = local['git']['rev-parse', 'origin/master']().strip() + local['git']('fetch') + head_sha = local['git']('rev-parse', 'origin/master').strip() # Don't bother trying to update if our sha is the same if head_sha == repo_config['sha']: diff --git a/pre_commit/constants.py b/pre_commit/constants.py index 4df764bf..b89c29f8 100644 --- a/pre_commit/constants.py +++ b/pre_commit/constants.py @@ -1,8 +1,13 @@ +from __future__ import unicode_literals + + CONFIG_FILE = '.pre-commit-config.yaml' MANIFEST_FILE = 'hooks.yaml' YAML_DUMP_KWARGS = { 'default_flow_style': False, + # Use unicode + 'encoding': None, 'indent': 4, } 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 bfaabd98..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 @@ -49,21 +51,21 @@ def get_conflicted_files(): # This will get the rest of the changes made after the merge. # If they resolved the merge conflict by choosing a mesh of both sides # this will also include the conflicted files - tree_hash = local['git']['write-tree']().strip() - merge_diff_filenames = local['git'][ + tree_hash = local['git']('write-tree').strip() + merge_diff_filenames = local['git']( 'diff', '-m', tree_hash, 'HEAD', 'MERGE_HEAD', '--name-only', - ]().splitlines() + ).splitlines() return set(merge_conflict_filenames) | set(merge_diff_filenames) @memoize_by_cwd def get_staged_files(): - return local['git']['diff', '--staged', '--name-only']().splitlines() + return local['git']('diff', '--staged', '--name-only').splitlines() @memoize_by_cwd def get_all_files(): - return local['git']['ls-files']().splitlines() + return local['git']('ls-files').splitlines() def get_files_matching(all_file_list_strategy): 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 new file mode 100644 index 00000000..2fa5f535 --- /dev/null +++ b/testing/fixtures.py @@ -0,0 +1,69 @@ +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 +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'] + + +def git_dir(tmpdir_factory): + path = tmpdir_factory.get() + with local.cwd(path): + git('init') + return path + + +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): + 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 + + +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 009b9eb5..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 @@ -33,7 +35,7 @@ def copy_tree_to_path(src_dir, dest_dir): def get_head_sha(dir): with local.cwd(dir): - return local['git']['rev-parse', 'HEAD']().strip() + return local['git']('rev-parse', 'HEAD').strip() def is_valid_according_to_schema(obj, schema): 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 03da32a3..50ce031d 100644 --- a/tests/commands/autoupdate_test.py +++ b/tests/commands/autoupdate_test.py @@ -1,64 +1,45 @@ from __future__ import unicode_literals -import os -import os.path 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 -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_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(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_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) + write_config('.', config) + 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 +47,40 @@ 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_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, + ) + write_config('.', config) + 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 +91,46 @@ 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_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, + ) + write_config('.', config) + 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 diff --git a/tests/commands/install_test.py b/tests/commands/install_test.py index dab7740e..53965e6e 100644 --- a/tests/commands/install_test.py +++ b/tests/commands/install_test.py @@ -1,3 +1,4 @@ +from __future__ import absolute_import from __future__ import unicode_literals import io @@ -8,10 +9,12 @@ import stat from pre_commit.commands.install import install from pre_commit.runner import Runner +from testing.fixtures import git_dir -def test_install_pre_commit(empty_git_dir): - runner = Runner(empty_git_dir) +def test_install_pre_commit(tmpdir_factory): + path = git_dir(tmpdir_factory) + runner = Runner(path) ret = install(runner) assert ret == 0 assert os.path.exists(runner.pre_commit_path) diff --git a/tests/commands/run_test.py b/tests/commands/run_test.py index 5813ec03..1e03587b 100644 --- a/tests/commands/run_test.py +++ b/tests/commands/run_test.py @@ -11,11 +11,26 @@ 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(): - local['touch']['foo.py']() - local['git']['add', 'foo.py']() + local['touch']('foo.py') + local['git']('add', 'foo.py') def get_write_mock_output(write_mock): @@ -75,11 +90,9 @@ def test_run_all_hooks_failing( ({'verbose': True}, ('foo.py\nHello World',), 0, True), ({'hook': 'bash_hook'}, ('Bash hook', 'Passed'), 0, True), ({'hook': 'nope'}, ('No hook with id `nope`',), 1, True), - # All the files in the repo. - # This seems kind of weird but it is beacuse py.test reuses fixtures ( {'all_files': True, 'verbose': True}, - ('hooks.yaml', 'bin/hook.sh', 'foo.py', 'dummy'), + ('foo.py'), 0, True, ), @@ -153,7 +166,7 @@ def test_merge_conflict_modified(in_merge_conflict, mock_out_store_directory): def test_merge_conflict_resolved(in_merge_conflict, mock_out_store_directory): - local['git']['add', '.']() + local['git']('add', '.') ret, printed = _do_run(in_merge_conflict, _get_opts()) for msg in ('Checking merge-conflict files only.', 'Bash hook', 'Passed'): assert msg in printed diff --git a/tests/commands/uninstall_test.py b/tests/commands/uninstall_test.py index fcecf9d0..9d5a38ed 100644 --- a/tests/commands/uninstall_test.py +++ b/tests/commands/uninstall_test.py @@ -1,3 +1,4 @@ +from __future__ import absolute_import from __future__ import unicode_literals import os.path @@ -5,16 +6,19 @@ import os.path from pre_commit.runner import Runner from pre_commit.commands.install import install from pre_commit.commands.uninstall import uninstall +from testing.fixtures import git_dir -def test_uninstall_pre_commit_does_not_blow_up_when_not_there(empty_git_dir): - runner = Runner(empty_git_dir) +def test_uninstall_does_not_blow_up_when_not_there(tmpdir_factory): + path = git_dir(tmpdir_factory) + runner = Runner(path) ret = uninstall(runner) assert ret == 0 -def test_uninstall(empty_git_dir): - runner = Runner(empty_git_dir) +def test_uninstall(tmpdir_factory): + path = git_dir(tmpdir_factory) + runner = Runner(path) assert not os.path.exists(runner.pre_commit_path) install(runner) assert os.path.exists(runner.pre_commit_path) diff --git a/tests/conftest.py b/tests/conftest.py index 4f0a381d..9cc5f03b 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,24 +1,22 @@ 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.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'] @pytest.yield_fixture @@ -44,189 +42,35 @@ def in_tmpdir(tmpdir_factory): @pytest.yield_fixture -def empty_git_dir(in_tmpdir): - local['git']['init']() - yield in_tmpdir +def in_merge_conflict(tmpdir_factory): + path = make_consuming_repo(tmpdir_factory, 'script_hooks_repo') + with local.cwd(path): + local['touch']('dummy') + git('add', 'dummy') + git('add', C.CONFIG_FILE) + git('commit', '-m', 'Add config.') - -def add_and_commit(): - local['git']['add', '.']() - local['git']['commit', '-m', 'random commit {0}'.format(time.time())]() - - -@pytest.yield_fixture -def dummy_git_repo(empty_git_dir): - # This is needed otherwise there is no `HEAD` - local['touch']['dummy']() - add_and_commit() - yield empty_git_dir - - -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, empty_git_dir): - _make_repo_from_configs(config_for_script_hooks_repo) - yield empty_git_dir - - -@pytest.yield_fixture -def repo_with_failing_hook(failing_hook_repo, empty_git_dir): - _make_repo_from_configs(_make_config(failing_hook_repo, 'failing_hook')) - yield empty_git_dir - - -@pytest.yield_fixture -def in_merge_conflict(repo_with_passing_hook): - local['git']['add', C.CONFIG_FILE]() - local['git']['commit', '-m' 'add hooks file']() - local['git']['clone', '.', 'foo']() - with local.cwd('foo'): - local['git']['checkout', 'origin/master', '-b', 'foo']() - with open('conflict_file', 'w') as conflict_file: + conflict_path = tmpdir_factory.get() + git('clone', path, conflict_path) + with local.cwd(conflict_path): + git('checkout', 'origin/master', '-b', 'foo') + with io.open('conflict_file', 'w') as conflict_file: conflict_file.write('herp\nderp\n') - local['git']['add', 'conflict_file']() - with open('foo_only_file', 'w') as foo_only_file: + git('add', 'conflict_file') + with io.open('foo_only_file', 'w') as foo_only_file: foo_only_file.write('foo') - local['git']['add', 'foo_only_file']() - local['git']['commit', '-m', 'conflict_file']() - local['git']['checkout', 'origin/master', '-b', 'bar']() - with open('conflict_file', 'w') as conflict_file: + git('add', 'foo_only_file') + git('commit', '-m', 'conflict_file') + git('checkout', 'origin/master', '-b', 'bar') + with io.open('conflict_file', 'w') as conflict_file: conflict_file.write('harp\nddrp\n') - local['git']['add', 'conflict_file']() - with open('bar_only_file', 'w') as bar_only_file: + git('add', 'conflict_file') + with io.open('bar_only_file', 'w') as bar_only_file: bar_only_file.write('bar') - local['git']['add', 'bar_only_file']() - local['git']['commit', '-m', 'conflict_file']() - local['git']['merge', 'foo'](retcode=None) - yield os.path.join(repo_with_passing_hook, 'foo') + git('add', 'bar_only_file') + git('commit', '-m', 'conflict_file') + git('merge', 'foo', retcode=None) + 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 dff80e91..9b22359d 100644 --- a/tests/git_test.py +++ b/tests/git_test.py @@ -1,21 +1,33 @@ +from __future__ import absolute_import +from __future__ import unicode_literals + +import os.path import pytest from plumbum import local from pre_commit import git +from testing.fixtures import git_dir -def test_get_root(empty_git_dir): - assert git.get_root() == empty_git_dir - - foo = local.path('foo') - foo.mkdir() - - with local.cwd(foo): - assert git.get_root() == empty_git_dir +def test_get_root_at_root(tmpdir_factory): + path = git_dir(tmpdir_factory) + with local.cwd(path): + assert git.get_root() == path -def test_is_not_in_merge_conflict(empty_git_dir): - assert git.is_in_merge_conflict() is False +def test_get_root_deeper(tmpdir_factory): + path = git_dir(tmpdir_factory) + + foo_path = os.path.join(path, 'foo') + os.mkdir(foo_path) + with local.cwd(foo_path): + assert git.get_root() == path + + +def test_is_not_in_merge_conflict(tmpdir_factory): + path = git_dir(tmpdir_factory) + with local.cwd(path): + assert git.is_in_merge_conflict() is False def test_is_in_merge_conflict(in_merge_conflict): @@ -77,14 +89,14 @@ def test_exclude_removes_files(get_files_matching_func): def resolve_conflict(): with open('conflict_file', 'w') as conflicted_file: conflicted_file.write('herp\nderp\n') - local['git']['add', 'conflict_file']() + local['git']('add', 'conflict_file') def test_get_conflicted_files(in_merge_conflict): resolve_conflict() with open('other_file', 'w') as other_file: other_file.write('oh hai') - local['git']['add', 'other_file']() + local['git']('add', 'other_file') ret = set(git.get_conflicted_files()) assert ret == set(('conflict_file', 'other_file')) 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 cd52f0b9..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 ed333152..76401650 100644 --- a/tests/runner_test.py +++ b/tests/runner_test.py @@ -1,9 +1,14 @@ +from __future__ import absolute_import +from __future__ import unicode_literals + import os import os.path -import pytest +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): @@ -13,24 +18,26 @@ def test_init_has_no_side_effects(tmpdir): assert os.getcwd() == current_wd -def test_create_sets_correct_directory(empty_git_dir): - runner = Runner.create() - assert runner.git_root == empty_git_dir - assert os.getcwd() == empty_git_dir +def test_create_sets_correct_directory(tmpdir_factory): + path = git_dir(tmpdir_factory) + with local.cwd(path): + runner = Runner.create() + assert runner.git_root == path + assert os.getcwd() == path -@pytest.yield_fixture -def git_dir_with_directory(empty_git_dir): - os.mkdir('foo') - yield empty_git_dir +def test_create_changes_to_git_root(tmpdir_factory): + path = git_dir(tmpdir_factory) + with local.cwd(path): + # Change into some directory, create should set to root + foo_path = os.path.join(path, 'foo') + os.mkdir(foo_path) + os.chdir(foo_path) + assert os.getcwd() != path - -def test_changes_to_root_of_git_dir(git_dir_with_directory): - os.chdir('foo') - assert os.getcwd() != git_dir_with_directory - runner = Runner.create() - assert runner.git_root == git_dir_with_directory - assert os.getcwd() == git_dir_with_directory + runner = Runner.create() + assert runner.git_root == path + assert os.getcwd() == path def test_config_file_path(): @@ -39,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/staged_files_only_test.py b/tests/staged_files_only_test.py index a70c6447..e549bd42 100644 --- a/tests/staged_files_only_test.py +++ b/tests/staged_files_only_test.py @@ -1,3 +1,4 @@ +from __future__ import absolute_import from __future__ import unicode_literals import io @@ -10,6 +11,7 @@ from plumbum import local from pre_commit.staged_files_only import staged_files_only from testing.auto_namedtuple import auto_namedtuple +from testing.fixtures import git_dir from testing.util import get_resource_path @@ -17,17 +19,19 @@ FOO_CONTENTS = '\n'.join(('1', '2', '3', '4', '5', '6', '7', '8', '')) def get_short_git_status(): - git_status = local['git']['status', '-s']() + git_status = local['git']('status', '-s') return dict(reversed(line.split()) for line in git_status.splitlines()) @pytest.yield_fixture -def foo_staged(empty_git_dir): - with io.open('foo', 'w') as foo_file: - foo_file.write(FOO_CONTENTS) - local['git']['add', 'foo']() - foo_filename = os.path.join(empty_git_dir, 'foo') - yield auto_namedtuple(path=empty_git_dir, foo_filename=foo_filename) +def foo_staged(tmpdir_factory): + path = git_dir(tmpdir_factory) + with local.cwd(path): + with io.open('foo', 'w') as foo_file: + foo_file.write(FOO_CONTENTS) + local['git']('add', 'foo') + foo_filename = os.path.join(path, 'foo') + yield auto_namedtuple(path=path, foo_filename=foo_filename) def _test_foo_state(path, foo_contents=FOO_CONTENTS, status='A'): @@ -96,11 +100,13 @@ def test_foo_both_modify_conflicting(foo_staged, cmd_runner): @pytest.yield_fixture -def img_staged(empty_git_dir): - img_filename = os.path.join(empty_git_dir, 'img.jpg') - shutil.copy(get_resource_path('img1.jpg'), img_filename) - local['git']['add', 'img.jpg']() - yield auto_namedtuple(path=empty_git_dir, img_filename=img_filename) +def img_staged(tmpdir_factory): + path = git_dir(tmpdir_factory) + with local.cwd(path): + img_filename = os.path.join(path, 'img.jpg') + shutil.copy(get_resource_path('img1.jpg'), img_filename) + local['git']('add', 'img.jpg') + yield auto_namedtuple(path=path, img_filename=img_filename) def _test_img_state(path, expected_file='img1.jpg', status='A'): @@ -149,35 +155,39 @@ def test_img_conflict(img_staged, cmd_runner): @pytest.yield_fixture -def submodule_with_commits(empty_git_dir): - local['git']['commit', '--allow-empty', '-m', 'foo']() - sha1 = local['git']['rev-parse', 'HEAD']().strip() - local['git']['commit', '--allow-empty', '-m', 'bar']() - sha2 = local['git']['rev-parse', 'HEAD']().strip() - yield auto_namedtuple(path=empty_git_dir, sha1=sha1, sha2=sha2) +def submodule_with_commits(tmpdir_factory): + path = git_dir(tmpdir_factory) + with local.cwd(path): + local['git']('commit', '--allow-empty', '-m', 'foo') + sha1 = local['git']('rev-parse', 'HEAD').strip() + local['git']('commit', '--allow-empty', '-m', 'bar') + sha2 = local['git']('rev-parse', 'HEAD').strip() + yield auto_namedtuple(path=path, sha1=sha1, sha2=sha2) def checkout_submodule(sha): with local.cwd('sub'): - local['git']['checkout', sha]() + local['git']('checkout', sha) @pytest.yield_fixture -def sub_staged(submodule_with_commits, empty_git_dir): - local['git']['submodule', 'add', submodule_with_commits.path, 'sub']() - checkout_submodule(submodule_with_commits.sha1) - local['git']['add', 'sub']() - yield auto_namedtuple( - path=empty_git_dir, - sub_path=os.path.join(empty_git_dir, 'sub'), - submodule=submodule_with_commits, - ) +def sub_staged(submodule_with_commits, tmpdir_factory): + path = git_dir(tmpdir_factory) + with local.cwd(path): + local['git']('submodule', 'add', submodule_with_commits.path, 'sub') + checkout_submodule(submodule_with_commits.sha1) + local['git']('add', 'sub') + yield auto_namedtuple( + path=path, + sub_path=os.path.join(path, 'sub'), + submodule=submodule_with_commits, + ) def _test_sub_state(path, sha='sha1', status='A'): assert os.path.exists(path.sub_path) with local.cwd(path.sub_path): - actual_sha = local['git']['rev-parse', 'HEAD']().strip() + actual_sha = local['git']('rev-parse', 'HEAD').strip() assert actual_sha == getattr(path.submodule, sha) actual_status = get_short_git_status()['sub'] assert actual_status == status diff --git a/tests/store_test.py b/tests/store_test.py index 2bdea9de..5ebfe05d 100644 --- a/tests/store_test.py +++ b/tests/store_test.py @@ -1,3 +1,6 @@ +from __future__ import absolute_import +from __future__ import unicode_literals + import io import mock import os @@ -10,6 +13,7 @@ from pre_commit import five from pre_commit.store import _get_default_directory from pre_commit.store import logger from pre_commit.store import Store +from testing.fixtures import git_dir from testing.util import get_head_sha @@ -71,13 +75,14 @@ def log_info_mock(): yield info_mock -def test_clone(store, empty_git_dir, log_info_mock): - with local.cwd(empty_git_dir): +def test_clone(store, tmpdir_factory, log_info_mock): + path = git_dir(tmpdir_factory) + with local.cwd(path): local['git']('commit', '--allow-empty', '-m', 'foo') - sha = get_head_sha(empty_git_dir) + sha = get_head_sha(path) local['git']('commit', '--allow-empty', '-m', 'bar') - ret = store.clone(empty_git_dir, sha) + ret = store.clone(path, sha) # Should have printed some stuff log_info_mock.assert_called_with('This may take a few minutes...') 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