def test_logging_handler_no_color(): print_mock = mock.Mock() handler = LoggingHandler(False, print_mock) handler.emit(FakeLogRecord('hi', 'WARNING', 30)) print_mock.assert_called_once_with( '[WARNING] hi\n', )
def test_logging_handler_color(): print_mock = mock.Mock() handler = LoggingHandler(True, print_mock) handler.emit(FakeLogRecord('hi', 'WARNING', 30)) print_mock.assert_called_once_with( color.YELLOW + '[WARNING]' + color.NORMAL + ' hi\n', )
def run(runner, args, write=sys_stdout_write_wrapper, environ=os.environ): # Set up our logging handler logger.addHandler(LoggingHandler(args.color, write=write)) logger.setLevel(logging.INFO) # Check if we have unresolved merge conflict files and fail fast. if _has_unmerged_paths(runner): logger.error('Unmerged files. Resolve before committing.') return 1 if (args.source and not args.origin) or \ (args.origin and not args.source): logger.error('--origin and --source depend on each other.') return 1 # Don't stash if specified or files are specified if args.no_stash or args.all_files or args.files: ctx = noop_context() else: ctx = staged_files_only(runner.cmd_runner) with ctx: if args.hook: return _run_hook(runner, args, write=write) else: return _run_hooks(runner, args, write=write, environ=environ)
def install(runner, overwrite=False, hooks=False): """Install the pre-commit hooks.""" pre_commit_file = resource_filename('pre-commit-hook') # If we have an existing hook, move it to pre-commit.legacy if (os.path.exists(runner.pre_commit_path) and not is_our_pre_commit(runner.pre_commit_path) and not is_previous_pre_commit(runner.pre_commit_path)): os.rename(runner.pre_commit_path, runner.pre_commit_legacy_path) # If we specify overwrite, we simply delete the legacy file if overwrite and os.path.exists(runner.pre_commit_legacy_path): os.remove(runner.pre_commit_legacy_path) elif os.path.exists(runner.pre_commit_legacy_path): print('Running in migration mode with existing hooks at {0}\n' 'Use -f to use only pre-commit.'.format( runner.pre_commit_legacy_path, )) with io.open(runner.pre_commit_path, 'w') as pre_commit_file_obj: contents = io.open(pre_commit_file).read().format( sys_executable=sys.executable, ) pre_commit_file_obj.write(contents) make_executable(runner.pre_commit_path) print('pre-commit installed at {0}'.format(runner.pre_commit_path)) # If they requested we install all of the hooks, do so. if hooks: # Set up our logging handler logger.addHandler(LoggingHandler(False)) logger.setLevel(logging.INFO) for repository in runner.repositories: repository.require_installed() return 0
def run(runner, args, write=sys_stdout_write_wrapper, environ=os.environ): no_stash = args.no_stash or args.all_files or bool(args.files) # Set up our logging handler logger.addHandler(LoggingHandler(args.color, write=write)) logger.setLevel(logging.INFO) # Check if we have unresolved merge conflict files and fail fast. if _has_unmerged_paths(runner): logger.error('Unmerged files. Resolve before committing.') return 1 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 args.allow_unstaged_config: logger.warn( 'You have an unstaged config file and have specified the ' '--allow-unstaged-config option.\n' 'Note that your config will be stashed before the config is ' 'parsed unless --no-stash is specified.', ) else: logger.error( 'Your .pre-commit-config.yaml is unstaged.\n' '`git add .pre-commit-config.yaml` to fix this.\n' 'Run pre-commit with --allow-unstaged-config to silence this.' ) return 1 if no_stash: ctx = noop_context() else: ctx = staged_files_only(runner.cmd_runner) with ctx: repo_hooks = list(get_repo_hooks(runner)) if args.hook: repo_hooks = [ (repo, hook) for repo, hook in repo_hooks if hook['id'] == args.hook ] if not repo_hooks: write('No hook with id `{}`\n'.format(args.hook)) return 1 # Filter hooks for stages repo_hooks = [ (repo, hook) for repo, hook in repo_hooks if not hook['stages'] or args.hook_stage in hook['stages'] ] return _run_hooks(repo_hooks, args, write, environ)
def autoupdate(runner): """Auto-update the pre-commit config to the latest versions of repos.""" # Set up our logging handler logger.addHandler(LoggingHandler(False)) logger.setLevel(logging.WARNING) retv = 0 output_configs = [] changed = False input_configs = load_config( runner.config_file_path, load_strategy=ordered_load, ) for repo_config in input_configs: if is_local_hooks(repo_config): output_configs.append(repo_config) continue sys.stdout.write('Updating {}...'.format(repo_config['repo'])) sys.stdout.flush() try: new_repo_config = _update_repository(repo_config, runner) except RepositoryCannotBeUpdatedError as error: print(error.args[0]) output_configs.append(repo_config) retv = 1 continue if new_repo_config['sha'] != repo_config['sha']: changed = True print( 'updating {} -> {}.'.format( repo_config['sha'], new_repo_config['sha'], ) ) output_configs.append(new_repo_config) else: print('already up to date.') output_configs.append(repo_config) if changed: with open(runner.config_file_path, 'w') as config_file: config_file.write( ordered_dump( remove_defaults(output_configs, CONFIG_JSON_SCHEMA), **C.YAML_DUMP_KWARGS ) ) return retv
def install(runner, overwrite=False, hooks=False, hook_type='pre-commit'): """Install the pre-commit hooks.""" hook_path = runner.get_hook_path(hook_type) legacy_path = hook_path + '.legacy' mkdirp(os.path.dirname(hook_path)) # If we have an existing hook, move it to pre-commit.legacy if (os.path.lexists(hook_path) and not is_our_pre_commit(hook_path) and not is_previous_pre_commit(hook_path)): os.rename(hook_path, legacy_path) # If we specify overwrite, we simply delete the legacy file if overwrite and os.path.exists(legacy_path): os.remove(legacy_path) elif os.path.exists(legacy_path): print('Running in migration mode with existing hooks at {0}\n' 'Use -f to use only pre-commit.'.format(legacy_path, )) with io.open(hook_path, 'w') as pre_commit_file_obj: if hook_type == 'pre-push': with io.open(resource_filename('pre-push-tmpl')) as fp: pre_push_contents = fp.read() else: pre_push_contents = '' contents = io.open(resource_filename('hook-tmpl')).read().format( sys_executable=sys.executable, hook_type=hook_type, pre_push=pre_push_contents, ) pre_commit_file_obj.write(contents) make_executable(hook_path) print('pre-commit installed at {0}'.format(hook_path)) # If they requested we install all of the hooks, do so. if hooks: # Set up our logging handler logger.addHandler(LoggingHandler(False)) logger.setLevel(logging.INFO) for repository in runner.repositories: repository.require_installed() return 0
def test_logging_handler_no_color(cap_out): handler = LoggingHandler(False) handler.emit(FakeLogRecord('hi', 'WARNING', 30)) assert cap_out.get() == '[WARNING] hi\n'
def test_logging_handler_color(cap_out): handler = LoggingHandler(True) handler.emit(FakeLogRecord('hi', 'WARNING', 30)) ret = cap_out.get() assert ret == color.YELLOW + '[WARNING]' + color.NORMAL + ' hi\n'
def test_logging_handler_no_color(cap_out): handler = LoggingHandler(False) handler.emit(_log_record('hi', logging.WARNING)) assert cap_out.get() == '[WARNING] hi\n'
def test_logging_handler_color(cap_out): handler = LoggingHandler(True) handler.emit(_log_record('hi', logging.WARNING)) ret = cap_out.get() assert ret == f'{color.YELLOW}[WARNING]{color.NORMAL} hi\n'
def test_logging_handler_no_color(): print_mock = mock.Mock() handler = LoggingHandler(False, print_mock) handler.emit(FakeLogRecord('hi', 'WARNING', 30)) print_mock.assert_called_once_with('[WARNING] hi\n', )