mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Code cleanup and tests
This commit is contained in:
parent
5f392f0ba5
commit
9b92f96ed0
7 changed files with 92 additions and 21 deletions
|
|
@ -20,7 +20,9 @@ before_install:
|
||||||
- git --version
|
- git --version
|
||||||
after_success:
|
after_success:
|
||||||
- coveralls
|
- coveralls
|
||||||
sudo: false
|
sudo: required
|
||||||
|
services:
|
||||||
|
- docker
|
||||||
cache:
|
cache:
|
||||||
directories:
|
directories:
|
||||||
- $HOME/.cache/pip
|
- $HOME/.cache/pip
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
## Local development
|
## Local development
|
||||||
|
|
||||||
- The tests depend on having at least the following installed (possibly not
|
- The complete test suite depends on having at least the following installed (possibly not
|
||||||
a complete list)
|
a complete list)
|
||||||
- git (A sufficiently newer version is required to run pre-push tests)
|
- git (A sufficiently newer version is required to run pre-push tests)
|
||||||
- python
|
- python
|
||||||
|
|
@ -10,6 +10,7 @@
|
||||||
- python3.5 (Required by a test which checks different python versions)
|
- python3.5 (Required by a test which checks different python versions)
|
||||||
- tox (or virtualenv)
|
- tox (or virtualenv)
|
||||||
- ruby + gem
|
- ruby + gem
|
||||||
|
- docker
|
||||||
|
|
||||||
### Setting up an environemnt
|
### Setting up an environemnt
|
||||||
|
|
||||||
|
|
@ -63,7 +64,7 @@ function_call(
|
||||||
```
|
```
|
||||||
|
|
||||||
Some notable features:
|
Some notable features:
|
||||||
- The intial parenthese is at the end of the line
|
- The initial parenthesis is at the end of the line
|
||||||
- Parameters are indented one indentation level further than the function name
|
- Parameters are indented one indentation level further than the function name
|
||||||
- The last parameter contains a trailing comma (This helps make `git blame`
|
- The last parameter contains a trailing comma (This helps make `git blame`
|
||||||
more accurate and reduces merge conflicts when adding / removing parameters).
|
more accurate and reduces merge conflicts when adding / removing parameters).
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,10 @@
|
||||||
|
from __future__ import absolute_import
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import hashlib
|
import hashlib
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
from pre_commit import five
|
||||||
from pre_commit.languages import helpers
|
from pre_commit.languages import helpers
|
||||||
from pre_commit.util import clean_path_on_failure
|
from pre_commit.util import clean_path_on_failure
|
||||||
from pre_commit.util import mkdirp
|
from pre_commit.util import mkdirp
|
||||||
|
|
@ -10,12 +12,11 @@ from pre_commit.xargs import xargs
|
||||||
|
|
||||||
|
|
||||||
ENVIRONMENT_DIR = 'docker'
|
ENVIRONMENT_DIR = 'docker'
|
||||||
|
PRE_COMMIT_LABEL = 'PRE_COMMIT'
|
||||||
|
|
||||||
|
|
||||||
def md5(s):
|
def md5(s):
|
||||||
m = hashlib.md5()
|
return five.to_text(hashlib.md5(s).hexdigest())
|
||||||
m.update(s)
|
|
||||||
return m.hexdigest()
|
|
||||||
|
|
||||||
|
|
||||||
def docker_tag(repo_cmd_runner):
|
def docker_tag(repo_cmd_runner):
|
||||||
|
|
@ -24,39 +25,62 @@ def docker_tag(repo_cmd_runner):
|
||||||
).lower()
|
).lower()
|
||||||
|
|
||||||
|
|
||||||
|
def docker_is_running():
|
||||||
|
return xargs(('docker',), ['ps'])[0] == 0
|
||||||
|
|
||||||
|
|
||||||
|
def assert_docker_available():
|
||||||
|
assert docker_is_running(), (
|
||||||
|
'Docker is either not running or not configured in this environment'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def build_docker_image(repo_cmd_runner):
|
||||||
|
cmd = (
|
||||||
|
'docker', 'build', '--pull',
|
||||||
|
'--tag', docker_tag(repo_cmd_runner),
|
||||||
|
'--label', PRE_COMMIT_LABEL,
|
||||||
|
'.'
|
||||||
|
)
|
||||||
|
helpers.run_setup_cmd(repo_cmd_runner, cmd)
|
||||||
|
|
||||||
|
|
||||||
def install_environment(
|
def install_environment(
|
||||||
repo_cmd_runner,
|
repo_cmd_runner,
|
||||||
version='default',
|
version='default',
|
||||||
additional_dependencies=(),
|
additional_dependencies=(),
|
||||||
):
|
):
|
||||||
assert repo_cmd_runner.exists('Dockerfile')
|
assert repo_cmd_runner.exists('Dockerfile'), (
|
||||||
# I don't know of anybody trying to juggle multiple docker installations
|
'No Dockerfile was found in the hook repository'
|
||||||
# so this seems sufficient
|
)
|
||||||
|
assert version == 'default', (
|
||||||
|
'Pre-commit does not support language_version for docker '
|
||||||
|
)
|
||||||
|
assert_docker_available()
|
||||||
|
|
||||||
directory = helpers.environment_dir(ENVIRONMENT_DIR, 'default')
|
directory = helpers.environment_dir(ENVIRONMENT_DIR, 'default')
|
||||||
mkdirp(os.path.join(repo_cmd_runner.path(), directory))
|
mkdirp(os.path.join(repo_cmd_runner.path(), directory))
|
||||||
|
|
||||||
cmd = (
|
|
||||||
'docker', 'build', '--pull',
|
|
||||||
'--tag', docker_tag(repo_cmd_runner),
|
|
||||||
'.'
|
|
||||||
)
|
|
||||||
|
|
||||||
# Docker doesn't really have relevant disk environment, but pre-commit
|
# Docker doesn't really have relevant disk environment, but pre-commit
|
||||||
# still needs to cleanup it's state files on failure
|
# still needs to cleanup it's state files on failure
|
||||||
env_dir = repo_cmd_runner.path(directory)
|
env_dir = repo_cmd_runner.path(directory)
|
||||||
with clean_path_on_failure(env_dir):
|
with clean_path_on_failure(env_dir):
|
||||||
helpers.run_setup_cmd(repo_cmd_runner, cmd)
|
build_docker_image(repo_cmd_runner)
|
||||||
|
|
||||||
|
|
||||||
def run_hook(repo_cmd_runner, hook, file_args):
|
def run_hook(repo_cmd_runner, hook, file_args):
|
||||||
|
assert_docker_available()
|
||||||
|
# Rebuild the docker image in case it has gone missing, as many people do
|
||||||
|
# automated cleanup of docker images.
|
||||||
|
build_docker_image(repo_cmd_runner)
|
||||||
|
# the docker lib doesn't return stdout on non-zero exit codes,
|
||||||
|
# so we run the container directly on the command line
|
||||||
cmd = (
|
cmd = (
|
||||||
'docker', 'run',
|
'docker', 'run',
|
||||||
'-t',
|
'--rm',
|
||||||
'-v', '{}:/src'.format(os.getcwd()),
|
'-v', '{}:/src'.format(os.getcwd()),
|
||||||
'--workdir', '/src',
|
'--workdir', '/src',
|
||||||
|
'--entrypoint', hook['entry'],
|
||||||
docker_tag(repo_cmd_runner)
|
docker_tag(repo_cmd_runner)
|
||||||
)
|
)
|
||||||
|
return xargs(cmd + tuple(hook['args']), file_args)
|
||||||
return xargs(
|
|
||||||
cmd + (hook['entry'],) + tuple(hook['args']), file_args,
|
|
||||||
)
|
|
||||||
|
|
|
||||||
3
testing/resources/docker_hooks_repo/Dockerfile
Normal file
3
testing/resources/docker_hooks_repo/Dockerfile
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
FROM cogniteev/echo
|
||||||
|
|
||||||
|
CMD ["echo", "This is overwritten by the hooks.yaml 'entry'"]
|
||||||
11
testing/resources/docker_hooks_repo/hooks.yaml
Normal file
11
testing/resources/docker_hooks_repo/hooks.yaml
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
- id: docker-hook
|
||||||
|
name: Docker test hook
|
||||||
|
entry: echo
|
||||||
|
language: docker
|
||||||
|
files: \.txt$
|
||||||
|
|
||||||
|
- id: docker-hook-failing
|
||||||
|
name: Docker test hook with nonzero exit code
|
||||||
|
entry: bork
|
||||||
|
language: docker
|
||||||
|
files: \.txt$
|
||||||
|
|
@ -6,6 +6,7 @@ import shutil
|
||||||
import jsonschema
|
import jsonschema
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
from pre_commit.languages.docker import docker_is_running
|
||||||
from pre_commit.util import cmd_output
|
from pre_commit.util import cmd_output
|
||||||
from pre_commit.util import cwd
|
from pre_commit.util import cwd
|
||||||
|
|
||||||
|
|
@ -57,6 +58,11 @@ def cmd_output_mocked_pre_commit_home(*args, **kwargs):
|
||||||
return cmd_output(*args, env=env, **kwargs)
|
return cmd_output(*args, env=env, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
skipif_cant_run_docker = pytest.mark.skipif(
|
||||||
|
docker_is_running() is False,
|
||||||
|
reason='Docker isn\'t running or can\'t be accessed'
|
||||||
|
)
|
||||||
|
|
||||||
skipif_slowtests_false = pytest.mark.skipif(
|
skipif_slowtests_false = pytest.mark.skipif(
|
||||||
os.environ.get('slowtests') == 'false',
|
os.environ.get('slowtests') == 'false',
|
||||||
reason='slowtests=false',
|
reason='slowtests=false',
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,7 @@ from testing.fixtures import git_dir
|
||||||
from testing.fixtures import make_config_from_repo
|
from testing.fixtures import make_config_from_repo
|
||||||
from testing.fixtures import make_repo
|
from testing.fixtures import make_repo
|
||||||
from testing.fixtures import modify_manifest
|
from testing.fixtures import modify_manifest
|
||||||
|
from testing.util import skipif_cant_run_docker
|
||||||
from testing.util import skipif_slowtests_false
|
from testing.util import skipif_slowtests_false
|
||||||
from testing.util import xfailif_no_pcre_support
|
from testing.util import xfailif_no_pcre_support
|
||||||
from testing.util import xfailif_windows_no_node
|
from testing.util import xfailif_windows_no_node
|
||||||
|
|
@ -129,6 +130,29 @@ def test_versioned_python_hook(tempdir_factory, store):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@skipif_slowtests_false
|
||||||
|
@skipif_cant_run_docker
|
||||||
|
@pytest.mark.integration
|
||||||
|
def test_run_a_docker_hook(tempdir_factory, store):
|
||||||
|
_test_hook_repo(
|
||||||
|
tempdir_factory, store, 'docker_hooks_repo',
|
||||||
|
'docker-hook',
|
||||||
|
['Hello World from docker'], b'Hello World from docker\n',
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@skipif_slowtests_false
|
||||||
|
@skipif_cant_run_docker
|
||||||
|
@pytest.mark.integration
|
||||||
|
def test_run_a_failing_docker_hook(tempdir_factory, store):
|
||||||
|
_test_hook_repo(
|
||||||
|
tempdir_factory, store, 'docker_hooks_repo',
|
||||||
|
'docker-hook-failing',
|
||||||
|
['Hello World from docker'], b'',
|
||||||
|
expected_return_code=1
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@skipif_slowtests_false
|
@skipif_slowtests_false
|
||||||
@xfailif_windows_no_node
|
@xfailif_windows_no_node
|
||||||
@pytest.mark.integration
|
@pytest.mark.integration
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue