mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-18 08:34:41 +04:00
Remove stateful Runner
This commit is contained in:
parent
1d40cc2104
commit
fe409f1a43
17 changed files with 209 additions and 315 deletions
|
|
@ -13,12 +13,10 @@ from pre_commit.clientlib import load_config
|
|||
from pre_commit.commands.autoupdate import _update_repo
|
||||
from pre_commit.commands.autoupdate import autoupdate
|
||||
from pre_commit.commands.autoupdate import RepositoryCannotBeUpdatedError
|
||||
from pre_commit.runner import Runner
|
||||
from pre_commit.util import cmd_output
|
||||
from testing.auto_namedtuple import auto_namedtuple
|
||||
from testing.fixtures import add_config_to_repo
|
||||
from testing.fixtures import config_with_local_hooks
|
||||
from testing.fixtures import git_dir
|
||||
from testing.fixtures import make_config_from_repo
|
||||
from testing.fixtures import make_repo
|
||||
from testing.fixtures import write_config
|
||||
|
|
@ -45,7 +43,7 @@ def test_autoupdate_up_to_date_repo(up_to_date_repo, in_tmpdir, store):
|
|||
with open(C.CONFIG_FILE) as f:
|
||||
before = f.read()
|
||||
assert '^$' not in before
|
||||
ret = autoupdate(Runner('.', C.CONFIG_FILE), store, tags_only=False)
|
||||
ret = autoupdate(C.CONFIG_FILE, store, tags_only=False)
|
||||
with open(C.CONFIG_FILE) as f:
|
||||
after = f.read()
|
||||
assert ret == 0
|
||||
|
|
@ -72,7 +70,7 @@ def test_autoupdate_old_revision_broken(tempdir_factory, in_tmpdir, store):
|
|||
write_config('.', config)
|
||||
with open(C.CONFIG_FILE) as f:
|
||||
before = f.read()
|
||||
ret = autoupdate(Runner('.', C.CONFIG_FILE), store, tags_only=False)
|
||||
ret = autoupdate(C.CONFIG_FILE, store, tags_only=False)
|
||||
with open(C.CONFIG_FILE) as f:
|
||||
after = f.read()
|
||||
assert ret == 0
|
||||
|
|
@ -112,7 +110,7 @@ def test_autoupdate_out_of_date_repo(out_of_date_repo, in_tmpdir, store):
|
|||
|
||||
with open(C.CONFIG_FILE) as f:
|
||||
before = f.read()
|
||||
ret = autoupdate(Runner('.', C.CONFIG_FILE), store, tags_only=False)
|
||||
ret = autoupdate(C.CONFIG_FILE, store, tags_only=False)
|
||||
with open(C.CONFIG_FILE) as f:
|
||||
after = f.read()
|
||||
assert ret == 0
|
||||
|
|
@ -133,11 +131,10 @@ def test_autoupdate_out_of_date_repo_with_correct_repo_name(
|
|||
# Write out the config
|
||||
write_config('.', config)
|
||||
|
||||
runner = Runner('.', C.CONFIG_FILE)
|
||||
with open(C.CONFIG_FILE) as f:
|
||||
before = f.read()
|
||||
repo_name = 'file://{}'.format(out_of_date_repo.path)
|
||||
ret = autoupdate(runner, store, tags_only=False, repos=(repo_name,))
|
||||
ret = autoupdate(C.CONFIG_FILE, store, tags_only=False, repos=(repo_name,))
|
||||
with open(C.CONFIG_FILE) as f:
|
||||
after = f.read()
|
||||
assert ret == 0
|
||||
|
|
@ -155,11 +152,10 @@ def test_autoupdate_out_of_date_repo_with_wrong_repo_name(
|
|||
)
|
||||
write_config('.', config)
|
||||
|
||||
runner = Runner('.', C.CONFIG_FILE)
|
||||
with open(C.CONFIG_FILE) as f:
|
||||
before = f.read()
|
||||
# It will not update it, because the name doesn't match
|
||||
ret = autoupdate(runner, store, tags_only=False, repos=('dne',))
|
||||
ret = autoupdate(C.CONFIG_FILE, store, tags_only=False, repos=('dne',))
|
||||
with open(C.CONFIG_FILE) as f:
|
||||
after = f.read()
|
||||
assert ret == 0
|
||||
|
|
@ -180,7 +176,7 @@ def test_does_not_reformat(in_tmpdir, out_of_date_repo, store):
|
|||
with open(C.CONFIG_FILE, 'w') as f:
|
||||
f.write(config)
|
||||
|
||||
autoupdate(Runner('.', C.CONFIG_FILE), store, tags_only=False)
|
||||
autoupdate(C.CONFIG_FILE, store, tags_only=False)
|
||||
with open(C.CONFIG_FILE) as f:
|
||||
after = f.read()
|
||||
expected = fmt.format(out_of_date_repo.path, out_of_date_repo.head_rev)
|
||||
|
|
@ -210,7 +206,7 @@ def test_loses_formatting_when_not_detectable(
|
|||
with open(C.CONFIG_FILE, 'w') as f:
|
||||
f.write(config)
|
||||
|
||||
autoupdate(Runner('.', C.CONFIG_FILE), store, tags_only=False)
|
||||
autoupdate(C.CONFIG_FILE, store, tags_only=False)
|
||||
with open(C.CONFIG_FILE) as f:
|
||||
after = f.read()
|
||||
expected = (
|
||||
|
|
@ -235,7 +231,7 @@ def test_autoupdate_tagged_repo(tagged_repo, in_tmpdir, store):
|
|||
)
|
||||
write_config('.', config)
|
||||
|
||||
ret = autoupdate(Runner('.', C.CONFIG_FILE), store, tags_only=False)
|
||||
ret = autoupdate(C.CONFIG_FILE, store, tags_only=False)
|
||||
assert ret == 0
|
||||
with open(C.CONFIG_FILE) as f:
|
||||
assert 'v1.2.3' in f.read()
|
||||
|
|
@ -254,7 +250,7 @@ def test_autoupdate_tags_only(tagged_repo_with_more_commits, in_tmpdir, store):
|
|||
)
|
||||
write_config('.', config)
|
||||
|
||||
ret = autoupdate(Runner('.', C.CONFIG_FILE), store, tags_only=True)
|
||||
ret = autoupdate(C.CONFIG_FILE, store, tags_only=True)
|
||||
assert ret == 0
|
||||
with open(C.CONFIG_FILE) as f:
|
||||
assert 'v1.2.3' in f.read()
|
||||
|
|
@ -269,7 +265,7 @@ def test_autoupdate_latest_no_config(out_of_date_repo, in_tmpdir, store):
|
|||
cmd_output('git', '-C', out_of_date_repo.path, 'rm', '-r', ':/')
|
||||
cmd_output('git', '-C', out_of_date_repo.path, 'commit', '-m', 'rm')
|
||||
|
||||
ret = autoupdate(Runner('.', C.CONFIG_FILE), store, tags_only=False)
|
||||
ret = autoupdate(C.CONFIG_FILE, store, tags_only=False)
|
||||
assert ret == 1
|
||||
with open(C.CONFIG_FILE) as f:
|
||||
assert out_of_date_repo.original_rev in f.read()
|
||||
|
|
@ -313,20 +309,18 @@ def test_autoupdate_hook_disappearing_repo(
|
|||
|
||||
with open(C.CONFIG_FILE) as f:
|
||||
before = f.read()
|
||||
ret = autoupdate(Runner('.', C.CONFIG_FILE), store, tags_only=False)
|
||||
ret = autoupdate(C.CONFIG_FILE, store, tags_only=False)
|
||||
with open(C.CONFIG_FILE) as f:
|
||||
after = f.read()
|
||||
assert ret == 1
|
||||
assert before == after
|
||||
|
||||
|
||||
def test_autoupdate_local_hooks(tempdir_factory, store):
|
||||
git_path = git_dir(tempdir_factory)
|
||||
def test_autoupdate_local_hooks(in_git_dir, store):
|
||||
config = config_with_local_hooks()
|
||||
path = add_config_to_repo(git_path, config)
|
||||
runner = Runner(path, C.CONFIG_FILE)
|
||||
assert autoupdate(runner, store, tags_only=False) == 0
|
||||
new_config_writen = load_config(runner.config_file_path)
|
||||
add_config_to_repo('.', config)
|
||||
assert autoupdate(C.CONFIG_FILE, store, tags_only=False) == 0
|
||||
new_config_writen = load_config(C.CONFIG_FILE)
|
||||
assert len(new_config_writen['repos']) == 1
|
||||
assert new_config_writen['repos'][0] == config
|
||||
|
||||
|
|
@ -340,9 +334,8 @@ def test_autoupdate_local_hooks_with_out_of_date_repo(
|
|||
local_config = config_with_local_hooks()
|
||||
config = {'repos': [local_config, stale_config]}
|
||||
write_config('.', config)
|
||||
runner = Runner('.', C.CONFIG_FILE)
|
||||
assert autoupdate(runner, store, tags_only=False) == 0
|
||||
new_config_writen = load_config(runner.config_file_path)
|
||||
assert autoupdate(C.CONFIG_FILE, store, tags_only=False) == 0
|
||||
new_config_writen = load_config(C.CONFIG_FILE)
|
||||
assert len(new_config_writen['repos']) == 2
|
||||
assert new_config_writen['repos'][0] == local_config
|
||||
|
||||
|
|
@ -355,8 +348,8 @@ def test_autoupdate_meta_hooks(tmpdir, capsys, store):
|
|||
' hooks:\n'
|
||||
' - id: check-useless-excludes\n',
|
||||
)
|
||||
runner = Runner(tmpdir.strpath, C.CONFIG_FILE)
|
||||
ret = autoupdate(runner, store, tags_only=True)
|
||||
with tmpdir.as_cwd():
|
||||
ret = autoupdate(C.CONFIG_FILE, store, tags_only=True)
|
||||
assert ret == 0
|
||||
assert cfg.read() == (
|
||||
'repos:\n'
|
||||
|
|
@ -376,8 +369,8 @@ def test_updates_old_format_to_new_format(tmpdir, capsys, store):
|
|||
' entry: ./bin/foo.sh\n'
|
||||
' language: script\n',
|
||||
)
|
||||
runner = Runner(tmpdir.strpath, C.CONFIG_FILE)
|
||||
ret = autoupdate(runner, store, tags_only=True)
|
||||
with tmpdir.as_cwd():
|
||||
ret = autoupdate(C.CONFIG_FILE, store, tags_only=True)
|
||||
assert ret == 0
|
||||
contents = cfg.read()
|
||||
assert contents == (
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ from __future__ import unicode_literals
|
|||
import io
|
||||
import os.path
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
|
|
@ -20,7 +19,6 @@ from pre_commit.commands.install_uninstall import PRIOR_HASHES
|
|||
from pre_commit.commands.install_uninstall import shebang
|
||||
from pre_commit.commands.install_uninstall import uninstall
|
||||
from pre_commit.languages import python
|
||||
from pre_commit.runner import Runner
|
||||
from pre_commit.util import cmd_output
|
||||
from pre_commit.util import make_executable
|
||||
from pre_commit.util import mkdirp
|
||||
|
|
@ -65,62 +63,45 @@ def test_shebang_returns_default():
|
|||
assert shebang() == '#!/usr/bin/env python'
|
||||
|
||||
|
||||
def test_install_pre_commit(tempdir_factory, store):
|
||||
path = git_dir(tempdir_factory)
|
||||
runner = Runner(path, C.CONFIG_FILE)
|
||||
assert not install(runner, store)
|
||||
assert os.access(os.path.join(path, '.git/hooks/pre-commit'), os.X_OK)
|
||||
def test_install_pre_commit(in_git_dir, store):
|
||||
assert not install(C.CONFIG_FILE, store)
|
||||
assert os.access(in_git_dir.join('.git/hooks/pre-commit').strpath, os.X_OK)
|
||||
|
||||
assert not install(runner, store, hook_type='pre-push')
|
||||
assert os.access(os.path.join(path, '.git/hooks/pre-push'), os.X_OK)
|
||||
assert not install(C.CONFIG_FILE, store, hook_type='pre-push')
|
||||
assert os.access(in_git_dir.join('.git/hooks/pre-push').strpath, os.X_OK)
|
||||
|
||||
|
||||
def test_install_hooks_directory_not_present(tempdir_factory, store):
|
||||
path = git_dir(tempdir_factory)
|
||||
def test_install_hooks_directory_not_present(in_git_dir, store):
|
||||
# Simulate some git clients which don't make .git/hooks #234
|
||||
hooks = os.path.join(path, '.git/hooks')
|
||||
if os.path.exists(hooks): # pragma: no cover (latest git)
|
||||
shutil.rmtree(hooks)
|
||||
runner = Runner(path, C.CONFIG_FILE)
|
||||
install(runner, store)
|
||||
assert os.path.exists(os.path.join(path, '.git/hooks/pre-commit'))
|
||||
if in_git_dir.join('.git/hooks').exists(): # pragma: no cover (odd git)
|
||||
in_git_dir.join('.git/hooks').remove()
|
||||
install(C.CONFIG_FILE, store)
|
||||
assert in_git_dir.join('.git/hooks/pre-commit').exists()
|
||||
|
||||
|
||||
def test_install_refuses_core_hookspath(tempdir_factory, store):
|
||||
path = git_dir(tempdir_factory)
|
||||
with cwd(path):
|
||||
cmd_output('git', 'config', '--local', 'core.hooksPath', 'hooks')
|
||||
runner = Runner(path, C.CONFIG_FILE)
|
||||
assert install(runner, store)
|
||||
def test_install_refuses_core_hookspath(in_git_dir, store):
|
||||
cmd_output('git', 'config', '--local', 'core.hooksPath', 'hooks')
|
||||
assert install(C.CONFIG_FILE, store)
|
||||
|
||||
|
||||
@xfailif_no_symlink
|
||||
def test_install_hooks_dead_symlink(
|
||||
tempdir_factory, store,
|
||||
): # pragma: no cover (non-windows)
|
||||
path = git_dir(tempdir_factory)
|
||||
runner = Runner(path, C.CONFIG_FILE)
|
||||
mkdirp(os.path.join(path, '.git/hooks'))
|
||||
os.symlink('/fake/baz', os.path.join(path, '.git/hooks/pre-commit'))
|
||||
install(runner, store)
|
||||
assert os.path.exists(os.path.join(path, '.git/hooks/pre-commit'))
|
||||
@xfailif_no_symlink # pragma: no cover (non-windows)
|
||||
def test_install_hooks_dead_symlink(in_git_dir, store):
|
||||
hook = in_git_dir.join('.git/hooks').ensure_dir().join('pre-commit')
|
||||
os.symlink('/fake/baz', hook.strpath)
|
||||
install(C.CONFIG_FILE, store)
|
||||
assert hook.exists()
|
||||
|
||||
|
||||
def test_uninstall_does_not_blow_up_when_not_there(tempdir_factory):
|
||||
path = git_dir(tempdir_factory)
|
||||
runner = Runner(path, C.CONFIG_FILE)
|
||||
ret = uninstall(runner)
|
||||
assert ret == 0
|
||||
def test_uninstall_does_not_blow_up_when_not_there(in_git_dir):
|
||||
assert uninstall() == 0
|
||||
|
||||
|
||||
def test_uninstall(tempdir_factory, store):
|
||||
path = git_dir(tempdir_factory)
|
||||
runner = Runner(path, C.CONFIG_FILE)
|
||||
assert not os.path.exists(os.path.join(path, '.git/hooks/pre-commit'))
|
||||
install(runner, store)
|
||||
assert os.path.exists(os.path.join(path, '.git/hooks/pre-commit'))
|
||||
uninstall(runner)
|
||||
assert not os.path.exists(os.path.join(path, '.git/hooks/pre-commit'))
|
||||
def test_uninstall(in_git_dir, store):
|
||||
assert not in_git_dir.join('.git/hooks/pre-commit').exists()
|
||||
install(C.CONFIG_FILE, store)
|
||||
assert in_git_dir.join('.git/hooks/pre-commit').exists()
|
||||
uninstall()
|
||||
assert not in_git_dir.join('.git/hooks/pre-commit').exists()
|
||||
|
||||
|
||||
def _get_commit_output(tempdir_factory, touch_file='foo', **kwargs):
|
||||
|
|
@ -159,7 +140,7 @@ NORMAL_PRE_COMMIT_RUN = re.compile(
|
|||
def test_install_pre_commit_and_run(tempdir_factory, store):
|
||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||
with cwd(path):
|
||||
assert install(Runner(path, C.CONFIG_FILE), store) == 0
|
||||
assert install(C.CONFIG_FILE, store) == 0
|
||||
|
||||
ret, output = _get_commit_output(tempdir_factory)
|
||||
assert ret == 0
|
||||
|
|
@ -171,7 +152,7 @@ def test_install_pre_commit_and_run_custom_path(tempdir_factory, store):
|
|||
with cwd(path):
|
||||
cmd_output('git', 'mv', C.CONFIG_FILE, 'custom-config.yaml')
|
||||
cmd_output('git', 'commit', '-m', 'move pre-commit config')
|
||||
assert install(Runner(path, 'custom-config.yaml'), store) == 0
|
||||
assert install('custom-config.yaml', store) == 0
|
||||
|
||||
ret, output = _get_commit_output(tempdir_factory)
|
||||
assert ret == 0
|
||||
|
|
@ -186,7 +167,7 @@ def test_install_in_submodule_and_run(tempdir_factory, store):
|
|||
|
||||
sub_pth = os.path.join(parent_path, 'sub')
|
||||
with cwd(sub_pth):
|
||||
assert install(Runner(sub_pth, C.CONFIG_FILE), store) == 0
|
||||
assert install(C.CONFIG_FILE, store) == 0
|
||||
ret, output = _get_commit_output(tempdir_factory)
|
||||
assert ret == 0
|
||||
assert NORMAL_PRE_COMMIT_RUN.match(output)
|
||||
|
|
@ -199,7 +180,7 @@ def test_install_in_worktree_and_run(tempdir_factory, store):
|
|||
cmd_output('git', '-C', src_path, 'worktree', 'add', path, '-b', 'master')
|
||||
|
||||
with cwd(path):
|
||||
assert install(Runner(path, C.CONFIG_FILE), store) == 0
|
||||
assert install(C.CONFIG_FILE, store) == 0
|
||||
ret, output = _get_commit_output(tempdir_factory)
|
||||
assert ret == 0
|
||||
assert NORMAL_PRE_COMMIT_RUN.match(output)
|
||||
|
|
@ -216,7 +197,7 @@ def test_commit_am(tempdir_factory, store):
|
|||
with io.open('unstaged', 'w') as foo_file:
|
||||
foo_file.write('Oh hai')
|
||||
|
||||
assert install(Runner(path, C.CONFIG_FILE), store) == 0
|
||||
assert install(C.CONFIG_FILE, store) == 0
|
||||
|
||||
ret, output = _get_commit_output(tempdir_factory)
|
||||
assert ret == 0
|
||||
|
|
@ -225,7 +206,7 @@ def test_commit_am(tempdir_factory, store):
|
|||
def test_unicode_merge_commit_message(tempdir_factory, store):
|
||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||
with cwd(path):
|
||||
assert install(Runner(path, C.CONFIG_FILE), store) == 0
|
||||
assert install(C.CONFIG_FILE, store) == 0
|
||||
cmd_output('git', 'checkout', 'master', '-b', 'foo')
|
||||
cmd_output('git', 'commit', '--allow-empty', '-n', '-m', 'branch2')
|
||||
cmd_output('git', 'checkout', 'master')
|
||||
|
|
@ -240,8 +221,8 @@ def test_unicode_merge_commit_message(tempdir_factory, store):
|
|||
def test_install_idempotent(tempdir_factory, store):
|
||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||
with cwd(path):
|
||||
assert install(Runner(path, C.CONFIG_FILE), store) == 0
|
||||
assert install(Runner(path, C.CONFIG_FILE), store) == 0
|
||||
assert install(C.CONFIG_FILE, store) == 0
|
||||
assert install(C.CONFIG_FILE, store) == 0
|
||||
|
||||
ret, output = _get_commit_output(tempdir_factory)
|
||||
assert ret == 0
|
||||
|
|
@ -261,7 +242,7 @@ def test_environment_not_sourced(tempdir_factory, store):
|
|||
with cwd(path):
|
||||
# Patch the executable to simulate rming virtualenv
|
||||
with mock.patch.object(sys, 'executable', '/does-not-exist'):
|
||||
assert install(Runner(path, C.CONFIG_FILE), store) == 0
|
||||
assert install(C.CONFIG_FILE, store) == 0
|
||||
|
||||
# Use a specific homedir to ignore --user installs
|
||||
homedir = tempdir_factory.get()
|
||||
|
|
@ -300,7 +281,7 @@ FAILING_PRE_COMMIT_RUN = re.compile(
|
|||
def test_failing_hooks_returns_nonzero(tempdir_factory, store):
|
||||
path = make_consuming_repo(tempdir_factory, 'failing_hook_repo')
|
||||
with cwd(path):
|
||||
assert install(Runner(path, C.CONFIG_FILE), store) == 0
|
||||
assert install(C.CONFIG_FILE, store) == 0
|
||||
|
||||
ret, output = _get_commit_output(tempdir_factory)
|
||||
assert ret == 1
|
||||
|
|
@ -325,8 +306,6 @@ def _write_legacy_hook(path):
|
|||
def test_install_existing_hooks_no_overwrite(tempdir_factory, store):
|
||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||
with cwd(path):
|
||||
runner = Runner(path, C.CONFIG_FILE)
|
||||
|
||||
_write_legacy_hook(path)
|
||||
|
||||
# Make sure we installed the "old" hook correctly
|
||||
|
|
@ -335,7 +314,7 @@ def test_install_existing_hooks_no_overwrite(tempdir_factory, store):
|
|||
assert EXISTING_COMMIT_RUN.match(output)
|
||||
|
||||
# Now install pre-commit (no-overwrite)
|
||||
assert install(runner, store) == 0
|
||||
assert install(C.CONFIG_FILE, store) == 0
|
||||
|
||||
# We should run both the legacy and pre-commit hooks
|
||||
ret, output = _get_commit_output(tempdir_factory)
|
||||
|
|
@ -347,13 +326,11 @@ def test_install_existing_hooks_no_overwrite(tempdir_factory, store):
|
|||
def test_install_existing_hook_no_overwrite_idempotent(tempdir_factory, store):
|
||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||
with cwd(path):
|
||||
runner = Runner(path, C.CONFIG_FILE)
|
||||
|
||||
_write_legacy_hook(path)
|
||||
|
||||
# Install twice
|
||||
assert install(runner, store) == 0
|
||||
assert install(runner, store) == 0
|
||||
assert install(C.CONFIG_FILE, store) == 0
|
||||
assert install(C.CONFIG_FILE, store) == 0
|
||||
|
||||
# We should run both the legacy and pre-commit hooks
|
||||
ret, output = _get_commit_output(tempdir_factory)
|
||||
|
|
@ -372,15 +349,13 @@ FAIL_OLD_HOOK = re.compile(
|
|||
def test_failing_existing_hook_returns_1(tempdir_factory, store):
|
||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||
with cwd(path):
|
||||
runner = Runner(path, C.CONFIG_FILE)
|
||||
|
||||
# Write out a failing "old" hook
|
||||
mkdirp(os.path.join(path, '.git/hooks'))
|
||||
with io.open(os.path.join(path, '.git/hooks/pre-commit'), 'w') as f:
|
||||
f.write('#!/usr/bin/env bash\necho "fail!"\nexit 1\n')
|
||||
make_executable(f.name)
|
||||
|
||||
assert install(runner, store) == 0
|
||||
assert install(C.CONFIG_FILE, store) == 0
|
||||
|
||||
# We should get a failure from the legacy hook
|
||||
ret, output = _get_commit_output(tempdir_factory)
|
||||
|
|
@ -391,8 +366,7 @@ def test_failing_existing_hook_returns_1(tempdir_factory, store):
|
|||
def test_install_overwrite_no_existing_hooks(tempdir_factory, store):
|
||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||
with cwd(path):
|
||||
runner = Runner(path, C.CONFIG_FILE)
|
||||
assert install(runner, store, overwrite=True) == 0
|
||||
assert install(C.CONFIG_FILE, store, overwrite=True) == 0
|
||||
|
||||
ret, output = _get_commit_output(tempdir_factory)
|
||||
assert ret == 0
|
||||
|
|
@ -402,10 +376,8 @@ def test_install_overwrite_no_existing_hooks(tempdir_factory, store):
|
|||
def test_install_overwrite(tempdir_factory, store):
|
||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||
with cwd(path):
|
||||
runner = Runner(path, C.CONFIG_FILE)
|
||||
|
||||
_write_legacy_hook(path)
|
||||
assert install(runner, store, overwrite=True) == 0
|
||||
assert install(C.CONFIG_FILE, store, overwrite=True) == 0
|
||||
|
||||
ret, output = _get_commit_output(tempdir_factory)
|
||||
assert ret == 0
|
||||
|
|
@ -415,13 +387,11 @@ def test_install_overwrite(tempdir_factory, store):
|
|||
def test_uninstall_restores_legacy_hooks(tempdir_factory, store):
|
||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||
with cwd(path):
|
||||
runner = Runner(path, C.CONFIG_FILE)
|
||||
|
||||
_write_legacy_hook(path)
|
||||
|
||||
# Now install and uninstall pre-commit
|
||||
assert install(runner, store) == 0
|
||||
assert uninstall(runner) == 0
|
||||
assert install(C.CONFIG_FILE, store) == 0
|
||||
assert uninstall() == 0
|
||||
|
||||
# Make sure we installed the "old" hook correctly
|
||||
ret, output = _get_commit_output(tempdir_factory, touch_file='baz')
|
||||
|
|
@ -432,8 +402,6 @@ def test_uninstall_restores_legacy_hooks(tempdir_factory, store):
|
|||
def test_replace_old_commit_script(tempdir_factory, store):
|
||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||
with cwd(path):
|
||||
runner = Runner(path, C.CONFIG_FILE)
|
||||
|
||||
# Install a script that looks like our old script
|
||||
pre_commit_contents = resource_text('hook-tmpl')
|
||||
new_contents = pre_commit_contents.replace(
|
||||
|
|
@ -446,7 +414,7 @@ def test_replace_old_commit_script(tempdir_factory, store):
|
|||
make_executable(f.name)
|
||||
|
||||
# Install normally
|
||||
assert install(runner, store) == 0
|
||||
assert install(C.CONFIG_FILE, store) == 0
|
||||
|
||||
ret, output = _get_commit_output(tempdir_factory)
|
||||
assert ret == 0
|
||||
|
|
@ -456,13 +424,12 @@ def test_replace_old_commit_script(tempdir_factory, store):
|
|||
def test_uninstall_doesnt_remove_not_our_hooks(tempdir_factory):
|
||||
path = git_dir(tempdir_factory)
|
||||
with cwd(path):
|
||||
runner = Runner(path, C.CONFIG_FILE)
|
||||
mkdirp(os.path.join(path, '.git/hooks'))
|
||||
with io.open(os.path.join(path, '.git/hooks/pre-commit'), 'w') as f:
|
||||
f.write('#!/usr/bin/env bash\necho 1\n')
|
||||
make_executable(f.name)
|
||||
|
||||
assert uninstall(runner) == 0
|
||||
assert uninstall() == 0
|
||||
|
||||
assert os.path.exists(os.path.join(path, '.git/hooks/pre-commit'))
|
||||
|
||||
|
|
@ -478,7 +445,7 @@ PRE_INSTALLED = re.compile(
|
|||
def test_installs_hooks_with_hooks_True(tempdir_factory, store):
|
||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||
with cwd(path):
|
||||
install(Runner(path, C.CONFIG_FILE), store, hooks=True)
|
||||
install(C.CONFIG_FILE, store, hooks=True)
|
||||
ret, output = _get_commit_output(
|
||||
tempdir_factory, pre_commit_home=store.directory,
|
||||
)
|
||||
|
|
@ -490,9 +457,8 @@ def test_installs_hooks_with_hooks_True(tempdir_factory, store):
|
|||
def test_install_hooks_command(tempdir_factory, store):
|
||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||
with cwd(path):
|
||||
runner = Runner(path, C.CONFIG_FILE)
|
||||
install(runner, store)
|
||||
install_hooks(runner, store)
|
||||
install(C.CONFIG_FILE, store)
|
||||
install_hooks(C.CONFIG_FILE, store)
|
||||
ret, output = _get_commit_output(
|
||||
tempdir_factory, pre_commit_home=store.directory,
|
||||
)
|
||||
|
|
@ -504,7 +470,7 @@ def test_install_hooks_command(tempdir_factory, store):
|
|||
def test_installed_from_venv(tempdir_factory, store):
|
||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||
with cwd(path):
|
||||
install(Runner(path, C.CONFIG_FILE), store)
|
||||
install(C.CONFIG_FILE, store)
|
||||
# No environment so pre-commit is not on the path when running!
|
||||
# Should still pick up the python from when we installed
|
||||
ret, output = _get_commit_output(
|
||||
|
|
@ -543,7 +509,7 @@ def test_pre_push_integration_failing(tempdir_factory, store):
|
|||
path = tempdir_factory.get()
|
||||
cmd_output('git', 'clone', upstream, path)
|
||||
with cwd(path):
|
||||
install(Runner(path, C.CONFIG_FILE), store, hook_type='pre-push')
|
||||
install(C.CONFIG_FILE, store, hook_type='pre-push')
|
||||
# commit succeeds because pre-commit is only installed for pre-push
|
||||
assert _get_commit_output(tempdir_factory)[0] == 0
|
||||
assert _get_commit_output(tempdir_factory, touch_file='zzz')[0] == 0
|
||||
|
|
@ -561,7 +527,7 @@ def test_pre_push_integration_accepted(tempdir_factory, store):
|
|||
path = tempdir_factory.get()
|
||||
cmd_output('git', 'clone', upstream, path)
|
||||
with cwd(path):
|
||||
install(Runner(path, C.CONFIG_FILE), store, hook_type='pre-push')
|
||||
install(C.CONFIG_FILE, store, hook_type='pre-push')
|
||||
assert _get_commit_output(tempdir_factory)[0] == 0
|
||||
|
||||
retc, output = _get_push_output(tempdir_factory)
|
||||
|
|
@ -581,7 +547,7 @@ def test_pre_push_force_push_without_fetch(tempdir_factory, store):
|
|||
assert _get_push_output(tempdir_factory)[0] == 0
|
||||
|
||||
with cwd(path2):
|
||||
install(Runner(path2, C.CONFIG_FILE), store, hook_type='pre-push')
|
||||
install(C.CONFIG_FILE, store, hook_type='pre-push')
|
||||
assert _get_commit_output(tempdir_factory, commit_msg='force!')[0] == 0
|
||||
|
||||
retc, output = _get_push_output(tempdir_factory, opts=('--force',))
|
||||
|
|
@ -596,7 +562,7 @@ def test_pre_push_new_upstream(tempdir_factory, store):
|
|||
path = tempdir_factory.get()
|
||||
cmd_output('git', 'clone', upstream, path)
|
||||
with cwd(path):
|
||||
install(Runner(path, C.CONFIG_FILE), store, hook_type='pre-push')
|
||||
install(C.CONFIG_FILE, store, hook_type='pre-push')
|
||||
assert _get_commit_output(tempdir_factory)[0] == 0
|
||||
|
||||
cmd_output('git', 'remote', 'rename', 'origin', 'upstream')
|
||||
|
|
@ -612,7 +578,7 @@ def test_pre_push_integration_empty_push(tempdir_factory, store):
|
|||
path = tempdir_factory.get()
|
||||
cmd_output('git', 'clone', upstream, path)
|
||||
with cwd(path):
|
||||
install(Runner(path, C.CONFIG_FILE), store, hook_type='pre-push')
|
||||
install(C.CONFIG_FILE, store, hook_type='pre-push')
|
||||
_get_push_output(tempdir_factory)
|
||||
retc, output = _get_push_output(tempdir_factory)
|
||||
assert output == 'Everything up-to-date\n'
|
||||
|
|
@ -624,8 +590,6 @@ def test_pre_push_legacy(tempdir_factory, store):
|
|||
path = tempdir_factory.get()
|
||||
cmd_output('git', 'clone', upstream, path)
|
||||
with cwd(path):
|
||||
runner = Runner(path, C.CONFIG_FILE)
|
||||
|
||||
mkdirp(os.path.join(path, '.git/hooks'))
|
||||
with io.open(os.path.join(path, '.git/hooks/pre-push'), 'w') as f:
|
||||
f.write(
|
||||
|
|
@ -637,7 +601,7 @@ def test_pre_push_legacy(tempdir_factory, store):
|
|||
)
|
||||
make_executable(f.name)
|
||||
|
||||
install(runner, store, hook_type='pre-push')
|
||||
install(C.CONFIG_FILE, store, hook_type='pre-push')
|
||||
assert _get_commit_output(tempdir_factory)[0] == 0
|
||||
|
||||
retc, output = _get_push_output(tempdir_factory)
|
||||
|
|
@ -651,8 +615,7 @@ def test_pre_push_legacy(tempdir_factory, store):
|
|||
def test_commit_msg_integration_failing(
|
||||
commit_msg_repo, tempdir_factory, store,
|
||||
):
|
||||
runner = Runner(commit_msg_repo, C.CONFIG_FILE)
|
||||
install(runner, store, hook_type='commit-msg')
|
||||
install(C.CONFIG_FILE, store, hook_type='commit-msg')
|
||||
retc, out = _get_commit_output(tempdir_factory)
|
||||
assert retc == 1
|
||||
assert out.startswith('Must have "Signed off by:"...')
|
||||
|
|
@ -662,8 +625,7 @@ def test_commit_msg_integration_failing(
|
|||
def test_commit_msg_integration_passing(
|
||||
commit_msg_repo, tempdir_factory, store,
|
||||
):
|
||||
runner = Runner(commit_msg_repo, C.CONFIG_FILE)
|
||||
install(runner, store, hook_type='commit-msg')
|
||||
install(C.CONFIG_FILE, store, hook_type='commit-msg')
|
||||
msg = 'Hi\nSigned off by: me, lol'
|
||||
retc, out = _get_commit_output(tempdir_factory, commit_msg=msg)
|
||||
assert retc == 0
|
||||
|
|
@ -673,8 +635,6 @@ def test_commit_msg_integration_passing(
|
|||
|
||||
|
||||
def test_commit_msg_legacy(commit_msg_repo, tempdir_factory, store):
|
||||
runner = Runner(commit_msg_repo, C.CONFIG_FILE)
|
||||
|
||||
hook_path = os.path.join(commit_msg_repo, '.git/hooks/commit-msg')
|
||||
mkdirp(os.path.dirname(hook_path))
|
||||
with io.open(hook_path, 'w') as hook_file:
|
||||
|
|
@ -686,7 +646,7 @@ def test_commit_msg_legacy(commit_msg_repo, tempdir_factory, store):
|
|||
)
|
||||
make_executable(hook_path)
|
||||
|
||||
install(runner, store, hook_type='commit-msg')
|
||||
install(C.CONFIG_FILE, store, hook_type='commit-msg')
|
||||
|
||||
msg = 'Hi\nSigned off by: asottile'
|
||||
retc, out = _get_commit_output(tempdir_factory, commit_msg=msg)
|
||||
|
|
@ -699,11 +659,9 @@ def test_commit_msg_legacy(commit_msg_repo, tempdir_factory, store):
|
|||
def test_install_disallow_mising_config(tempdir_factory, store):
|
||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||
with cwd(path):
|
||||
runner = Runner(path, C.CONFIG_FILE)
|
||||
|
||||
remove_config_from_repo(path)
|
||||
ret = install(
|
||||
runner, store, overwrite=True, skip_on_missing_conf=False,
|
||||
C.CONFIG_FILE, store, overwrite=True, skip_on_missing_conf=False,
|
||||
)
|
||||
assert ret == 0
|
||||
|
||||
|
|
@ -714,11 +672,9 @@ def test_install_disallow_mising_config(tempdir_factory, store):
|
|||
def test_install_allow_mising_config(tempdir_factory, store):
|
||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||
with cwd(path):
|
||||
runner = Runner(path, C.CONFIG_FILE)
|
||||
|
||||
remove_config_from_repo(path)
|
||||
ret = install(
|
||||
runner, store, overwrite=True, skip_on_missing_conf=True,
|
||||
C.CONFIG_FILE, store, overwrite=True, skip_on_missing_conf=True,
|
||||
)
|
||||
assert ret == 0
|
||||
|
||||
|
|
@ -734,11 +690,9 @@ def test_install_allow_mising_config(tempdir_factory, store):
|
|||
def test_install_temporarily_allow_mising_config(tempdir_factory, store):
|
||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||
with cwd(path):
|
||||
runner = Runner(path, C.CONFIG_FILE)
|
||||
|
||||
remove_config_from_repo(path)
|
||||
ret = install(
|
||||
runner, store, overwrite=True, skip_on_missing_conf=False,
|
||||
C.CONFIG_FILE, store, overwrite=True, skip_on_missing_conf=False,
|
||||
)
|
||||
assert ret == 0
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ import pytest
|
|||
import pre_commit.constants as C
|
||||
from pre_commit.commands.migrate_config import _indent
|
||||
from pre_commit.commands.migrate_config import migrate_config
|
||||
from pre_commit.runner import Runner
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
|
|
@ -33,7 +32,8 @@ def test_migrate_config_normal_format(tmpdir, capsys):
|
|||
' entry: ./bin/foo.sh\n'
|
||||
' language: script\n',
|
||||
)
|
||||
assert not migrate_config(Runner(tmpdir.strpath, C.CONFIG_FILE))
|
||||
with tmpdir.as_cwd():
|
||||
assert not migrate_config(C.CONFIG_FILE)
|
||||
out, _ = capsys.readouterr()
|
||||
assert out == 'Configuration has been migrated.\n'
|
||||
contents = cfg.read()
|
||||
|
|
@ -61,7 +61,8 @@ def test_migrate_config_document_marker(tmpdir):
|
|||
' entry: ./bin/foo.sh\n'
|
||||
' language: script\n',
|
||||
)
|
||||
assert not migrate_config(Runner(tmpdir.strpath, C.CONFIG_FILE))
|
||||
with tmpdir.as_cwd():
|
||||
assert not migrate_config(C.CONFIG_FILE)
|
||||
contents = cfg.read()
|
||||
assert contents == (
|
||||
'# comment\n'
|
||||
|
|
@ -88,7 +89,8 @@ def test_migrate_config_list_literal(tmpdir):
|
|||
' }]\n'
|
||||
'}]',
|
||||
)
|
||||
assert not migrate_config(Runner(tmpdir.strpath, C.CONFIG_FILE))
|
||||
with tmpdir.as_cwd():
|
||||
assert not migrate_config(C.CONFIG_FILE)
|
||||
contents = cfg.read()
|
||||
assert contents == (
|
||||
'repos:\n'
|
||||
|
|
@ -114,7 +116,8 @@ def test_already_migrated_configuration_noop(tmpdir, capsys):
|
|||
)
|
||||
cfg = tmpdir.join(C.CONFIG_FILE)
|
||||
cfg.write(contents)
|
||||
assert not migrate_config(Runner(tmpdir.strpath, C.CONFIG_FILE))
|
||||
with tmpdir.as_cwd():
|
||||
assert not migrate_config(C.CONFIG_FILE)
|
||||
out, _ = capsys.readouterr()
|
||||
assert out == 'Configuration is already migrated.\n'
|
||||
assert cfg.read() == contents
|
||||
|
|
@ -133,7 +136,8 @@ def test_migrate_config_sha_to_rev(tmpdir):
|
|||
)
|
||||
cfg = tmpdir.join(C.CONFIG_FILE)
|
||||
cfg.write(contents)
|
||||
assert not migrate_config(Runner(tmpdir.strpath, C.CONFIG_FILE))
|
||||
with tmpdir.as_cwd():
|
||||
assert not migrate_config(C.CONFIG_FILE)
|
||||
contents = cfg.read()
|
||||
assert contents == (
|
||||
'repos:\n'
|
||||
|
|
|
|||
|
|
@ -16,7 +16,6 @@ from pre_commit.commands.run import _filter_by_include_exclude
|
|||
from pre_commit.commands.run import _get_skips
|
||||
from pre_commit.commands.run import _has_unmerged_paths
|
||||
from pre_commit.commands.run import run
|
||||
from pre_commit.runner import Runner
|
||||
from pre_commit.util import cmd_output
|
||||
from pre_commit.util import make_executable
|
||||
from testing.fixtures import add_config_to_repo
|
||||
|
|
@ -49,9 +48,8 @@ def stage_a_file(filename='foo.py'):
|
|||
|
||||
|
||||
def _do_run(cap_out, store, repo, args, environ={}, config_file=C.CONFIG_FILE):
|
||||
runner = Runner(repo, config_file)
|
||||
with cwd(runner.git_root): # replicates Runner.create behaviour
|
||||
ret = run(runner, store, args, environ=environ)
|
||||
with cwd(repo): # replicates `main._adjust_args_and_chdir` behaviour
|
||||
ret = run(config_file, store, args, environ=environ)
|
||||
printed = cap_out.get_bytes()
|
||||
return ret, printed
|
||||
|
||||
|
|
@ -435,7 +433,7 @@ def test_stdout_write_bug_py26(repo_with_failing_hook, store, tempdir_factory):
|
|||
config['repos'][0]['hooks'][0]['args'] = ['☃']
|
||||
stage_a_file()
|
||||
|
||||
install(Runner(repo_with_failing_hook, C.CONFIG_FILE), store)
|
||||
install(C.CONFIG_FILE, store)
|
||||
|
||||
# Have to use subprocess because pytest monkeypatches sys.stdout
|
||||
_, stdout, _ = cmd_output_mocked_pre_commit_home(
|
||||
|
|
@ -465,7 +463,7 @@ def test_lots_of_files(store, tempdir_factory):
|
|||
open(filename, 'w').close()
|
||||
|
||||
cmd_output('git', 'add', '.')
|
||||
install(Runner(git_path, C.CONFIG_FILE), store)
|
||||
install(C.CONFIG_FILE, store)
|
||||
|
||||
cmd_output_mocked_pre_commit_home(
|
||||
'git', 'commit', '-m', 'Commit!',
|
||||
|
|
|
|||
|
|
@ -63,6 +63,13 @@ def in_tmpdir(tempdir_factory):
|
|||
yield path
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def in_git_dir(tmpdir):
|
||||
with tmpdir.as_cwd():
|
||||
cmd_output('git', 'init')
|
||||
yield tmpdir
|
||||
|
||||
|
||||
def _make_conflict():
|
||||
cmd_output('git', 'checkout', 'origin/master', '-b', 'foo')
|
||||
with io.open('conflict_file', 'w') as conflict_file:
|
||||
|
|
|
|||
|
|
@ -7,9 +7,44 @@ import os.path
|
|||
import mock
|
||||
import pytest
|
||||
|
||||
import pre_commit.constants as C
|
||||
from pre_commit import main
|
||||
from testing.auto_namedtuple import auto_namedtuple
|
||||
from testing.util import cwd
|
||||
|
||||
|
||||
class Args(object):
|
||||
def __init__(self, **kwargs):
|
||||
kwargs.setdefault('command', 'help')
|
||||
kwargs.setdefault('config', C.CONFIG_FILE)
|
||||
self.__dict__.update(kwargs)
|
||||
|
||||
|
||||
def test_adjust_args_and_chdir_noop(in_git_dir):
|
||||
args = Args(command='run', files=['f1', 'f2'])
|
||||
main._adjust_args_and_chdir(args)
|
||||
assert os.getcwd() == in_git_dir
|
||||
assert args.config == C.CONFIG_FILE
|
||||
assert args.files == ['f1', 'f2']
|
||||
|
||||
|
||||
def test_adjust_args_and_chdir_relative_things(in_git_dir):
|
||||
in_git_dir.join('foo/cfg.yaml').ensure()
|
||||
in_git_dir.join('foo').chdir()
|
||||
|
||||
args = Args(command='run', files=['f1', 'f2'], config='cfg.yaml')
|
||||
main._adjust_args_and_chdir(args)
|
||||
assert os.getcwd() == in_git_dir
|
||||
assert args.config == os.path.join('foo', 'cfg.yaml')
|
||||
assert args.files == [os.path.join('foo', 'f1'), os.path.join('foo', 'f2')]
|
||||
|
||||
|
||||
def test_adjust_args_and_chdir_non_relative_config(in_git_dir):
|
||||
in_git_dir.join('foo').ensure_dir().chdir()
|
||||
|
||||
args = Args()
|
||||
main._adjust_args_and_chdir(args)
|
||||
assert os.getcwd() == in_git_dir
|
||||
assert args.config == C.CONFIG_FILE
|
||||
|
||||
|
||||
FNS = (
|
||||
|
|
@ -28,18 +63,6 @@ def mock_commands():
|
|||
mck.stop()
|
||||
|
||||
|
||||
class CalledExit(Exception):
|
||||
pass
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def argparse_exit_mock():
|
||||
with mock.patch.object(
|
||||
argparse.ArgumentParser, 'exit', side_effect=CalledExit,
|
||||
) as exit_mock:
|
||||
yield exit_mock
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def argparse_parse_args_spy():
|
||||
parse_args_mock = mock.Mock()
|
||||
|
|
@ -62,15 +85,13 @@ def assert_only_one_mock_called(mock_objs):
|
|||
assert total_call_count == 1
|
||||
|
||||
|
||||
def test_overall_help(mock_commands, argparse_exit_mock):
|
||||
with pytest.raises(CalledExit):
|
||||
def test_overall_help(mock_commands):
|
||||
with pytest.raises(SystemExit):
|
||||
main.main(['--help'])
|
||||
|
||||
|
||||
def test_help_command(
|
||||
mock_commands, argparse_exit_mock, argparse_parse_args_spy,
|
||||
):
|
||||
with pytest.raises(CalledExit):
|
||||
def test_help_command(mock_commands, argparse_parse_args_spy):
|
||||
with pytest.raises(SystemExit):
|
||||
main.main(['help'])
|
||||
|
||||
argparse_parse_args_spy.assert_has_calls([
|
||||
|
|
@ -79,10 +100,8 @@ def test_help_command(
|
|||
])
|
||||
|
||||
|
||||
def test_help_other_command(
|
||||
mock_commands, argparse_exit_mock, argparse_parse_args_spy,
|
||||
):
|
||||
with pytest.raises(CalledExit):
|
||||
def test_help_other_command(mock_commands, argparse_parse_args_spy):
|
||||
with pytest.raises(SystemExit):
|
||||
main.main(['help', 'run'])
|
||||
|
||||
argparse_parse_args_spy.assert_has_calls([
|
||||
|
|
@ -105,16 +124,12 @@ def test_try_repo(mock_store_dir):
|
|||
|
||||
|
||||
def test_help_cmd_in_empty_directory(
|
||||
in_tmpdir,
|
||||
mock_commands,
|
||||
tempdir_factory,
|
||||
argparse_exit_mock,
|
||||
argparse_parse_args_spy,
|
||||
):
|
||||
path = tempdir_factory.get()
|
||||
|
||||
with cwd(path):
|
||||
with pytest.raises(CalledExit):
|
||||
main.main(['help', 'run'])
|
||||
with pytest.raises(SystemExit):
|
||||
main.main(['help', 'run'])
|
||||
|
||||
argparse_parse_args_spy.assert_has_calls([
|
||||
mock.call(['help', 'run']),
|
||||
|
|
@ -122,12 +137,9 @@ def test_help_cmd_in_empty_directory(
|
|||
])
|
||||
|
||||
|
||||
def test_expected_fatal_error_no_git_repo(
|
||||
tempdir_factory, cap_out, mock_store_dir,
|
||||
):
|
||||
with cwd(tempdir_factory.get()):
|
||||
with pytest.raises(SystemExit):
|
||||
main.main([])
|
||||
def test_expected_fatal_error_no_git_repo(in_tmpdir, cap_out, mock_store_dir):
|
||||
with pytest.raises(SystemExit):
|
||||
main.main([])
|
||||
log_file = os.path.join(mock_store_dir, 'pre-commit.log')
|
||||
assert cap_out.get() == (
|
||||
'An error has occurred: FatalError: git failed. '
|
||||
|
|
|
|||
|
|
@ -1,44 +0,0 @@
|
|||
from __future__ import absolute_import
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import os.path
|
||||
|
||||
import pre_commit.constants as C
|
||||
from pre_commit.runner import Runner
|
||||
from testing.fixtures import git_dir
|
||||
from testing.util import cwd
|
||||
|
||||
|
||||
def test_init_has_no_side_effects(tmpdir):
|
||||
current_wd = os.getcwd()
|
||||
runner = Runner(tmpdir.strpath, C.CONFIG_FILE)
|
||||
assert runner.git_root == tmpdir.strpath
|
||||
assert os.getcwd() == current_wd
|
||||
|
||||
|
||||
def test_create_sets_correct_directory(tempdir_factory):
|
||||
path = git_dir(tempdir_factory)
|
||||
with cwd(path):
|
||||
runner = Runner.create(C.CONFIG_FILE)
|
||||
assert os.path.normcase(runner.git_root) == os.path.normcase(path)
|
||||
assert os.path.normcase(os.getcwd()) == os.path.normcase(path)
|
||||
|
||||
|
||||
def test_create_changes_to_git_root(tempdir_factory):
|
||||
path = git_dir(tempdir_factory)
|
||||
with cwd(path):
|
||||
# Change into some directory, create should set to root
|
||||
foo_path = os.path.join(path, 'foo')
|
||||
os.mkdir(foo_path)
|
||||
os.chdir(foo_path)
|
||||
assert os.getcwd() != path
|
||||
|
||||
runner = Runner.create(C.CONFIG_FILE)
|
||||
assert os.path.normcase(runner.git_root) == os.path.normcase(path)
|
||||
assert os.path.normcase(os.getcwd()) == os.path.normcase(path)
|
||||
|
||||
|
||||
def test_config_file_path():
|
||||
runner = Runner(os.path.join('foo', 'bar'), C.CONFIG_FILE)
|
||||
expected_path = os.path.join('foo', 'bar', C.CONFIG_FILE)
|
||||
assert runner.config_file_path == expected_path
|
||||
|
|
@ -297,13 +297,6 @@ def test_non_utf8_conflicting_diff(foo_staged, patch_dir):
|
|||
_test_foo_state(foo_staged, contents, 'AM', encoding='latin-1')
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def in_git_dir(tmpdir):
|
||||
with tmpdir.as_cwd():
|
||||
cmd_output('git', 'init', '.')
|
||||
yield tmpdir
|
||||
|
||||
|
||||
def _write(b):
|
||||
with open('foo', 'wb') as f:
|
||||
f.write(b)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue