Skip to content

Commit

Permalink
[cleanup] deprecate sphinx.testing.util.strip_escseq in favor of …
Browse files Browse the repository at this point in the history
…``sphinx.util.console.strip_colors`` (sphinx-doc#12186)
  • Loading branch information
picnixz authored Mar 23, 2024
1 parent 22cee42 commit f24eef7
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 28 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ Deprecated
the public properties :attr:`!sphinx.testing.util.SphinxTestApp.status`
and :attr:`!sphinx.testing.util.SphinxTestApp.warning` instead.
Patch by Bénédikt Tran.
* tests: :func:`!sphinx.testing.util.strip_escseq` is deprecated in favor of
:func:`!sphinx.util.console.strip_colors`.
Patch by Bénédikt Tran.

Features added
--------------
Expand Down
5 changes: 5 additions & 0 deletions doc/extdev/deprecated.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ The following is a list of deprecated interfaces.
- Removed
- Alternatives

* - ``sphinx.testing.util.strip_escseq``
- 7.3
- 9.0
- ``sphinx.util.console.strip_colors``

* - Old-style Makefiles in ``sphinx-quickstart``
and the :option:`!-M`, :option:`!-m`, :option:`!--no-use-make-mode`,
and :option:`!--use-make-mode` options
Expand Down
25 changes: 19 additions & 6 deletions sphinx/testing/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import contextlib
import os
import re
import sys
import warnings
from io import StringIO
Expand All @@ -20,12 +19,13 @@
import sphinx.application
import sphinx.locale
import sphinx.pycode
from sphinx.util.console import strip_colors
from sphinx.util.docutils import additional_nodes

if TYPE_CHECKING:
from collections.abc import Mapping
from pathlib import Path
from typing import Any
from typing import Any, Final

from docutils.nodes import Node

Expand Down Expand Up @@ -224,10 +224,6 @@ def build(self, *args: Any, **kwargs: Any) -> None:
# otherwise, we can use built cache


def strip_escseq(text: str) -> str:
return re.sub('\x1b.*?m', '', text)


def _clean_up_global_state() -> None:
# clean up Docutils global state
directives._directives.clear() # type: ignore[attr-defined]
Expand All @@ -244,3 +240,20 @@ def _clean_up_global_state() -> None:

# clean up autodoc global state
sphinx.pycode.ModuleAnalyzer.cache.clear()


_DEPRECATED_OBJECTS: Final[dict[str, tuple[object, str, tuple[int, int]]]] = {
'strip_escseq': (strip_colors, 'sphinx.util.console.strip_colors', (9, 0)),
}


def __getattr__(name: str) -> Any:
if name not in _DEPRECATED_OBJECTS:
msg = f'module {__name__!r} has no attribute {name!r}'
raise AttributeError(msg)

from sphinx.deprecation import _deprecation_warning

deprecated_object, canonical_name, remove = _DEPRECATED_OBJECTS[name]
_deprecation_warning(__name__, name, canonical_name, remove=remove)
return deprecated_object
8 changes: 7 additions & 1 deletion sphinx/util/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import re
import shutil
import sys
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from typing import Final

try:
# check if colorama is installed to support color on Windows
Expand All @@ -23,6 +27,8 @@
\dK # ANSI Erase in Line
)""",
re.VERBOSE | re.ASCII)
_ansi_color_re: Final[re.Pattern[str]] = re.compile('\x1b.*?m')

codes: dict[str, str] = {}


Expand Down Expand Up @@ -93,7 +99,7 @@ def escseq(name: str) -> str:


def strip_colors(s: str) -> str:
return re.compile('\x1b.*?m').sub('', s)
return _ansi_color_re.sub('', s)


def _strip_escape_sequences(s: str) -> str:
Expand Down
7 changes: 4 additions & 3 deletions tests/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@

import sphinx.application
from sphinx.errors import ExtensionError
from sphinx.testing.util import SphinxTestApp, strip_escseq
from sphinx.testing.util import SphinxTestApp
from sphinx.util import logging
from sphinx.util.console import strip_colors

if TYPE_CHECKING:
import os
Expand Down Expand Up @@ -79,13 +80,13 @@ def test_emit_with_nonascii_name_node(app, status, warning):

def test_extensions(app, status, warning):
app.setup_extension('shutil')
warning = strip_escseq(warning.getvalue())
warning = strip_colors(warning.getvalue())
assert "extension 'shutil' has no setup() function" in warning


def test_extension_in_blacklist(app, status, warning):
app.setup_extension('sphinxjp.themecore')
msg = strip_escseq(warning.getvalue())
msg = strip_colors(warning.getvalue())
assert msg.startswith("WARNING: the extension 'sphinxjp.themecore' was")


Expand Down
6 changes: 3 additions & 3 deletions tests/test_builders/test_build_linkcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
RateLimit,
)
from sphinx.deprecation import RemovedInSphinx80Warning
from sphinx.testing.util import strip_escseq
from sphinx.util import requests
from sphinx.util.console import strip_colors

from tests.utils import CERT_FILE, http_server

Expand Down Expand Up @@ -588,7 +588,7 @@ def test_linkcheck_allowed_redirects(app, warning):
}

assert ("index.rst:3: WARNING: redirect http://localhost:7777/path2 - with Found to "
"http://localhost:7777/?redirected=1\n" in strip_escseq(warning.getvalue()))
"http://localhost:7777/?redirected=1\n" in strip_colors(warning.getvalue()))
assert len(warning.getvalue().splitlines()) == 1


Expand Down Expand Up @@ -785,7 +785,7 @@ def test_too_many_requests_retry_after_int_delay(app, capsys, status):
"info": "",
}
rate_limit_log = "-rate limited- http://localhost:7777/ | sleeping...\n"
assert rate_limit_log in strip_escseq(status.getvalue())
assert rate_limit_log in strip_colors(status.getvalue())
_stdout, stderr = capsys.readouterr()
assert stderr == textwrap.dedent(
"""\
Expand Down
4 changes: 2 additions & 2 deletions tests/test_builders/test_build_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from sphinx.testing.util import strip_escseq
from sphinx.util.console import strip_colors

ENV_WARNINGS = """\
{root}/autodoc_fodder.py:docstring of autodoc_fodder.MarkupError:\\d+: \
Expand Down Expand Up @@ -42,7 +42,7 @@


def _check_warnings(expected_warnings: str, warning: str) -> None:
warnings = strip_escseq(re.sub(re.escape(os.sep) + '{1,2}', '/', warning))
warnings = strip_colors(re.sub(re.escape(os.sep) + '{1,2}', '/', warning))
assert re.match(f'{expected_warnings}$', warnings), (
"Warnings don't match:\n"
+ f'--- Expected (regex):\n{expected_warnings}\n'
Expand Down
5 changes: 3 additions & 2 deletions tests/test_intl/test_intl.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
from docutils import nodes

from sphinx import locale
from sphinx.testing.util import assert_node, etree_parse, strip_escseq
from sphinx.testing.util import assert_node, etree_parse
from sphinx.util.console import strip_colors
from sphinx.util.nodes import NodeMatcher

_CATALOG_LOCALE = 'xx'
Expand Down Expand Up @@ -1593,7 +1594,7 @@ def test_image_glob_intl_using_figure_language_filename(app):


def getwarning(warnings):
return strip_escseq(warnings.getvalue().replace(os.sep, '/'))
return strip_colors(warnings.getvalue().replace(os.sep, '/'))


@pytest.mark.sphinx('html', testroot='basic',
Expand Down
16 changes: 8 additions & 8 deletions tests/test_util/test_util_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import pytest

from sphinx.testing.util import strip_escseq
from sphinx.util import logging
from sphinx.util.console import strip_colors
from sphinx.util.display import (
SkipProgressMessage,
display_chunk,
Expand All @@ -28,7 +28,7 @@ def test_status_iterator_length_0(app, status, warning):
status.seek(0)
status.truncate(0)
yields = list(status_iterator(['hello', 'sphinx', 'world'], 'testing ... '))
output = strip_escseq(status.getvalue())
output = strip_colors(status.getvalue())
assert 'testing ... hello sphinx world \n' in output
assert yields == ['hello', 'sphinx', 'world']

Expand All @@ -43,7 +43,7 @@ def test_status_iterator_verbosity_0(app, status, warning, monkeypatch):
status.truncate(0)
yields = list(status_iterator(['hello', 'sphinx', 'world'], 'testing ... ',
length=3, verbosity=0))
output = strip_escseq(status.getvalue())
output = strip_colors(status.getvalue())
assert 'testing ... [ 33%] hello\r' in output
assert 'testing ... [ 67%] sphinx\r' in output
assert 'testing ... [100%] world\r\n' in output
Expand All @@ -60,7 +60,7 @@ def test_status_iterator_verbosity_1(app, status, warning, monkeypatch):
status.truncate(0)
yields = list(status_iterator(['hello', 'sphinx', 'world'], 'testing ... ',
length=3, verbosity=1))
output = strip_escseq(status.getvalue())
output = strip_colors(status.getvalue())
assert 'testing ... [ 33%] hello\n' in output
assert 'testing ... [ 67%] sphinx\n' in output
assert 'testing ... [100%] world\n\n' in output
Expand All @@ -75,14 +75,14 @@ def test_progress_message(app, status, warning):
with progress_message('testing'):
logger.info('blah ', nonl=True)

output = strip_escseq(status.getvalue())
output = strip_colors(status.getvalue())
assert 'testing... blah done\n' in output

# skipping case
with progress_message('testing'):
raise SkipProgressMessage('Reason: %s', 'error') # NoQA: EM101

output = strip_escseq(status.getvalue())
output = strip_colors(status.getvalue())
assert 'testing... skipped\nReason: error\n' in output

# error case
Expand All @@ -92,7 +92,7 @@ def test_progress_message(app, status, warning):
except Exception:
pass

output = strip_escseq(status.getvalue())
output = strip_colors(status.getvalue())
assert 'testing... failed\n' in output

# decorator
Expand All @@ -101,5 +101,5 @@ def func():
logger.info('in func ', nonl=True)

func()
output = strip_escseq(status.getvalue())
output = strip_colors(status.getvalue())
assert 'testing... in func done\n' in output
5 changes: 2 additions & 3 deletions tests/test_util/test_util_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from docutils import nodes

from sphinx.errors import SphinxWarning
from sphinx.testing.util import strip_escseq
from sphinx.util import logging, osutil
from sphinx.util.console import colorize, strip_colors
from sphinx.util.logging import is_suppressed_warning, prefixed_warnings
Expand Down Expand Up @@ -110,7 +109,7 @@ def test_once_warning_log(app, status, warning):
logger.warning('message: %d', 1, once=True)
logger.warning('message: %d', 2, once=True)

assert 'WARNING: message: 1\nWARNING: message: 2\n' in strip_escseq(warning.getvalue())
assert 'WARNING: message: 1\nWARNING: message: 2\n' in strip_colors(warning.getvalue())


def test_is_suppressed_warning():
Expand Down Expand Up @@ -278,7 +277,7 @@ def test_pending_warnings(app, status, warning):
assert 'WARNING: message3' not in warning.getvalue()

# actually logged as ordered
assert 'WARNING: message2\nWARNING: message3' in strip_escseq(warning.getvalue())
assert 'WARNING: message2\nWARNING: message3' in strip_colors(warning.getvalue())


def test_colored_logs(app, status, warning):
Expand Down

0 comments on commit f24eef7

Please sign in to comment.