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

Skip to content

Commit 47bad12

Browse files
committed
Made env generation polymorphic
1 parent 5ca8f4f commit 47bad12

2 files changed

Lines changed: 53 additions & 20 deletions

File tree

pre_commit/repository.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,27 @@
88
from pre_commit.util import cached_property
99

1010

11+
def install_python(repo):
12+
assert local.path('setup.py').exists()
13+
local['virtualenv']['py_env']()
14+
local['bash']['-c', 'source py_env/bin/activate && pip install .']()
15+
16+
17+
def install_ruby(repo):
18+
raise NotImplementedError
19+
20+
21+
def install_node(repo):
22+
raise NotImplementedError
23+
24+
25+
language_to_repo_setup_strategy = {
26+
'python': install_python,
27+
'ruby': install_ruby,
28+
'node': install_node,
29+
}
30+
31+
1132
class Repository(object):
1233
def __init__(self, repo_config):
1334
self.repo_config = repo_config
@@ -59,25 +80,8 @@ def create(self):
5980
with self.in_checkout():
6081
local['git']['checkout', self.sha]()
6182

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-
7583
def install(self):
76-
# Create if we have not already
77-
self.create()
78-
# TODO: need to take in the config here and determine if we actually
79-
# need to run any installers (and what languages to install)
8084
with self.in_checkout():
81-
if local.path('setup.py').exists():
82-
local['virtualenv']['py_env']()
83-
local['bash']['-c', 'source py_env/bin/activate && pip install .']()
85+
for language in C.SUPPORTED_LANGUAGES:
86+
if language in self.languages:
87+
language_to_repo_setup_strategy[language](self)

tests/util_test.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
import pytest
3+
import time
4+
5+
from pre_commit.util import cached_property
6+
7+
8+
@pytest.fixture
9+
def class_with_cached_property():
10+
class Foo(object):
11+
@cached_property
12+
def foo(self):
13+
return "Foo" + str(time.time())
14+
15+
return Foo
16+
17+
18+
def test_cached_property(class_with_cached_property):
19+
instance = class_with_cached_property()
20+
val = instance.foo
21+
val2 = instance.foo
22+
assert val is val2
23+
24+
25+
def test_unbound_cached_property(class_with_cached_property):
26+
# Make sure we don't blow up when accessing the property unbound
27+
prop = class_with_cached_property.foo
28+
assert isinstance(prop, cached_property)
29+

0 commit comments

Comments
 (0)