diff --git a/pre_commit/commands/autoupdate.py b/pre_commit/commands/autoupdate.py index d93d7e11..f40a7c55 100644 --- a/pre_commit/commands/autoupdate.py +++ b/pre_commit/commands/autoupdate.py @@ -112,14 +112,14 @@ def _write_new_config_file(path, output): 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.""" - migrate_config(runner, quiet=True) + migrate_config(config_file, quiet=True) retv = 0 output_repos = [] changed = False - input_config = load_config(runner.config_file_path) + input_config = load_config(config_file) for repo_config in input_config['repos']: if ( @@ -152,6 +152,6 @@ def autoupdate(runner, store, tags_only, repos=()): if changed: output_config = input_config.copy() 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 diff --git a/pre_commit/commands/install_uninstall.py b/pre_commit/commands/install_uninstall.py index 6bd4602b..3e70b4c9 100644 --- a/pre_commit/commands/install_uninstall.py +++ b/pre_commit/commands/install_uninstall.py @@ -8,6 +8,7 @@ import sys from pre_commit import git from pre_commit import output +from pre_commit.clientlib import load_config from pre_commit.languages import python from pre_commit.repository import repositories from pre_commit.util import cmd_output @@ -31,8 +32,8 @@ TEMPLATE_START = '# start templated\n' TEMPLATE_END = '# end templated\n' -def _hook_paths(git_root, hook_type): - pth = os.path.join(git.get_git_dir(git_root), 'hooks', hook_type) +def _hook_paths(hook_type): + pth = os.path.join(git.get_git_dir(), 'hooks', hook_type) return pth, '{}.legacy'.format(pth) @@ -55,7 +56,8 @@ def shebang(): 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, ): """Install the pre-commit hooks.""" @@ -66,7 +68,7 @@ def install( ) 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)) @@ -84,7 +86,7 @@ def install( ) params = { - 'CONFIG': runner.config_file, + 'CONFIG': config_file, 'HOOK_TYPE': hook_type, 'INSTALL_PYTHON': sys.executable, '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 hooks: - install_hooks(runner, store) + install_hooks(config_file, store) return 0 -def install_hooks(runner, store): - for repository in repositories(runner.config, store): +def install_hooks(config_file, store): + for repository in repositories(load_config(config_file), store): repository.require_installed() -def uninstall(runner, hook_type='pre-commit'): +def uninstall(hook_type='pre-commit'): """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 not os.path.exists(hook_path) or not is_our_script(hook_path): diff --git a/pre_commit/commands/migrate_config.py b/pre_commit/commands/migrate_config.py index b43367fb..3f73bb83 100644 --- a/pre_commit/commands/migrate_config.py +++ b/pre_commit/commands/migrate_config.py @@ -45,15 +45,15 @@ def _migrate_sha_to_rev(contents): return reg.sub(r'\1rev:', contents) -def migrate_config(runner, quiet=False): - with io.open(runner.config_file_path) as f: +def migrate_config(config_file, quiet=False): + with io.open(config_file) as f: orig_contents = contents = f.read() contents = _migrate_map(contents) contents = _migrate_sha_to_rev(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) print('Configuration has been migrated.') diff --git a/pre_commit/commands/run.py b/pre_commit/commands/run.py index dbf56410..f2ff7b38 100644 --- a/pre_commit/commands/run.py +++ b/pre_commit/commands/run.py @@ -11,6 +11,7 @@ from identify.identify import tags_from_path from pre_commit import color from pre_commit import git from pre_commit import output +from pre_commit.clientlib import load_config from pre_commit.output import get_hook_message from pre_commit.repository import repositories from pre_commit.staged_files_only import staged_files_only @@ -214,16 +215,16 @@ def _has_unmerged_paths(): return bool(stdout.strip()) -def _has_unstaged_config(runner): +def _has_unstaged_config(config_file): 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, ) # be explicit, other git errors don't mean it has an unstaged config. 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) # 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): logger.error('Specify both --origin and --source.') return 1 - if _has_unstaged_config(runner) and not no_stash: + if _has_unstaged_config(config_file) and not no_stash: logger.error( '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 @@ -252,7 +253,8 @@ def run(runner, store, args, environ=os.environ): with ctx: 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: if ( (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}: repo.require_installed() - return _run_hooks(runner.config, repo_hooks, args, environ) + return _run_hooks(config, repo_hooks, args, environ) diff --git a/pre_commit/commands/try_repo.py b/pre_commit/commands/try_repo.py index 431db141..e964987c 100644 --- a/pre_commit/commands/try_repo.py +++ b/pre_commit/commands/try_repo.py @@ -11,7 +11,6 @@ from pre_commit import git from pre_commit import output from pre_commit.clientlib import load_manifest from pre_commit.commands.run import run -from pre_commit.runner import Runner from pre_commit.store import Store from pre_commit.util import tmpdir @@ -43,4 +42,4 @@ def try_repo(args): output.write(config_s) output.write_line('=' * 79) - return run(Runner('.', config_filename), store, args) + return run(config_filename, store, args) diff --git a/pre_commit/git.py b/pre_commit/git.py index a9261163..84db66ea 100644 --- a/pre_commit/git.py +++ b/pre_commit/git.py @@ -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') _, out, _ = cmd_output('git', 'rev-parse', *opts, cwd=git_root) for line, opt in zip(out.splitlines(), opts): diff --git a/pre_commit/languages/ruby.py b/pre_commit/languages/ruby.py index 7bd14f19..484df47c 100644 --- a/pre_commit/languages/ruby.py +++ b/pre_commit/languages/ruby.py @@ -88,12 +88,12 @@ def _install_rbenv(prefix, version='default'): # pragma: windows no cover 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: - 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) # 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( diff --git a/pre_commit/main.py b/pre_commit/main.py index fafe36b1..a5a4a817 100644 --- a/pre_commit/main.py +++ b/pre_commit/main.py @@ -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.error_handler import error_handler from pre_commit.logging_handler import add_logging_handler -from pre_commit.runner import Runner 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): argv = argv if argv is not None else sys.argv[1:] argv = [five.to_text(arg) for arg in argv] @@ -222,43 +235,40 @@ def main(argv=None): parser.parse_args([args.help_cmd, '--help']) elif args.command == '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(): add_logging_handler(args.color) - runner = Runner.create(args.config) + + _adjust_args_and_chdir(args) + store = Store() git.check_for_cygwin_mismatch() if args.command == 'install': return install( - runner, store, + args.config, store, overwrite=args.overwrite, hooks=args.install_hooks, hook_type=args.hook_type, skip_on_missing_conf=args.allow_missing_config, ) elif args.command == 'install-hooks': - return install_hooks(runner, store) + return install_hooks(args.config, store) elif args.command == 'uninstall': - return uninstall(runner, hook_type=args.hook_type) + return uninstall(hook_type=args.hook_type) elif args.command == 'clean': return clean(store) elif args.command == 'autoupdate': if args.tags_only: logger.warning('--tags-only is the default') return autoupdate( - runner, store, + args.config, store, tags_only=not args.bleeding_edge, repos=args.repos, ) elif args.command == 'migrate-config': - return migrate_config(runner) + return migrate_config(args.config) elif args.command == 'run': - return run(runner, store, args) + return run(args.config, store, args) elif args.command == 'sample-config': return sample_config() elif args.command == 'try-repo': diff --git a/pre_commit/runner.py b/pre_commit/runner.py deleted file mode 100644 index 53107007..00000000 --- a/pre_commit/runner.py +++ /dev/null @@ -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) diff --git a/tests/commands/autoupdate_test.py b/tests/commands/autoupdate_test.py index b6e81b2a..34c7292b 100644 --- a/tests/commands/autoupdate_test.py +++ b/tests/commands/autoupdate_test.py @@ -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 == ( diff --git a/tests/commands/install_uninstall_test.py b/tests/commands/install_uninstall_test.py index dbf663e9..25a21641 100644 --- a/tests/commands/install_uninstall_test.py +++ b/tests/commands/install_uninstall_test.py @@ -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 diff --git a/tests/commands/migrate_config_test.py b/tests/commands/migrate_config_test.py index a2a34b66..da599f10 100644 --- a/tests/commands/migrate_config_test.py +++ b/tests/commands/migrate_config_test.py @@ -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' diff --git a/tests/commands/run_test.py b/tests/commands/run_test.py index e6258d31..bb233f28 100644 --- a/tests/commands/run_test.py +++ b/tests/commands/run_test.py @@ -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!', diff --git a/tests/conftest.py b/tests/conftest.py index 82daccd4..95fc410e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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: diff --git a/tests/main_test.py b/tests/main_test.py index 65adc477..83e7d22f 100644 --- a/tests/main_test.py +++ b/tests/main_test.py @@ -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. ' diff --git a/tests/runner_test.py b/tests/runner_test.py deleted file mode 100644 index 8d1c0421..00000000 --- a/tests/runner_test.py +++ /dev/null @@ -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 diff --git a/tests/staged_files_only_test.py b/tests/staged_files_only_test.py index 42f7ecae..73a6b585 100644 --- a/tests/staged_files_only_test.py +++ b/tests/staged_files_only_test.py @@ -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)