mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Repository now parses languages and manifests
This commit is contained in:
parent
d77d01cd22
commit
5ca8f4ffa8
8 changed files with 114 additions and 23 deletions
4
Makefile
4
Makefile
|
|
@ -3,6 +3,8 @@ TEST_TARGETS =
|
||||||
ITEST_TARGETS = -m integration
|
ITEST_TARGETS = -m integration
|
||||||
UTEST_TARGETS = -m "not(integration)"
|
UTEST_TARGETS = -m "not(integration)"
|
||||||
|
|
||||||
|
DEBUG=
|
||||||
|
|
||||||
all: _tests
|
all: _tests
|
||||||
|
|
||||||
integration:
|
integration:
|
||||||
|
|
@ -19,7 +21,7 @@ itests: itest
|
||||||
itest: integration _tests
|
itest: integration _tests
|
||||||
|
|
||||||
_tests: py_env
|
_tests: py_env
|
||||||
bash -c 'source py_env/bin/activate && py.test tests $(TEST_TARGETS)'
|
bash -c 'source py_env/bin/activate && py.test tests $(TEST_TARGETS) $(DEBUG)'
|
||||||
|
|
||||||
ucoverage: unit coverage
|
ucoverage: unit coverage
|
||||||
icoverage: integration coverage
|
icoverage: integration coverage
|
||||||
|
|
|
||||||
|
|
@ -48,4 +48,6 @@ def get_validator(
|
||||||
|
|
||||||
additional_validation_strategy(obj)
|
additional_validation_strategy(obj)
|
||||||
|
|
||||||
|
return obj
|
||||||
|
|
||||||
return validate
|
return validate
|
||||||
|
|
@ -2,24 +2,50 @@
|
||||||
import contextlib
|
import contextlib
|
||||||
from plumbum import local
|
from plumbum import local
|
||||||
|
|
||||||
|
import pre_commit.constants as C
|
||||||
|
from pre_commit.clientlib.validate_manifest import validate_manifest
|
||||||
from pre_commit.hooks_workspace import in_hooks_workspace
|
from pre_commit.hooks_workspace import in_hooks_workspace
|
||||||
|
from pre_commit.util import cached_property
|
||||||
|
|
||||||
|
|
||||||
class Repository(object):
|
class Repository(object):
|
||||||
def __init__(self, repo_config):
|
def __init__(self, repo_config):
|
||||||
self.repo_config = repo_config
|
self.repo_config = repo_config
|
||||||
|
|
||||||
@property
|
@cached_property
|
||||||
def repo_url(self):
|
def repo_url(self):
|
||||||
return self.repo_config['repo']
|
return self.repo_config['repo']
|
||||||
|
|
||||||
@property
|
@cached_property
|
||||||
def sha(self):
|
def sha(self):
|
||||||
return self.repo_config['sha']
|
return self.repo_config['sha']
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def languages(self):
|
||||||
|
return set(filter(None, (
|
||||||
|
hook.get('language') for hook in self.hooks.values()
|
||||||
|
)))
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def hooks(self):
|
||||||
|
return dict(
|
||||||
|
(hook['id'], dict(hook, **self.manifest[hook['id']]))
|
||||||
|
for hook in self.repo_config['hooks']
|
||||||
|
)
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def manifest(self):
|
||||||
|
with self.in_checkout():
|
||||||
|
return dict(
|
||||||
|
(hook['id'], hook)
|
||||||
|
for hook in validate_manifest(C.MANIFEST_FILE)
|
||||||
|
)
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def in_checkout(self):
|
def in_checkout(self):
|
||||||
with in_hooks_workspace():
|
with in_hooks_workspace():
|
||||||
|
# SMELL:
|
||||||
|
self.create()
|
||||||
with local.cwd(self.sha):
|
with local.cwd(self.sha):
|
||||||
yield
|
yield
|
||||||
|
|
||||||
|
|
@ -33,6 +59,19 @@ class Repository(object):
|
||||||
with self.in_checkout():
|
with self.in_checkout():
|
||||||
local['git']['checkout', self.sha]()
|
local['git']['checkout', self.sha]()
|
||||||
|
|
||||||
|
# TODO: make this shit polymorphic
|
||||||
|
|
||||||
|
def _install_python(self):
|
||||||
|
assert local.path('setup.py').exists()
|
||||||
|
local['virtualenv']['py_env']()
|
||||||
|
local['bash']['-c', 'source py_env/bin/activate && pip install .']()
|
||||||
|
|
||||||
|
def _install_ruby(self):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def _install_node(self):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
def install(self):
|
def install(self):
|
||||||
# Create if we have not already
|
# Create if we have not already
|
||||||
self.create()
|
self.create()
|
||||||
|
|
|
||||||
16
pre_commit/util.py
Normal file
16
pre_commit/util.py
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
|
||||||
|
class cached_property(object):
|
||||||
|
"""Like @property, but caches the value."""
|
||||||
|
|
||||||
|
def __init__(self, func):
|
||||||
|
self.__name__ = func.__name__
|
||||||
|
self.__module__ = func.__module__
|
||||||
|
self.__doc__ = func.__doc__
|
||||||
|
self._func = func
|
||||||
|
|
||||||
|
def __get__(self, obj, cls):
|
||||||
|
if obj is None:
|
||||||
|
return self
|
||||||
|
value = self._func(obj)
|
||||||
|
obj.__dict__[self.__name__] = value
|
||||||
|
return value
|
||||||
|
|
@ -65,3 +65,8 @@ def test_passes_array_schema(array_validator):
|
||||||
def test_raises_when_additional_validation_fails(additional_validator):
|
def test_raises_when_additional_validation_fails(additional_validator):
|
||||||
with pytest.raises(AdditionalValidatorError):
|
with pytest.raises(AdditionalValidatorError):
|
||||||
additional_validator()
|
additional_validator()
|
||||||
|
|
||||||
|
|
||||||
|
def test_returns_object_after_validating(noop_validator):
|
||||||
|
ret = noop_validator('tests/data/array_yaml_file.yaml')
|
||||||
|
assert ret == ['foo', 'bar']
|
||||||
|
|
@ -29,7 +29,7 @@ def test_additional_manifest_check_raises_for_bad_language():
|
||||||
@pytest.mark.parametrize(('obj'), (
|
@pytest.mark.parametrize(('obj'), (
|
||||||
[{}],
|
[{}],
|
||||||
[{'language': 'python'}],
|
[{'language': 'python'}],
|
||||||
[{'language': 'python>2.6'}],
|
[{'language': 'ruby'}],
|
||||||
))
|
))
|
||||||
def test_additional_manifest_check_is_ok_with_missing_language(obj):
|
def test_additional_manifest_check_is_ok_with_missing_language(obj):
|
||||||
additional_manifest_check(obj)
|
additional_manifest_check(obj)
|
||||||
|
|
|
||||||
|
|
@ -31,22 +31,17 @@ def dummy_git_repo(empty_git_dir):
|
||||||
|
|
||||||
|
|
||||||
@pytest.yield_fixture
|
@pytest.yield_fixture
|
||||||
def dummy_pre_commit_hooks_git_repo(dummy_git_repo):
|
def python_pre_commit_git_repo(dummy_git_repo):
|
||||||
local.path(C.MANIFEST_FILE).write("""
|
local.path(C.MANIFEST_FILE).write("""
|
||||||
hooks:
|
-
|
||||||
-
|
|
||||||
id: foo
|
id: foo
|
||||||
name: Foo
|
name: Foo
|
||||||
entry: foo
|
entry: foo
|
||||||
language: python>2.6
|
language: python
|
||||||
""")
|
""")
|
||||||
|
|
||||||
add_and_commit()
|
add_and_commit()
|
||||||
|
|
||||||
yield dummy_git_repo
|
|
||||||
|
|
||||||
@pytest.yield_fixture
|
|
||||||
def python_pre_commit_git_repo(dummy_pre_commit_hooks_git_repo):
|
|
||||||
local.path('setup.py').write("""
|
local.path('setup.py').write("""
|
||||||
from setuptools import find_packages
|
from setuptools import find_packages
|
||||||
from setuptools import setup
|
from setuptools import setup
|
||||||
|
|
@ -61,8 +56,7 @@ setup(
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
"""
|
""")
|
||||||
)
|
|
||||||
|
|
||||||
foo_module = local.path('foo')
|
foo_module = local.path('foo')
|
||||||
|
|
||||||
|
|
@ -73,12 +67,11 @@ setup(
|
||||||
local.path('main.py').write("""
|
local.path('main.py').write("""
|
||||||
def func():
|
def func():
|
||||||
return 0
|
return 0
|
||||||
"""
|
""")
|
||||||
)
|
|
||||||
|
|
||||||
add_and_commit()
|
add_and_commit()
|
||||||
|
|
||||||
yield dummy_pre_commit_hooks_git_repo
|
yield dummy_git_repo
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,10 @@
|
||||||
import os
|
import os
|
||||||
|
import jsonschema
|
||||||
import pytest
|
import pytest
|
||||||
from pre_commit import git
|
|
||||||
|
|
||||||
import pre_commit.constants as C
|
import pre_commit.constants as C
|
||||||
|
from pre_commit import git
|
||||||
|
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
|
||||||
from pre_commit.repository import Repository
|
from pre_commit.repository import Repository
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -40,3 +41,36 @@ def test_install_python_repo_in_env(python_pre_commit_git_repo, config_for_pytho
|
||||||
'py_env',
|
'py_env',
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_repo_config():
|
||||||
|
config = {
|
||||||
|
'repo': 'git@github.com:pre-commit/pre-commit-hooks',
|
||||||
|
'sha': '5e713f8878b7d100c0e059f8cc34be4fc2e8f897',
|
||||||
|
'hooks': [{
|
||||||
|
'id': 'pyflakes',
|
||||||
|
'files': '*.py',
|
||||||
|
}],
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonschema.validate([config], CONFIG_JSON_SCHEMA)
|
||||||
|
|
||||||
|
return config
|
||||||
|
|
||||||
|
|
||||||
|
def test_repo_url(mock_repo_config):
|
||||||
|
repo = Repository(mock_repo_config)
|
||||||
|
assert repo.repo_url == 'git@github.com:pre-commit/pre-commit-hooks'
|
||||||
|
|
||||||
|
|
||||||
|
def test_sha(mock_repo_config):
|
||||||
|
repo = Repository(mock_repo_config)
|
||||||
|
assert repo.sha == '5e713f8878b7d100c0e059f8cc34be4fc2e8f897'
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.integration
|
||||||
|
def test_languages(config_for_python_pre_commit_git_repo):
|
||||||
|
repo = Repository(config_for_python_pre_commit_git_repo)
|
||||||
|
assert repo.languages == set(['python'])
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue