-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_core.py
More file actions
127 lines (102 loc) · 3.74 KB
/
test_core.py
File metadata and controls
127 lines (102 loc) · 3.74 KB
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
import io
import logging
import re
import unittest
import vmodule
LOG_LINE_TIMESTAMP_RE = re.compile(r"^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3} ", re.M)
LOG_LINE_NUMERIC_LINE_RE = re.compile(r"^([A-Z0-9_]+\s+[a-z_.]+:)\d+(?= )", re.M)
def log_some_messages() -> None:
logging.getLogger("a.b.c").warning("Warn")
logging.getLogger("a.b.c").info("Info")
logging.getLogger("a.b.c").debug("Debug")
logging.getLogger("x.y.z").log(vmodule.VLOG_1, "Vlog %d", 1)
logging.getLogger("x.y.z").log(vmodule.VLOG_2, "Vlog %d", 2)
class CoreTest(unittest.TestCase):
def test_defaults(self) -> None:
# let basicConfig do its work
del logging.root.handlers[:]
# clear out anything that might have leaked in from another test
logging.Logger.manager.loggerDict = {}
stream = io.StringIO()
vmodule.vmodule_init(None, "", stream=stream)
log_some_messages()
output = LOG_LINE_TIMESTAMP_RE.sub("", stream.getvalue())
output = LOG_LINE_NUMERIC_LINE_RE.sub(lambda m: (m.group(1) + "<n>"), output)
self.assertEqual(
"""\
WARNING a.b.c:<n> Warn
""",
output,
)
def test_root_verbosity_zero_is_info(self) -> None:
# let basicConfig do its work
del logging.root.handlers[:]
# clear out anything that might have leaked in from another test
logging.Logger.manager.loggerDict = {}
stream = io.StringIO()
vmodule.vmodule_init(0, "", stream=stream)
log_some_messages()
output = LOG_LINE_TIMESTAMP_RE.sub("", stream.getvalue())
output = LOG_LINE_NUMERIC_LINE_RE.sub(lambda m: (m.group(1) + "<n>"), output)
self.assertEqual(
"""\
WARNING a.b.c:<n> Warn
INFO a.b.c:<n> Info
""",
output,
)
def test_root_verbosity(self) -> None:
# let basicConfig do its work
del logging.root.handlers[:]
# clear out anything that might have leaked in from another test
logging.Logger.manager.loggerDict = {}
stream = io.StringIO()
vmodule.vmodule_init(1, "", stream=stream)
log_some_messages()
output = LOG_LINE_TIMESTAMP_RE.sub("", stream.getvalue())
output = LOG_LINE_NUMERIC_LINE_RE.sub(lambda m: (m.group(1) + "<n>"), output)
self.assertEqual(
"""\
WARNING a.b.c:<n> Warn
INFO a.b.c:<n> Info
VLOG_1 x.y.z:<n> Vlog 1
""",
output,
)
def test_vmodule_verbosity(self) -> None:
# let basicConfig do its work
del logging.root.handlers[:]
# clear out anything that might have leaked in from another test
logging.Logger.manager.loggerDict = {}
stream = io.StringIO()
vmodule.vmodule_init(None, "x.y.z=1", stream=stream)
log_some_messages()
output = LOG_LINE_TIMESTAMP_RE.sub("", stream.getvalue())
output = LOG_LINE_NUMERIC_LINE_RE.sub(lambda m: (m.group(1) + "<n>"), output)
self.assertEqual(
"""\
WARNING a.b.c:<n> Warn
VLOG_1 x.y.z:<n> Vlog 1
""",
output,
)
def test_debug_is_ten(self) -> None:
# let basicConfig do its work
del logging.root.handlers[:]
# clear out anything that might have leaked in from another test
logging.Logger.manager.loggerDict = {}
stream = io.StringIO()
vmodule.vmodule_init(10, "", stream=stream)
log_some_messages()
output = LOG_LINE_TIMESTAMP_RE.sub("", stream.getvalue())
output = LOG_LINE_NUMERIC_LINE_RE.sub(lambda m: (m.group(1) + "<n>"), output)
self.assertEqual(
"""\
WARNING a.b.c:<n> Warn
INFO a.b.c:<n> Info
DEBUG a.b.c:<n> Debug
VLOG_1 x.y.z:<n> Vlog 1
VLOG_2 x.y.z:<n> Vlog 2
""",
output,
)