| 1 |
"""Test logging util.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import os |
| 6 |
from contextlib import contextmanager |
| 7 |
from pathlib import Path |
| 8 |
from typing import TYPE_CHECKING |
| 9 |
|
| 10 |
import pytest |
| 11 |
from docutils import nodes |
| 12 |
|
| 13 |
from sphinx._cli.util.errors import strip_escape_sequences |
| 14 |
from sphinx.cmd.build import build_main |
| 15 |
from sphinx.util import logging |
| 16 |
from sphinx.util.console import colorize |
| 17 |
from sphinx.util.logging import is_suppressed_warning, prefixed_warnings |
| 18 |
from sphinx.util.parallel import ParallelTasks |
| 19 |
|
| 20 |
from tests.utils import TEST_ROOTS_DIR |
| 21 |
|
| 22 |
if TYPE_CHECKING: |
| 23 |
import io |
| 24 |
from collections.abc import Iterator |
| 25 |
|
| 26 |
from sphinx.testing.util import SphinxTestApp |
| 27 |
|
| 28 |
|
| 29 |
@pytest.mark.sphinx('html', testroot='root') |
| 30 |
def test_info_and_warning(app: SphinxTestApp) -> None: |
| 31 |
logging.setup(app, app.status, app.warning, verbosity=2) |
| 32 |
logger = logging.getLogger(__name__) |
| 33 |
|
| 34 |
logger.debug('message1') |
| 35 |
logger.info('message2') |
| 36 |
logger.warning('message3') |
| 37 |
logger.critical('message4') |
| 38 |
logger.error('message5') |
| 39 |
|
| 40 |
assert 'message1' in app.status.getvalue() |
| 41 |
assert 'message2' in app.status.getvalue() |
| 42 |
assert 'message3' not in app.status.getvalue() |
| 43 |
assert 'message4' not in app.status.getvalue() |
| 44 |
assert 'message5' not in app.status.getvalue() |
| 45 |
|
| 46 |
assert 'message1' not in app.warning.getvalue() |
| 47 |
assert 'message2' not in app.warning.getvalue() |
| 48 |
assert 'WARNING: message3' in app.warning.getvalue() |
| 49 |
assert 'CRITICAL: message4' in app.warning.getvalue() |
| 50 |
assert 'ERROR: message5' in app.warning.getvalue() |
| 51 |
|
| 52 |
|
| 53 |
@pytest.mark.sphinx('html', testroot='root') |
| 54 |
def test_Exception(app: SphinxTestApp) -> None: |
| 55 |
logging.setup(app, app.status, app.warning) |
| 56 |
logger = logging.getLogger(__name__) |
| 57 |
|
| 58 |
logger.info(Exception) |
| 59 |
assert "<class 'Exception'>" in app.status.getvalue() |
| 60 |
|
| 61 |
|
| 62 |
@pytest.mark.sphinx('html', testroot='root') |
| 63 |
def test_verbosity_filter(app: SphinxTestApp) -> None: |
| 64 |
# verbosity = 0: INFO |
| 65 |
logging.setup(app, app.status, app.warning, verbosity=0) |
| 66 |
logger = logging.getLogger(__name__) |
| 67 |
|
| 68 |
logger.info('message1') |
| 69 |
logger.verbose('message2') |
| 70 |
logger.debug('message3') |
| 71 |
|
| 72 |
assert 'message1' in app.status.getvalue() |
| 73 |
assert 'message2' not in app.status.getvalue() |
| 74 |
assert 'message3' not in app.status.getvalue() |
| 75 |
assert 'message4' not in app.status.getvalue() |
| 76 |
|
| 77 |
# verbosity = 1: VERBOSE |
| 78 |
logging.setup(app, app.status, app.warning, verbosity=1) |
| 79 |
logger = logging.getLogger(__name__) |
| 80 |
|
| 81 |
logger.info('message1') |
| 82 |
logger.verbose('message2') |
| 83 |
logger.debug('message3') |
| 84 |
|
| 85 |
assert 'message1' in app.status.getvalue() |
| 86 |
assert 'message2' in app.status.getvalue() |
| 87 |
assert 'message3' not in app.status.getvalue() |
| 88 |
assert 'message4' not in app.status.getvalue() |
| 89 |
|
| 90 |
# verbosity = 2: DEBUG |
| 91 |
logging.setup(app, app.status, app.warning, verbosity=2) |
| 92 |
logger = logging.getLogger(__name__) |
| 93 |
|
| 94 |
logger.info('message1') |
| 95 |
logger.verbose('message2') |
| 96 |
logger.debug('message3') |
| 97 |
|
| 98 |
assert 'message1' in app.status.getvalue() |
| 99 |
assert 'message2' in app.status.getvalue() |
| 100 |
assert 'message3' in app.status.getvalue() |
| 101 |
assert 'message4' not in app.status.getvalue() |
| 102 |
|
| 103 |
|
| 104 |
@pytest.mark.sphinx('html', testroot='root') |
| 105 |
def test_nonl_info_log(app: SphinxTestApp) -> None: |
| 106 |
logging.setup(app, app.status, app.warning) |
| 107 |
logger = logging.getLogger(__name__) |
| 108 |
|
| 109 |
logger.info('message1', nonl=True) |
| 110 |
logger.info('message2') |
| 111 |
logger.info('message3') |
| 112 |
|
| 113 |
assert 'message1message2\nmessage3' in app.status.getvalue() |
| 114 |
|
| 115 |
|
| 116 |
@pytest.mark.sphinx('html', testroot='root') |
| 117 |
def test_once_warning_log(app: SphinxTestApp) -> None: |
| 118 |
logging.setup(app, app.status, app.warning) |
| 119 |
logger = logging.getLogger(__name__) |
| 120 |
|
| 121 |
logger.warning('message: %d', 1, once=True) |
| 122 |
logger.warning('message: %d', 1, once=True) |
| 123 |
logger.warning('message: %d', 2, once=True) |
| 124 |
|
| 125 |
warnings = strip_escape_sequences(app.warning.getvalue()) |
| 126 |
assert 'WARNING: message: 1\nWARNING: message: 2\n' in warnings |
| 127 |
|
| 128 |
|
| 129 |
def test_is_suppressed_warning() -> None: |
| 130 |
suppress_warnings = ['ref', 'files.*', 'rest.duplicated_labels'] |
| 131 |
|
| 132 |
assert is_suppressed_warning(None, None, suppress_warnings) is False # type: ignore[arg-type] |
| 133 |
assert is_suppressed_warning('ref', None, suppress_warnings) is True # type: ignore[arg-type] |
| 134 |
assert is_suppressed_warning('ref', 'numref', suppress_warnings) is True |
| 135 |
assert is_suppressed_warning('ref', 'option', suppress_warnings) is True |
| 136 |
assert is_suppressed_warning('files', 'image', suppress_warnings) is True |
| 137 |
assert is_suppressed_warning('files', 'stylesheet', suppress_warnings) is True |
| 138 |
assert is_suppressed_warning('rest', None, suppress_warnings) is False # type: ignore[arg-type] |
| 139 |
assert is_suppressed_warning('rest', 'syntax', suppress_warnings) is False |
| 140 |
assert is_suppressed_warning('rest', 'duplicated_labels', suppress_warnings) is True |
| 141 |
|
| 142 |
|
| 143 |
@pytest.mark.sphinx('html', testroot='root') |
| 144 |
def test_suppress_warnings(app: SphinxTestApp) -> None: |
| 145 |
logging.setup(app, app.status, app.warning) |
| 146 |
logger = logging.getLogger(__name__) |
| 147 |
|
| 148 |
app._warncount = 0 # force reset |
| 149 |
|
| 150 |
app.config.suppress_warnings = [] |
| 151 |
app.warning.truncate(0) |
| 152 |
logger.warning('message0', type='test') |
| 153 |
logger.warning('message1', type='test', subtype='logging') |
| 154 |
logger.warning('message2', type='test', subtype='crash') |
| 155 |
logger.warning('message3', type='actual', subtype='logging') |
| 156 |
assert 'message0' in app.warning.getvalue() |
| 157 |
assert 'message1' in app.warning.getvalue() |
| 158 |
assert 'message2' in app.warning.getvalue() |
| 159 |
assert 'message3' in app.warning.getvalue() |
| 160 |
assert app._warncount == 4 |
| 161 |
|
| 162 |
app.config.suppress_warnings = ['test'] |
| 163 |
app.warning.truncate(0) |
| 164 |
logger.warning('message0', type='test') |
| 165 |
logger.warning('message1', type='test', subtype='logging') |
| 166 |
logger.warning('message2', type='test', subtype='crash') |
| 167 |
logger.warning('message3', type='actual', subtype='logging') |
| 168 |
assert 'message0' not in app.warning.getvalue() |
| 169 |
assert 'message1' not in app.warning.getvalue() |
| 170 |
assert 'message2' not in app.warning.getvalue() |
| 171 |
assert 'message3' in app.warning.getvalue() |
| 172 |
assert app._warncount == 5 |
| 173 |
|
| 174 |
app.config.suppress_warnings = ['test.logging'] |
| 175 |
app.warning.truncate(0) |
| 176 |
logger.warning('message0', type='test') |
| 177 |
logger.warning('message1', type='test', subtype='logging') |
| 178 |
logger.warning('message2', type='test', subtype='crash') |
| 179 |
logger.warning('message3', type='actual', subtype='logging') |
| 180 |
assert 'message0' in app.warning.getvalue() |
| 181 |
assert 'message1' not in app.warning.getvalue() |
| 182 |
assert 'message2' in app.warning.getvalue() |
| 183 |
assert 'message3' in app.warning.getvalue() |
| 184 |
assert app._warncount == 8 |
| 185 |
|
| 186 |
|
| 187 |
@pytest.mark.sphinx('html', testroot='root') |
| 188 |
def test_info_location(app: SphinxTestApp) -> None: |
| 189 |
logging.setup(app, app.status, app.warning) |
| 190 |
logger = logging.getLogger(__name__) |
| 191 |
|
| 192 |
logger.info('message1', location='index') |
| 193 |
assert 'index.txt: message1' in app.status.getvalue() |
| 194 |
|
| 195 |
logger.info('message2', location=('index', 10)) |
| 196 |
assert 'index.txt:10: message2' in app.status.getvalue() |
| 197 |
|
| 198 |
logger.info('message3', location=None) |
| 199 |
assert '\nmessage3' in app.status.getvalue() |
| 200 |
|
| 201 |
node = nodes.Element() |
| 202 |
node.source, node.line = ('index.txt', 10) |
| 203 |
logger.info('message4', location=node) |
| 204 |
assert 'index.txt:10: message4' in app.status.getvalue() |
| 205 |
|
| 206 |
node.source, node.line = ('index.txt', None) |
| 207 |
logger.info('message5', location=node) |
| 208 |
assert 'index.txt:: message5' in app.status.getvalue() |
| 209 |
|
| 210 |
node.source, node.line = (None, 10) |
| 211 |
logger.info('message6', location=node) |
| 212 |
assert '<unknown>:10: message6' in app.status.getvalue() |
| 213 |
|
| 214 |
node.source, node.line = (None, None) |
| 215 |
logger.info('message7', location=node) |
| 216 |
assert '\nmessage7' in app.status.getvalue() |
| 217 |
|
| 218 |
|
| 219 |
@pytest.mark.sphinx('html', testroot='root') |
| 220 |
def test_warning_location(app: SphinxTestApp) -> None: |
| 221 |
logging.setup(app, app.status, app.warning) |
| 222 |
logger = logging.getLogger(__name__) |
| 223 |
|
| 224 |
logger.warning('message1', location='index') |
| 225 |
assert 'index.txt: WARNING: message1' in app.warning.getvalue() |
| 226 |
|
| 227 |
logger.warning('message2', location=('index', 10)) |
| 228 |
assert 'index.txt:10: WARNING: message2' in app.warning.getvalue() |
| 229 |
|
| 230 |
logger.warning('message3', location=None) |
| 231 |
assert colorize('red', 'WARNING: message3') in app.warning.getvalue() |
| 232 |
|
| 233 |
node = nodes.Element() |
| 234 |
node.source, node.line = ('index.txt', 10) |
| 235 |
logger.warning('message4', location=node) |
| 236 |
assert 'index.txt:10: WARNING: message4' in app.warning.getvalue() |
| 237 |
|
| 238 |
node.source, node.line = ('index.txt', None) |
| 239 |
logger.warning('message5', location=node) |
| 240 |
assert 'index.txt:: WARNING: message5' in app.warning.getvalue() |
| 241 |
|
| 242 |
node.source, node.line = (None, 10) |
| 243 |
logger.warning('message6', location=node) |
| 244 |
assert '<unknown>:10: WARNING: message6' in app.warning.getvalue() |
| 245 |
|
| 246 |
node.source, node.line = (None, None) |
| 247 |
logger.warning('message7', location=node) |
| 248 |
assert colorize('red', 'WARNING: message7') in app.warning.getvalue() |
| 249 |
|
| 250 |
|
| 251 |
@pytest.mark.sphinx('html', testroot='root') |
| 252 |
def test_suppress_logging(app: SphinxTestApp) -> None: |
| 253 |
logging.setup(app, app.status, app.warning) |
| 254 |
logger = logging.getLogger(__name__) |
| 255 |
|
| 256 |
logger.warning('message1') |
| 257 |
with logging.suppress_logging(): |
| 258 |
logger.warning('message2') |
| 259 |
assert 'WARNING: message1' in app.warning.getvalue() |
| 260 |
assert 'WARNING: message2' not in app.warning.getvalue() |
| 261 |
|
| 262 |
assert 'WARNING: message1' in app.warning.getvalue() |
| 263 |
assert 'WARNING: message2' not in app.warning.getvalue() |
| 264 |
|
| 265 |
|
| 266 |
@pytest.mark.sphinx('html', testroot='root') |
| 267 |
def test_pending_warnings(app: SphinxTestApp) -> None: |
| 268 |
logging.setup(app, app.status, app.warning) |
| 269 |
logger = logging.getLogger(__name__) |
| 270 |
|
| 271 |
logger.warning('message1') |
| 272 |
with logging.pending_warnings(): |
| 273 |
# not logged yet (buffered) in here |
| 274 |
logger.warning('message2') |
| 275 |
logger.warning('message3') |
| 276 |
assert 'WARNING: message1' in app.warning.getvalue() |
| 277 |
assert 'WARNING: message2' not in app.warning.getvalue() |
| 278 |
assert 'WARNING: message3' not in app.warning.getvalue() |
| 279 |
|
| 280 |
# actually logged as ordered |
| 281 |
warnings = strip_escape_sequences(app.warning.getvalue()) |
| 282 |
assert 'WARNING: message2\nWARNING: message3' in warnings |
| 283 |
|
| 284 |
|
| 285 |
@contextmanager |
| 286 |
def force_colors() -> Iterator[None]: |
| 287 |
forcecolor = os.environ.get('FORCE_COLOR', None) |
| 288 |
|
| 289 |
try: |
| 290 |
os.environ['FORCE_COLOR'] = '1' |
| 291 |
yield |
| 292 |
finally: |
| 293 |
if forcecolor is None: |
| 294 |
os.environ.pop('FORCE_COLOR', None) |
| 295 |
else: |
| 296 |
os.environ['FORCE_COLOR'] = forcecolor |
| 297 |
|
| 298 |
|
| 299 |
def test_log_no_ansi_colors(tmp_path: Path) -> None: |
| 300 |
with force_colors(): |
| 301 |
wfile = tmp_path / 'warnings.txt' |
| 302 |
srcdir = TEST_ROOTS_DIR / 'test-nitpicky-warnings' |
| 303 |
argv = list(map(str, ['-b', 'html', srcdir, tmp_path, '-n', '-w', wfile])) |
| 304 |
retcode = build_main(argv) |
| 305 |
assert retcode == 0 |
| 306 |
|
| 307 |
content = wfile.read_text(encoding='utf8') |
| 308 |
assert '\x1b[91m' not in content |
| 309 |
|
| 310 |
|
| 311 |
@pytest.mark.sphinx('html', testroot='root') |
| 312 |
def test_colored_logs(app: SphinxTestApp) -> None: |
| 313 |
logging.setup(app, app.status, app.warning, verbosity=2) |
| 314 |
logger = logging.getLogger(__name__) |
| 315 |
|
| 316 |
# default colors |
| 317 |
logger.debug('message1') |
| 318 |
logger.verbose('message2') |
| 319 |
logger.info('message3') |
| 320 |
logger.warning('message4') |
| 321 |
logger.critical('message5') |
| 322 |
logger.error('message6') |
| 323 |
|
| 324 |
assert colorize('darkgray', 'message1') in app.status.getvalue() |
| 325 |
assert 'message2\n' in app.status.getvalue() # not colored |
| 326 |
assert 'message3\n' in app.status.getvalue() # not colored |
| 327 |
assert colorize('red', 'WARNING: message4') in app.warning.getvalue() |
| 328 |
assert 'CRITICAL: message5\n' in app.warning.getvalue() # not colored |
| 329 |
assert colorize('darkred', 'ERROR: message6') in app.warning.getvalue() |
| 330 |
|
| 331 |
# color specification |
| 332 |
logger.debug('message7', color='white') |
| 333 |
logger.info('message8', color='red') |
| 334 |
assert colorize('white', 'message7') in app.status.getvalue() |
| 335 |
assert colorize('red', 'message8') in app.status.getvalue() |
| 336 |
|
| 337 |
|
| 338 |
@pytest.mark.xfail( |
| 339 |
os.name != 'posix', |
| 340 |
reason='Parallel mode does not work on Windows', |
| 341 |
) |
| 342 |
def test_logging_in_ParallelTasks(app: SphinxTestApp) -> None: |
| 343 |
logging.setup(app, app.status, app.warning) |
| 344 |
logger = logging.getLogger(__name__) |
| 345 |
|
| 346 |
def child_process() -> None: |
| 347 |
logger.info('message1') |
| 348 |
logger.warning('message2', location='index') |
| 349 |
|
| 350 |
tasks = ParallelTasks(1) |
| 351 |
tasks.add_task(child_process) |
| 352 |
tasks.join() |
| 353 |
assert 'message1' in app.status.getvalue() |
| 354 |
assert 'index.txt: WARNING: message2' in app.warning.getvalue() |
| 355 |
|
| 356 |
|
| 357 |
@pytest.mark.sphinx('html', testroot='root') |
| 358 |
def test_output_with_unencodable_char(app: SphinxTestApp) -> None: |
| 359 |
class StreamWriter: |
| 360 |
def __init__(self, stream: io.StringIO) -> None: |
| 361 |
self.stream = stream |
| 362 |
|
| 363 |
def write(self, object: str) -> None: |
| 364 |
self.stream.write(object.encode('cp1252').decode('cp1252')) |
| 365 |
|
| 366 |
logging.setup(app, StreamWriter(app.status), app.warning, verbosity=0) # type: ignore[arg-type] |
| 367 |
logger = logging.getLogger(__name__) |
| 368 |
|
| 369 |
# info with UnicodeEncodeError |
| 370 |
app.status.truncate(0) |
| 371 |
app.status.seek(0) |
| 372 |
logger.info('unicode \u206d...') |
| 373 |
assert app.status.getvalue() == 'unicode ?...\n' |
| 374 |
|
| 375 |
|
| 376 |
@pytest.mark.sphinx('html', testroot='root') |
| 377 |
def test_prefixed_warnings(app: SphinxTestApp) -> None: |
| 378 |
logging.setup(app, app.status, app.warning) |
| 379 |
logger = logging.getLogger(__name__) |
| 380 |
|
| 381 |
logger.warning('message1') |
| 382 |
with prefixed_warnings('PREFIX:'): |
| 383 |
logger.warning('message2') |
| 384 |
with prefixed_warnings('Another PREFIX:'): |
| 385 |
logger.warning('message3') |
| 386 |
logger.warning('message4') |
| 387 |
logger.warning('message5') |
| 388 |
|
| 389 |
assert 'WARNING: message1' in app.warning.getvalue() |
| 390 |
assert 'WARNING: PREFIX: message2' in app.warning.getvalue() |
| 391 |
assert 'WARNING: Another PREFIX: message3' in app.warning.getvalue() |
| 392 |
assert 'WARNING: PREFIX: message4' in app.warning.getvalue() |
| 393 |
assert 'WARNING: message5' in app.warning.getvalue() |
| 394 |
|
| 395 |
|
| 396 |
def test_get_node_location_abspath() -> None: |
| 397 |
# Ensure that node locations are reported as an absolute path, |
| 398 |
# even if the source attribute is a relative path. |
| 399 |
|
| 400 |
relative_filename = Path('relative', 'path.txt') |
| 401 |
absolute_filename = relative_filename.resolve() |
| 402 |
|
| 403 |
n = nodes.Element() |
| 404 |
n.source = str(relative_filename) |
| 405 |
|
| 406 |
location = logging.get_node_location(n) |
| 407 |
|
| 408 |
assert location == f'{absolute_filename}:' |
| 409 |
|
| 410 |
|
| 411 |
@pytest.mark.sphinx('html', testroot='root', confoverrides={'show_warning_types': True}) |
| 412 |
def test_show_warning_types(app: SphinxTestApp) -> None: |
| 413 |
logging.setup(app, app.status, app.warning) |
| 414 |
logger = logging.getLogger(__name__) |
| 415 |
logger.warning('message2') |
| 416 |
logger.warning('message3', type='test') |
| 417 |
logger.warning('message4', type='test', subtype='logging') |
| 418 |
|
| 419 |
warnings = strip_escape_sequences(app.warning.getvalue()).splitlines() |
| 420 |
|
| 421 |
assert warnings == [ |
| 422 |
'WARNING: message2', |
| 423 |
'WARNING: message3 [test]', |
| 424 |
'WARNING: message4 [test.logging]', |
| 425 |
] |