def test_pre_merge_commit_integration(tempdir_factory, store): output_pattern = re_assert.Matches( r'^\[INFO\] Initializing environment for .+\n' r'Bash hook\.+Passed\n' r"Merge made by the '(ort|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 output_pattern.assert_matches(output)
def test_try_repo_with_specific_hook(cap_out, tempdir_factory): _run_try_repo(tempdir_factory, hook='bash_hook', verbose=True) start, config, rest = _get_out(cap_out) assert start == '' config_pattern = re_assert.Matches( '^repos:\n' '- repo: .+\n' ' rev: .+\n' ' hooks:\n' ' - id: bash_hook\n$', ) config_pattern.assert_matches(config) assert rest == '''\
def test_too_new_version(tempdir_factory, store, fake_log_handler): path = make_repo(tempdir_factory, 'script_hooks_repo') with modify_manifest(path) as manifest: manifest[0]['minimum_pre_commit_version'] = '999.0.0' config = make_config_from_repo(path) with pytest.raises(SystemExit): _get_hook(config, store, 'bash_hook') msg = fake_log_handler.handle.call_args[0][0].msg pattern = re_assert.Matches( r'^The hook `bash_hook` requires pre-commit version 999\.0\.0 but ' r'version \d+\.\d+\.\d+ is installed. ' r'Perhaps run `pip install --upgrade pre-commit`\.$', ) pattern.assert_matches(msg)
def test_try_repo_repo_only(cap_out, tempdir_factory): with mock.patch.object(time, 'time', return_value=0.0): _run_try_repo(tempdir_factory, verbose=True) start, config, rest = _get_out(cap_out) assert start == '' config_pattern = re_assert.Matches( '^repos:\n' '- repo: .+\n' ' rev: .+\n' ' hooks:\n' ' - id: bash_hook\n' ' - id: bash_hook2\n' ' - id: bash_hook3\n$', ) config_pattern.assert_matches(config) assert rest == '''\
def test_local_dart_additional_dependencies_versioned(store): config = { 'repo': 'local', 'hooks': [{ 'id': 'local-dart', 'name': 'local-dart', 'entry': 'secure-random -l 4 -b 16', 'language': 'dart', 'additional_dependencies': ['encrypt:5.0.0'], }], } hook = _get_hook(config, store, 'local-dart') ret, out = _hook_run(hook, (), color=False) assert ret == 0 re_assert.Matches('^[a-f0-9]{8}\r?\n$').assert_matches(out.decode())
def test_log_and_exit(cap_out, mock_store_dir): tb = ( 'Traceback (most recent call last):\n' ' File "<stdin>", line 2, in <module>\n' 'pre_commit.errors.FatalError: hai\n' ) with pytest.raises(SystemExit) as excinfo: error_handler._log_and_exit('msg', 1, FatalError('hai'), tb) assert excinfo.value.code == 1 printed = cap_out.get() log_file = os.path.join(mock_store_dir, 'pre-commit.log') assert printed == f'msg: FatalError: hai\nCheck the log at {log_file}\n' assert os.path.exists(log_file) with open(log_file) as f: logged = f.read() pattern = re_assert.Matches( r'^### version information\n' r'\n' r'```\n' r'pre-commit version: \d+\.\d+\.\d+\n' r'git --version: git version .+\n' r'sys.version:\n' r'( .*\n)*' r'sys.executable: .*\n' r'os.name: .*\n' r'sys.platform: .*\n' r'```\n' r'\n' r'### error information\n' r'\n' r'```\n' r'msg: FatalError: hai\n' r'```\n' r'\n' r'```\n' r'Traceback \(most recent call last\):\n' r' File "<stdin>", line 2, in <module>\n' r'pre_commit\.errors\.FatalError: hai\n' r'```\n', ) pattern.assert_matches(logged)
def test_error_handler_keyboardinterrupt(mocked_log_and_exit): exc = KeyboardInterrupt() with error_handler.error_handler(): raise exc mocked_log_and_exit.assert_called_once_with( 'Interrupted (^C)', exc, # Tested below mock.ANY, ) pattern = re_assert.Matches( r'Traceback \(most recent call last\):\n' r' File ".+pre_commit.error_handler.py", line \d+, in error_handler\n' r' yield\n' r' File ".+tests.error_handler_test.py", line \d+, ' r'in test_error_handler_keyboardinterrupt\n' r' raise exc\n' r'KeyboardInterrupt\n', ) pattern.assert_matches(mocked_log_and_exit.call_args[0][2])
def test_error_handler_uncaught_error(mocked_log_and_exit): exc = ValueError('another test') with error_handler.error_handler(): raise exc mocked_log_and_exit.assert_called_once_with( 'An unexpected error has occurred', exc, # Tested below mock.ANY, ) pattern = re_assert.Matches( r'Traceback \(most recent call last\):\n' r' File ".+pre_commit.error_handler.py", line \d+, in error_handler\n' r' yield\n' r' File ".+tests.error_handler_test.py", line \d+, ' r'in test_error_handler_uncaught_error\n' r' raise exc\n' r'ValueError: another test\n', ) pattern.assert_matches(mocked_log_and_exit.call_args[0][2])
def test_try_repo_uncommitted_changes(cap_out, tempdir_factory): repo = make_repo(tempdir_factory, 'script_hooks_repo') # make an uncommitted change with modify_manifest(repo, commit=False) as manifest: manifest[0]['name'] = 'modified name!' with cwd(git_dir(tempdir_factory)): open('test-fie', 'a').close() cmd_output('git', 'add', '.') assert not try_repo(try_repo_opts(repo)) start, config, rest = _get_out(cap_out) assert start == '[WARNING] Creating temporary repo with uncommitted changes...\n' # noqa: E501 config_pattern = re_assert.Matches( '^repos:\n' '- repo: .+shadow-repo\n' ' rev: .+\n' ' hooks:\n' ' - id: bash_hook\n$', ) config_pattern.assert_matches(config) assert rest == 'modified name!...........................................................Passed\n' # noqa: E501
def test_error_handler_fatal_error(mocked_log_and_exit): exc = FatalError('just a test') with error_handler.error_handler(): raise exc mocked_log_and_exit.assert_called_once_with( 'An error has occurred', exc, # Tested below mock.ANY, ) pattern = re_assert.Matches( r'Traceback \(most recent call last\):\n' r' File ".+pre_commit.error_handler.py", line \d+, in error_handler\n' r' yield\n' r' File ".+tests.error_handler_test.py", line \d+, ' r'in test_error_handler_fatal_error\n' r' raise exc\n' r'(pre_commit\.errors\.)?FatalError: just a test\n', ) pattern.assert_matches(mocked_log_and_exit.call_args[0][2])
retcode=None, tempdir_factory=tempdir_factory, **kwargs, ) # osx does this different :( FILES_CHANGED = (r'(' r' 1 file changed, 0 insertions\(\+\), 0 deletions\(-\)\n' r'|' r' 0 files changed\n' r')') NORMAL_PRE_COMMIT_RUN = re_assert.Matches( fr'^\[INFO\] Initializing environment for .+\.\n' fr'Bash hook\.+Passed\n' fr'\[master [a-f0-9]{{7}}\] commit!\n' fr'{FILES_CHANGED}' fr' create mode 100644 foo\n$', ) def test_install_pre_commit_and_run(tempdir_factory, store): path = make_consuming_repo(tempdir_factory, 'script_hooks_repo') with cwd(path): assert install(C.CONFIG_FILE, store, hook_types=['pre-commit']) == 0 ret, output = _get_commit_output(tempdir_factory) assert ret == 0 NORMAL_PRE_COMMIT_RUN.assert_matches(output) def test_install_pre_commit_and_run_custom_path(tempdir_factory, store):