def test_lots_of_files(mock_out_store_directory, tempdir_factory): # windows xargs seems to have a bug, here's a regression test for # our workaround git_path = make_consuming_repo(tempdir_factory, 'python_hooks_repo') with cwd(git_path): # Override files so we run against them with modify_config() as config: config['repos'][0]['hooks'][0]['files'] = '' # Write a crap ton of files for i in range(400): filename = '{}{}'.format('a' * 100, i) open(filename, 'w').close() cmd_output('bash', '-c', 'git add .') install(Runner(git_path, C.CONFIG_FILE)) cmd_output_mocked_pre_commit_home( 'git', 'commit', '-m', 'Commit!', # git commit puts pre-commit to stderr stderr=subprocess.STDOUT, tempdir_factory=tempdir_factory, )
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 cmd_output('git', 'checkout', 'master', '-b', 'foo') cmd_output('git', 'commit', '--allow-empty', '-n', '-m', 'branch2') cmd_output('git', 'checkout', 'master') cmd_output('git', 'merge', 'foo', '--no-ff', '--no-commit', '-m', '☃') # Used to crash cmd_output_mocked_pre_commit_home( 'git', 'commit', '--no-edit', tempdir_factory=tempdir_factory, )
def test_pre_merge_commit_integration(tempdir_factory, store): expected = re.compile( r'^\[INFO\] Initializing environment for .+\n' r'Bash hook\.+Passed\n' r"Merge made by the 'recursive' strategy.\n" r' foo \| 0\n' r' 1 file changed, 0 insertions\(\+\), 0 deletions\(-\)\n' r' create mode 100644 foo\n$', ) path = make_consuming_repo(tempdir_factory, 'script_hooks_repo') with cwd(path): ret = install(C.CONFIG_FILE, store, hook_types=['pre-merge-commit']) assert ret == 0 cmd_output('git', 'checkout', 'master', '-b', 'feature') _get_commit_output(tempdir_factory) cmd_output('git', 'checkout', 'master') ret, output, _ = cmd_output_mocked_pre_commit_home( 'git', 'merge', '--no-ff', '--no-edit', 'feature', tempdir_factory=tempdir_factory, ) assert ret == 0 assert expected.match(output)
def test_files_running_subdir( repo_with_passing_hook, mock_out_store_directory, tempdir_factory, ): with cwd(repo_with_passing_hook): install(Runner(repo_with_passing_hook, C.CONFIG_FILE)) os.mkdir('subdir') open('subdir/foo.py', 'w').close() cmd_output('git', 'add', 'subdir/foo.py') with cwd('subdir'): # Use subprocess to demonstrate behaviour in main _, stdout, _ = cmd_output_mocked_pre_commit_home( sys.executable, '-m', 'pre_commit.main', 'run', '-v', # Files relative to where we are (#339) '--files', 'foo.py', tempdir_factory=tempdir_factory, ) assert 'subdir/foo.py'.replace('/', os.sep) in stdout
def test_stdout_write_bug_py26( repo_with_failing_hook, mock_out_store_directory, tempdir_factory, ): with cwd(repo_with_failing_hook): with modify_config() as config: config['repos'][0]['hooks'][0]['args'] = ['☃'] stage_a_file() install(Runner(repo_with_failing_hook, C.CONFIG_FILE)) # Have to use subprocess because pytest monkeypatches sys.stdout _, stdout, _ = cmd_output_mocked_pre_commit_home( 'git', 'commit', '-m', 'Commit!', # git commit puts pre-commit to stderr stderr=subprocess.STDOUT, retcode=None, tempdir_factory=tempdir_factory, ) assert 'UnicodeEncodeError' not in stdout # Doesn't actually happen, but a reasonable assertion assert 'UnicodeDecodeError' not in stdout
def _get_push_output(tempdir_factory): return cmd_output_mocked_pre_commit_home( 'git', 'push', 'origin', 'HEAD:new_branch', # git push puts pre-commit to stderr stderr=subprocess.STDOUT, tempdir_factory=tempdir_factory, retcode=None, )[:2]
def _get_push_output(tempdir_factory, opts=()): return cmd_output_mocked_pre_commit_home('git', 'push', 'origin', 'HEAD:new_branch', *opts, tempdir_factory=tempdir_factory, retcode=None)[:2]
def test_non_ascii_hook_id(repo_with_passing_hook, tempdir_factory): with cwd(repo_with_passing_hook): _, stdout, _ = cmd_output_mocked_pre_commit_home( sys.executable, '-m', 'pre_commit.main', 'run', '☃', retcode=None, tempdir_factory=tempdir_factory, ) assert 'UnicodeDecodeError' not in stdout # Doesn't actually happen, but a reasonable assertion assert 'UnicodeEncodeError' not in stdout
def _get_commit_output(tempdir_factory, touch_file='foo', **kwargs): cmd_output('touch', touch_file) cmd_output('git', 'add', touch_file) return cmd_output_mocked_pre_commit_home( 'git', 'commit', '-am', 'Commit!', '--allow-empty', # git commit puts pre-commit to stderr stderr=subprocess.STDOUT, retcode=None, tempdir_factory=tempdir_factory, **kwargs )[:2]
def test_lots_of_files(mock_out_store_directory, tempdir_factory): # windows xargs seems to have a bug, here's a regression test for # our workaround git_path = make_consuming_repo(tempdir_factory, 'python_hooks_repo') with cwd(git_path): # Override files so we run against them with modify_config() as config: config[0]['hooks'][0]['files'] = '' # Write a crap ton of files for i in range(400): filename = '{}{}'.format('a' * 100, i) open(filename, 'w').close() cmd_output('bash', '-c', 'git add .') install(Runner(git_path, C.CONFIG_FILE)) cmd_output_mocked_pre_commit_home( 'git', 'commit', '-m', 'Commit!', # git commit puts pre-commit to stderr stderr=subprocess.STDOUT, tempdir_factory=tempdir_factory, )
def test_error_handler_no_tty(tempdir_factory): output = cmd_output_mocked_pre_commit_home( sys.executable, '-c', 'from __future__ import unicode_literals\n' 'from pre_commit.error_handler import error_handler\n' 'with error_handler():\n' ' raise ValueError("\\u2603")\n', retcode=1, tempdir_factory=tempdir_factory, ) assert output[1].replace('\r', '') == ( 'An unexpected error has occurred: ValueError: ☃\n' 'Check the log at ~/.pre-commit/pre-commit.log\n' )
def test_files_running_subdir(repo_with_passing_hook, tempdir_factory): with cwd(repo_with_passing_hook): os.mkdir('subdir') open('subdir/foo.py', 'w').close() cmd_output('git', 'add', 'subdir/foo.py') with cwd('subdir'): # Use subprocess to demonstrate behaviour in main _, stdout, _ = cmd_output_mocked_pre_commit_home( sys.executable, '-m', 'pre_commit.main', 'run', '-v', # Files relative to where we are (#339) '--files', 'foo.py', tempdir_factory=tempdir_factory, ) assert 'subdir/foo.py'.replace('/', os.sep) in stdout
def test_error_handler_no_tty(tempdir_factory): pre_commit_home = tempdir_factory.get() ret, out, _ = cmd_output_mocked_pre_commit_home( sys.executable, '-c', 'from pre_commit.error_handler import error_handler\n' 'with error_handler():\n' ' raise ValueError("\\u2603")\n', retcode=1, tempdir_factory=tempdir_factory, pre_commit_home=pre_commit_home, ) log_file = os.path.join(pre_commit_home, 'pre-commit.log') out_lines = out.splitlines() assert out_lines[-2] == 'An unexpected error has occurred: ValueError: ☃' assert out_lines[-1] == f'Check the log at {log_file}'
def test_error_handler_no_tty(tempdir_factory): pre_commit_home = tempdir_factory.get() output = cmd_output_mocked_pre_commit_home( sys.executable, '-c', 'from __future__ import unicode_literals\n' 'from pre_commit.error_handler import error_handler\n' 'with error_handler():\n' ' raise ValueError("\\u2603")\n', retcode=1, tempdir_factory=tempdir_factory, pre_commit_home=pre_commit_home, ) log_file = os.path.join(pre_commit_home, 'pre-commit.log') assert output[1].replace('\r', '') == ( 'An unexpected error has occurred: ValueError: ☃\n' 'Check the log at {}\n'.format(log_file) )
def test_post_merge_integration(tempdir_factory, store): path = git_dir(tempdir_factory) config = [ { 'repo': 'local', 'hooks': [{ 'id': 'post-merge', 'name': 'Post merge', 'entry': 'touch post-merge.tmp', 'language': 'system', 'always_run': True, 'verbose': True, 'stages': ['post-merge'], }], }, ] write_config(path, config) with cwd(path): # create a simple diamond of commits for a non-trivial merge open('init', 'a').close() cmd_output('git', 'add', '.') git_commit() open('master', 'a').close() cmd_output('git', 'add', '.') git_commit() cmd_output('git', 'checkout', '-b', 'branch', 'HEAD^') open('branch', 'a').close() cmd_output('git', 'add', '.') git_commit() cmd_output('git', 'checkout', 'master') install(C.CONFIG_FILE, store, hook_types=['post-merge']) retc, stdout, stderr = cmd_output_mocked_pre_commit_home( 'git', 'merge', 'branch', tempdir_factory=tempdir_factory, ) assert retc == 0 assert os.path.exists('post-merge.tmp')
def test_stdout_write_bug_py26( repo_with_failing_hook, mock_out_store_directory, tempdir_factory, ): with cwd(repo_with_failing_hook): with modify_config() as config: config[0]['hooks'][0]['args'] = ['☃'] stage_a_file() install(Runner(repo_with_failing_hook, C.CONFIG_FILE)) # Have to use subprocess because pytest monkeypatches sys.stdout _, stdout, _ = cmd_output_mocked_pre_commit_home( 'git', 'commit', '-m', 'Commit!', # git commit puts pre-commit to stderr stderr=subprocess.STDOUT, retcode=None, tempdir_factory=tempdir_factory, ) assert 'UnicodeEncodeError' not in stdout # Doesn't actually happen, but a reasonable assertion assert 'UnicodeDecodeError' not in stdout
def test_hook_install_failure(mock_out_store_directory, tempdir_factory): git_path = make_consuming_repo(tempdir_factory, 'not_installable_repo') with cwd(git_path): install(Runner(git_path, C.CONFIG_FILE)) _, stdout, _ = cmd_output_mocked_pre_commit_home( 'git', 'commit', '-m', 'Commit!', # git commit puts pre-commit to stderr stderr=subprocess.STDOUT, retcode=None, encoding=None, tempdir_factory=tempdir_factory, ) assert b'UnicodeDecodeError' not in stdout # Doesn't actually happen, but a reasonable assertion assert b'UnicodeEncodeError' not in stdout # Sanity check our output assert ( b'An unexpected error has occurred: CalledProcessError: ' in stdout ) assert '☃'.encode('UTF-8') + '²'.encode('latin1') in stdout