mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
updated import style; put the version info on top of error message; fixed tests
This commit is contained in:
parent
247d45af05
commit
de63b6a850
3 changed files with 26 additions and 23 deletions
|
|
@ -9,9 +9,9 @@ import traceback
|
||||||
|
|
||||||
import six
|
import six
|
||||||
|
|
||||||
|
import pre_commit.constants as C
|
||||||
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
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -28,12 +28,14 @@ def _to_bytes(exc):
|
||||||
|
|
||||||
def _log_and_exit(msg, exc, formatted):
|
def _log_and_exit(msg, exc, formatted):
|
||||||
error_msg = b''.join((
|
error_msg = b''.join((
|
||||||
|
_to_bytes('### version information\n'),
|
||||||
|
_to_bytes('pre-commit.version={}\n'.format(C.VERSION)),
|
||||||
|
_to_bytes('sys.version={}\n'.format(sys.version.replace('\n', ' '))),
|
||||||
|
_to_bytes('sys.executable={}\n'.format(sys.executable)),
|
||||||
|
_to_bytes('### error information\n'),
|
||||||
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()
|
||||||
|
|
|
||||||
|
|
@ -104,29 +104,30 @@ 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')
|
||||||
printed_lines = printed.split('\n')
|
printed_lines = printed.splitlines()
|
||||||
assert len(printed_lines) == 6, printed_lines
|
print(printed_lines)
|
||||||
assert printed_lines[0] == 'msg: FatalError: hai'
|
assert len(printed_lines) == 7
|
||||||
|
assert printed_lines[0] == '### version information'
|
||||||
assert re.match(r'^pre-commit.version=\d+\.\d+\.\d+$', printed_lines[1])
|
assert re.match(r'^pre-commit.version=\d+\.\d+\.\d+$', printed_lines[1])
|
||||||
assert printed_lines[2].startswith('sys.version=')
|
assert printed_lines[2].startswith('sys.version=')
|
||||||
assert printed_lines[3].startswith('sys.executable=')
|
assert printed_lines[3].startswith('sys.executable=')
|
||||||
assert printed_lines[4] == 'Check the log at {}'.format(log_file)
|
assert printed_lines[4] == '### error information'
|
||||||
assert printed_lines[5] == '' # checks for \n at the end of last line
|
assert printed_lines[5] == 'msg: FatalError: hai'
|
||||||
|
assert printed_lines[6] == 'Check the log at {}'.format(log_file)
|
||||||
|
|
||||||
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:
|
||||||
logged_lines = f.read().split('\n')
|
logged_lines = f.read().splitlines()
|
||||||
assert len(logged_lines) == 6, logged_lines
|
assert len(logged_lines) == 7
|
||||||
assert logged_lines[0] == 'msg: FatalError: hai'
|
assert printed_lines[0] == '### version information'
|
||||||
assert re.match(
|
assert re.match(
|
||||||
r'^pre-commit.version=\d+\.\d+\.\d+$',
|
r'^pre-commit.version=\d+\.\d+\.\d+$',
|
||||||
printed_lines[1],
|
printed_lines[1],
|
||||||
)
|
)
|
||||||
assert logged_lines[2].startswith('sys.version=')
|
assert logged_lines[2].startswith('sys.version=')
|
||||||
assert logged_lines[3].startswith('sys.executable=')
|
assert logged_lines[3].startswith('sys.executable=')
|
||||||
assert logged_lines[4] == "I'm a stacktrace"
|
assert logged_lines[5] == 'msg: FatalError: hai'
|
||||||
# checks for \n at the end of stack trace
|
assert logged_lines[6] == "I'm a stacktrace"
|
||||||
assert printed_lines[5] == ''
|
|
||||||
|
|
||||||
|
|
||||||
def test_error_handler_non_ascii_exception(mock_store_dir):
|
def test_error_handler_non_ascii_exception(mock_store_dir):
|
||||||
|
|
@ -148,7 +149,8 @@ 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')
|
||||||
output_lines = output[1].replace('\r', '').split('\n')
|
output_lines = output[1].replace('\r', '').splitlines()
|
||||||
assert output_lines[0] == 'An unexpected error has occurred: ValueError: ☃'
|
assert (
|
||||||
assert output_lines[-2] == 'Check the log at {}'.format(log_file)
|
output_lines[-2] == 'An unexpected error has occurred: ValueError: ☃'
|
||||||
assert output_lines[-1] == '' # checks for \n at the end of stack trace
|
)
|
||||||
|
assert output_lines[-1] == 'Check the log at {}'.format(log_file)
|
||||||
|
|
|
||||||
|
|
@ -164,14 +164,13 @@ 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')
|
||||||
cap_out_lines = cap_out.get().split('\n')
|
cap_out_lines = cap_out.get().splitlines()
|
||||||
assert (
|
assert (
|
||||||
cap_out_lines[0] ==
|
cap_out_lines[-2] ==
|
||||||
'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?'
|
'Is it installed, and are you in a Git repository directory?'
|
||||||
)
|
)
|
||||||
assert cap_out_lines[-2] == 'Check the log at {}'.format(log_file)
|
assert cap_out_lines[-1] == '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):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue