This commit is contained in:
marqueewinq 2019-09-20 15:05:51 +03:00
parent 0245a67831
commit 247d45af05
3 changed files with 34 additions and 14 deletions

View file

@ -4,12 +4,14 @@ from __future__ import unicode_literals
import contextlib import contextlib
import os.path import os.path
import sys
import traceback import traceback
import six import six
from pre_commit import five from pre_commit import five
from pre_commit import output from pre_commit import output
from pre_commit.constants import VERSION as pre_commit_version
from pre_commit.store import Store from pre_commit.store import Store
@ -29,6 +31,9 @@ def _log_and_exit(msg, exc, formatted):
five.to_bytes(msg), b': ', five.to_bytes(msg), b': ',
five.to_bytes(type(exc).__name__), b': ', five.to_bytes(type(exc).__name__), b': ',
_to_bytes(exc), b'\n', _to_bytes(exc), b'\n',
_to_bytes('pre-commit.version={}\n'.format(pre_commit_version)),
_to_bytes('sys.version={}\n'.format(sys.version.replace('\n', ' '))),
_to_bytes('sys.executable={}\n'.format(sys.executable)),
)) ))
output.write(error_msg) output.write(error_msg)
store = Store() store = Store()

View file

@ -104,17 +104,29 @@ def test_log_and_exit(cap_out, mock_store_dir):
printed = cap_out.get() printed = cap_out.get()
log_file = os.path.join(mock_store_dir, 'pre-commit.log') log_file = os.path.join(mock_store_dir, 'pre-commit.log')
assert printed == ( printed_lines = printed.split('\n')
'msg: FatalError: hai\n' assert len(printed_lines) == 6, printed_lines
'Check the log at {}\n'.format(log_file) assert printed_lines[0] == 'msg: FatalError: hai'
) assert re.match(r'^pre-commit.version=\d+\.\d+\.\d+$', printed_lines[1])
assert printed_lines[2].startswith('sys.version=')
assert printed_lines[3].startswith('sys.executable=')
assert printed_lines[4] == 'Check the log at {}'.format(log_file)
assert printed_lines[5] == '' # checks for \n at the end of last line
assert os.path.exists(log_file) assert os.path.exists(log_file)
with io.open(log_file) as f: with io.open(log_file) as f:
assert f.read() == ( logged_lines = f.read().split('\n')
'msg: FatalError: hai\n' assert len(logged_lines) == 6, logged_lines
"I'm a stacktrace\n" assert logged_lines[0] == 'msg: FatalError: hai'
assert re.match(
r'^pre-commit.version=\d+\.\d+\.\d+$',
printed_lines[1],
) )
assert logged_lines[2].startswith('sys.version=')
assert logged_lines[3].startswith('sys.executable=')
assert logged_lines[4] == "I'm a stacktrace"
# checks for \n at the end of stack trace
assert printed_lines[5] == ''
def test_error_handler_non_ascii_exception(mock_store_dir): def test_error_handler_non_ascii_exception(mock_store_dir):
@ -136,7 +148,7 @@ def test_error_handler_no_tty(tempdir_factory):
pre_commit_home=pre_commit_home, pre_commit_home=pre_commit_home,
) )
log_file = os.path.join(pre_commit_home, 'pre-commit.log') log_file = os.path.join(pre_commit_home, 'pre-commit.log')
assert output[1].replace('\r', '') == ( output_lines = output[1].replace('\r', '').split('\n')
'An unexpected error has occurred: ValueError: ☃\n' assert output_lines[0] == 'An unexpected error has occurred: ValueError: ☃'
'Check the log at {}\n'.format(log_file) assert output_lines[-2] == 'Check the log at {}'.format(log_file)
) assert output_lines[-1] == '' # checks for \n at the end of stack trace

View file

@ -164,11 +164,14 @@ def test_expected_fatal_error_no_git_repo(in_tmpdir, cap_out, mock_store_dir):
with pytest.raises(SystemExit): with pytest.raises(SystemExit):
main.main([]) 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() == ( cap_out_lines = cap_out.get().split('\n')
assert (
cap_out_lines[0] ==
'An error has occurred: FatalError: git failed. ' 'An error has occurred: FatalError: git failed. '
'Is it installed, and are you in a Git repository directory?\n' 'Is it installed, and are you in a Git repository directory?'
'Check the log at {}\n'.format(log_file)
) )
assert cap_out_lines[-2] == 'Check the log at {}'.format(log_file)
assert cap_out_lines[-1] == '' # checks for \n at the end of error message
def test_warning_on_tags_only(mock_commands, cap_out, mock_store_dir): def test_warning_on_tags_only(mock_commands, cap_out, mock_store_dir):