mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
moved the installing code to its own package
This commit is contained in:
parent
320bb0a679
commit
1d8af9ce50
7 changed files with 167 additions and 152 deletions
|
|
@ -1,6 +1,5 @@
|
||||||
import os
|
import os
|
||||||
import pkg_resources
|
import pkg_resources
|
||||||
import contextlib
|
|
||||||
|
|
||||||
import pre_commit.constants as C
|
import pre_commit.constants as C
|
||||||
from plumbum import local
|
from plumbum import local
|
||||||
|
|
@ -9,7 +8,6 @@ from plumbum import local
|
||||||
def get_root():
|
def get_root():
|
||||||
return local['git']['rev-parse', '--show-toplevel']().strip()
|
return local['git']['rev-parse', '--show-toplevel']().strip()
|
||||||
|
|
||||||
|
|
||||||
def get_pre_commit_path():
|
def get_pre_commit_path():
|
||||||
return os.path.join(get_root(), '.git/hooks/pre-commit')
|
return os.path.join(get_root(), '.git/hooks/pre-commit')
|
||||||
|
|
||||||
|
|
@ -30,40 +28,4 @@ def remove_pre_commit():
|
||||||
local.path(get_pre_commit_path()).delete()
|
local.path(get_pre_commit_path()).delete()
|
||||||
|
|
||||||
|
|
||||||
class PreCommitProject(object):
|
|
||||||
|
|
||||||
def __init__(self, git_repo_path, sha):
|
|
||||||
self.git_repo_path = git_repo_path
|
|
||||||
self.sha = sha
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
|
||||||
def in_checkout(self):
|
|
||||||
with local.cwd(get_pre_commit_dir_path()):
|
|
||||||
with local.cwd(self.sha):
|
|
||||||
yield
|
|
||||||
|
|
||||||
def create(self):
|
|
||||||
create_pre_commit_package_dir()
|
|
||||||
|
|
||||||
with local.cwd(get_pre_commit_dir_path()):
|
|
||||||
local['git']['clone', self.git_repo_path, self.sha]()
|
|
||||||
with self.in_checkout():
|
|
||||||
local['git']['checkout', self.sha]()
|
|
||||||
|
|
||||||
def install(self):
|
|
||||||
with self.in_checkout():
|
|
||||||
if local.path('setup.py').exists():
|
|
||||||
local['virtualenv']['py_env']()
|
|
||||||
local['bash']['-c', 'source py_env/bin/activate && pip install .']()
|
|
||||||
print local.cwd.getpath()
|
|
||||||
|
|
||||||
def create_repo_in_env(git_repo_path, sha):
|
|
||||||
project = PreCommitProject(git_repo_path, sha)
|
|
||||||
project.create()
|
|
||||||
|
|
||||||
def install_pre_commit(git_repo_path, sha):
|
|
||||||
project = PreCommitProject(git_repo_path, sha)
|
|
||||||
project.create()
|
|
||||||
project.install()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
0
pre_commit/installer/__init__.py
Normal file
0
pre_commit/installer/__init__.py
Normal file
44
pre_commit/installer/repo_installer.py
Normal file
44
pre_commit/installer/repo_installer.py
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
import contextlib
|
||||||
|
|
||||||
|
from plumbum import local
|
||||||
|
from pre_commit import git
|
||||||
|
|
||||||
|
class RepoInstaller(object):
|
||||||
|
|
||||||
|
def __init__(self, git_repo_path, sha):
|
||||||
|
self.git_repo_path = git_repo_path
|
||||||
|
self.sha = sha
|
||||||
|
|
||||||
|
@contextlib.contextmanager
|
||||||
|
def in_checkout(self):
|
||||||
|
with local.cwd(git.get_pre_commit_dir_path()):
|
||||||
|
with local.cwd(self.sha):
|
||||||
|
yield
|
||||||
|
|
||||||
|
def create(self):
|
||||||
|
git.create_pre_commit_package_dir()
|
||||||
|
|
||||||
|
with local.cwd(git.get_pre_commit_dir_path()):
|
||||||
|
if local.path(self.sha).exists():
|
||||||
|
# Project already exists, no reason to re-create it
|
||||||
|
return
|
||||||
|
|
||||||
|
local['git']['clone', self.git_repo_path, self.sha]()
|
||||||
|
with self.in_checkout():
|
||||||
|
local['git']['checkout', self.sha]()
|
||||||
|
|
||||||
|
def install(self):
|
||||||
|
with self.in_checkout():
|
||||||
|
if local.path('setup.py').exists():
|
||||||
|
local['virtualenv']['py_env']()
|
||||||
|
local['bash']['-c', 'source py_env/bin/activate && pip install .']()
|
||||||
|
|
||||||
|
|
||||||
|
def create_repo_in_env(git_repo_path, sha):
|
||||||
|
project = RepoInstaller(git_repo_path, sha)
|
||||||
|
project.create()
|
||||||
|
|
||||||
|
def install_pre_commit(git_repo_path, sha):
|
||||||
|
project = RepoInstaller(git_repo_path, sha)
|
||||||
|
project.create()
|
||||||
|
project.install()
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
import pre_commit.constants as C
|
||||||
from plumbum import local
|
from plumbum import local
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -8,3 +9,72 @@ def empty_git_dir(tmpdir):
|
||||||
with local.cwd(tmpdir.strpath):
|
with local.cwd(tmpdir.strpath):
|
||||||
local['git']['init']()
|
local['git']['init']()
|
||||||
yield tmpdir.strpath
|
yield tmpdir.strpath
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def add_and_commit():
|
||||||
|
local['git']['add', '.']()
|
||||||
|
local['git']['commit', '-m', 'random commit', '--author', 'A U Thor <author@example.com>']()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.yield_fixture
|
||||||
|
def dummy_git_repo(empty_git_dir):
|
||||||
|
local['touch']['dummy']()
|
||||||
|
add_and_commit()
|
||||||
|
|
||||||
|
yield empty_git_dir
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.yield_fixture
|
||||||
|
def dummy_pre_commit_hooks_git_repo(dummy_git_repo):
|
||||||
|
local.path(C.MANIFEST_FILE).write("""
|
||||||
|
hooks:
|
||||||
|
-
|
||||||
|
id: foo
|
||||||
|
name: Foo
|
||||||
|
entry: foo
|
||||||
|
language: python>2.6
|
||||||
|
""")
|
||||||
|
|
||||||
|
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(
|
||||||
|
"""
|
||||||
|
from setuptools import find_packages
|
||||||
|
from setuptools import setup
|
||||||
|
|
||||||
|
setup(
|
||||||
|
name='Foo',
|
||||||
|
version='0.0.0',
|
||||||
|
packages=find_packages('.'),
|
||||||
|
entry_points={
|
||||||
|
'console_scripts': [
|
||||||
|
'entry = 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():
|
||||||
|
return 0
|
||||||
|
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
add_and_commit()
|
||||||
|
|
||||||
|
yield dummy_pre_commit_hooks_git_repo
|
||||||
|
|
|
||||||
|
|
@ -1,85 +1,9 @@
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import pytest
|
|
||||||
|
|
||||||
from plumbum import local
|
from plumbum import local
|
||||||
from pre_commit import git
|
from pre_commit import git
|
||||||
|
|
||||||
import pre_commit.constants as C
|
|
||||||
|
|
||||||
def add_and_commit():
|
|
||||||
local['git']['add', '.']()
|
|
||||||
local['git']['config', 'user.email', 'ken@struys.ca']
|
|
||||||
local['git']['config', 'user.name', 'Ken Struys']
|
|
||||||
local['git']['commit', '-m', 'random commit']()
|
|
||||||
|
|
||||||
|
|
||||||
def get_sha(git_repo):
|
|
||||||
with local.cwd(git_repo):
|
|
||||||
return (local['git']['log', '--format="%H"'] | local['head']['-n1'])().strip('"\n')
|
|
||||||
|
|
||||||
@pytest.yield_fixture
|
|
||||||
def dummy_git_repo(empty_git_dir):
|
|
||||||
local['touch']['dummy']()
|
|
||||||
add_and_commit()
|
|
||||||
|
|
||||||
yield empty_git_dir
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.yield_fixture
|
|
||||||
def dummy_pre_commit_hooks_git_repo(dummy_git_repo):
|
|
||||||
local.path(C.MANIFEST_FILE).write("""
|
|
||||||
hooks:
|
|
||||||
-
|
|
||||||
id: foo
|
|
||||||
name: Foo
|
|
||||||
entry: foo
|
|
||||||
language: python>2.6
|
|
||||||
""")
|
|
||||||
|
|
||||||
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(
|
|
||||||
"""
|
|
||||||
from setuptools import find_packages
|
|
||||||
from setuptools import setup
|
|
||||||
|
|
||||||
setup(
|
|
||||||
name='Foo',
|
|
||||||
version='0.0.0',
|
|
||||||
packages=find_packages('.'),
|
|
||||||
entry_points={
|
|
||||||
'console_scripts': [
|
|
||||||
'entry = 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():
|
|
||||||
return 0
|
|
||||||
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
|
|
||||||
add_and_commit()
|
|
||||||
|
|
||||||
yield dummy_pre_commit_hooks_git_repo
|
|
||||||
|
|
||||||
|
|
||||||
def test_get_root(empty_git_dir):
|
def test_get_root(empty_git_dir):
|
||||||
assert git.get_root() == empty_git_dir
|
assert git.get_root() == empty_git_dir
|
||||||
|
|
@ -111,40 +35,3 @@ def test_remove_pre_commit(empty_git_dir):
|
||||||
assert not os.path.exists(git.get_pre_commit_path())
|
assert not os.path.exists(git.get_pre_commit_path())
|
||||||
|
|
||||||
|
|
||||||
def test_create_repo_in_env(empty_git_dir, dummy_git_repo):
|
|
||||||
sha = get_sha(dummy_git_repo)
|
|
||||||
git.create_repo_in_env(dummy_git_repo, sha)
|
|
||||||
assert os.path.exists(os.path.join(dummy_git_repo, C.PRE_COMMIT_DIR, sha))
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.integration
|
|
||||||
def test_install_python_repo_in_env(empty_git_dir, python_pre_commit_git_repo):
|
|
||||||
sha = get_sha(python_pre_commit_git_repo)
|
|
||||||
git.install_pre_commit(python_pre_commit_git_repo, sha)
|
|
||||||
assert os.path.exists(os.path.join(python_pre_commit_git_repo, C.PRE_COMMIT_DIR, sha, 'py_env'))
|
|
||||||
|
|
||||||
@pytest.mark.integration
|
|
||||||
def test_install_config(empty_git_dir, python_pre_commit_git_repo):
|
|
||||||
|
|
||||||
|
|
||||||
config = [
|
|
||||||
{
|
|
||||||
'repo': python_pre_commit_git_repo,
|
|
||||||
'sha': get_sha(python_pre_commit_git_repo),
|
|
||||||
'hooks': [
|
|
||||||
{
|
|
||||||
'id': 'foo',
|
|
||||||
'args': [
|
|
||||||
{
|
|
||||||
'type': 'files',
|
|
||||||
'opt': '*.py'
|
|
||||||
},
|
|
||||||
]
|
|
||||||
}
|
|
||||||
],
|
|
||||||
},
|
|
||||||
]
|
|
||||||
for repo in config:
|
|
||||||
git.install_pre_commit(repo['repo'], repo['sha'])
|
|
||||||
|
|
||||||
print python_pre_commit_git_repo
|
|
||||||
0
tests/installer/__init__.py
Normal file
0
tests/installer/__init__.py
Normal file
52
tests/installer/repo_installer_test.py
Normal file
52
tests/installer/repo_installer_test.py
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
import pytest
|
||||||
|
import os
|
||||||
|
|
||||||
|
import pre_commit.constants as C
|
||||||
|
from plumbum import local
|
||||||
|
from pre_commit.installer.repo_installer import create_repo_in_env
|
||||||
|
from pre_commit.installer.repo_installer import install_pre_commit
|
||||||
|
|
||||||
|
|
||||||
|
def get_sha(git_repo):
|
||||||
|
with local.cwd(git_repo):
|
||||||
|
return (local['git']['log', '--format="%H"'] | local['head']['-n1'])().strip('"\n')
|
||||||
|
|
||||||
|
@pytest.mark.integration
|
||||||
|
def test_create_repo_in_env(empty_git_dir, dummy_git_repo):
|
||||||
|
sha = get_sha(dummy_git_repo)
|
||||||
|
create_repo_in_env(dummy_git_repo, sha)
|
||||||
|
|
||||||
|
assert os.path.exists(os.path.join(dummy_git_repo, C.PRE_COMMIT_DIR, sha))
|
||||||
|
|
||||||
|
@pytest.mark.integration
|
||||||
|
def test_install_python_repo_in_env(empty_git_dir, python_pre_commit_git_repo):
|
||||||
|
sha = get_sha(python_pre_commit_git_repo)
|
||||||
|
install_pre_commit(python_pre_commit_git_repo, sha)
|
||||||
|
|
||||||
|
assert os.path.exists(os.path.join(python_pre_commit_git_repo, C.PRE_COMMIT_DIR, sha, 'py_env'))
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.integration
|
||||||
|
def test_install_config(empty_git_dir, python_pre_commit_git_repo):
|
||||||
|
|
||||||
|
config = [
|
||||||
|
{
|
||||||
|
'repo': python_pre_commit_git_repo,
|
||||||
|
'sha': get_sha(python_pre_commit_git_repo),
|
||||||
|
'hooks': [
|
||||||
|
{
|
||||||
|
'id': 'foo',
|
||||||
|
'args': [
|
||||||
|
{
|
||||||
|
'type': 'files',
|
||||||
|
'opt': '*.py'
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
},
|
||||||
|
]
|
||||||
|
for repo in config:
|
||||||
|
install_pre_commit(repo['repo'], repo['sha'])
|
||||||
|
|
||||||
|
assert os.path.exists(os.path.join(python_pre_commit_git_repo, C.PRE_COMMIT_DIR, config[0]['sha'], 'py_env'))
|
||||||
Loading…
Add table
Add a link
Reference in a new issue