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

Skip to content

Commit abea886

Browse files
committed
Refactored how the installer works
1 parent 8b0247e commit abea886

8 files changed

Lines changed: 105 additions & 70 deletions

File tree

pre_commit/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
CONFIG_FILE = '.pre-commit-config.yaml'
33

4-
PRE_COMMIT_DIR = '.pre-commit-files'
4+
HOOKS_WORKSPACE = '.pre-commit-files'
55

66
MANIFEST_FILE = 'manifest.yaml'
77

pre_commit/git.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
import os
22
import pkg_resources
33

4-
import pre_commit.constants as C
54
from plumbum import local
65

76

7+
# TODO: optimization: memoize based on local.cwd.getpath()
88
def get_root():
99
return local['git']['rev-parse', '--show-toplevel']().strip()
1010

11+
1112
def get_pre_commit_path():
1213
return os.path.join(get_root(), '.git/hooks/pre-commit')
1314

1415

15-
def get_pre_commit_dir_path():
16-
return os.path.join(get_root(), C.PRE_COMMIT_DIR)
17-
18-
def create_pre_commit_package_dir():
19-
local.path(get_pre_commit_dir_path()).mkdir()
20-
2116
def create_pre_commit():
2217
path = get_pre_commit_path()
2318
pre_commit_file = pkg_resources.resource_filename('pre_commit', 'resources/pre-commit.sh')
@@ -28,4 +23,6 @@ def remove_pre_commit():
2823
local.path(get_pre_commit_path()).delete()
2924

3025

31-
26+
def get_head_sha(git_repo_path):
27+
with local.cwd(git_repo_path):
28+
return (local['git']['rev-parse', 'HEAD'])().strip()

pre_commit/hooks_workspace.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
import contextlib
3+
import os.path
4+
from plumbum import local
5+
6+
import pre_commit.constants as C
7+
from pre_commit import git
8+
9+
10+
def get_pre_commit_dir_path():
11+
return os.path.join(git.get_root(), C.HOOKS_WORKSPACE)
12+
13+
@contextlib.contextmanager
14+
def in_hooks_workspace():
15+
"""Change into the hooks workspace. If it does not exist create it."""
16+
if not os.path.exists(get_pre_commit_dir_path()):
17+
local.path(get_pre_commit_dir_path()).mkdir()
18+
19+
with local.cwd(get_pre_commit_dir_path()):
20+
yield

pre_commit/repo_installer.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,44 @@
1-
import contextlib
21

2+
import contextlib
33
from plumbum import local
4-
from pre_commit import git
4+
5+
from pre_commit.hooks_workspace import in_hooks_workspace
56

67

78
class RepoInstaller(object):
8-
def __init__(self, git_repo_path, sha):
9-
self.git_repo_path = git_repo_path
10-
self.sha = sha
9+
def __init__(self, repo_config):
10+
self.repo_config = repo_config
11+
12+
@property
13+
def repo_url(self):
14+
return self.repo_config['repo']
15+
16+
@property
17+
def sha(self):
18+
return self.repo_config['sha']
1119

1220
@contextlib.contextmanager
1321
def in_checkout(self):
14-
with local.cwd(git.get_pre_commit_dir_path()):
22+
with in_hooks_workspace():
1523
with local.cwd(self.sha):
1624
yield
1725

1826
def create(self):
19-
git.create_pre_commit_package_dir()
20-
21-
with local.cwd(git.get_pre_commit_dir_path()):
27+
with in_hooks_workspace():
2228
if local.path(self.sha).exists():
2329
# Project already exists, no reason to re-create it
2430
return
2531

26-
local['git']['clone', self.git_repo_path, self.sha]()
32+
local['git']['clone', self.repo_url, self.sha]()
2733
with self.in_checkout():
2834
local['git']['checkout', self.sha]()
2935

3036
def install(self):
37+
# Create if we have not already
38+
self.create()
3139
# TODO: need to take in the config here and determine if we actually
3240
# need to run any installers (and what languages to install)
3341
with self.in_checkout():
3442
if local.path('setup.py').exists():
3543
local['virtualenv']['py_env']()
3644
local['bash']['-c', 'source py_env/bin/activate && pip install .']()
37-
38-
39-
def create_repo_in_env(git_repo_path, sha):
40-
project = RepoInstaller(git_repo_path, sha)
41-
project.create()
42-
43-
def install_pre_commit(git_repo_path, sha):
44-
project = RepoInstaller(git_repo_path, sha)
45-
project.create()
46-
project.install()

tests/conftest.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
from __future__ import absolute_import
12

3+
import jsonschema
24
import pytest
3-
import pre_commit.constants as C
5+
import time
46
from plumbum import local
57

8+
import pre_commit.constants as C
9+
from pre_commit import git
10+
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
11+
612

713
@pytest.yield_fixture
814
def empty_git_dir(tmpdir):
@@ -11,10 +17,9 @@ def empty_git_dir(tmpdir):
1117
yield tmpdir.strpath
1218

1319

14-
1520
def add_and_commit():
1621
local['git']['add', '.']()
17-
local['git']['commit', '-m', 'random commit']()
22+
local['git']['commit', '-m', 'random commit {0}'.format(time.time())]()
1823

1924

2025
@pytest.yield_fixture
@@ -42,8 +47,7 @@ def dummy_pre_commit_hooks_git_repo(dummy_git_repo):
4247

4348
@pytest.yield_fixture
4449
def python_pre_commit_git_repo(dummy_pre_commit_hooks_git_repo):
45-
local.path('setup.py').write(
46-
"""
50+
local.path('setup.py').write("""
4751
from setuptools import find_packages
4852
from setuptools import setup
4953
@@ -53,7 +57,7 @@ def python_pre_commit_git_repo(dummy_pre_commit_hooks_git_repo):
5357
packages=find_packages('.'),
5458
entry_points={
5559
'console_scripts': [
56-
'entry = foo.main:func'
60+
'foo = foo.main:func'
5761
],
5862
}
5963
)
@@ -66,15 +70,28 @@ def python_pre_commit_git_repo(dummy_pre_commit_hooks_git_repo):
6670

6771
with local.cwd(foo_module):
6872
local.path('__init__.py').write('')
69-
local.path('main.py').write(
70-
"""
71-
73+
local.path('main.py').write("""
7274
def func():
7375
return 0
74-
7576
"""
7677
)
7778

7879
add_and_commit()
7980

8081
yield dummy_pre_commit_hooks_git_repo
82+
83+
84+
@pytest.fixture
85+
def config_for_python_pre_commit_git_repo(python_pre_commit_git_repo):
86+
config = {
87+
'repo': python_pre_commit_git_repo,
88+
'sha': git.get_head_sha(python_pre_commit_git_repo),
89+
'hooks': [{
90+
'id': 'foo',
91+
'files': '*.py',
92+
}],
93+
}
94+
95+
jsonschema.validate([config], CONFIG_JSON_SCHEMA)
96+
97+
return config

tests/git_test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ def test_get_root(empty_git_dir):
1616

1717

1818
def test_get_pre_commit_path(empty_git_dir):
19-
assert git.get_pre_commit_path() == '{0}/.git/hooks/pre-commit'.format(empty_git_dir)
19+
assert git.get_pre_commit_path() == '{0}/.git/hooks/pre-commit'.format(
20+
empty_git_dir,
21+
)
2022

2123

2224
def test_create_pre_commit(empty_git_dir):

tests/hooks_workspace_test.py

Whitespace-only changes.

tests/repo_installer_test.py

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,61 @@
22

33
import jsonschema
44
import pytest
5-
from plumbum import local
5+
from pre_commit import git
66

77
import pre_commit.constants as C
88
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
9-
from pre_commit.repo_installer import create_repo_in_env
10-
from pre_commit.repo_installer import install_pre_commit
9+
from pre_commit.repo_installer import RepoInstaller
1110

1211

13-
def get_sha(git_repo):
14-
with local.cwd(git_repo):
15-
return (local['git']['log', '--format="%H"'] | local['head']['-n1'])().strip('"\n')
12+
@pytest.fixture
13+
def dummy_repo_config(dummy_git_repo):
14+
# This is not a valid config, but it is pretty close
15+
return {
16+
'repo': dummy_git_repo,
17+
'sha': git.get_head_sha(dummy_git_repo),
18+
'hooks': [],
19+
}
20+
1621

1722
@pytest.mark.integration
18-
def test_create_repo_in_env(empty_git_dir, dummy_git_repo):
19-
sha = get_sha(dummy_git_repo)
20-
create_repo_in_env(dummy_git_repo, sha)
23+
def test_create_repo_in_env(dummy_repo_config, dummy_git_repo):
24+
repo_installer = RepoInstaller(dummy_repo_config)
25+
repo_installer.create()
2126

22-
assert os.path.exists(os.path.join(dummy_git_repo, C.PRE_COMMIT_DIR, sha))
27+
assert os.path.exists(
28+
os.path.join(dummy_git_repo, C.HOOKS_WORKSPACE, repo_installer.sha),
29+
)
2330

2431
@pytest.mark.integration
25-
def test_install_python_repo_in_env(empty_git_dir, python_pre_commit_git_repo):
26-
sha = get_sha(python_pre_commit_git_repo)
27-
install_pre_commit(python_pre_commit_git_repo, sha)
32+
def test_install_python_repo_in_env(python_pre_commit_git_repo, config_for_python_pre_commit_git_repo):
33+
repo_installer = RepoInstaller(config_for_python_pre_commit_git_repo)
34+
# TODO: do we need create here?
35+
repo_installer.install()
2836

29-
assert os.path.exists(os.path.join(python_pre_commit_git_repo, C.PRE_COMMIT_DIR, sha, 'py_env'))
37+
assert os.path.exists(
38+
os.path.join(
39+
python_pre_commit_git_repo,
40+
C.HOOKS_WORKSPACE,
41+
repo_installer.sha,
42+
'py_env',
43+
),
44+
)
3045

3146

3247
@pytest.fixture
3348
def simple_config(python_pre_commit_git_repo):
3449
config = [
3550
{
3651
'repo': python_pre_commit_git_repo,
37-
'sha': get_sha(python_pre_commit_git_repo),
52+
'sha': git.get_head_sha(python_pre_commit_git_repo),
3853
'hooks': [
3954
{
4055
'id': 'foo',
4156
'files': '*.py',
42-
}
57+
},
4358
],
4459
},
4560
]
4661
jsonschema.validate(config, CONFIG_JSON_SCHEMA)
4762
return config
48-
49-
50-
@pytest.mark.integration
51-
def test_install_config(empty_git_dir, python_pre_commit_git_repo, simple_config):
52-
for repo in simple_config:
53-
install_pre_commit(repo['repo'], repo['sha'])
54-
55-
assert os.path.exists(
56-
os.path.join(
57-
python_pre_commit_git_repo,
58-
C.PRE_COMMIT_DIR, simple_config[0]['sha'],
59-
'py_env',
60-
),
61-
)

0 commit comments

Comments
 (0)