mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Refactored how the installer works
This commit is contained in:
parent
8b0247e17f
commit
abea886a3d
8 changed files with 105 additions and 70 deletions
|
|
@ -1,8 +1,14 @@
|
|||
from __future__ import absolute_import
|
||||
|
||||
import jsonschema
|
||||
import pytest
|
||||
import pre_commit.constants as C
|
||||
import time
|
||||
from plumbum import local
|
||||
|
||||
import pre_commit.constants as C
|
||||
from pre_commit import git
|
||||
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
|
||||
|
||||
|
||||
@pytest.yield_fixture
|
||||
def empty_git_dir(tmpdir):
|
||||
|
|
@ -11,10 +17,9 @@ def empty_git_dir(tmpdir):
|
|||
yield tmpdir.strpath
|
||||
|
||||
|
||||
|
||||
def add_and_commit():
|
||||
local['git']['add', '.']()
|
||||
local['git']['commit', '-m', 'random commit']()
|
||||
local['git']['commit', '-m', 'random commit {0}'.format(time.time())]()
|
||||
|
||||
|
||||
@pytest.yield_fixture
|
||||
|
|
@ -42,8 +47,7 @@ hooks:
|
|||
|
||||
@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 setup
|
||||
|
||||
|
|
@ -53,7 +57,7 @@ setup(
|
|||
packages=find_packages('.'),
|
||||
entry_points={
|
||||
'console_scripts': [
|
||||
'entry = foo.main:func'
|
||||
'foo = foo.main:func'
|
||||
],
|
||||
}
|
||||
)
|
||||
|
|
@ -66,15 +70,28 @@ setup(
|
|||
|
||||
with local.cwd(foo_module):
|
||||
local.path('__init__.py').write('')
|
||||
local.path('main.py').write(
|
||||
"""
|
||||
|
||||
local.path('main.py').write("""
|
||||
def func():
|
||||
return 0
|
||||
|
||||
"""
|
||||
)
|
||||
|
||||
add_and_commit()
|
||||
|
||||
yield dummy_pre_commit_hooks_git_repo
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def config_for_python_pre_commit_git_repo(python_pre_commit_git_repo):
|
||||
config = {
|
||||
'repo': python_pre_commit_git_repo,
|
||||
'sha': git.get_head_sha(python_pre_commit_git_repo),
|
||||
'hooks': [{
|
||||
'id': 'foo',
|
||||
'files': '*.py',
|
||||
}],
|
||||
}
|
||||
|
||||
jsonschema.validate([config], CONFIG_JSON_SCHEMA)
|
||||
|
||||
return config
|
||||
|
|
@ -16,7 +16,9 @@ def test_get_root(empty_git_dir):
|
|||
|
||||
|
||||
def test_get_pre_commit_path(empty_git_dir):
|
||||
assert git.get_pre_commit_path() == '{0}/.git/hooks/pre-commit'.format(empty_git_dir)
|
||||
assert git.get_pre_commit_path() == '{0}/.git/hooks/pre-commit'.format(
|
||||
empty_git_dir,
|
||||
)
|
||||
|
||||
|
||||
def test_create_pre_commit(empty_git_dir):
|
||||
|
|
|
|||
0
tests/hooks_workspace_test.py
Normal file
0
tests/hooks_workspace_test.py
Normal file
|
|
@ -2,31 +2,46 @@ import os
|
|||
|
||||
import jsonschema
|
||||
import pytest
|
||||
from plumbum import local
|
||||
from pre_commit import git
|
||||
|
||||
import pre_commit.constants as C
|
||||
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
|
||||
from pre_commit.repo_installer import create_repo_in_env
|
||||
from pre_commit.repo_installer import install_pre_commit
|
||||
from pre_commit.repo_installer import RepoInstaller
|
||||
|
||||
|
||||
def get_sha(git_repo):
|
||||
with local.cwd(git_repo):
|
||||
return (local['git']['log', '--format="%H"'] | local['head']['-n1'])().strip('"\n')
|
||||
@pytest.fixture
|
||||
def dummy_repo_config(dummy_git_repo):
|
||||
# This is not a valid config, but it is pretty close
|
||||
return {
|
||||
'repo': dummy_git_repo,
|
||||
'sha': git.get_head_sha(dummy_git_repo),
|
||||
'hooks': [],
|
||||
}
|
||||
|
||||
|
||||
@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)
|
||||
def test_create_repo_in_env(dummy_repo_config, dummy_git_repo):
|
||||
repo_installer = RepoInstaller(dummy_repo_config)
|
||||
repo_installer.create()
|
||||
|
||||
assert os.path.exists(os.path.join(dummy_git_repo, C.PRE_COMMIT_DIR, sha))
|
||||
assert os.path.exists(
|
||||
os.path.join(dummy_git_repo, C.HOOKS_WORKSPACE, repo_installer.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)
|
||||
def test_install_python_repo_in_env(python_pre_commit_git_repo, config_for_python_pre_commit_git_repo):
|
||||
repo_installer = RepoInstaller(config_for_python_pre_commit_git_repo)
|
||||
# TODO: do we need create here?
|
||||
repo_installer.install()
|
||||
|
||||
assert os.path.exists(os.path.join(python_pre_commit_git_repo, C.PRE_COMMIT_DIR, sha, 'py_env'))
|
||||
assert os.path.exists(
|
||||
os.path.join(
|
||||
python_pre_commit_git_repo,
|
||||
C.HOOKS_WORKSPACE,
|
||||
repo_installer.sha,
|
||||
'py_env',
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
|
@ -34,28 +49,14 @@ def simple_config(python_pre_commit_git_repo):
|
|||
config = [
|
||||
{
|
||||
'repo': python_pre_commit_git_repo,
|
||||
'sha': get_sha(python_pre_commit_git_repo),
|
||||
'sha': git.get_head_sha(python_pre_commit_git_repo),
|
||||
'hooks': [
|
||||
{
|
||||
'id': 'foo',
|
||||
'files': '*.py',
|
||||
}
|
||||
},
|
||||
],
|
||||
},
|
||||
]
|
||||
jsonschema.validate(config, CONFIG_JSON_SCHEMA)
|
||||
return config
|
||||
|
||||
|
||||
@pytest.mark.integration
|
||||
def test_install_config(empty_git_dir, python_pre_commit_git_repo, simple_config):
|
||||
for repo in simple_config:
|
||||
install_pre_commit(repo['repo'], repo['sha'])
|
||||
|
||||
assert os.path.exists(
|
||||
os.path.join(
|
||||
python_pre_commit_git_repo,
|
||||
C.PRE_COMMIT_DIR, simple_config[0]['sha'],
|
||||
'py_env',
|
||||
),
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue