Refactor fixtures in tests.

This commit is contained in:
Anthony Sottile 2014-06-15 18:48:43 -07:00
parent 3baefd57e2
commit 85a76617c1
43 changed files with 249 additions and 278 deletions

View file

@ -1,4 +1,5 @@
from __future__ import print_function from __future__ import print_function
from __future__ import unicode_literals
import argparse import argparse
import jsonschema import jsonschema

View file

@ -1,3 +1,5 @@
from __future__ import unicode_literals
import sys import sys
from pre_commit.clientlib.validate_base import get_run_function from pre_commit.clientlib.validate_base import get_run_function

View file

@ -1,3 +1,5 @@
from __future__ import unicode_literals
import sys import sys
from pre_commit.clientlib.validate_base import get_run_function from pre_commit.clientlib.validate_base import get_run_function

View file

@ -1,3 +1,5 @@
from __future__ import unicode_literals
import sys import sys
RED = '\033[41m' RED = '\033[41m'

View file

@ -1,3 +1,6 @@
from __future__ import unicode_literals
CONFIG_FILE = '.pre-commit-config.yaml' CONFIG_FILE = '.pre-commit-config.yaml'
MANIFEST_FILE = 'hooks.yaml' MANIFEST_FILE = 'hooks.yaml'

View file

@ -1,3 +1,5 @@
from __future__ import unicode_literals
"""five: six, redux""" """five: six, redux"""
# pylint:disable=invalid-name # pylint:disable=invalid-name
PY2 = (str is bytes) PY2 = (str is bytes)

View file

@ -1,3 +1,5 @@
from __future__ import unicode_literals
import functools import functools
import logging import logging
import os import os

View file

@ -1,3 +1,5 @@
from __future__ import unicode_literals
import copy import copy
import jsonschema import jsonschema
import jsonschema.validators import jsonschema.validators

View file

@ -1,3 +1,5 @@
from __future__ import unicode_literals
from pre_commit.languages import node from pre_commit.languages import node
from pre_commit.languages import python from pre_commit.languages import python
from pre_commit.languages import ruby from pre_commit.languages import ruby

View file

@ -1,3 +1,4 @@
from __future__ import unicode_literals
def run_hook(env, hook, file_args): def run_hook(env, hook, file_args):

View file

@ -1,3 +1,5 @@
from __future__ import unicode_literals
import contextlib import contextlib
from pre_commit.languages import helpers from pre_commit.languages import helpers

View file

@ -1,3 +1,5 @@
from __future__ import unicode_literals
import contextlib import contextlib
from pre_commit.languages import helpers from pre_commit.languages import helpers

View file

@ -1,3 +1,5 @@
from __future__ import unicode_literals
ENVIRONMENT_DIR = None ENVIRONMENT_DIR = None

View file

@ -1,3 +1,5 @@
from __future__ import unicode_literals
import shlex import shlex

View file

@ -1,3 +1,5 @@
from __future__ import unicode_literals
import logging import logging
import sys import sys

View file

@ -1,3 +1,5 @@
from __future__ import unicode_literals
import os.path import os.path
from asottile.cached_property import cached_property from asottile.cached_property import cached_property

View file

@ -1,3 +1,5 @@
from __future__ import unicode_literals
import subprocess import subprocess
from pre_commit import color from pre_commit import color

View file

@ -1,3 +1,5 @@
from __future__ import unicode_literals
import os import os
import os.path import os.path
import subprocess import subprocess

View file

@ -1,3 +1,5 @@
from __future__ import unicode_literals
from asottile.cached_property import cached_property from asottile.cached_property import cached_property
from asottile.ordereddict import OrderedDict from asottile.ordereddict import OrderedDict

View file

@ -1,3 +1,5 @@
from __future__ import unicode_literals
import os import os
import os.path import os.path
from asottile.cached_property import cached_property from asottile.cached_property import cached_property

View file

@ -1,3 +1,5 @@
from __future__ import unicode_literals
import contextlib import contextlib
import io import io
import logging import logging

View file

@ -1,3 +1,5 @@
from __future__ import unicode_literals
import contextlib import contextlib
import functools import functools
import os import os

View file

@ -1,3 +1,5 @@
from __future__ import unicode_literals
import collections import collections

View file

@ -1,8 +1,10 @@
from __future__ import absolute_import from __future__ import absolute_import
from __future__ import unicode_literals from __future__ import unicode_literals
import io
import os.path import os.path
from asottile.ordereddict import OrderedDict from asottile.ordereddict import OrderedDict
from asottile.yaml import ordered_dump
from plumbum import local from plumbum import local
import pre_commit.constants as C import pre_commit.constants as C
@ -25,7 +27,7 @@ def git_dir(tmpdir_factory):
return path return path
def make_hooks_repo(tmpdir_factory, repo_source): def make_repo(tmpdir_factory, repo_source):
path = git_dir(tmpdir_factory) path = git_dir(tmpdir_factory)
copy_tree_to_path(get_resource_path(repo_source), path) copy_tree_to_path(get_resource_path(repo_source), path)
with local.cwd(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] return wrapped_config[0]
else: else:
return config 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

View file

@ -1,3 +1,5 @@
from __future__ import unicode_literals
import jsonschema import jsonschema
import os import os
import os.path import os.path

View file

@ -1,3 +1,5 @@
from __future__ import unicode_literals
import pytest import pytest
from asottile.ordereddict import OrderedDict from asottile.ordereddict import OrderedDict
from asottile.yaml import ordered_load from asottile.yaml import ordered_load

View file

@ -1,3 +1,5 @@
from __future__ import unicode_literals
import pytest import pytest
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA

View file

@ -1,3 +1,5 @@
from __future__ import unicode_literals
import pytest import pytest
from pre_commit.clientlib.validate_manifest import additional_manifest_check from pre_commit.clientlib.validate_manifest import additional_manifest_check

View file

@ -1,3 +1,5 @@
from __future__ import unicode_literals
import mock import mock
import pytest import pytest
import sys import sys

View file

@ -1,10 +1,8 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import io
import pytest import pytest
import shutil import shutil
from asottile.ordereddict import OrderedDict from asottile.ordereddict import OrderedDict
from asottile.yaml import ordered_dump
from plumbum import local from plumbum import local
import pre_commit.constants as C import pre_commit.constants as C
@ -14,14 +12,15 @@ from pre_commit.commands.autoupdate import RepositoryCannotBeUpdatedError
from pre_commit.runner import Runner from pre_commit.runner import Runner
from testing.auto_namedtuple import auto_namedtuple from testing.auto_namedtuple import auto_namedtuple
from testing.fixtures import make_config_from_repo 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_head_sha
from testing.util import get_resource_path from testing.util import get_resource_path
@pytest.yield_fixture @pytest.yield_fixture
def up_to_date_repo(tmpdir_factory): 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): 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 # Write out the config
config = make_config_from_repo(up_to_date_repo, check=False) config = make_config_from_repo(up_to_date_repo, check=False)
with io.open(C.CONFIG_FILE, 'w') as config_file: write_config('.', config)
config_file.write(ordered_dump([config], **C.YAML_DUMP_KWARGS))
before = open(C.CONFIG_FILE).read() before = open(C.CONFIG_FILE).read()
assert '^$' not in before assert '^$' not in before
@ -50,7 +48,7 @@ def test_autoupdate_up_to_date_repo(
@pytest.yield_fixture @pytest.yield_fixture
def out_of_date_repo(tmpdir_factory): 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) original_sha = get_head_sha(path)
# Make a commit # Make a commit
@ -79,8 +77,7 @@ def test_autoupdate_out_of_date_repo(
config = make_config_from_repo( config = make_config_from_repo(
out_of_date_repo.path, sha=out_of_date_repo.original_sha, check=False, out_of_date_repo.path, sha=out_of_date_repo.original_sha, check=False,
) )
with io.open(C.CONFIG_FILE, 'w') as config_file: write_config('.', config)
config_file.write(ordered_dump([config], **C.YAML_DUMP_KWARGS))
before = open(C.CONFIG_FILE).read() before = open(C.CONFIG_FILE).read()
runner = Runner('.') runner = Runner('.')
@ -95,7 +92,7 @@ def test_autoupdate_out_of_date_repo(
@pytest.yield_fixture @pytest.yield_fixture
def hook_disappearing_repo(tmpdir_factory): 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) original_sha = get_head_sha(path)
with local.cwd(path): with local.cwd(path):
@ -130,8 +127,7 @@ def test_autoupdate_hook_disappearing_repo(
hooks=[OrderedDict((('id', 'foo'),))], hooks=[OrderedDict((('id', 'foo'),))],
check=False, check=False,
) )
with io.open(C.CONFIG_FILE, 'w') as config_file: write_config('.', config)
config_file.write(ordered_dump([config], **C.YAML_DUMP_KWARGS))
before = open(C.CONFIG_FILE).read() before = open(C.CONFIG_FILE).read()
runner = Runner('.') runner = Runner('.')

View file

@ -11,6 +11,21 @@ from pre_commit.commands.run import _has_unmerged_paths
from pre_commit.commands.run import run from pre_commit.commands.run import run
from pre_commit.runner import Runner from pre_commit.runner import Runner
from testing.auto_namedtuple import auto_namedtuple 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(): def stage_a_file():

View file

@ -1,26 +1,19 @@
from __future__ import absolute_import from __future__ import absolute_import
from __future__ import unicode_literals from __future__ import unicode_literals
import io
import mock import mock
import os import os
import os.path import os.path
import pytest import pytest
import time
import yaml
from plumbum import local from plumbum import local
import pre_commit.constants as C import pre_commit.constants as C
from pre_commit import five 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.prefixed_command_runner import PrefixedCommandRunner
from pre_commit.runner import Runner from pre_commit.runner import Runner
from pre_commit.store import Store from pre_commit.store import Store
from testing.fixtures import git_dir from testing.fixtures import make_consuming_repo
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'] git = local['git']
@ -48,194 +41,36 @@ def in_tmpdir(tmpdir_factory):
yield path yield path
def add_and_commit():
git('add', '.')
git('commit', '-m', 'random commit {0}'.format(time.time()))
@pytest.yield_fixture @pytest.yield_fixture
def dummy_git_repo(tmpdir_factory): def in_merge_conflict(tmpdir_factory):
path = git_dir(tmpdir_factory) path = make_consuming_repo(tmpdir_factory, 'script_hooks_repo')
with local.cwd(path): with local.cwd(path):
# This is needed otherwise there is no `HEAD`
local['touch']('dummy')
add_and_commit()
yield path
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') local['touch']('dummy')
git('add', 'dummy') git('add', 'dummy')
git('add', C.CONFIG_FILE) git('add', C.CONFIG_FILE)
git('commit', '-m' 'add hooks file') git('commit', '-m', 'Add config.')
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') 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') conflict_file.write('herp\nderp\n')
git('add', 'conflict_file') 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') foo_only_file.write('foo')
git('add', 'foo_only_file') git('add', 'foo_only_file')
git('commit', '-m', 'conflict_file') git('commit', '-m', 'conflict_file')
git('checkout', 'origin/master', '-b', 'bar') 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') conflict_file.write('harp\nddrp\n')
git('add', 'conflict_file') 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') bar_only_file.write('bar')
git('add', 'bar_only_file') git('add', 'bar_only_file')
git('commit', '-m', 'conflict_file') git('commit', '-m', 'conflict_file')
git('merge', 'foo', retcode=None) 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) @pytest.yield_fixture(scope='session', autouse=True)

View file

@ -1,4 +1,5 @@
from __future__ import absolute_import from __future__ import absolute_import
from __future__ import unicode_literals
import os.path import os.path
import pytest import pytest

View file

@ -1,3 +1,5 @@
from __future__ import unicode_literals
import jsonschema.exceptions import jsonschema.exceptions
import pytest import pytest

View file

@ -1,3 +1,5 @@
from __future__ import unicode_literals
import pytest import pytest
from pre_commit.languages.all import all_languages from pre_commit.languages.all import all_languages

View file

@ -1,3 +1,5 @@
from __future__ import unicode_literals
import os.path import os.path
from pre_commit.languages.ruby import _install_rbenv from pre_commit.languages.ruby import _install_rbenv

View file

@ -1,3 +1,5 @@
from __future__ import unicode_literals
import mock import mock
from pre_commit import color from pre_commit import color

View file

@ -1,13 +1,18 @@
from __future__ import absolute_import
from __future__ import unicode_literals
import pytest import pytest
from pre_commit.manifest import Manifest from pre_commit.manifest import Manifest
from testing.fixtures import make_repo
from testing.util import get_head_sha from testing.util import get_head_sha
@pytest.yield_fixture @pytest.yield_fixture
def manifest(store, script_hooks_repo): def manifest(store, tmpdir_factory):
head_sha = get_head_sha(script_hooks_repo) path = make_repo(tmpdir_factory, 'script_hooks_repo')
repo_path_getter = store.get_repo_path_getter(script_hooks_repo, head_sha) head_sha = get_head_sha(path)
repo_path_getter = store.get_repo_path_getter(path, head_sha)
yield Manifest(repo_path_getter) yield Manifest(repo_path_getter)

View file

@ -1,3 +1,5 @@
from __future__ import unicode_literals
import pytest import pytest
from pre_commit import color from pre_commit import color

View file

@ -1,3 +1,5 @@
from __future__ import unicode_literals
import os import os
import mock import mock
import pytest import pytest
@ -9,7 +11,9 @@ from pre_commit.prefixed_command_runner import PrefixedCommandRunner
def test_CalledProcessError_str(): 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) == ( assert str(error) == (
"Command: ['git', 'status']\n" "Command: ['git', 'status']\n"
"Return code: 1\n" "Return code: 1\n"

View file

@ -1,3 +1,7 @@
from __future__ import absolute_import
from __future__ import unicode_literals
import mock
import os.path import os.path
import pytest import pytest
from plumbum import local 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.clientlib.validate_config import validate_config_extra
from pre_commit.jsonschema_extensions import apply_defaults from pre_commit.jsonschema_extensions import apply_defaults
from pre_commit.repository import Repository 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 from testing.util import skipif_slowtests_false
@pytest.mark.integration @pytest.mark.integration
def test_install_python_repo_in_env(config_for_python_hooks_repo, store): def test_install_python_repo_in_env(tmpdir_factory, store):
repo = Repository.create(config_for_python_hooks_repo, store) path = make_repo(tmpdir_factory, 'python_hooks_repo')
config = make_config_from_repo(path)
repo = Repository.create(config, store)
repo.install() repo.install()
assert os.path.exists(os.path.join(store.directory, repo.sha, 'py_env')) assert os.path.exists(os.path.join(store.directory, repo.sha, 'py_env'))
@pytest.mark.integration def _test_hook_repo(tmpdir_factory, store, repo_path, hook_id, args, expected):
def test_run_a_python_hook(config_for_python_hooks_repo, store): path = make_repo(tmpdir_factory, repo_path)
repo = Repository.create(config_for_python_hooks_repo, store) config = make_config_from_repo(path)
ret = repo.run_hook('foo', ['/dev/null']) repo = Repository.create(config, store)
ret = repo.run_hook(hook_id, args)
assert ret[0] == 0 assert ret[0] == 0
assert ret[1] == "['/dev/null']\nHello World\n" assert ret[1] == expected
@pytest.mark.integration @pytest.mark.integration
def test_run_versioned_hook(config_for_python3_hooks_repo, store): def test_python_hook(tmpdir_factory, store):
repo = Repository.create(config_for_python3_hooks_repo, store) _test_hook_repo(
ret = repo.run_hook('python3-hook', ['/dev/null']) tmpdir_factory, store, 'python_hooks_repo',
assert ret[0] == 0 'foo', ['/dev/null'], "['/dev/null']\nHello World\n",
assert ret[1] == "3.3\n['/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 @skipif_slowtests_false
@pytest.mark.integration @pytest.mark.integration
def test_run_versioned_node_hook(config_for_node_0_11_8_hooks_repo, store): def test_run_a_node_hook(tmpdir_factory, store):
repo = Repository.create(config_for_node_0_11_8_hooks_repo, store) _test_hook_repo(
ret = repo.run_hook('node-11-8-hook', ['/dev/null']) tmpdir_factory, store, 'node_hooks_repo',
assert ret[0] == 0 'foo', [], 'Hello World\n',
assert ret[1] == 'v0.11.8\nHello World\n' )
@pytest.mark.herpderp
@skipif_slowtests_false @skipif_slowtests_false
@pytest.mark.integration @pytest.mark.integration
def test_run_versioned_ruby_hook(config_for_ruby_1_9_3_hooks_repo, store): def test_run_versioned_node_hook(tmpdir_factory, store):
repo = Repository.create(config_for_ruby_1_9_3_hooks_repo, store) _test_hook_repo(
ret = repo.run_hook('ruby_hook', []) tmpdir_factory, store, 'node_0_11_8_hooks_repo',
assert ret[0] == 0 'node-11-8-hook', ['/dev/null'], 'v0.11.8\nHello World\n',
assert ret[1] == '1.9.3\n484\nHello world from a ruby hook\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 @pytest.mark.integration
def test_lots_of_files(config_for_python_hooks_repo, store): def test_system_hook_with_spaces(tmpdir_factory, store):
repo = Repository.create(config_for_python_hooks_repo, store) _test_hook_repo(
ret = repo.run_hook('foo', ['/dev/null'] * 15000) tmpdir_factory, store, 'system_hook_with_spaces_repo',
assert ret[0] == 0 'system-hook-with-spaces', [], 'Hello World\n',
)
@pytest.mark.integration @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 # Note: this doubles as a test for `system` hooks
repo = Repository.create(config_for_prints_cwd_repo, store) path = git_dir(tmpdir_factory)
ret = repo.run_hook('prints_cwd', []) with local.cwd(path):
assert ret[0] == 0 _test_hook_repo(
assert ret[1] == repo.repo_url + '\n' tmpdir_factory, store, 'prints_cwd_repo',
'prints_cwd', [], path + '\n',
)
@pytest.mark.integration @pytest.mark.integration
def test_system_hook_with_spaces(config_for_system_hook_with_spaces, store): def test_lots_of_files(tmpdir_factory, store):
repo = Repository.create(config_for_system_hook_with_spaces, store) _test_hook_repo(
ret = repo.run_hook('system-hook-with-spaces', []) tmpdir_factory, store, 'script_hooks_repo',
assert ret[0] == 0 'bash_hook', ['/dev/null'] * 15000, mock.ANY,
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'
@pytest.fixture @pytest.fixture
@ -129,37 +147,49 @@ def test_sha(mock_repo_config):
@pytest.mark.integration @pytest.mark.integration
def test_languages(config_for_python_hooks_repo, store): def test_languages(tmpdir_factory, store):
repo = Repository.create(config_for_python_hooks_repo, 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')]) assert repo.languages == set([('python', 'default')])
def test_reinstall(config_for_python_hooks_repo, store): def test_reinstall(tmpdir_factory, store):
repo = Repository.create(config_for_python_hooks_repo, store) path = make_repo(tmpdir_factory, 'python_hooks_repo')
config = make_config_from_repo(path)
repo = Repository.create(config, store)
repo.require_installed() repo.require_installed()
# Reinstall with same repo should not trigger another install # Reinstall with same repo should not trigger another install
# TODO: how to assert this? # TODO: how to assert this?
repo.require_installed() repo.require_installed()
# Reinstall on another run should not trigger another install # Reinstall on another run should not trigger another install
# TODO: how to assert this? # TODO: how to assert this?
repo = Repository.create(config_for_python_hooks_repo, store) repo = Repository.create(config, store)
repo.require_installed() repo.require_installed()
@pytest.mark.integration @pytest.mark.integration
def test_really_long_file_paths(config_for_python_hooks_repo, store): def test_really_long_file_paths(tmpdir_factory, store):
path = 'really_long' * 10 base_path = tmpdir_factory.get()
local['git']('init', path) really_long_path = os.path.join(base_path, 'really_long' * 10)
with local.cwd(path): local['git']('init', really_long_path)
repo = Repository.create(config_for_python_hooks_repo, store)
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() repo.require_installed()
@pytest.mark.integration @pytest.mark.integration
def test_config_overrides_repo_specifics(config_for_script_hooks_repo, store): def test_config_overrides_repo_specifics(tmpdir_factory, store):
repo = Repository.create(config_for_script_hooks_repo, 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'] == '' assert repo.hooks['bash_hook']['files'] == ''
# Set the file regex to something else # Set the file regex to something else
config_for_script_hooks_repo['hooks'][0]['files'] = '\\.sh$' config['hooks'][0]['files'] = '\\.sh$'
repo = Repository.create(config_for_script_hooks_repo, store) repo = Repository.create(config, store)
assert repo.hooks['bash_hook']['files'] == '\\.sh$' assert repo.hooks['bash_hook']['files'] == '\\.sh$'

View file

@ -8,6 +8,7 @@ from plumbum import local
import pre_commit.constants as C import pre_commit.constants as C
from pre_commit.runner import Runner from pre_commit.runner import Runner
from testing.fixtures import git_dir from testing.fixtures import git_dir
from testing.fixtures import make_repo
def test_init_has_no_side_effects(tmpdir): def test_init_has_no_side_effects(tmpdir):
@ -45,9 +46,10 @@ def test_config_file_path():
assert runner.config_file_path == expected_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 # 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 len(runner.repositories) == 2
assert [repo.repo_url for repo in runner.repositories] == [ assert [repo.repo_url for repo in runner.repositories] == [
'git@github.com:pre-commit/pre-commit-hooks', 'git@github.com:pre-commit/pre-commit-hooks',

View file

@ -1,3 +1,5 @@
from __future__ import unicode_literals
import mock import mock
import pytest import pytest
import os import os