File: test_errors.py

package info (click to toggle)
sphinx-theme-builder 0.2.0b2-5
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 448 kB
  • sloc: python: 2,227; sh: 19; makefile: 3
file content (319 lines) | stat: -rw-r--r-- 12,206 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
"""Test the presentation style of exceptions."""

import textwrap
from io import StringIO
from typing import TYPE_CHECKING, Optional

import pytest
import rich
import rich.console
import rich.text

from sphinx_theme_builder._internal.errors import DiagnosticError

if TYPE_CHECKING:
    from typing import Literal


class TestDiagnosticErrorInitialisation:
    def test_fails_without_reference(self) -> None:
        class DerivedError(DiagnosticError):
            pass

        with pytest.raises(AssertionError) as exc_info:
            DerivedError(message="", context=None, hint_stmt=None)

        assert str(exc_info.value) == "error reference not provided!"

    def test_can_fetch_reference_from_subclass(self) -> None:
        class DerivedError(DiagnosticError):
            reference = "subclass-reference"

        obj = DerivedError(message="", context=None, hint_stmt=None)
        assert obj.reference == "subclass-reference"

    def test_can_fetch_reference_from_arguments(self) -> None:
        class DerivedError(DiagnosticError):
            pass

        obj = DerivedError(
            message="", context=None, hint_stmt=None, reference="subclass-reference"
        )
        assert obj.reference == "subclass-reference"

    @pytest.mark.parametrize(
        "name",
        [
            "BADNAME",
            "BadName",
            "bad_name",
            "BAD_NAME",
            "_bad",
            "bad-name-",
            "bad--name",
            "-bad-name",
            "bad-name-due-to-1-number",
        ],
    )
    def test_rejects_non_kebab_case_names(self, name: str) -> None:
        class DerivedError(DiagnosticError):
            reference = name

        with pytest.raises(AssertionError) as exc_info:
            DerivedError(message="", context=None, hint_stmt=None)

        assert str(exc_info.value) == "error reference must be kebab-case!"


def assert_presentation_matches(
    error: DiagnosticError,
    expected: str,
    *,
    color_system: 'Optional[Literal["auto", "standard", "256", "truecolor", "windows"]]'
) -> None:
    expected_output = textwrap.dedent(expected)

    stream = StringIO()
    console = rich.console.Console(file=stream, color_system=color_system)
    console.print(error)

    assert stream.getvalue() == expected_output


class TestDiagnosticPipErrorPresentation:
    def test_complete_string(self) -> None:
        # GIVEN
        error = DiagnosticError(
            reference="ooops-an-error-occured",
            message=(
                "This is an error message describing the issues."
                "\nIt can have multiple lines."
            ),
            context=(
                "This is some context associated with that error."
                "\nAny relevant additional details are mentioned here."
            ),
            hint_stmt=(
                "This is a hint, that will help you figure this out."
                "\nAnd the hint can have multiple lines."
            ),
            note_stmt=(
                "This is to draw your [b]attention[/] toward about something important."
                "\nAnd this can also have multiple lines."
            ),
        )

        # WHEN / THEN
        assert str(error) == (
            "<DiagnosticError: "
            "https://sphinx-theme-builder.rtfd.io/errors/#ooops-an-error-occured"
            ">"
        )
        assert repr(error) == (
            "<DiagnosticError("
            "reference='ooops-an-error-occured', "
            "message='This is an error message describing the issues.\\n"
            "It can have multiple lines.', "
            "context='This is some context associated with that error.\\n"
            "Any relevant additional details are mentioned here.', "
            "note_stmt='This is to draw your [b]attention[/] toward about something important.\\n"
            "And this can also have multiple lines.', "
            "hint_stmt='This is a hint, that will help you figure this out.\\n"
            "And the hint can have multiple lines.'"
            ")>"
        )

    def test_complete(self) -> None:
        assert_presentation_matches(
            DiagnosticError(
                reference="ooops-an-error-occured",
                message=(
                    "This is an error message describing the issues."
                    "\nIt can have multiple lines."
                ),
                context=(
                    "This is some context associated with that error."
                    "\nAny relevant additional details are mentioned here."
                ),
                hint_stmt=(
                    "This is a hint, that will help you figure this out."
                    "\nAnd the hint can have multiple lines."
                ),
                note_stmt=(
                    "This is to draw your [b]attention[/] toward about something important."
                    "\nAnd this can also have multiple lines."
                ),
            ),
            """\
                error: ooops-an-error-occured

                × This is an error message describing the issues.
                │ It can have multiple lines.
                ╰─> This is some context associated with that error.
                    Any relevant additional details are mentioned here.

                note: This is to draw your attention toward about something important.
                      And this can also have multiple lines.
                hint: This is a hint, that will help you figure this out.
                      And the hint can have multiple lines.
                link: https://sphinx-theme-builder.rtfd.io/errors/#ooops-an-error-occured
            """,
            color_system=None,
        )

    def test_complete_colors(self) -> None:
        assert_presentation_matches(
            DiagnosticError(
                reference="ooops-an-error-occured",
                message=(
                    "This is an error message describing the issues."
                    "\nIt can have multiple lines."
                ),
                context=(
                    "This is some context associated with that error."
                    "\nAny relevant additional details are mentioned here."
                ),
                hint_stmt=rich.text.Text(
                    "This is a hint, that will help you figure this out."
                    "\nAnd the [b]hint[/] can have multiple lines."
                ),
                note_stmt=(
                    "This is to draw your [b]attention[/] toward about something important."
                    "\nAnd this can also have multiple lines."
                ),
            ),
            # Yes, I know this is dumb.
            """\
                \x1b[1;31merror\x1b[0m: \x1b[1mooops-an-error-occured\x1b[0m

                \x1b[31m×\x1b[0m This is an error message describing the issues.
                \x1b[31m│\x1b[0m It can have multiple lines.
                \x1b[31m╰─>\x1b[0m This is some context associated with that error.
                \x1b[31m   \x1b[0m Any relevant additional details are mentioned here.

                \x1b[1;35mnote\x1b[0m: This is to draw your \x1b[1mattention\x1b[0m toward about something important.
                      And this can also have multiple lines.
                \x1b[1;36mhint\x1b[0m: This is a hint, that will help you figure this out.
                      And the [b]hint[/] can have multiple lines.
                \x1b[1mlink\x1b[0m: \x1b[4;94mhttps://sphinx-theme-builder.rtfd.io/errors/#ooops-an-error-occured\x1b[0m
            """,
            color_system="256",
        )

    def test_no_note_no_hint_no_context(self) -> None:
        # GIVEN
        assert_presentation_matches(
            DiagnosticError(
                reference="ooops-an-error-occured",
                message=(
                    "This is an error message describing the issues."
                    "\nIt can have multiple lines."
                ),
                context=None,
                hint_stmt=None,
            ),
            """\
                error: ooops-an-error-occured

                × This is an error message describing the issues.
                  It can have multiple lines.

                link: https://sphinx-theme-builder.rtfd.io/errors/#ooops-an-error-occured
            """,
            color_system=None,
        )

    def test_no_note_no_hint(self) -> None:
        # GIVEN
        assert_presentation_matches(
            DiagnosticError(
                reference="ooops-an-error-occured",
                message=(
                    "This is an error message describing the issues."
                    "\nIt can have multiple lines."
                ),
                context=(
                    "This is some context associated with that error."
                    "\nAny relevant additional details are mentioned here."
                ),
                hint_stmt=None,
            ),
            """\
                error: ooops-an-error-occured

                × This is an error message describing the issues.
                │ It can have multiple lines.
                ╰─> This is some context associated with that error.
                    Any relevant additional details are mentioned here.

                link: https://sphinx-theme-builder.rtfd.io/errors/#ooops-an-error-occured
            """,
            color_system=None,
        )

    def test_no_note(self) -> None:
        # GIVEN
        assert_presentation_matches(
            DiagnosticError(
                reference="ooops-an-error-occured",
                message=(
                    "This is an error message describing the issues."
                    "\nIt can have multiple lines."
                ),
                context=(
                    "This is some context associated with that error."
                    "\nAny relevant additional details are mentioned here."
                ),
                hint_stmt=(
                    "This is a hint, that will help you figure this out."
                    "\nAnd the hint can have multiple lines."
                ),
            ),
            """\
                error: ooops-an-error-occured

                × This is an error message describing the issues.
                │ It can have multiple lines.
                ╰─> This is some context associated with that error.
                    Any relevant additional details are mentioned here.

                hint: This is a hint, that will help you figure this out.
                      And the hint can have multiple lines.
                link: https://sphinx-theme-builder.rtfd.io/errors/#ooops-an-error-occured
            """,
            color_system=None,
        )

    def test_no_hint(self) -> None:
        # GIVEN
        assert_presentation_matches(
            DiagnosticError(
                reference="ooops-an-error-occured",
                message=(
                    "This is an error message describing the issues."
                    "\nIt can have multiple lines."
                ),
                context=(
                    "This is some context associated with that error."
                    "\nAny relevant additional details are mentioned here."
                ),
                hint_stmt=None,
                note_stmt=(
                    "This is to draw your [b]attention[/] toward about something important."
                    "\nAnd this can also have multiple lines."
                ),
            ),
            """\
                error: ooops-an-error-occured

                × This is an error message describing the issues.
                │ It can have multiple lines.
                ╰─> This is some context associated with that error.
                    Any relevant additional details are mentioned here.

                note: This is to draw your attention toward about something important.
                      And this can also have multiple lines.
                link: https://sphinx-theme-builder.rtfd.io/errors/#ooops-an-error-occured
            """,
            color_system=None,
        )