mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Merge pull request #895 from pre-commit/rip_runner
Remove stateful Runner
This commit is contained in:
commit
c5c0a0699b
17 changed files with 209 additions and 315 deletions
|
|
@ -112,14 +112,14 @@ def _write_new_config_file(path, output):
|
||||||
f.write(to_write)
|
f.write(to_write)
|
||||||
|
|
||||||
|
|
||||||
def autoupdate(runner, store, tags_only, repos=()):
|
def autoupdate(config_file, store, tags_only, repos=()):
|
||||||
"""Auto-update the pre-commit config to the latest versions of repos."""
|
"""Auto-update the pre-commit config to the latest versions of repos."""
|
||||||
migrate_config(runner, quiet=True)
|
migrate_config(config_file, quiet=True)
|
||||||
retv = 0
|
retv = 0
|
||||||
output_repos = []
|
output_repos = []
|
||||||
changed = False
|
changed = False
|
||||||
|
|
||||||
input_config = load_config(runner.config_file_path)
|
input_config = load_config(config_file)
|
||||||
|
|
||||||
for repo_config in input_config['repos']:
|
for repo_config in input_config['repos']:
|
||||||
if (
|
if (
|
||||||
|
|
@ -152,6 +152,6 @@ def autoupdate(runner, store, tags_only, repos=()):
|
||||||
if changed:
|
if changed:
|
||||||
output_config = input_config.copy()
|
output_config = input_config.copy()
|
||||||
output_config['repos'] = output_repos
|
output_config['repos'] = output_repos
|
||||||
_write_new_config_file(runner.config_file_path, output_config)
|
_write_new_config_file(config_file, output_config)
|
||||||
|
|
||||||
return retv
|
return retv
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import sys
|
||||||
|
|
||||||
from pre_commit import git
|
from pre_commit import git
|
||||||
from pre_commit import output
|
from pre_commit import output
|
||||||
|
from pre_commit.clientlib import load_config
|
||||||
from pre_commit.languages import python
|
from pre_commit.languages import python
|
||||||
from pre_commit.repository import repositories
|
from pre_commit.repository import repositories
|
||||||
from pre_commit.util import cmd_output
|
from pre_commit.util import cmd_output
|
||||||
|
|
@ -31,8 +32,8 @@ TEMPLATE_START = '# start templated\n'
|
||||||
TEMPLATE_END = '# end templated\n'
|
TEMPLATE_END = '# end templated\n'
|
||||||
|
|
||||||
|
|
||||||
def _hook_paths(git_root, hook_type):
|
def _hook_paths(hook_type):
|
||||||
pth = os.path.join(git.get_git_dir(git_root), 'hooks', hook_type)
|
pth = os.path.join(git.get_git_dir(), 'hooks', hook_type)
|
||||||
return pth, '{}.legacy'.format(pth)
|
return pth, '{}.legacy'.format(pth)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -55,7 +56,8 @@ def shebang():
|
||||||
|
|
||||||
|
|
||||||
def install(
|
def install(
|
||||||
runner, store, overwrite=False, hooks=False, hook_type='pre-commit',
|
config_file, store,
|
||||||
|
overwrite=False, hooks=False, hook_type='pre-commit',
|
||||||
skip_on_missing_conf=False,
|
skip_on_missing_conf=False,
|
||||||
):
|
):
|
||||||
"""Install the pre-commit hooks."""
|
"""Install the pre-commit hooks."""
|
||||||
|
|
@ -66,7 +68,7 @@ def install(
|
||||||
)
|
)
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
hook_path, legacy_path = _hook_paths(runner.git_root, hook_type)
|
hook_path, legacy_path = _hook_paths(hook_type)
|
||||||
|
|
||||||
mkdirp(os.path.dirname(hook_path))
|
mkdirp(os.path.dirname(hook_path))
|
||||||
|
|
||||||
|
|
@ -84,7 +86,7 @@ def install(
|
||||||
)
|
)
|
||||||
|
|
||||||
params = {
|
params = {
|
||||||
'CONFIG': runner.config_file,
|
'CONFIG': config_file,
|
||||||
'HOOK_TYPE': hook_type,
|
'HOOK_TYPE': hook_type,
|
||||||
'INSTALL_PYTHON': sys.executable,
|
'INSTALL_PYTHON': sys.executable,
|
||||||
'SKIP_ON_MISSING_CONFIG': skip_on_missing_conf,
|
'SKIP_ON_MISSING_CONFIG': skip_on_missing_conf,
|
||||||
|
|
@ -108,19 +110,19 @@ def install(
|
||||||
|
|
||||||
# If they requested we install all of the hooks, do so.
|
# If they requested we install all of the hooks, do so.
|
||||||
if hooks:
|
if hooks:
|
||||||
install_hooks(runner, store)
|
install_hooks(config_file, store)
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
def install_hooks(runner, store):
|
def install_hooks(config_file, store):
|
||||||
for repository in repositories(runner.config, store):
|
for repository in repositories(load_config(config_file), store):
|
||||||
repository.require_installed()
|
repository.require_installed()
|
||||||
|
|
||||||
|
|
||||||
def uninstall(runner, hook_type='pre-commit'):
|
def uninstall(hook_type='pre-commit'):
|
||||||
"""Uninstall the pre-commit hooks."""
|
"""Uninstall the pre-commit hooks."""
|
||||||
hook_path, legacy_path = _hook_paths(runner.git_root, hook_type)
|
hook_path, legacy_path = _hook_paths(hook_type)
|
||||||
|
|
||||||
# If our file doesn't exist or it isn't ours, gtfo.
|
# If our file doesn't exist or it isn't ours, gtfo.
|
||||||
if not os.path.exists(hook_path) or not is_our_script(hook_path):
|
if not os.path.exists(hook_path) or not is_our_script(hook_path):
|
||||||
|
|
|
||||||
|
|
@ -45,15 +45,15 @@ def _migrate_sha_to_rev(contents):
|
||||||
return reg.sub(r'\1rev:', contents)
|
return reg.sub(r'\1rev:', contents)
|
||||||
|
|
||||||
|
|
||||||
def migrate_config(runner, quiet=False):
|
def migrate_config(config_file, quiet=False):
|
||||||
with io.open(runner.config_file_path) as f:
|
with io.open(config_file) as f:
|
||||||
orig_contents = contents = f.read()
|
orig_contents = contents = f.read()
|
||||||
|
|
||||||
contents = _migrate_map(contents)
|
contents = _migrate_map(contents)
|
||||||
contents = _migrate_sha_to_rev(contents)
|
contents = _migrate_sha_to_rev(contents)
|
||||||
|
|
||||||
if contents != orig_contents:
|
if contents != orig_contents:
|
||||||
with io.open(runner.config_file_path, 'w') as f:
|
with io.open(config_file, 'w') as f:
|
||||||
f.write(contents)
|
f.write(contents)
|
||||||
|
|
||||||
print('Configuration has been migrated.')
|
print('Configuration has been migrated.')
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ from identify.identify import tags_from_path
|
||||||
from pre_commit import color
|
from pre_commit import color
|
||||||
from pre_commit import git
|
from pre_commit import git
|
||||||
from pre_commit import output
|
from pre_commit import output
|
||||||
|
from pre_commit.clientlib import load_config
|
||||||
from pre_commit.output import get_hook_message
|
from pre_commit.output import get_hook_message
|
||||||
from pre_commit.repository import repositories
|
from pre_commit.repository import repositories
|
||||||
from pre_commit.staged_files_only import staged_files_only
|
from pre_commit.staged_files_only import staged_files_only
|
||||||
|
|
@ -214,16 +215,16 @@ def _has_unmerged_paths():
|
||||||
return bool(stdout.strip())
|
return bool(stdout.strip())
|
||||||
|
|
||||||
|
|
||||||
def _has_unstaged_config(runner):
|
def _has_unstaged_config(config_file):
|
||||||
retcode, _, _ = cmd_output(
|
retcode, _, _ = cmd_output(
|
||||||
'git', 'diff', '--no-ext-diff', '--exit-code', runner.config_file_path,
|
'git', 'diff', '--no-ext-diff', '--exit-code', config_file,
|
||||||
retcode=None,
|
retcode=None,
|
||||||
)
|
)
|
||||||
# be explicit, other git errors don't mean it has an unstaged config.
|
# be explicit, other git errors don't mean it has an unstaged config.
|
||||||
return retcode == 1
|
return retcode == 1
|
||||||
|
|
||||||
|
|
||||||
def run(runner, store, args, environ=os.environ):
|
def run(config_file, store, args, environ=os.environ):
|
||||||
no_stash = args.all_files or bool(args.files)
|
no_stash = args.all_files or bool(args.files)
|
||||||
|
|
||||||
# Check if we have unresolved merge conflict files and fail fast.
|
# Check if we have unresolved merge conflict files and fail fast.
|
||||||
|
|
@ -233,10 +234,10 @@ def run(runner, store, args, environ=os.environ):
|
||||||
if bool(args.source) != bool(args.origin):
|
if bool(args.source) != bool(args.origin):
|
||||||
logger.error('Specify both --origin and --source.')
|
logger.error('Specify both --origin and --source.')
|
||||||
return 1
|
return 1
|
||||||
if _has_unstaged_config(runner) and not no_stash:
|
if _has_unstaged_config(config_file) and not no_stash:
|
||||||
logger.error(
|
logger.error(
|
||||||
'Your pre-commit configuration is unstaged.\n'
|
'Your pre-commit configuration is unstaged.\n'
|
||||||
'`git add {}` to fix this.'.format(runner.config_file),
|
'`git add {}` to fix this.'.format(config_file),
|
||||||
)
|
)
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
|
@ -252,7 +253,8 @@ def run(runner, store, args, environ=os.environ):
|
||||||
|
|
||||||
with ctx:
|
with ctx:
|
||||||
repo_hooks = []
|
repo_hooks = []
|
||||||
for repo in repositories(runner.config, store):
|
config = load_config(config_file)
|
||||||
|
for repo in repositories(config, store):
|
||||||
for _, hook in repo.hooks:
|
for _, hook in repo.hooks:
|
||||||
if (
|
if (
|
||||||
(not args.hook or hook['id'] == args.hook) and
|
(not args.hook or hook['id'] == args.hook) and
|
||||||
|
|
@ -267,4 +269,4 @@ def run(runner, store, args, environ=os.environ):
|
||||||
for repo in {repo for repo, _ in repo_hooks}:
|
for repo in {repo for repo, _ in repo_hooks}:
|
||||||
repo.require_installed()
|
repo.require_installed()
|
||||||
|
|
||||||
return _run_hooks(runner.config, repo_hooks, args, environ)
|
return _run_hooks(config, repo_hooks, args, environ)
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,6 @@ from pre_commit import git
|
||||||
from pre_commit import output
|
from pre_commit import output
|
||||||
from pre_commit.clientlib import load_manifest
|
from pre_commit.clientlib import load_manifest
|
||||||
from pre_commit.commands.run import run
|
from pre_commit.commands.run import run
|
||||||
from pre_commit.runner import Runner
|
|
||||||
from pre_commit.store import Store
|
from pre_commit.store import Store
|
||||||
from pre_commit.util import tmpdir
|
from pre_commit.util import tmpdir
|
||||||
|
|
||||||
|
|
@ -43,4 +42,4 @@ def try_repo(args):
|
||||||
output.write(config_s)
|
output.write(config_s)
|
||||||
output.write_line('=' * 79)
|
output.write_line('=' * 79)
|
||||||
|
|
||||||
return run(Runner('.', config_filename), store, args)
|
return run(config_filename, store, args)
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ def get_root():
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def get_git_dir(git_root):
|
def get_git_dir(git_root='.'):
|
||||||
opts = ('--git-common-dir', '--git-dir')
|
opts = ('--git-common-dir', '--git-dir')
|
||||||
_, out, _ = cmd_output('git', 'rev-parse', *opts, cwd=git_root)
|
_, out, _ = cmd_output('git', 'rev-parse', *opts, cwd=git_root)
|
||||||
for line, opt in zip(out.splitlines(), opts):
|
for line, opt in zip(out.splitlines(), opts):
|
||||||
|
|
|
||||||
|
|
@ -88,12 +88,12 @@ def _install_rbenv(prefix, version='default'): # pragma: windows no cover
|
||||||
activate_file.write('export RBENV_VERSION="{}"\n'.format(version))
|
activate_file.write('export RBENV_VERSION="{}"\n'.format(version))
|
||||||
|
|
||||||
|
|
||||||
def _install_ruby(runner, version): # pragma: windows no cover
|
def _install_ruby(prefix, version): # pragma: windows no cover
|
||||||
try:
|
try:
|
||||||
helpers.run_setup_cmd(runner, ('rbenv', 'download', version))
|
helpers.run_setup_cmd(prefix, ('rbenv', 'download', version))
|
||||||
except CalledProcessError: # pragma: no cover (usually find with download)
|
except CalledProcessError: # pragma: no cover (usually find with download)
|
||||||
# Failed to download from mirror for some reason, build it instead
|
# Failed to download from mirror for some reason, build it instead
|
||||||
helpers.run_setup_cmd(runner, ('rbenv', 'install', version))
|
helpers.run_setup_cmd(prefix, ('rbenv', 'install', version))
|
||||||
|
|
||||||
|
|
||||||
def install_environment(
|
def install_environment(
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,6 @@ from pre_commit.commands.sample_config import sample_config
|
||||||
from pre_commit.commands.try_repo import try_repo
|
from pre_commit.commands.try_repo import try_repo
|
||||||
from pre_commit.error_handler import error_handler
|
from pre_commit.error_handler import error_handler
|
||||||
from pre_commit.logging_handler import add_logging_handler
|
from pre_commit.logging_handler import add_logging_handler
|
||||||
from pre_commit.runner import Runner
|
|
||||||
from pre_commit.store import Store
|
from pre_commit.store import Store
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -89,6 +88,20 @@ def _add_run_options(parser):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _adjust_args_and_chdir(args):
|
||||||
|
# `--config` was specified relative to the non-root working directory
|
||||||
|
if os.path.exists(args.config):
|
||||||
|
args.config = os.path.abspath(args.config)
|
||||||
|
if args.command in {'run', 'try-repo'}:
|
||||||
|
args.files = [os.path.abspath(filename) for filename in args.files]
|
||||||
|
|
||||||
|
os.chdir(git.get_root())
|
||||||
|
|
||||||
|
args.config = os.path.relpath(args.config)
|
||||||
|
if args.command in {'run', 'try-repo'}:
|
||||||
|
args.files = [os.path.relpath(filename) for filename in args.files]
|
||||||
|
|
||||||
|
|
||||||
def main(argv=None):
|
def main(argv=None):
|
||||||
argv = argv if argv is not None else sys.argv[1:]
|
argv = argv if argv is not None else sys.argv[1:]
|
||||||
argv = [five.to_text(arg) for arg in argv]
|
argv = [five.to_text(arg) for arg in argv]
|
||||||
|
|
@ -222,43 +235,40 @@ def main(argv=None):
|
||||||
parser.parse_args([args.help_cmd, '--help'])
|
parser.parse_args([args.help_cmd, '--help'])
|
||||||
elif args.command == 'help':
|
elif args.command == 'help':
|
||||||
parser.parse_args(['--help'])
|
parser.parse_args(['--help'])
|
||||||
elif args.command in {'run', 'try-repo'}:
|
|
||||||
args.files = [
|
|
||||||
os.path.relpath(os.path.abspath(filename), git.get_root())
|
|
||||||
for filename in args.files
|
|
||||||
]
|
|
||||||
|
|
||||||
with error_handler():
|
with error_handler():
|
||||||
add_logging_handler(args.color)
|
add_logging_handler(args.color)
|
||||||
runner = Runner.create(args.config)
|
|
||||||
|
_adjust_args_and_chdir(args)
|
||||||
|
|
||||||
store = Store()
|
store = Store()
|
||||||
git.check_for_cygwin_mismatch()
|
git.check_for_cygwin_mismatch()
|
||||||
|
|
||||||
if args.command == 'install':
|
if args.command == 'install':
|
||||||
return install(
|
return install(
|
||||||
runner, store,
|
args.config, store,
|
||||||
overwrite=args.overwrite, hooks=args.install_hooks,
|
overwrite=args.overwrite, hooks=args.install_hooks,
|
||||||
hook_type=args.hook_type,
|
hook_type=args.hook_type,
|
||||||
skip_on_missing_conf=args.allow_missing_config,
|
skip_on_missing_conf=args.allow_missing_config,
|
||||||
)
|
)
|
||||||
elif args.command == 'install-hooks':
|
elif args.command == 'install-hooks':
|
||||||
return install_hooks(runner, store)
|
return install_hooks(args.config, store)
|
||||||
elif args.command == 'uninstall':
|
elif args.command == 'uninstall':
|
||||||
return uninstall(runner, hook_type=args.hook_type)
|
return uninstall(hook_type=args.hook_type)
|
||||||
elif args.command == 'clean':
|
elif args.command == 'clean':
|
||||||
return clean(store)
|
return clean(store)
|
||||||
elif args.command == 'autoupdate':
|
elif args.command == 'autoupdate':
|
||||||
if args.tags_only:
|
if args.tags_only:
|
||||||
logger.warning('--tags-only is the default')
|
logger.warning('--tags-only is the default')
|
||||||
return autoupdate(
|
return autoupdate(
|
||||||
runner, store,
|
args.config, store,
|
||||||
tags_only=not args.bleeding_edge,
|
tags_only=not args.bleeding_edge,
|
||||||
repos=args.repos,
|
repos=args.repos,
|
||||||
)
|
)
|
||||||
elif args.command == 'migrate-config':
|
elif args.command == 'migrate-config':
|
||||||
return migrate_config(runner)
|
return migrate_config(args.config)
|
||||||
elif args.command == 'run':
|
elif args.command == 'run':
|
||||||
return run(runner, store, args)
|
return run(args.config, store, args)
|
||||||
elif args.command == 'sample-config':
|
elif args.command == 'sample-config':
|
||||||
return sample_config()
|
return sample_config()
|
||||||
elif args.command == 'try-repo':
|
elif args.command == 'try-repo':
|
||||||
|
|
|
||||||
|
|
@ -1,36 +0,0 @@
|
||||||
from __future__ import unicode_literals
|
|
||||||
|
|
||||||
import os.path
|
|
||||||
|
|
||||||
from cached_property import cached_property
|
|
||||||
|
|
||||||
from pre_commit import git
|
|
||||||
from pre_commit.clientlib import load_config
|
|
||||||
|
|
||||||
|
|
||||||
class Runner(object):
|
|
||||||
"""A `Runner` represents the execution context of the hooks. Notably the
|
|
||||||
repository under test.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, git_root, config_file):
|
|
||||||
self.git_root = git_root
|
|
||||||
self.config_file = config_file
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def create(cls, config_file):
|
|
||||||
"""Creates a Runner by doing the following:
|
|
||||||
- Finds the root of the current git repository
|
|
||||||
- chdir to that directory
|
|
||||||
"""
|
|
||||||
root = git.get_root()
|
|
||||||
os.chdir(root)
|
|
||||||
return cls(root, config_file)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def config_file_path(self):
|
|
||||||
return os.path.join(self.git_root, self.config_file)
|
|
||||||
|
|
||||||
@cached_property
|
|
||||||
def config(self):
|
|
||||||
return load_config(self.config_file_path)
|
|
||||||
|
|
@ -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 _update_repo
|
||||||
from pre_commit.commands.autoupdate import autoupdate
|
from pre_commit.commands.autoupdate import autoupdate
|
||||||
from pre_commit.commands.autoupdate import RepositoryCannotBeUpdatedError
|
from pre_commit.commands.autoupdate import RepositoryCannotBeUpdatedError
|
||||||
from pre_commit.runner import Runner
|
|
||||||
from pre_commit.util import cmd_output
|
from pre_commit.util import cmd_output
|
||||||
from testing.auto_namedtuple import auto_namedtuple
|
from testing.auto_namedtuple import auto_namedtuple
|
||||||
from testing.fixtures import add_config_to_repo
|
from testing.fixtures import add_config_to_repo
|
||||||
from testing.fixtures import config_with_local_hooks
|
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_config_from_repo
|
||||||
from testing.fixtures import make_repo
|
from testing.fixtures import make_repo
|
||||||
from testing.fixtures import write_config
|
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:
|
with open(C.CONFIG_FILE) as f:
|
||||||
before = f.read()
|
before = f.read()
|
||||||
assert '^$' not in before
|
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:
|
with open(C.CONFIG_FILE) as f:
|
||||||
after = f.read()
|
after = f.read()
|
||||||
assert ret == 0
|
assert ret == 0
|
||||||
|
|
@ -72,7 +70,7 @@ def test_autoupdate_old_revision_broken(tempdir_factory, in_tmpdir, store):
|
||||||
write_config('.', config)
|
write_config('.', config)
|
||||||
with open(C.CONFIG_FILE) as f:
|
with open(C.CONFIG_FILE) as f:
|
||||||
before = f.read()
|
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:
|
with open(C.CONFIG_FILE) as f:
|
||||||
after = f.read()
|
after = f.read()
|
||||||
assert ret == 0
|
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:
|
with open(C.CONFIG_FILE) as f:
|
||||||
before = f.read()
|
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:
|
with open(C.CONFIG_FILE) as f:
|
||||||
after = f.read()
|
after = f.read()
|
||||||
assert ret == 0
|
assert ret == 0
|
||||||
|
|
@ -133,11 +131,10 @@ def test_autoupdate_out_of_date_repo_with_correct_repo_name(
|
||||||
# Write out the config
|
# Write out the config
|
||||||
write_config('.', config)
|
write_config('.', config)
|
||||||
|
|
||||||
runner = Runner('.', C.CONFIG_FILE)
|
|
||||||
with open(C.CONFIG_FILE) as f:
|
with open(C.CONFIG_FILE) as f:
|
||||||
before = f.read()
|
before = f.read()
|
||||||
repo_name = 'file://{}'.format(out_of_date_repo.path)
|
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:
|
with open(C.CONFIG_FILE) as f:
|
||||||
after = f.read()
|
after = f.read()
|
||||||
assert ret == 0
|
assert ret == 0
|
||||||
|
|
@ -155,11 +152,10 @@ def test_autoupdate_out_of_date_repo_with_wrong_repo_name(
|
||||||
)
|
)
|
||||||
write_config('.', config)
|
write_config('.', config)
|
||||||
|
|
||||||
runner = Runner('.', C.CONFIG_FILE)
|
|
||||||
with open(C.CONFIG_FILE) as f:
|
with open(C.CONFIG_FILE) as f:
|
||||||
before = f.read()
|
before = f.read()
|
||||||
# It will not update it, because the name doesn't match
|
# 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:
|
with open(C.CONFIG_FILE) as f:
|
||||||
after = f.read()
|
after = f.read()
|
||||||
assert ret == 0
|
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:
|
with open(C.CONFIG_FILE, 'w') as f:
|
||||||
f.write(config)
|
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:
|
with open(C.CONFIG_FILE) as f:
|
||||||
after = f.read()
|
after = f.read()
|
||||||
expected = fmt.format(out_of_date_repo.path, out_of_date_repo.head_rev)
|
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:
|
with open(C.CONFIG_FILE, 'w') as f:
|
||||||
f.write(config)
|
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:
|
with open(C.CONFIG_FILE) as f:
|
||||||
after = f.read()
|
after = f.read()
|
||||||
expected = (
|
expected = (
|
||||||
|
|
@ -235,7 +231,7 @@ def test_autoupdate_tagged_repo(tagged_repo, in_tmpdir, store):
|
||||||
)
|
)
|
||||||
write_config('.', config)
|
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
|
assert ret == 0
|
||||||
with open(C.CONFIG_FILE) as f:
|
with open(C.CONFIG_FILE) as f:
|
||||||
assert 'v1.2.3' in f.read()
|
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)
|
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
|
assert ret == 0
|
||||||
with open(C.CONFIG_FILE) as f:
|
with open(C.CONFIG_FILE) as f:
|
||||||
assert 'v1.2.3' in f.read()
|
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, 'rm', '-r', ':/')
|
||||||
cmd_output('git', '-C', out_of_date_repo.path, 'commit', '-m', 'rm')
|
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
|
assert ret == 1
|
||||||
with open(C.CONFIG_FILE) as f:
|
with open(C.CONFIG_FILE) as f:
|
||||||
assert out_of_date_repo.original_rev in f.read()
|
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:
|
with open(C.CONFIG_FILE) as f:
|
||||||
before = f.read()
|
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:
|
with open(C.CONFIG_FILE) as f:
|
||||||
after = f.read()
|
after = f.read()
|
||||||
assert ret == 1
|
assert ret == 1
|
||||||
assert before == after
|
assert before == after
|
||||||
|
|
||||||
|
|
||||||
def test_autoupdate_local_hooks(tempdir_factory, store):
|
def test_autoupdate_local_hooks(in_git_dir, store):
|
||||||
git_path = git_dir(tempdir_factory)
|
|
||||||
config = config_with_local_hooks()
|
config = config_with_local_hooks()
|
||||||
path = add_config_to_repo(git_path, config)
|
add_config_to_repo('.', config)
|
||||||
runner = Runner(path, C.CONFIG_FILE)
|
assert autoupdate(C.CONFIG_FILE, store, tags_only=False) == 0
|
||||||
assert autoupdate(runner, store, tags_only=False) == 0
|
new_config_writen = load_config(C.CONFIG_FILE)
|
||||||
new_config_writen = load_config(runner.config_file_path)
|
|
||||||
assert len(new_config_writen['repos']) == 1
|
assert len(new_config_writen['repos']) == 1
|
||||||
assert new_config_writen['repos'][0] == config
|
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()
|
local_config = config_with_local_hooks()
|
||||||
config = {'repos': [local_config, stale_config]}
|
config = {'repos': [local_config, stale_config]}
|
||||||
write_config('.', config)
|
write_config('.', config)
|
||||||
runner = Runner('.', C.CONFIG_FILE)
|
assert autoupdate(C.CONFIG_FILE, store, tags_only=False) == 0
|
||||||
assert autoupdate(runner, store, tags_only=False) == 0
|
new_config_writen = load_config(C.CONFIG_FILE)
|
||||||
new_config_writen = load_config(runner.config_file_path)
|
|
||||||
assert len(new_config_writen['repos']) == 2
|
assert len(new_config_writen['repos']) == 2
|
||||||
assert new_config_writen['repos'][0] == local_config
|
assert new_config_writen['repos'][0] == local_config
|
||||||
|
|
||||||
|
|
@ -355,8 +348,8 @@ def test_autoupdate_meta_hooks(tmpdir, capsys, store):
|
||||||
' hooks:\n'
|
' hooks:\n'
|
||||||
' - id: check-useless-excludes\n',
|
' - id: check-useless-excludes\n',
|
||||||
)
|
)
|
||||||
runner = Runner(tmpdir.strpath, C.CONFIG_FILE)
|
with tmpdir.as_cwd():
|
||||||
ret = autoupdate(runner, store, tags_only=True)
|
ret = autoupdate(C.CONFIG_FILE, store, tags_only=True)
|
||||||
assert ret == 0
|
assert ret == 0
|
||||||
assert cfg.read() == (
|
assert cfg.read() == (
|
||||||
'repos:\n'
|
'repos:\n'
|
||||||
|
|
@ -376,8 +369,8 @@ def test_updates_old_format_to_new_format(tmpdir, capsys, store):
|
||||||
' entry: ./bin/foo.sh\n'
|
' entry: ./bin/foo.sh\n'
|
||||||
' language: script\n',
|
' language: script\n',
|
||||||
)
|
)
|
||||||
runner = Runner(tmpdir.strpath, C.CONFIG_FILE)
|
with tmpdir.as_cwd():
|
||||||
ret = autoupdate(runner, store, tags_only=True)
|
ret = autoupdate(C.CONFIG_FILE, store, tags_only=True)
|
||||||
assert ret == 0
|
assert ret == 0
|
||||||
contents = cfg.read()
|
contents = cfg.read()
|
||||||
assert contents == (
|
assert contents == (
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ from __future__ import unicode_literals
|
||||||
import io
|
import io
|
||||||
import os.path
|
import os.path
|
||||||
import re
|
import re
|
||||||
import shutil
|
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
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 shebang
|
||||||
from pre_commit.commands.install_uninstall import uninstall
|
from pre_commit.commands.install_uninstall import uninstall
|
||||||
from pre_commit.languages import python
|
from pre_commit.languages import python
|
||||||
from pre_commit.runner import Runner
|
|
||||||
from pre_commit.util import cmd_output
|
from pre_commit.util import cmd_output
|
||||||
from pre_commit.util import make_executable
|
from pre_commit.util import make_executable
|
||||||
from pre_commit.util import mkdirp
|
from pre_commit.util import mkdirp
|
||||||
|
|
@ -65,62 +63,45 @@ def test_shebang_returns_default():
|
||||||
assert shebang() == '#!/usr/bin/env python'
|
assert shebang() == '#!/usr/bin/env python'
|
||||||
|
|
||||||
|
|
||||||
def test_install_pre_commit(tempdir_factory, store):
|
def test_install_pre_commit(in_git_dir, store):
|
||||||
path = git_dir(tempdir_factory)
|
assert not install(C.CONFIG_FILE, store)
|
||||||
runner = Runner(path, C.CONFIG_FILE)
|
assert os.access(in_git_dir.join('.git/hooks/pre-commit').strpath, os.X_OK)
|
||||||
assert not install(runner, store)
|
|
||||||
assert os.access(os.path.join(path, '.git/hooks/pre-commit'), os.X_OK)
|
|
||||||
|
|
||||||
assert not install(runner, store, hook_type='pre-push')
|
assert not install(C.CONFIG_FILE, store, hook_type='pre-push')
|
||||||
assert os.access(os.path.join(path, '.git/hooks/pre-push'), os.X_OK)
|
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):
|
def test_install_hooks_directory_not_present(in_git_dir, store):
|
||||||
path = git_dir(tempdir_factory)
|
|
||||||
# Simulate some git clients which don't make .git/hooks #234
|
# Simulate some git clients which don't make .git/hooks #234
|
||||||
hooks = os.path.join(path, '.git/hooks')
|
if in_git_dir.join('.git/hooks').exists(): # pragma: no cover (odd git)
|
||||||
if os.path.exists(hooks): # pragma: no cover (latest git)
|
in_git_dir.join('.git/hooks').remove()
|
||||||
shutil.rmtree(hooks)
|
install(C.CONFIG_FILE, store)
|
||||||
runner = Runner(path, C.CONFIG_FILE)
|
assert in_git_dir.join('.git/hooks/pre-commit').exists()
|
||||||
install(runner, store)
|
|
||||||
assert os.path.exists(os.path.join(path, '.git/hooks/pre-commit'))
|
|
||||||
|
|
||||||
|
|
||||||
def test_install_refuses_core_hookspath(tempdir_factory, store):
|
def test_install_refuses_core_hookspath(in_git_dir, store):
|
||||||
path = git_dir(tempdir_factory)
|
cmd_output('git', 'config', '--local', 'core.hooksPath', 'hooks')
|
||||||
with cwd(path):
|
assert install(C.CONFIG_FILE, store)
|
||||||
cmd_output('git', 'config', '--local', 'core.hooksPath', 'hooks')
|
|
||||||
runner = Runner(path, C.CONFIG_FILE)
|
|
||||||
assert install(runner, store)
|
|
||||||
|
|
||||||
|
|
||||||
@xfailif_no_symlink
|
@xfailif_no_symlink # pragma: no cover (non-windows)
|
||||||
def test_install_hooks_dead_symlink(
|
def test_install_hooks_dead_symlink(in_git_dir, store):
|
||||||
tempdir_factory, store,
|
hook = in_git_dir.join('.git/hooks').ensure_dir().join('pre-commit')
|
||||||
): # pragma: no cover (non-windows)
|
os.symlink('/fake/baz', hook.strpath)
|
||||||
path = git_dir(tempdir_factory)
|
install(C.CONFIG_FILE, store)
|
||||||
runner = Runner(path, C.CONFIG_FILE)
|
assert hook.exists()
|
||||||
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'))
|
|
||||||
|
|
||||||
|
|
||||||
def test_uninstall_does_not_blow_up_when_not_there(tempdir_factory):
|
def test_uninstall_does_not_blow_up_when_not_there(in_git_dir):
|
||||||
path = git_dir(tempdir_factory)
|
assert uninstall() == 0
|
||||||
runner = Runner(path, C.CONFIG_FILE)
|
|
||||||
ret = uninstall(runner)
|
|
||||||
assert ret == 0
|
|
||||||
|
|
||||||
|
|
||||||
def test_uninstall(tempdir_factory, store):
|
def test_uninstall(in_git_dir, store):
|
||||||
path = git_dir(tempdir_factory)
|
assert not in_git_dir.join('.git/hooks/pre-commit').exists()
|
||||||
runner = Runner(path, C.CONFIG_FILE)
|
install(C.CONFIG_FILE, store)
|
||||||
assert not os.path.exists(os.path.join(path, '.git/hooks/pre-commit'))
|
assert in_git_dir.join('.git/hooks/pre-commit').exists()
|
||||||
install(runner, store)
|
uninstall()
|
||||||
assert os.path.exists(os.path.join(path, '.git/hooks/pre-commit'))
|
assert not in_git_dir.join('.git/hooks/pre-commit').exists()
|
||||||
uninstall(runner)
|
|
||||||
assert not os.path.exists(os.path.join(path, '.git/hooks/pre-commit'))
|
|
||||||
|
|
||||||
|
|
||||||
def _get_commit_output(tempdir_factory, touch_file='foo', **kwargs):
|
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):
|
def test_install_pre_commit_and_run(tempdir_factory, store):
|
||||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||||
with cwd(path):
|
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)
|
ret, output = _get_commit_output(tempdir_factory)
|
||||||
assert ret == 0
|
assert ret == 0
|
||||||
|
|
@ -171,7 +152,7 @@ def test_install_pre_commit_and_run_custom_path(tempdir_factory, store):
|
||||||
with cwd(path):
|
with cwd(path):
|
||||||
cmd_output('git', 'mv', C.CONFIG_FILE, 'custom-config.yaml')
|
cmd_output('git', 'mv', C.CONFIG_FILE, 'custom-config.yaml')
|
||||||
cmd_output('git', 'commit', '-m', 'move pre-commit config')
|
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)
|
ret, output = _get_commit_output(tempdir_factory)
|
||||||
assert ret == 0
|
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')
|
sub_pth = os.path.join(parent_path, 'sub')
|
||||||
with cwd(sub_pth):
|
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)
|
ret, output = _get_commit_output(tempdir_factory)
|
||||||
assert ret == 0
|
assert ret == 0
|
||||||
assert NORMAL_PRE_COMMIT_RUN.match(output)
|
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')
|
cmd_output('git', '-C', src_path, 'worktree', 'add', path, '-b', 'master')
|
||||||
|
|
||||||
with cwd(path):
|
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)
|
ret, output = _get_commit_output(tempdir_factory)
|
||||||
assert ret == 0
|
assert ret == 0
|
||||||
assert NORMAL_PRE_COMMIT_RUN.match(output)
|
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:
|
with io.open('unstaged', 'w') as foo_file:
|
||||||
foo_file.write('Oh hai')
|
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)
|
ret, output = _get_commit_output(tempdir_factory)
|
||||||
assert ret == 0
|
assert ret == 0
|
||||||
|
|
@ -225,7 +206,7 @@ def test_commit_am(tempdir_factory, store):
|
||||||
def test_unicode_merge_commit_message(tempdir_factory, store):
|
def test_unicode_merge_commit_message(tempdir_factory, store):
|
||||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||||
with cwd(path):
|
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', 'checkout', 'master', '-b', 'foo')
|
||||||
cmd_output('git', 'commit', '--allow-empty', '-n', '-m', 'branch2')
|
cmd_output('git', 'commit', '--allow-empty', '-n', '-m', 'branch2')
|
||||||
cmd_output('git', 'checkout', 'master')
|
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):
|
def test_install_idempotent(tempdir_factory, store):
|
||||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||||
with cwd(path):
|
with cwd(path):
|
||||||
assert install(Runner(path, C.CONFIG_FILE), store) == 0
|
assert install(C.CONFIG_FILE, store) == 0
|
||||||
assert install(Runner(path, C.CONFIG_FILE), store) == 0
|
assert install(C.CONFIG_FILE, store) == 0
|
||||||
|
|
||||||
ret, output = _get_commit_output(tempdir_factory)
|
ret, output = _get_commit_output(tempdir_factory)
|
||||||
assert ret == 0
|
assert ret == 0
|
||||||
|
|
@ -261,7 +242,7 @@ def test_environment_not_sourced(tempdir_factory, store):
|
||||||
with cwd(path):
|
with cwd(path):
|
||||||
# Patch the executable to simulate rming virtualenv
|
# Patch the executable to simulate rming virtualenv
|
||||||
with mock.patch.object(sys, 'executable', '/does-not-exist'):
|
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
|
# Use a specific homedir to ignore --user installs
|
||||||
homedir = tempdir_factory.get()
|
homedir = tempdir_factory.get()
|
||||||
|
|
@ -300,7 +281,7 @@ FAILING_PRE_COMMIT_RUN = re.compile(
|
||||||
def test_failing_hooks_returns_nonzero(tempdir_factory, store):
|
def test_failing_hooks_returns_nonzero(tempdir_factory, store):
|
||||||
path = make_consuming_repo(tempdir_factory, 'failing_hook_repo')
|
path = make_consuming_repo(tempdir_factory, 'failing_hook_repo')
|
||||||
with cwd(path):
|
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)
|
ret, output = _get_commit_output(tempdir_factory)
|
||||||
assert ret == 1
|
assert ret == 1
|
||||||
|
|
@ -325,8 +306,6 @@ def _write_legacy_hook(path):
|
||||||
def test_install_existing_hooks_no_overwrite(tempdir_factory, store):
|
def test_install_existing_hooks_no_overwrite(tempdir_factory, store):
|
||||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||||
with cwd(path):
|
with cwd(path):
|
||||||
runner = Runner(path, C.CONFIG_FILE)
|
|
||||||
|
|
||||||
_write_legacy_hook(path)
|
_write_legacy_hook(path)
|
||||||
|
|
||||||
# Make sure we installed the "old" hook correctly
|
# 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)
|
assert EXISTING_COMMIT_RUN.match(output)
|
||||||
|
|
||||||
# Now install pre-commit (no-overwrite)
|
# 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
|
# We should run both the legacy and pre-commit hooks
|
||||||
ret, output = _get_commit_output(tempdir_factory)
|
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):
|
def test_install_existing_hook_no_overwrite_idempotent(tempdir_factory, store):
|
||||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||||
with cwd(path):
|
with cwd(path):
|
||||||
runner = Runner(path, C.CONFIG_FILE)
|
|
||||||
|
|
||||||
_write_legacy_hook(path)
|
_write_legacy_hook(path)
|
||||||
|
|
||||||
# Install twice
|
# Install twice
|
||||||
assert install(runner, store) == 0
|
assert install(C.CONFIG_FILE, store) == 0
|
||||||
assert install(runner, store) == 0
|
assert install(C.CONFIG_FILE, store) == 0
|
||||||
|
|
||||||
# We should run both the legacy and pre-commit hooks
|
# We should run both the legacy and pre-commit hooks
|
||||||
ret, output = _get_commit_output(tempdir_factory)
|
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):
|
def test_failing_existing_hook_returns_1(tempdir_factory, store):
|
||||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||||
with cwd(path):
|
with cwd(path):
|
||||||
runner = Runner(path, C.CONFIG_FILE)
|
|
||||||
|
|
||||||
# Write out a failing "old" hook
|
# Write out a failing "old" hook
|
||||||
mkdirp(os.path.join(path, '.git/hooks'))
|
mkdirp(os.path.join(path, '.git/hooks'))
|
||||||
with io.open(os.path.join(path, '.git/hooks/pre-commit'), 'w') as f:
|
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')
|
f.write('#!/usr/bin/env bash\necho "fail!"\nexit 1\n')
|
||||||
make_executable(f.name)
|
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
|
# We should get a failure from the legacy hook
|
||||||
ret, output = _get_commit_output(tempdir_factory)
|
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):
|
def test_install_overwrite_no_existing_hooks(tempdir_factory, store):
|
||||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||||
with cwd(path):
|
with cwd(path):
|
||||||
runner = Runner(path, C.CONFIG_FILE)
|
assert install(C.CONFIG_FILE, store, overwrite=True) == 0
|
||||||
assert install(runner, store, overwrite=True) == 0
|
|
||||||
|
|
||||||
ret, output = _get_commit_output(tempdir_factory)
|
ret, output = _get_commit_output(tempdir_factory)
|
||||||
assert ret == 0
|
assert ret == 0
|
||||||
|
|
@ -402,10 +376,8 @@ def test_install_overwrite_no_existing_hooks(tempdir_factory, store):
|
||||||
def test_install_overwrite(tempdir_factory, store):
|
def test_install_overwrite(tempdir_factory, store):
|
||||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||||
with cwd(path):
|
with cwd(path):
|
||||||
runner = Runner(path, C.CONFIG_FILE)
|
|
||||||
|
|
||||||
_write_legacy_hook(path)
|
_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)
|
ret, output = _get_commit_output(tempdir_factory)
|
||||||
assert ret == 0
|
assert ret == 0
|
||||||
|
|
@ -415,13 +387,11 @@ def test_install_overwrite(tempdir_factory, store):
|
||||||
def test_uninstall_restores_legacy_hooks(tempdir_factory, store):
|
def test_uninstall_restores_legacy_hooks(tempdir_factory, store):
|
||||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||||
with cwd(path):
|
with cwd(path):
|
||||||
runner = Runner(path, C.CONFIG_FILE)
|
|
||||||
|
|
||||||
_write_legacy_hook(path)
|
_write_legacy_hook(path)
|
||||||
|
|
||||||
# Now install and uninstall pre-commit
|
# Now install and uninstall pre-commit
|
||||||
assert install(runner, store) == 0
|
assert install(C.CONFIG_FILE, store) == 0
|
||||||
assert uninstall(runner) == 0
|
assert uninstall() == 0
|
||||||
|
|
||||||
# Make sure we installed the "old" hook correctly
|
# Make sure we installed the "old" hook correctly
|
||||||
ret, output = _get_commit_output(tempdir_factory, touch_file='baz')
|
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):
|
def test_replace_old_commit_script(tempdir_factory, store):
|
||||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||||
with cwd(path):
|
with cwd(path):
|
||||||
runner = Runner(path, C.CONFIG_FILE)
|
|
||||||
|
|
||||||
# Install a script that looks like our old script
|
# Install a script that looks like our old script
|
||||||
pre_commit_contents = resource_text('hook-tmpl')
|
pre_commit_contents = resource_text('hook-tmpl')
|
||||||
new_contents = pre_commit_contents.replace(
|
new_contents = pre_commit_contents.replace(
|
||||||
|
|
@ -446,7 +414,7 @@ def test_replace_old_commit_script(tempdir_factory, store):
|
||||||
make_executable(f.name)
|
make_executable(f.name)
|
||||||
|
|
||||||
# Install normally
|
# Install normally
|
||||||
assert install(runner, store) == 0
|
assert install(C.CONFIG_FILE, store) == 0
|
||||||
|
|
||||||
ret, output = _get_commit_output(tempdir_factory)
|
ret, output = _get_commit_output(tempdir_factory)
|
||||||
assert ret == 0
|
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):
|
def test_uninstall_doesnt_remove_not_our_hooks(tempdir_factory):
|
||||||
path = git_dir(tempdir_factory)
|
path = git_dir(tempdir_factory)
|
||||||
with cwd(path):
|
with cwd(path):
|
||||||
runner = Runner(path, C.CONFIG_FILE)
|
|
||||||
mkdirp(os.path.join(path, '.git/hooks'))
|
mkdirp(os.path.join(path, '.git/hooks'))
|
||||||
with io.open(os.path.join(path, '.git/hooks/pre-commit'), 'w') as f:
|
with io.open(os.path.join(path, '.git/hooks/pre-commit'), 'w') as f:
|
||||||
f.write('#!/usr/bin/env bash\necho 1\n')
|
f.write('#!/usr/bin/env bash\necho 1\n')
|
||||||
make_executable(f.name)
|
make_executable(f.name)
|
||||||
|
|
||||||
assert uninstall(runner) == 0
|
assert uninstall() == 0
|
||||||
|
|
||||||
assert os.path.exists(os.path.join(path, '.git/hooks/pre-commit'))
|
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):
|
def test_installs_hooks_with_hooks_True(tempdir_factory, store):
|
||||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||||
with cwd(path):
|
with cwd(path):
|
||||||
install(Runner(path, C.CONFIG_FILE), store, hooks=True)
|
install(C.CONFIG_FILE, store, hooks=True)
|
||||||
ret, output = _get_commit_output(
|
ret, output = _get_commit_output(
|
||||||
tempdir_factory, pre_commit_home=store.directory,
|
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):
|
def test_install_hooks_command(tempdir_factory, store):
|
||||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||||
with cwd(path):
|
with cwd(path):
|
||||||
runner = Runner(path, C.CONFIG_FILE)
|
install(C.CONFIG_FILE, store)
|
||||||
install(runner, store)
|
install_hooks(C.CONFIG_FILE, store)
|
||||||
install_hooks(runner, store)
|
|
||||||
ret, output = _get_commit_output(
|
ret, output = _get_commit_output(
|
||||||
tempdir_factory, pre_commit_home=store.directory,
|
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):
|
def test_installed_from_venv(tempdir_factory, store):
|
||||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||||
with cwd(path):
|
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!
|
# No environment so pre-commit is not on the path when running!
|
||||||
# Should still pick up the python from when we installed
|
# Should still pick up the python from when we installed
|
||||||
ret, output = _get_commit_output(
|
ret, output = _get_commit_output(
|
||||||
|
|
@ -543,7 +509,7 @@ def test_pre_push_integration_failing(tempdir_factory, store):
|
||||||
path = tempdir_factory.get()
|
path = tempdir_factory.get()
|
||||||
cmd_output('git', 'clone', upstream, path)
|
cmd_output('git', 'clone', upstream, path)
|
||||||
with cwd(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
|
# 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)[0] == 0
|
||||||
assert _get_commit_output(tempdir_factory, touch_file='zzz')[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()
|
path = tempdir_factory.get()
|
||||||
cmd_output('git', 'clone', upstream, path)
|
cmd_output('git', 'clone', upstream, path)
|
||||||
with cwd(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
|
assert _get_commit_output(tempdir_factory)[0] == 0
|
||||||
|
|
||||||
retc, output = _get_push_output(tempdir_factory)
|
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
|
assert _get_push_output(tempdir_factory)[0] == 0
|
||||||
|
|
||||||
with cwd(path2):
|
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
|
assert _get_commit_output(tempdir_factory, commit_msg='force!')[0] == 0
|
||||||
|
|
||||||
retc, output = _get_push_output(tempdir_factory, opts=('--force',))
|
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()
|
path = tempdir_factory.get()
|
||||||
cmd_output('git', 'clone', upstream, path)
|
cmd_output('git', 'clone', upstream, path)
|
||||||
with cwd(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
|
assert _get_commit_output(tempdir_factory)[0] == 0
|
||||||
|
|
||||||
cmd_output('git', 'remote', 'rename', 'origin', 'upstream')
|
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()
|
path = tempdir_factory.get()
|
||||||
cmd_output('git', 'clone', upstream, path)
|
cmd_output('git', 'clone', upstream, path)
|
||||||
with cwd(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)
|
_get_push_output(tempdir_factory)
|
||||||
retc, output = _get_push_output(tempdir_factory)
|
retc, output = _get_push_output(tempdir_factory)
|
||||||
assert output == 'Everything up-to-date\n'
|
assert output == 'Everything up-to-date\n'
|
||||||
|
|
@ -624,8 +590,6 @@ def test_pre_push_legacy(tempdir_factory, store):
|
||||||
path = tempdir_factory.get()
|
path = tempdir_factory.get()
|
||||||
cmd_output('git', 'clone', upstream, path)
|
cmd_output('git', 'clone', upstream, path)
|
||||||
with cwd(path):
|
with cwd(path):
|
||||||
runner = Runner(path, C.CONFIG_FILE)
|
|
||||||
|
|
||||||
mkdirp(os.path.join(path, '.git/hooks'))
|
mkdirp(os.path.join(path, '.git/hooks'))
|
||||||
with io.open(os.path.join(path, '.git/hooks/pre-push'), 'w') as f:
|
with io.open(os.path.join(path, '.git/hooks/pre-push'), 'w') as f:
|
||||||
f.write(
|
f.write(
|
||||||
|
|
@ -637,7 +601,7 @@ def test_pre_push_legacy(tempdir_factory, store):
|
||||||
)
|
)
|
||||||
make_executable(f.name)
|
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
|
assert _get_commit_output(tempdir_factory)[0] == 0
|
||||||
|
|
||||||
retc, output = _get_push_output(tempdir_factory)
|
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(
|
def test_commit_msg_integration_failing(
|
||||||
commit_msg_repo, tempdir_factory, store,
|
commit_msg_repo, tempdir_factory, store,
|
||||||
):
|
):
|
||||||
runner = Runner(commit_msg_repo, C.CONFIG_FILE)
|
install(C.CONFIG_FILE, store, hook_type='commit-msg')
|
||||||
install(runner, store, hook_type='commit-msg')
|
|
||||||
retc, out = _get_commit_output(tempdir_factory)
|
retc, out = _get_commit_output(tempdir_factory)
|
||||||
assert retc == 1
|
assert retc == 1
|
||||||
assert out.startswith('Must have "Signed off by:"...')
|
assert out.startswith('Must have "Signed off by:"...')
|
||||||
|
|
@ -662,8 +625,7 @@ def test_commit_msg_integration_failing(
|
||||||
def test_commit_msg_integration_passing(
|
def test_commit_msg_integration_passing(
|
||||||
commit_msg_repo, tempdir_factory, store,
|
commit_msg_repo, tempdir_factory, store,
|
||||||
):
|
):
|
||||||
runner = Runner(commit_msg_repo, C.CONFIG_FILE)
|
install(C.CONFIG_FILE, store, hook_type='commit-msg')
|
||||||
install(runner, store, hook_type='commit-msg')
|
|
||||||
msg = 'Hi\nSigned off by: me, lol'
|
msg = 'Hi\nSigned off by: me, lol'
|
||||||
retc, out = _get_commit_output(tempdir_factory, commit_msg=msg)
|
retc, out = _get_commit_output(tempdir_factory, commit_msg=msg)
|
||||||
assert retc == 0
|
assert retc == 0
|
||||||
|
|
@ -673,8 +635,6 @@ def test_commit_msg_integration_passing(
|
||||||
|
|
||||||
|
|
||||||
def test_commit_msg_legacy(commit_msg_repo, tempdir_factory, store):
|
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')
|
hook_path = os.path.join(commit_msg_repo, '.git/hooks/commit-msg')
|
||||||
mkdirp(os.path.dirname(hook_path))
|
mkdirp(os.path.dirname(hook_path))
|
||||||
with io.open(hook_path, 'w') as hook_file:
|
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)
|
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'
|
msg = 'Hi\nSigned off by: asottile'
|
||||||
retc, out = _get_commit_output(tempdir_factory, commit_msg=msg)
|
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):
|
def test_install_disallow_mising_config(tempdir_factory, store):
|
||||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||||
with cwd(path):
|
with cwd(path):
|
||||||
runner = Runner(path, C.CONFIG_FILE)
|
|
||||||
|
|
||||||
remove_config_from_repo(path)
|
remove_config_from_repo(path)
|
||||||
ret = install(
|
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
|
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):
|
def test_install_allow_mising_config(tempdir_factory, store):
|
||||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||||
with cwd(path):
|
with cwd(path):
|
||||||
runner = Runner(path, C.CONFIG_FILE)
|
|
||||||
|
|
||||||
remove_config_from_repo(path)
|
remove_config_from_repo(path)
|
||||||
ret = install(
|
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
|
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):
|
def test_install_temporarily_allow_mising_config(tempdir_factory, store):
|
||||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||||
with cwd(path):
|
with cwd(path):
|
||||||
runner = Runner(path, C.CONFIG_FILE)
|
|
||||||
|
|
||||||
remove_config_from_repo(path)
|
remove_config_from_repo(path)
|
||||||
ret = install(
|
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
|
assert ret == 0
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,6 @@ import pytest
|
||||||
import pre_commit.constants as C
|
import pre_commit.constants as C
|
||||||
from pre_commit.commands.migrate_config import _indent
|
from pre_commit.commands.migrate_config import _indent
|
||||||
from pre_commit.commands.migrate_config import migrate_config
|
from pre_commit.commands.migrate_config import migrate_config
|
||||||
from pre_commit.runner import Runner
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
|
|
@ -33,7 +32,8 @@ def test_migrate_config_normal_format(tmpdir, capsys):
|
||||||
' entry: ./bin/foo.sh\n'
|
' entry: ./bin/foo.sh\n'
|
||||||
' language: script\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()
|
out, _ = capsys.readouterr()
|
||||||
assert out == 'Configuration has been migrated.\n'
|
assert out == 'Configuration has been migrated.\n'
|
||||||
contents = cfg.read()
|
contents = cfg.read()
|
||||||
|
|
@ -61,7 +61,8 @@ def test_migrate_config_document_marker(tmpdir):
|
||||||
' entry: ./bin/foo.sh\n'
|
' entry: ./bin/foo.sh\n'
|
||||||
' language: script\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()
|
contents = cfg.read()
|
||||||
assert contents == (
|
assert contents == (
|
||||||
'# comment\n'
|
'# comment\n'
|
||||||
|
|
@ -88,7 +89,8 @@ def test_migrate_config_list_literal(tmpdir):
|
||||||
' }]\n'
|
' }]\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()
|
contents = cfg.read()
|
||||||
assert contents == (
|
assert contents == (
|
||||||
'repos:\n'
|
'repos:\n'
|
||||||
|
|
@ -114,7 +116,8 @@ def test_already_migrated_configuration_noop(tmpdir, capsys):
|
||||||
)
|
)
|
||||||
cfg = tmpdir.join(C.CONFIG_FILE)
|
cfg = tmpdir.join(C.CONFIG_FILE)
|
||||||
cfg.write(contents)
|
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()
|
out, _ = capsys.readouterr()
|
||||||
assert out == 'Configuration is already migrated.\n'
|
assert out == 'Configuration is already migrated.\n'
|
||||||
assert cfg.read() == contents
|
assert cfg.read() == contents
|
||||||
|
|
@ -133,7 +136,8 @@ def test_migrate_config_sha_to_rev(tmpdir):
|
||||||
)
|
)
|
||||||
cfg = tmpdir.join(C.CONFIG_FILE)
|
cfg = tmpdir.join(C.CONFIG_FILE)
|
||||||
cfg.write(contents)
|
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()
|
contents = cfg.read()
|
||||||
assert contents == (
|
assert contents == (
|
||||||
'repos:\n'
|
'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 _get_skips
|
||||||
from pre_commit.commands.run import _has_unmerged_paths
|
from pre_commit.commands.run import _has_unmerged_paths
|
||||||
from pre_commit.commands.run import run
|
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 cmd_output
|
||||||
from pre_commit.util import make_executable
|
from pre_commit.util import make_executable
|
||||||
from testing.fixtures import add_config_to_repo
|
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):
|
def _do_run(cap_out, store, repo, args, environ={}, config_file=C.CONFIG_FILE):
|
||||||
runner = Runner(repo, config_file)
|
with cwd(repo): # replicates `main._adjust_args_and_chdir` behaviour
|
||||||
with cwd(runner.git_root): # replicates Runner.create behaviour
|
ret = run(config_file, store, args, environ=environ)
|
||||||
ret = run(runner, store, args, environ=environ)
|
|
||||||
printed = cap_out.get_bytes()
|
printed = cap_out.get_bytes()
|
||||||
return ret, printed
|
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'] = ['☃']
|
config['repos'][0]['hooks'][0]['args'] = ['☃']
|
||||||
stage_a_file()
|
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
|
# Have to use subprocess because pytest monkeypatches sys.stdout
|
||||||
_, stdout, _ = cmd_output_mocked_pre_commit_home(
|
_, stdout, _ = cmd_output_mocked_pre_commit_home(
|
||||||
|
|
@ -465,7 +463,7 @@ def test_lots_of_files(store, tempdir_factory):
|
||||||
open(filename, 'w').close()
|
open(filename, 'w').close()
|
||||||
|
|
||||||
cmd_output('git', 'add', '.')
|
cmd_output('git', 'add', '.')
|
||||||
install(Runner(git_path, C.CONFIG_FILE), store)
|
install(C.CONFIG_FILE, store)
|
||||||
|
|
||||||
cmd_output_mocked_pre_commit_home(
|
cmd_output_mocked_pre_commit_home(
|
||||||
'git', 'commit', '-m', 'Commit!',
|
'git', 'commit', '-m', 'Commit!',
|
||||||
|
|
|
||||||
|
|
@ -63,6 +63,13 @@ def in_tmpdir(tempdir_factory):
|
||||||
yield path
|
yield path
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def in_git_dir(tmpdir):
|
||||||
|
with tmpdir.as_cwd():
|
||||||
|
cmd_output('git', 'init')
|
||||||
|
yield tmpdir
|
||||||
|
|
||||||
|
|
||||||
def _make_conflict():
|
def _make_conflict():
|
||||||
cmd_output('git', 'checkout', 'origin/master', '-b', 'foo')
|
cmd_output('git', 'checkout', 'origin/master', '-b', 'foo')
|
||||||
with io.open('conflict_file', 'w') as conflict_file:
|
with io.open('conflict_file', 'w') as conflict_file:
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,44 @@ import os.path
|
||||||
import mock
|
import mock
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
import pre_commit.constants as C
|
||||||
from pre_commit import main
|
from pre_commit import main
|
||||||
from testing.auto_namedtuple import auto_namedtuple
|
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 = (
|
FNS = (
|
||||||
|
|
@ -28,18 +63,6 @@ def mock_commands():
|
||||||
mck.stop()
|
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
|
@pytest.fixture
|
||||||
def argparse_parse_args_spy():
|
def argparse_parse_args_spy():
|
||||||
parse_args_mock = mock.Mock()
|
parse_args_mock = mock.Mock()
|
||||||
|
|
@ -62,15 +85,13 @@ def assert_only_one_mock_called(mock_objs):
|
||||||
assert total_call_count == 1
|
assert total_call_count == 1
|
||||||
|
|
||||||
|
|
||||||
def test_overall_help(mock_commands, argparse_exit_mock):
|
def test_overall_help(mock_commands):
|
||||||
with pytest.raises(CalledExit):
|
with pytest.raises(SystemExit):
|
||||||
main.main(['--help'])
|
main.main(['--help'])
|
||||||
|
|
||||||
|
|
||||||
def test_help_command(
|
def test_help_command(mock_commands, argparse_parse_args_spy):
|
||||||
mock_commands, argparse_exit_mock, argparse_parse_args_spy,
|
with pytest.raises(SystemExit):
|
||||||
):
|
|
||||||
with pytest.raises(CalledExit):
|
|
||||||
main.main(['help'])
|
main.main(['help'])
|
||||||
|
|
||||||
argparse_parse_args_spy.assert_has_calls([
|
argparse_parse_args_spy.assert_has_calls([
|
||||||
|
|
@ -79,10 +100,8 @@ def test_help_command(
|
||||||
])
|
])
|
||||||
|
|
||||||
|
|
||||||
def test_help_other_command(
|
def test_help_other_command(mock_commands, argparse_parse_args_spy):
|
||||||
mock_commands, argparse_exit_mock, argparse_parse_args_spy,
|
with pytest.raises(SystemExit):
|
||||||
):
|
|
||||||
with pytest.raises(CalledExit):
|
|
||||||
main.main(['help', 'run'])
|
main.main(['help', 'run'])
|
||||||
|
|
||||||
argparse_parse_args_spy.assert_has_calls([
|
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(
|
def test_help_cmd_in_empty_directory(
|
||||||
|
in_tmpdir,
|
||||||
mock_commands,
|
mock_commands,
|
||||||
tempdir_factory,
|
|
||||||
argparse_exit_mock,
|
|
||||||
argparse_parse_args_spy,
|
argparse_parse_args_spy,
|
||||||
):
|
):
|
||||||
path = tempdir_factory.get()
|
with pytest.raises(SystemExit):
|
||||||
|
main.main(['help', 'run'])
|
||||||
with cwd(path):
|
|
||||||
with pytest.raises(CalledExit):
|
|
||||||
main.main(['help', 'run'])
|
|
||||||
|
|
||||||
argparse_parse_args_spy.assert_has_calls([
|
argparse_parse_args_spy.assert_has_calls([
|
||||||
mock.call(['help', 'run']),
|
mock.call(['help', 'run']),
|
||||||
|
|
@ -122,12 +137,9 @@ def test_help_cmd_in_empty_directory(
|
||||||
])
|
])
|
||||||
|
|
||||||
|
|
||||||
def test_expected_fatal_error_no_git_repo(
|
def test_expected_fatal_error_no_git_repo(in_tmpdir, cap_out, mock_store_dir):
|
||||||
tempdir_factory, cap_out, mock_store_dir,
|
with pytest.raises(SystemExit):
|
||||||
):
|
main.main([])
|
||||||
with cwd(tempdir_factory.get()):
|
|
||||||
with pytest.raises(SystemExit):
|
|
||||||
main.main([])
|
|
||||||
log_file = os.path.join(mock_store_dir, 'pre-commit.log')
|
log_file = os.path.join(mock_store_dir, 'pre-commit.log')
|
||||||
assert cap_out.get() == (
|
assert cap_out.get() == (
|
||||||
'An error has occurred: FatalError: git failed. '
|
'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')
|
_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):
|
def _write(b):
|
||||||
with open('foo', 'wb') as f:
|
with open('foo', 'wb') as f:
|
||||||
f.write(b)
|
f.write(b)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue