Debian Codegraph @

tests/test_util/test_util_display.py

raw
1 """Tests util functions."""
2
3 from __future__ import annotations
4
5 from typing import TYPE_CHECKING
6
7 import pytest
8
9 from sphinx._cli.util.errors import strip_escape_sequences
10 from sphinx.util import logging
11 from sphinx.util.display import (
12 SkipProgressMessage,
13 display_chunk,
14 progress_message,
15 status_iterator,
16 )
17
18 if TYPE_CHECKING:
19 from sphinx.testing.util import SphinxTestApp
20
21
22 def test_display_chunk() -> None:
23 assert display_chunk('hello') == 'hello'
24 assert display_chunk(['hello']) == 'hello'
25 assert display_chunk(['hello', 'sphinx', 'world']) == 'hello .. world'
26 assert display_chunk(('hello',)) == 'hello'
27 assert display_chunk(('hello', 'sphinx', 'world')) == 'hello .. world'
28
29
30 @pytest.mark.sphinx('dummy', testroot='root')
31 def test_status_iterator_length_0(app: SphinxTestApp) -> None:
32 logging.setup(app, app.status, app.warning)
33
34 # test for status_iterator (length=0)
35 app.status.seek(0)
36 app.status.truncate(0)
37 yields = list(status_iterator(['hello', 'sphinx', 'world'], 'testing ... '))
38 output = strip_escape_sequences(app.status.getvalue())
39 assert 'testing ... hello sphinx world \n' in output
40 assert yields == ['hello', 'sphinx', 'world']
41
42
43 @pytest.mark.sphinx('dummy', testroot='root')
44 def test_status_iterator_verbosity_0(
45 app: SphinxTestApp, monkeypatch: pytest.MonkeyPatch
46 ) -> None:
47 monkeypatch.setenv('FORCE_COLOR', '1')
48 logging.setup(app, app.status, app.warning)
49
50 # test for status_iterator (verbosity=0)
51 app.status.seek(0)
52 app.status.truncate(0)
53 yields = status_iterator(
54 ['hello', 'sphinx', 'world'], 'testing ... ', length=3, verbosity=0
55 )
56 assert list(yields) == ['hello', 'sphinx', 'world']
57 output = strip_escape_sequences(app.status.getvalue())
58 assert 'testing ... [ 33%] hello\r' in output
59 assert 'testing ... [ 67%] sphinx\r' in output
60 assert 'testing ... [100%] world\r\n' in output
61
62
63 @pytest.mark.sphinx('dummy', testroot='root')
64 def test_status_iterator_verbosity_1(
65 app: SphinxTestApp, monkeypatch: pytest.MonkeyPatch
66 ) -> None:
67 monkeypatch.setenv('FORCE_COLOR', '1')
68 logging.setup(app, app.status, app.warning)
69
70 # test for status_iterator (verbosity=1)
71 app.status.seek(0)
72 app.status.truncate(0)
73 yields = status_iterator(
74 ['hello', 'sphinx', 'world'], 'testing ... ', length=3, verbosity=1
75 )
76 assert list(yields) == ['hello', 'sphinx', 'world']
77 output = strip_escape_sequences(app.status.getvalue())
78 assert 'testing ... [ 33%] hello\n' in output
79 assert 'testing ... [ 67%] sphinx\n' in output
80 assert 'testing ... [100%] world\n\n' in output
81
82
83 @pytest.mark.sphinx('html', testroot='root')
84 def test_progress_message(app: SphinxTestApp) -> None:
85 logging.setup(app, app.status, app.warning)
86 logger = logging.getLogger(__name__)
87
88 # standard case
89 with progress_message('testing'):
90 logger.info('blah ', nonl=True)
91
92 output = strip_escape_sequences(app.status.getvalue())
93 assert 'testing... blah done\n' in output
94
95 # skipping case
96 with progress_message('testing'):
97 raise SkipProgressMessage('Reason: %s', 'error') # NoQA: EM101,TRY003
98
99 output = strip_escape_sequences(app.status.getvalue())
100 assert 'testing... skipped\nReason: error\n' in output
101
102 # error case
103 try:
104 with progress_message('testing'):
105 raise RuntimeError # NoQA: TRY301
106 except Exception:
107 pass
108
109 output = strip_escape_sequences(app.status.getvalue())
110 assert 'testing... failed\n' in output
111
112 # decorator
113 @progress_message('testing')
114 def func() -> None:
115 logger.info('in func ', nonl=True)
116
117 func()
118 output = strip_escape_sequences(app.status.getvalue())
119 assert 'testing... in func done\n' in output