def test_nested_exceptions(self, caplog): with caplog.at_level(logging.DEBUG, logger="dvc"): try: raise Exception("first") except Exception as exc: first_traceback = traceback.format_exc() try: raise DvcException("second", cause=exc) except DvcException: second_traceback = traceback.format_exc() logger.exception("message") expected = ("{red}ERROR{nc}: message - second: first\n" "{red}{line}{nc}\n" "{stack_trace}" "{red}{line}{nc}\n" "\n" "{footer}".format(footer=formatter.footer, line="-" * 60, stack_trace="\n".join([ first_traceback, second_traceback ]), **colors)) assert expected == formatter.format(caplog.records[0])
def test_nested_exceptions(self, caplog, dt): with caplog.at_level(logging.DEBUG, logger="dvc"): try: raise Exception("first") except Exception as exc: try: raise DvcException("second") from exc except DvcException: stack_trace = traceback.format_exc() logger.exception("message") expected = ( "{green}{datetime}{nc} " "{red}ERROR{nc}: message - second: first\n" "{red}{line}{nc}\n" "{stack_trace}" "{red}{line}{nc}".format( line="-" * 60, stack_trace=stack_trace, **colors, datetime=dt, ) ) assert expected == formatter.format(caplog.records[0]) assert "Exception: first" in stack_trace assert "dvc.exceptions.DvcException: second" in stack_trace
def test_exception_with_description_and_without_message(self, caplog): with caplog.at_level(logging.INFO, logger="dvc"): try: raise Exception("description") except Exception: logger.exception("") expected = "{red}ERROR{nc}: description\n".format(**colors) assert expected == formatter.format(caplog.records[0])
def test_exception(self, caplog): with caplog.at_level(logging.INFO, logger="dvc"): try: raise ValueError except Exception: logger.exception("message") expected = "{red}ERROR{nc}: message\n".format(**colors) assert expected == formatter.format(caplog.records[0])
def test_exception_with_description_and_message(self, caplog): with caplog.at_level(logging.INFO, logger="dvc"): try: raise Exception("description") except Exception: logger.exception("message") expected = ("{red}ERROR{nc}: message - description\n" "\n" "{footer}".format(footer=formatter.footer, **colors)) assert expected == formatter.format(caplog.records[0])
def test_exception_under_verbose(self, caplog): with caplog.at_level(logging.DEBUG, logger="dvc"): try: raise Exception("description") except Exception: stack_trace = traceback.format_exc() logger.exception("") expected = ("{red}ERROR{nc}: description\n" "{red}{line}{nc}\n" "{stack_trace}" "{red}{line}{nc}\n".format(line="-" * 60, stack_trace=stack_trace, **colors)) assert expected == formatter.format(caplog.records[0])
def test_tb_only(self, caplog): with caplog.at_level(logging.DEBUG, logger="dvc"): try: raise Exception("description") except Exception: stack_trace = traceback.format_exc() logger.exception("something", extra={"tb_only": True}) expected = ("{red}ERROR{nc}: something\n" "{red}{line}{nc}\n" "{stack_trace}" "{red}{line}{nc}\n".format(line="-" * 60, stack_trace=stack_trace, **colors)) assert expected == formatter.format(caplog.records[0])