-
Notifications
You must be signed in to change notification settings - Fork 742
Expand file tree
/
Copy pathtest_logging.py
More file actions
158 lines (128 loc) · 4.03 KB
/
test_logging.py
File metadata and controls
158 lines (128 loc) · 4.03 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
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
154
155
156
157
158
from __future__ import annotations
import sys
import warnings
from contextlib import redirect_stdout
from datetime import datetime
from io import StringIO
from typing import TYPE_CHECKING
import pytest
import scanpy as sc
from scanpy import Verbosity
from scanpy import logging as log
from scanpy import settings as s
if TYPE_CHECKING:
from pathlib import Path
def test_defaults():
assert s.logpath is None
def test_records(caplog: pytest.LogCaptureFixture):
s.verbosity = Verbosity.debug
log.error("0")
log.warning("1")
log.info("2")
log.hint("3")
log.debug("4")
assert caplog.record_tuples == [
("root", 40, "0"),
("root", 30, "1"),
("root", 20, "2"),
("root", 15, "3"),
("root", 10, "4"),
]
def test_formats(capsys: pytest.CaptureFixture):
s.logfile = sys.stderr
s.verbosity = Verbosity.debug
log.error("0")
assert capsys.readouterr().err == "ERROR: 0\n"
log.warning("1")
assert capsys.readouterr().err == "WARNING: 1\n"
log.info("2")
assert capsys.readouterr().err == "2\n"
log.hint("3")
assert capsys.readouterr().err == "--> 3\n"
log.debug("4")
assert capsys.readouterr().err == " 4\n"
def test_deep(capsys: pytest.CaptureFixture):
s.logfile = sys.stderr
s.verbosity = Verbosity.hint
log.hint("0")
assert capsys.readouterr().err == "--> 0\n"
log.hint("1", deep="1!")
assert capsys.readouterr().err == "--> 1\n"
s.verbosity = Verbosity.debug
log.hint("2")
assert capsys.readouterr().err == "--> 2\n"
log.hint("3", deep="3!")
assert capsys.readouterr().err == "--> 3: 3!\n"
def test_logfile(tmp_path: Path, caplog: pytest.LogCaptureFixture):
s.verbosity = Verbosity.hint
io = StringIO()
s.logfile = io
assert s.logfile is io
assert s.logpath is None
log.error("test!")
assert io.getvalue() == "ERROR: test!\n"
# setting a logfile removes all handlers
assert not caplog.records
p = tmp_path / "test.log"
s.logpath = p
try:
assert s.logpath == p
assert s.logfile.name == str(p)
log.hint("test2")
log.debug("invisible")
assert s.logpath.read_text() == "--> test2\n"
# setting a logfile removes all handlers
assert not caplog.records
finally:
s.logfile.close() # TODO: make this unnecessary
def test_timing(monkeypatch, capsys: pytest.CaptureFixture):
counter = 0
class IncTime:
@staticmethod
def now(tz):
nonlocal counter
counter += 1
return datetime(2000, 1, 1, second=counter, microsecond=counter, tzinfo=tz)
monkeypatch.setattr(log, "datetime", IncTime)
s.logfile = sys.stderr
s.verbosity = Verbosity.debug
log.hint("1")
assert counter == 1
assert capsys.readouterr().err == "--> 1\n"
start = log.info("2")
assert counter == 2
assert capsys.readouterr().err == "2\n"
log.hint("3")
assert counter == 3
assert capsys.readouterr().err == "--> 3\n"
log.info("4", time=start)
assert counter == 4
assert capsys.readouterr().err == "4 (0:00:02)\n"
log.info("5 {time_passed}", time=start)
assert counter == 5
assert capsys.readouterr().err == "5 0:00:03\n"
@pytest.mark.parametrize(
"func",
[
sc.logging.print_header,
pytest.param(
sc.logging.print_versions,
marks=pytest.mark.filterwarnings("ignore:.*print_header:FutureWarning"),
),
sc.logging.print_version_and_date,
],
)
def test_call_outputs(func):
"""Tests that these functions print to stdout and don't error.
Checks that https://github.com/scverse/scanpy/issues/1437 is fixed.
"""
output_io = StringIO()
with redirect_stdout(output_io):
out = func()
if out is not None:
with warnings.catch_warnings():
# https://github.com/pallets/markupsafe/issues/487
warnings.simplefilter("ignore")
print(out)
output = output_io.getvalue()
assert output != ""