From 4b43fd8cdc1b7885bf975247ad195747c628e94f Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Mon, 16 Jun 2014 08:20:47 -0700 Subject: [PATCH] Add integration test for existing install behaviour --- pre_commit/store.py | 5 +++- testing/fixtures.py | 3 +++ tests/commands/install_test.py | 45 ++++++++++++++++++++++++++++++++++ tests/store_test.py | 8 ++++++ 4 files changed, 60 insertions(+), 1 deletion(-) diff --git a/pre_commit/store.py b/pre_commit/store.py index c35bcbae..c4942919 100644 --- a/pre_commit/store.py +++ b/pre_commit/store.py @@ -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): diff --git a/testing/fixtures.py b/testing/fixtures.py index 2fa5f535..3bf18273 100644 --- a/testing/fixtures.py +++ b/testing/fixtures.py @@ -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 diff --git a/tests/commands/install_test.py b/tests/commands/install_test.py index 53965e6e..aa6bd8b9 100644 --- a/tests/commands/install_test.py +++ b/tests/commands/install_test.py @@ -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) diff --git a/tests/store_test.py b/tests/store_test.py index 5ebfe05d..bc8cee6e 100644 --- a/tests/store_test.py +++ b/tests/store_test.py @@ -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()