Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5ca8f4f

Browse files
committed
Repository now parses languages and manifests
1 parent d77d01c commit 5ca8f4f

8 files changed

Lines changed: 114 additions & 23 deletions

File tree

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ TEST_TARGETS =
33
ITEST_TARGETS = -m integration
44
UTEST_TARGETS = -m "not(integration)"
55

6+
DEBUG=
7+
68
all: _tests
79

810
integration:
@@ -19,7 +21,7 @@ itests: itest
1921
itest: integration _tests
2022

2123
_tests: py_env
22-
bash -c 'source py_env/bin/activate && py.test tests $(TEST_TARGETS)'
24+
bash -c 'source py_env/bin/activate && py.test tests $(TEST_TARGETS) $(DEBUG)'
2325

2426
ucoverage: unit coverage
2527
icoverage: integration coverage

pre_commit/clientlib/validate_base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,6 @@ def validate(filename=None):
4848

4949
additional_validation_strategy(obj)
5050

51+
return obj
52+
5153
return validate

pre_commit/repository.py

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,50 @@
22
import contextlib
33
from plumbum import local
44

5+
import pre_commit.constants as C
6+
from pre_commit.clientlib.validate_manifest import validate_manifest
57
from pre_commit.hooks_workspace import in_hooks_workspace
8+
from pre_commit.util import cached_property
69

710

811
class Repository(object):
912
def __init__(self, repo_config):
1013
self.repo_config = repo_config
1114

12-
@property
15+
@cached_property
1316
def repo_url(self):
1417
return self.repo_config['repo']
1518

16-
@property
19+
@cached_property
1720
def sha(self):
1821
return self.repo_config['sha']
1922

23+
@cached_property
24+
def languages(self):
25+
return set(filter(None, (
26+
hook.get('language') for hook in self.hooks.values()
27+
)))
28+
29+
@cached_property
30+
def hooks(self):
31+
return dict(
32+
(hook['id'], dict(hook, **self.manifest[hook['id']]))
33+
for hook in self.repo_config['hooks']
34+
)
35+
36+
@cached_property
37+
def manifest(self):
38+
with self.in_checkout():
39+
return dict(
40+
(hook['id'], hook)
41+
for hook in validate_manifest(C.MANIFEST_FILE)
42+
)
43+
2044
@contextlib.contextmanager
2145
def in_checkout(self):
2246
with in_hooks_workspace():
47+
# SMELL:
48+
self.create()
2349
with local.cwd(self.sha):
2450
yield
2551

@@ -33,6 +59,19 @@ def create(self):
3359
with self.in_checkout():
3460
local['git']['checkout', self.sha]()
3561

62+
# TODO: make this shit polymorphic
63+
64+
def _install_python(self):
65+
assert local.path('setup.py').exists()
66+
local['virtualenv']['py_env']()
67+
local['bash']['-c', 'source py_env/bin/activate && pip install .']()
68+
69+
def _install_ruby(self):
70+
raise NotImplementedError
71+
72+
def _install_node(self):
73+
raise NotImplementedError
74+
3675
def install(self):
3776
# Create if we have not already
3877
self.create()

pre_commit/util.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
class cached_property(object):
3+
"""Like @property, but caches the value."""
4+
5+
def __init__(self, func):
6+
self.__name__ = func.__name__
7+
self.__module__ = func.__module__
8+
self.__doc__ = func.__doc__
9+
self._func = func
10+
11+
def __get__(self, obj, cls):
12+
if obj is None:
13+
return self
14+
value = self._func(obj)
15+
obj.__dict__[self.__name__] = value
16+
return value

tests/clientlib/validate_base_test.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,9 @@ def test_passes_array_schema(array_validator):
6464

6565
def test_raises_when_additional_validation_fails(additional_validator):
6666
with pytest.raises(AdditionalValidatorError):
67-
additional_validator()
67+
additional_validator()
68+
69+
70+
def test_returns_object_after_validating(noop_validator):
71+
ret = noop_validator('tests/data/array_yaml_file.yaml')
72+
assert ret == ['foo', 'bar']

tests/clientlib/validate_manifest_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def test_additional_manifest_check_raises_for_bad_language():
2929
@pytest.mark.parametrize(('obj'), (
3030
[{}],
3131
[{'language': 'python'}],
32-
[{'language': 'python>2.6'}],
32+
[{'language': 'ruby'}],
3333
))
3434
def test_additional_manifest_check_is_ok_with_missing_language(obj):
3535
additional_manifest_check(obj)

tests/conftest.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,17 @@ def dummy_git_repo(empty_git_dir):
3131

3232

3333
@pytest.yield_fixture
34-
def dummy_pre_commit_hooks_git_repo(dummy_git_repo):
34+
def python_pre_commit_git_repo(dummy_git_repo):
3535
local.path(C.MANIFEST_FILE).write("""
36-
hooks:
37-
-
38-
id: foo
39-
name: Foo
40-
entry: foo
41-
language: python>2.6
36+
-
37+
id: foo
38+
name: Foo
39+
entry: foo
40+
language: python
4241
""")
4342

4443
add_and_commit()
4544

46-
yield dummy_git_repo
47-
48-
@pytest.yield_fixture
49-
def python_pre_commit_git_repo(dummy_pre_commit_hooks_git_repo):
5045
local.path('setup.py').write("""
5146
from setuptools import find_packages
5247
from setuptools import setup
@@ -61,8 +56,7 @@ def python_pre_commit_git_repo(dummy_pre_commit_hooks_git_repo):
6156
],
6257
}
6358
)
64-
"""
65-
)
59+
""")
6660

6761
foo_module = local.path('foo')
6862

@@ -73,12 +67,11 @@ def python_pre_commit_git_repo(dummy_pre_commit_hooks_git_repo):
7367
local.path('main.py').write("""
7468
def func():
7569
return 0
76-
"""
77-
)
70+
""")
7871

7972
add_and_commit()
8073

81-
yield dummy_pre_commit_hooks_git_repo
74+
yield dummy_git_repo
8275

8376

8477
@pytest.fixture

tests/repository_test.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import os
2-
2+
import jsonschema
33
import pytest
4-
from pre_commit import git
54

65
import pre_commit.constants as C
6+
from pre_commit import git
7+
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
78
from pre_commit.repository import Repository
89

910

@@ -40,3 +41,36 @@ def test_install_python_repo_in_env(python_pre_commit_git_repo, config_for_pytho
4041
'py_env',
4142
),
4243
)
44+
45+
46+
@pytest.fixture
47+
def mock_repo_config():
48+
config = {
49+
'repo': '[email protected]:pre-commit/pre-commit-hooks',
50+
'sha': '5e713f8878b7d100c0e059f8cc34be4fc2e8f897',
51+
'hooks': [{
52+
'id': 'pyflakes',
53+
'files': '*.py',
54+
}],
55+
}
56+
57+
jsonschema.validate([config], CONFIG_JSON_SCHEMA)
58+
59+
return config
60+
61+
62+
def test_repo_url(mock_repo_config):
63+
repo = Repository(mock_repo_config)
64+
assert repo.repo_url == '[email protected]:pre-commit/pre-commit-hooks'
65+
66+
67+
def test_sha(mock_repo_config):
68+
repo = Repository(mock_repo_config)
69+
assert repo.sha == '5e713f8878b7d100c0e059f8cc34be4fc2e8f897'
70+
71+
72+
@pytest.mark.integration
73+
def test_languages(config_for_python_pre_commit_git_repo):
74+
repo = Repository(config_for_python_pre_commit_git_repo)
75+
assert repo.languages == set(['python'])
76+

0 commit comments

Comments
 (0)