Add integration test for existing install behaviour

This commit is contained in:
Anthony Sottile
2014-06-16 08:20:47 -07:00
parent 332e8b480b
commit 4b43fd8cdc
4 changed files with 60 additions and 1 deletions

View File

@@ -22,7 +22,10 @@ def _get_default_directory():
`Store.get_default_directory` can be mocked in tests and
`_get_default_directory` can be tested.
"""
return os.path.join(os.environ['HOME'], '.pre-commit')
return os.environ.get(
'PRE_COMMIT_HOME',
os.path.join(os.environ['HOME'], '.pre-commit'),
)
class Store(object):

View File

@@ -66,4 +66,7 @@ def make_consuming_repo(tmpdir_factory, repo_source):
config = make_config_from_repo(path)
git_path = git_dir(tmpdir_factory)
write_config(git_path, config)
with local.cwd(git_path):
git('add', C.CONFIG_FILE)
git('commit', '-m', 'Add hooks config')
return git_path

View File

@@ -4,12 +4,38 @@ from __future__ import unicode_literals
import io
import os
import os.path
import re
import pkg_resources
import subprocess
import stat
from plumbum import local
from pre_commit.commands.install import install
from pre_commit.runner import Runner
from testing.fixtures import git_dir
from testing.fixtures import make_consuming_repo
def _get_commit_output(tmpdir_factory):
# Don't want to write to home directory
env = dict(os.environ, **{'PRE_COMMIT_HOME': tmpdir_factory.get()})
return local['git'](
'commit', '-m', 'Commit!', '--allow-empty',
# git commit puts pre-commit to stderr
stderr=subprocess.STDOUT,
env=env,
)
NORMAL_PRE_COMMIT_RUN = re.compile(
r'^\[INFO\] Installing environment for .+.\n'
r'\[INFO\] Once installed this environment will be reused.\n'
r'\[INFO\] This may take a few minutes...\n'
r'Bash hook'
r'\.+'
r'\(no files to check\) Skipped\n'
r'\[master [a-f0-9]{7}\] Commit!\n$'
)
def test_install_pre_commit(tmpdir_factory):
@@ -26,3 +52,22 @@ def test_install_pre_commit(tmpdir_factory):
assert pre_commit_contents == expected_contents
stat_result = os.stat(runner.pre_commit_path)
assert stat_result.st_mode & (stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
def test_install_pre_commit_and_run(tmpdir_factory):
path = make_consuming_repo(tmpdir_factory, 'script_hooks_repo')
with local.cwd(path):
assert install(Runner(path)) == 0
output = _get_commit_output(tmpdir_factory)
assert NORMAL_PRE_COMMIT_RUN.match(output)
def test_install_idempotent(tmpdir_factory):
path = make_consuming_repo(tmpdir_factory, 'script_hooks_repo')
with local.cwd(path):
assert install(Runner(path)) == 0
assert install(Runner(path)) == 0
output = _get_commit_output(tmpdir_factory)
assert NORMAL_PRE_COMMIT_RUN.match(output)

View File

@@ -31,6 +31,14 @@ def test_get_default_directory_defaults_to_home():
assert ret == os.path.join(os.environ['HOME'], '.pre-commit')
def test_uses_environment_variable_when_present():
with mock.patch.dict(
os.environ, {'PRE_COMMIT_HOME': '/tmp/pre_commit_home'}
):
ret = _get_default_directory()
assert ret == '/tmp/pre_commit_home'
def test_store_require_created(store):
assert not os.path.exists(store.directory)
store.require_created()