File: test_util_display.py

package info (click to toggle)
sphinx 9.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 28,732 kB
  • sloc: python: 109,394; javascript: 37,318; perl: 449; makefile: 183; sh: 37; xml: 19; ansic: 2
file content (119 lines) | stat: -rw-r--r-- 3,806 bytes parent folder | download
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
"""Tests util functions."""

from __future__ import annotations

from typing import TYPE_CHECKING

import pytest

from sphinx._cli.util.errors import strip_escape_sequences
from sphinx.util import logging
from sphinx.util.display import (
    SkipProgressMessage,
    display_chunk,
    progress_message,
    status_iterator,
)

if TYPE_CHECKING:
    from sphinx.testing.util import SphinxTestApp


def test_display_chunk() -> None:
    assert display_chunk('hello') == 'hello'
    assert display_chunk(['hello']) == 'hello'
    assert display_chunk(['hello', 'sphinx', 'world']) == 'hello .. world'
    assert display_chunk(('hello',)) == 'hello'
    assert display_chunk(('hello', 'sphinx', 'world')) == 'hello .. world'


@pytest.mark.sphinx('dummy', testroot='root')
def test_status_iterator_length_0(app: SphinxTestApp) -> None:
    logging.setup(app, app.status, app.warning)

    # test for status_iterator (length=0)
    app.status.seek(0)
    app.status.truncate(0)
    yields = list(status_iterator(['hello', 'sphinx', 'world'], 'testing ... '))
    output = strip_escape_sequences(app.status.getvalue())
    assert 'testing ... hello sphinx world \n' in output
    assert yields == ['hello', 'sphinx', 'world']


@pytest.mark.sphinx('dummy', testroot='root')
def test_status_iterator_verbosity_0(
    app: SphinxTestApp, monkeypatch: pytest.MonkeyPatch
) -> None:
    monkeypatch.setenv('FORCE_COLOR', '1')
    logging.setup(app, app.status, app.warning)

    # test for status_iterator (verbosity=0)
    app.status.seek(0)
    app.status.truncate(0)
    yields = status_iterator(
        ['hello', 'sphinx', 'world'], 'testing ... ', length=3, verbosity=0
    )
    assert list(yields) == ['hello', 'sphinx', 'world']
    output = strip_escape_sequences(app.status.getvalue())
    assert 'testing ... [ 33%] hello\r' in output
    assert 'testing ... [ 67%] sphinx\r' in output
    assert 'testing ... [100%] world\r\n' in output


@pytest.mark.sphinx('dummy', testroot='root')
def test_status_iterator_verbosity_1(
    app: SphinxTestApp, monkeypatch: pytest.MonkeyPatch
) -> None:
    monkeypatch.setenv('FORCE_COLOR', '1')
    logging.setup(app, app.status, app.warning)

    # test for status_iterator (verbosity=1)
    app.status.seek(0)
    app.status.truncate(0)
    yields = status_iterator(
        ['hello', 'sphinx', 'world'], 'testing ... ', length=3, verbosity=1
    )
    assert list(yields) == ['hello', 'sphinx', 'world']
    output = strip_escape_sequences(app.status.getvalue())
    assert 'testing ... [ 33%] hello\n' in output
    assert 'testing ... [ 67%] sphinx\n' in output
    assert 'testing ... [100%] world\n\n' in output


@pytest.mark.sphinx('html', testroot='root')
def test_progress_message(app: SphinxTestApp) -> None:
    logging.setup(app, app.status, app.warning)
    logger = logging.getLogger(__name__)

    # standard case
    with progress_message('testing'):
        logger.info('blah ', nonl=True)

    output = strip_escape_sequences(app.status.getvalue())
    assert 'testing... blah done\n' in output

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

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

    # error case
    try:
        with progress_message('testing'):
            raise RuntimeError  # NoQA: TRY301
    except Exception:
        pass

    output = strip_escape_sequences(app.status.getvalue())
    assert 'testing... failed\n' in output

    # decorator
    @progress_message('testing')
    def func() -> None:
        logger.info('in func ', nonl=True)

    func()
    output = strip_escape_sequences(app.status.getvalue())
    assert 'testing... in func done\n' in output