1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
# SPDX-License-Identifier: MIT OR Apache-2.0
# This file is dual licensed under the terms of the Apache License, Version
# 2.0, and the MIT License. See the LICENSE file in the root of this
# repository for complete details.
import sys
import pytest
from pretend import stub
from structlog._frames import (
_find_first_app_frame_and_name,
_format_exception,
_format_stack,
)
class TestFindFirstAppFrameAndName:
def test_ignores_structlog_by_default(self):
"""
No matter what you pass in, structlog frames get always ignored.
"""
f1 = stub(f_globals={"__name__": "test"}, f_back=None)
f2 = stub(f_globals={"__name__": "structlog.blubb"}, f_back=f1)
f, n = _find_first_app_frame_and_name(_getframe=lambda: f2)
assert (f1, "test") == (f, n)
def test_ignoring_of_additional_frame_names_works(self):
"""
Additional names are properly ignored too.
"""
f1 = stub(f_globals={"__name__": "test"}, f_back=None)
f2 = stub(f_globals={"__name__": "ignored.bar"}, f_back=f1)
f3 = stub(f_globals={"__name__": "structlog.blubb"}, f_back=f2)
f, n = _find_first_app_frame_and_name(
additional_ignores=["ignored"], _getframe=lambda: f3
)
assert (f1, "test") == (f, n)
def test_tolerates_missing_name(self):
"""
Use ``?`` if `f_globals` lacks a `__name__` key
"""
f1 = stub(f_globals={}, f_back=None)
f, n = _find_first_app_frame_and_name(_getframe=lambda: f1)
assert (f1, "?") == (f, n)
def test_tolerates_name_explicitly_None_oneframe(self):
"""
Use ``?`` if `f_globals` has a `None` valued `__name__` key
"""
f1 = stub(f_globals={"__name__": None}, f_back=None)
f, n = _find_first_app_frame_and_name(_getframe=lambda: f1)
assert (f1, "?") == (f, n)
def test_tolerates_name_explicitly_None_manyframe(self):
"""
Use ``?`` if `f_globals` has a `None` valued `__name__` key,
multiple frames up.
"""
f1 = stub(f_globals={"__name__": None}, f_back=None)
f2 = stub(f_globals={"__name__": "structlog.blubb"}, f_back=f1)
f, n = _find_first_app_frame_and_name(_getframe=lambda: f2)
assert (f1, "?") == (f, n)
def test_tolerates_f_back_is_None(self):
"""
Use ``?`` if all frames are in ignored frames.
"""
f1 = stub(f_globals={"__name__": "structlog"}, f_back=None)
f, n = _find_first_app_frame_and_name(_getframe=lambda: f1)
assert (f1, "?") == (f, n)
@pytest.fixture
def exc_info():
"""
Fake a valid exc_info.
"""
try:
raise ValueError
except ValueError:
return sys.exc_info()
class TestFormatException:
def test_returns_str(self, exc_info):
"""
Always returns a native string.
"""
assert isinstance(_format_exception(exc_info), str)
def test_formats(self, exc_info):
"""
The passed exc_info is formatted.
"""
assert _format_exception(exc_info).startswith(
"Traceback (most recent call last):\n"
)
def test_no_trailing_nl(self, exc_info, monkeypatch):
"""
Trailing newlines are snipped off but if the string does not contain
one nothing is removed.
"""
from structlog._frames import traceback
monkeypatch.setattr(
traceback, "print_exception", lambda *a: a[-1].write("foo")
)
assert "foo" == _format_exception(exc_info)
class TestFormatStack:
def test_returns_str(self):
"""
Always returns a native string.
"""
assert isinstance(_format_stack(sys._getframe()), str)
def test_formats(self):
"""
The passed stack is formatted.
"""
assert _format_stack(sys._getframe()).startswith(
"Stack (most recent call last):\n"
)
def test_no_trailing_nl(self, monkeypatch):
"""
Trailing newlines are snipped off but if the string does not contain
one nothing is removed.
"""
from structlog._frames import traceback
monkeypatch.setattr(
traceback, "print_stack", lambda frame, file: file.write("foo")
)
assert _format_stack(sys._getframe()).endswith("foo")
|