-
Notifications
You must be signed in to change notification settings - Fork 728
/
Copy pathtest_exceptions_formatting.py
104 lines (83 loc) · 2.76 KB
/
test_exceptions_formatting.py
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
import os.path
import re
import subprocess
import sys
import pytest
from loguru import logger
def normalize(formatted_exception):
"""Normalize exception output for reproducible test cases"""
if sys.platform == "win32":
formatted_exception = re.sub(
r'File[^"]+"[^"]+\.py[^"]*"',
lambda m: m.group().replace("\\", "/"),
formatted_exception,
)
formatted_exception = re.sub(r"(\r\n|\r|\n)", "\n", formatted_exception)
formatted_exception = re.sub(r"\b0x[0-9a-fA-F]+\b", "0xDEADBEEF", formatted_exception)
return formatted_exception
def generate(output, outpath): # pragma: no cover
"""Generate new output file if exception formatting is updated"""
with open(outpath, "w") as file:
file.write(output)
@pytest.mark.parametrize(
"filename",
[
"chained_expression_direct",
"chained_expression_indirect",
"chaining_first",
"chaining_second",
"chaining_third",
"colorize",
"enqueue",
"enqueue_with_others_handlers",
"frame_values_backward",
"frame_values_forward",
"function",
"head_recursion",
"nested",
"nested_wrapping",
"not_enough_arguments",
"no_tb",
"raising_recursion",
"suppressed_expression_direct",
"suppressed_expression_indirect",
"tail_recursion",
"too_many_arguments",
],
)
def test_exceptions_formatting(filename):
cwd = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
python = sys.executable or "python"
filepath = os.path.join("tests", "exceptions", filename + ".py")
outpath = os.path.abspath(os.path.join(cwd, "tests", "exceptions", "output", filename + ".txt"))
with subprocess.Popen(
[python, filepath],
shell=False,
cwd=cwd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
) as proc:
stdout, stderr = proc.communicate()
print(stderr, file=sys.stderr)
assert proc.returncode == 0
assert stdout == ""
stderr = normalize(stderr)
# generate(stderr, outpath)
with open(outpath, "r") as file:
assert stderr == file.read()
def test_carret_not_masked(writer):
logger.add(writer, backtrace=True, colorize=False, format="")
@logger.catch
def f(n):
1 / n
f(n - 1)
f(30)
assert sum(line.startswith("> ") for line in writer.read().splitlines()) == 1
def test_no_exception(writer):
logger.add(writer, backtrace=False, colorize=False, format="{message}")
logger.exception("No Error.")
assert writer.read() in (
"No Error.\nNoneType\n",
"No Error.\nNoneType: None\n", # Old versions of Python 3.5
)