def test_log_and_exit(cap_out, mock_store_dir): with pytest.raises(SystemExit): error_handler._log_and_exit( 'msg', error_handler.FatalError('hai'), "I'm a stacktrace", ) printed = cap_out.get() log_file = os.path.join(mock_store_dir, 'pre-commit.log') assert printed == ('msg: FatalError: hai\n' 'Check the log at {}\n'.format(log_file)) assert os.path.exists(log_file) with io.open(log_file) as f: assert f.read() == ('msg: FatalError: hai\n' "I'm a stacktrace\n")
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.error_handler.FatalError: hai\n' ) with pytest.raises(SystemExit): error_handler._log_and_exit('msg', error_handler.FatalError('hai'), tb) 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'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\.error_handler\.FatalError: hai\n' r'```\n', ) pattern.assert_matches(logged)
def test_error_handler_fatal_error(mocked_log_and_exit): exc = error_handler.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, ) assert re.match( 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\.error_handler\.)?FatalError: just a test\n', mocked_log_and_exit.call_args[0][2], )
def test_log_and_exit(cap_out, mock_store_dir): with pytest.raises(SystemExit): error_handler._log_and_exit( 'msg', error_handler.FatalError('hai'), "I'm a stacktrace", ) 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() expected = (r'^### version information\n' r'\n' r'```\n' r'pre-commit version: \d+\.\d+\.\d+\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"I'm a stacktrace\n" r'```\n') assert re.match(expected, logged)