def test_backup_logging_on_fatal_error(self): sink = self._gen_sink_subclass() with self.captured_logging(level=logging.ERROR) as captured: with temporary_dir() as tmpdir: sink.reset_log_location(tmpdir) with mock.patch.object(sink, '_try_write_with_flush', autospec=sink) as mock_write: mock_write.side_effect = ExceptionSink.ExceptionSinkError( 'fake write failure') sink.log_exception('XXX') errors = list(captured.errors()) self.assertEqual(2, len(errors)) def format_log_rx(log_file_type): return '.*'.join( re.escape(s) for s in [ "pants.base.exception_sink: Error logging the message 'XXX' to the {log_file_type} file " "handle for".format(log_file_type=log_file_type), "at pid {pid}".format(pid=os.getpid()), "\nfake write failure", ]) assertRegex(self, str(errors[0]), format_log_rx('pid-specific')) assertRegex(self, str(errors[1]), format_log_rx('shared'))
def test_backup_logging_on_fatal_error(caplog): sink = _gen_sink_subclass() with temporary_dir() as tmpdir: sink.reset_log_location(tmpdir) with unittest.mock.patch.object(sink, "_try_write_with_flush", autospec=sink) as mock_write: mock_write.side_effect = ExceptionSink.ExceptionSinkError( "fake write failure") sink._log_exception("XXX") errors = [ record for record in caplog.records if record.levelname == "ERROR" ] assert len(errors) == 2 def assert_log(log_file_type: str, log): assert bool( re.search( rf"Error logging the message 'XXX' to the {log_file_type} file handle for .* at " rf"pid {os.getpid()}", log.msg, )) assert log.filename == "exception_sink.py" assert_log("pid-specific", errors[0]) assert_log("shared", errors[1])