Skip to content

Commit 4cb4158

Browse files
authored
test: add test for stack_info and exc_info formatting
1 parent 87c285e commit 4cb4158

File tree

1 file changed

+86
-24
lines changed

1 file changed

+86
-24
lines changed

tests/test_formatter.py

Lines changed: 86 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import re
33
import subprocess
44
import sys
5+
import traceback
56
from datetime import datetime
67

78
import pytest
@@ -125,6 +126,21 @@ def test_get_extra(record, expected):
125126
assert Logfmter.get_extra(record) == expected
126127

127128

129+
try:
130+
int("a")
131+
except ValueError:
132+
exc_info = sys.exc_info()
133+
st = traceback.format_stack(limit=1)
134+
stack_info = "".join(["Stack (most recent call last)\n", *st])
135+
stack_info_tb = stack_info.rstrip("\n").replace("\n", "\\n").replace('"', '\\"')
136+
exc_info_tb = (
137+
"".join(traceback.format_exception(*exc_info))
138+
.rstrip("\n")
139+
.replace("\n", "\\n")
140+
.replace('"', '\\"')
141+
)
142+
143+
128144
@pytest.mark.parametrize(
129145
"record,expected",
130146
[
@@ -147,17 +163,21 @@ def test_get_extra(record, expected):
147163
{
148164
"levelname": "INFO",
149165
"msg": "alpha",
150-
"exc_info": (
151-
Exception,
152-
Exception("alpha"),
153-
None,
154-
), # We don't pass a traceback, because they are difficult to fake.
166+
"exc_info": exc_info,
155167
},
156-
'at=INFO msg=alpha exc_info="Exception: alpha"',
168+
f'at=INFO msg=alpha exc_info="{exc_info_tb}"',
157169
),
158170
# If, for some odd reason, someone passes in an empty msg dictionary. It
159171
# should be properly formatted without extra spaces.
160172
({"levelname": "INFO", "msg": {}}, "at=INFO"),
173+
(
174+
{
175+
"levelname": "INFO",
176+
"msg": {},
177+
"stack_info": stack_info,
178+
},
179+
f'at=INFO stack_info="{stack_info_tb}"',
180+
),
161181
],
162182
)
163183
def test_format_default(record, expected):
@@ -215,7 +235,6 @@ def test_format_provided_keys(keys, mapping, record, expected):
215235
then it should be added as a parameter. Any provided mapping should also
216236
be utilized.
217237
"""
218-
219238
# Generate a real `logging.LogRecord` from the provided dictionary.
220239
record = logging.makeLogRecord(record)
221240

@@ -308,6 +327,7 @@ def test_external_tools_compatibility(value):
308327

309328
result = subprocess.run(
310329
["golang-logfmt-echo"],
330+
check=False,
311331
input=formatted,
312332
capture_output=True,
313333
text=True,
@@ -319,30 +339,72 @@ def test_external_tools_compatibility(value):
319339

320340

321341
@pytest.mark.parametrize(
322-
"record",
342+
"record,expected",
323343
[
324-
{
325-
"msg": "alpha",
326-
"levelname": "INFO",
327-
"funcName": "test_defaults",
328-
"module": "test_formatter",
329-
"lineno": "324",
330-
},
331-
{
332-
"msg": {"msg": "alpha"},
333-
"levelname": "INFO",
334-
"funcName": "test_defaults",
335-
"module": "test_formatter",
336-
"lineno": "324",
337-
},
344+
(
345+
{
346+
"msg": "alpha",
347+
"levelname": "INFO",
348+
"func": "funky",
349+
"funcName": "test_defaults",
350+
"module": "test_formatter",
351+
"lineno": "324",
352+
},
353+
"at=INFO msg=alpha func=funky",
354+
),
355+
(
356+
{
357+
"msg": "alpha",
358+
"levelname": "INFO",
359+
"funcName": "test_defaults",
360+
"module": "test_formatter",
361+
"lineno": "324",
362+
},
363+
"at=INFO func=test_formatter.test_defaults:324 msg=alpha",
364+
),
365+
(
366+
{
367+
"msg": {"msg": "alpha"},
368+
"levelname": "INFO",
369+
"funcName": "test_defaults",
370+
"module": "test_formatter",
371+
"lineno": "324",
372+
},
373+
"at=INFO func=test_formatter.test_defaults:324 msg=alpha",
374+
),
375+
# check if defaults formatter doesn't print the inherited stack_info / exc_info
376+
(
377+
{
378+
"msg": "alpha",
379+
"levelname": "INFO",
380+
"funcName": "test_defaults",
381+
"module": "test_formatter",
382+
"lineno": "324",
383+
"exc_info": exc_info,
384+
},
385+
"at=INFO func=test_formatter.test_defaults:324 msg=alpha "
386+
f'exc_info="{exc_info_tb}"',
387+
),
388+
(
389+
{
390+
"msg": {"msg": "alpha"},
391+
"levelname": "INFO",
392+
"funcName": "test_defaults",
393+
"module": "test_formatter",
394+
"lineno": "324",
395+
"stack_info": stack_info,
396+
},
397+
"at=INFO func=test_formatter.test_defaults:324 msg=alpha "
398+
f'stack_info="{stack_info_tb}"',
399+
),
338400
],
339401
)
340-
def test_defaults(record):
402+
def test_defaults(record, expected):
341403
record = logging.makeLogRecord(record)
342404

343405
assert (
344406
Logfmter(
345407
keys=["at", "func"], defaults={"func": "{module}.{funcName}:{lineno}"}
346408
).format(record)
347-
== "at=INFO func=test_formatter.test_defaults:324 msg=alpha"
409+
== expected
348410
)

0 commit comments

Comments
 (0)