Add Runner interface.

This commit is contained in:
Anthony Sottile 2014-03-23 16:22:24 -07:00
parent ae1d1681b2
commit 88686d298f
25 changed files with 282 additions and 148 deletions

View file

@ -7,6 +7,7 @@ import pytest
from pre_commit import git
from pre_commit.clientlib.validate_base import get_validator
from testing.util import get_resource_path
class AdditionalValidatorError(ValueError): pass
@ -42,7 +43,7 @@ def test_raises_for_non_existent_file(noop_validator):
def test_raises_for_invalid_yaml_file(noop_validator):
with pytest.raises(ValueError):
noop_validator('tests/data/non_parseable_yaml_file.yaml')
noop_validator(get_resource_path('non_parseable_yaml_file.yaml'))
def test_defaults_to_backup_filename(noop_validator):
@ -55,11 +56,11 @@ def test_defaults_to_backup_filename(noop_validator):
def test_raises_for_failing_schema(array_validator):
with pytest.raises(ValueError):
array_validator('tests/data/valid_yaml_but_invalid_manifest.yaml')
array_validator(get_resource_path('valid_yaml_but_invalid_manifest.yaml'))
def test_passes_array_schema(array_validator):
array_validator('tests/data/array_yaml_file.yaml')
array_validator(get_resource_path('array_yaml_file.yaml'))
def test_raises_when_additional_validation_fails(additional_validator):
@ -68,5 +69,5 @@ def test_raises_when_additional_validation_fails(additional_validator):
def test_returns_object_after_validating(noop_validator):
ret = noop_validator('tests/data/array_yaml_file.yaml')
ret = noop_validator(get_resource_path('array_yaml_file.yaml'))
assert ret == ['foo', 'bar']

View file

@ -1,14 +1,15 @@
from __future__ import absolute_import
import jsonschema
import simplejson
import pytest
import time
from plumbum import local
import pre_commit.constants as C
from pre_commit import git
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
from pre_commit.clientlib.validate_config import validate_config_extra
from testing.util import copy_tree_to_path
from testing.util import get_resource_path
@pytest.yield_fixture
@ -25,123 +26,69 @@ def add_and_commit():
@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
@pytest.yield_fixture
def python_pre_commit_git_repo(dummy_git_repo):
local.path(C.MANIFEST_FILE).write("""
-
id: foo
name: Foo
entry: foo
language: python
""")
def python_hooks_repo(dummy_git_repo):
copy_tree_to_path(
get_resource_path('python_hooks_repo'),
dummy_git_repo,
)
add_and_commit()
local.path('setup.py').write("""
from setuptools import find_packages
from setuptools import setup
setup(
name='Foo',
version='0.0.0',
packages=find_packages('.'),
entry_points={
'console_scripts': [
'foo = foo.main:func'
],
}
)
""")
foo_module = local.path('foo')
foo_module.mkdir()
with local.cwd(foo_module):
local.path('__init__.py').write('')
local.path('main.py').write("""
def func():
import sys
print repr(sys.argv[1:])
print 'Hello World'
return 0
""")
add_and_commit()
yield dummy_git_repo
@pytest.yield_fixture
def node_pre_commit_git_repo(dummy_git_repo):
local.path(C.MANIFEST_FILE).write("""
-
id: foo
name: Foo
entry: foo
language: node
""")
def node_hooks_repo(dummy_git_repo):
copy_tree_to_path(
get_resource_path('node_hooks_repo'),
dummy_git_repo,
)
add_and_commit()
yield dummy_git_repo
local.path('package.json').write(simplejson.dumps({
'name': 'foo',
'version': '0.0.1',
'bin': {
'foo': './bin/main.js'
},
}))
bin_dir = local.path('bin')
bin_dir.mkdir()
with local.cwd(bin_dir):
local.path('main.js').write(
"""#!/usr/bin/env node
console.log('Hello World');
""")
@pytest.yield_fixture
def consumer_repo(dummy_git_repo):
copy_tree_to_path(
get_resource_path('consumer_repo'),
dummy_git_repo,
)
add_and_commit()
yield dummy_git_repo
@pytest.fixture
def config_for_node_pre_commit_git_repo(node_pre_commit_git_repo):
def config_for_node_hooks_repo(node_hooks_repo):
config = {
'repo': node_pre_commit_git_repo,
'sha': git.get_head_sha(node_pre_commit_git_repo),
'repo': node_hooks_repo,
'sha': git.get_head_sha(node_hooks_repo),
'hooks': [{
'id': 'foo',
'files': '*.js',
'files': '\.js$',
}],
}
jsonschema.validate([config], CONFIG_JSON_SCHEMA)
validate_config_extra([config])
return config
@pytest.fixture
def config_for_python_pre_commit_git_repo(python_pre_commit_git_repo):
def config_for_python_hooks_repo(python_hooks_repo):
config = {
'repo': python_pre_commit_git_repo,
'sha': git.get_head_sha(python_pre_commit_git_repo),
'repo': python_hooks_repo,
'sha': git.get_head_sha(python_hooks_repo),
'hooks': [{
'id': 'foo',
'files': '*.py',
'files': '\.py$',
}],
}
jsonschema.validate([config], CONFIG_JSON_SCHEMA)
validate_config_extra([config])
return config

View file

@ -1,2 +0,0 @@
- foo
- bar

View file

@ -1 +0,0 @@
foo: "

View file

@ -1,7 +0,0 @@
- repo: git@github.com:pre-commit/pre-commit-hooks
hooks:
- id: pyflakes
- id: jslint
- id: trim_trailing_whitespace
files: '*.py'

View file

@ -1 +0,0 @@
foo: bar

View file

@ -28,13 +28,13 @@ def test_create_repo_in_env(dummy_repo_config, dummy_git_repo):
)
@pytest.mark.integration
def test_install_python_repo_in_env(python_pre_commit_git_repo, config_for_python_pre_commit_git_repo):
repo = Repository(config_for_python_pre_commit_git_repo)
def test_install_python_repo_in_env(python_hooks_repo, config_for_python_hooks_repo):
repo = Repository(config_for_python_hooks_repo)
repo.install()
assert os.path.exists(
os.path.join(
python_pre_commit_git_repo,
python_hooks_repo,
C.HOOKS_WORKSPACE,
repo.sha,
'py_env',
@ -43,8 +43,8 @@ def test_install_python_repo_in_env(python_pre_commit_git_repo, config_for_pytho
@pytest.mark.integration
def test_run_a_python_hook(config_for_python_pre_commit_git_repo):
repo = Repository(config_for_python_pre_commit_git_repo)
def test_run_a_python_hook(config_for_python_hooks_repo):
repo = Repository(config_for_python_hooks_repo)
repo.install()
ret = repo.run_hook('foo', ['/dev/null'])
@ -53,8 +53,8 @@ def test_run_a_python_hook(config_for_python_pre_commit_git_repo):
@pytest.mark.integration
def test_run_a_hook_lots_of_files(config_for_python_pre_commit_git_repo):
repo = Repository(config_for_python_pre_commit_git_repo)
def test_run_a_hook_lots_of_files(config_for_python_hooks_repo):
repo = Repository(config_for_python_hooks_repo)
repo.install()
ret = repo.run_hook('foo', ['/dev/null'] * 15000)
@ -66,8 +66,8 @@ def test_run_a_hook_lots_of_files(config_for_python_pre_commit_git_repo):
reason="TODO: make this test not super slow",
)
@pytest.mark.integration
def test_run_a_node_hook(config_for_node_pre_commit_git_repo):
repo = Repository(config_for_node_pre_commit_git_repo)
def test_run_a_node_hook(config_for_node_hooks_repo):
repo = Repository(config_for_node_hooks_repo)
repo.install()
ret = repo.run_hook('foo', [])
@ -101,6 +101,6 @@ def test_sha(mock_repo_config):
@pytest.mark.integration
def test_languages(config_for_python_pre_commit_git_repo):
repo = Repository(config_for_python_pre_commit_git_repo)
def test_languages(config_for_python_hooks_repo):
repo = Repository(config_for_python_hooks_repo)
assert repo.languages == set(['python'])

55
tests/runner_test.py Normal file
View file

@ -0,0 +1,55 @@
import os
import os.path
import pytest
import pre_commit.constants as C
from pre_commit.runner import Runner
def test_init_has_no_side_effects(tmpdir):
current_wd = os.getcwd()
runner = Runner(tmpdir.strpath)
assert runner.git_root == tmpdir.strpath
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
@pytest.yield_fixture
def git_dir_with_directory(empty_git_dir):
os.mkdir('foo')
yield empty_git_dir
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
def test_hooks_workspace_path():
runner = Runner('foo/bar')
expected_path = os.path.join('foo/bar', C.HOOKS_WORKSPACE)
assert runner.hooks_workspace_path == expected_path
def test_config_file_path():
runner = Runner('foo/bar')
expected_path = os.path.join('foo/bar', C.CONFIG_FILE)
assert runner.config_file_path == expected_path
def test_repositories(consumer_repo):
runner = Runner(consumer_repo)
assert len(runner.repositories) == 2
assert [repo.repo_url for repo in runner.repositories] == [
'git@github.com:pre-commit/pre-commit-hooks',
'git@github.com:pre-commit/pre-commit',
]