|
| 1 | +"""Tests the presentation style of exceptions.""" |
| 2 | + |
| 3 | +import textwrap |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from pip._internal.exceptions import DiagnosticPipError |
| 8 | + |
| 9 | + |
| 10 | +class TestDiagnosticPipErrorCreation: |
| 11 | + def test_fails_without_reference(self) -> None: |
| 12 | + class DerivedError(DiagnosticPipError): |
| 13 | + pass |
| 14 | + |
| 15 | + with pytest.raises(AssertionError) as exc_info: |
| 16 | + DerivedError(message="", context=None, hint_stmt=None) |
| 17 | + |
| 18 | + assert str(exc_info.value) == "error reference not provided!" |
| 19 | + |
| 20 | + def test_can_fetch_reference_from_subclass(self) -> None: |
| 21 | + class DerivedError(DiagnosticPipError): |
| 22 | + reference = "subclass-reference" |
| 23 | + |
| 24 | + obj = DerivedError(message="", context=None, hint_stmt=None) |
| 25 | + assert obj.reference == "subclass-reference" |
| 26 | + |
| 27 | + def test_can_fetch_reference_from_arguments(self) -> None: |
| 28 | + class DerivedError(DiagnosticPipError): |
| 29 | + pass |
| 30 | + |
| 31 | + obj = DerivedError( |
| 32 | + message="", context=None, hint_stmt=None, reference="subclass-reference" |
| 33 | + ) |
| 34 | + assert obj.reference == "subclass-reference" |
| 35 | + |
| 36 | + @pytest.mark.parametrize( |
| 37 | + "name", |
| 38 | + [ |
| 39 | + "BADNAME", |
| 40 | + "BadName", |
| 41 | + "bad_name", |
| 42 | + "BAD_NAME", |
| 43 | + "_bad", |
| 44 | + "bad-name-", |
| 45 | + "bad--name", |
| 46 | + "-bad-name", |
| 47 | + "bad-name-due-to-1-number", |
| 48 | + ], |
| 49 | + ) |
| 50 | + def test_rejects_non_kebab_case_names(self, name: str) -> None: |
| 51 | + class DerivedError(DiagnosticPipError): |
| 52 | + reference = name |
| 53 | + |
| 54 | + with pytest.raises(AssertionError) as exc_info: |
| 55 | + DerivedError(message="", context=None, hint_stmt=None) |
| 56 | + |
| 57 | + assert str(exc_info.value) == "error reference must be kebab-case!" |
| 58 | + |
| 59 | + |
| 60 | +class TestDiagnosticPipErrorPresentation_ASCII: |
| 61 | + def test_complete(self) -> None: |
| 62 | + err = DiagnosticPipError( |
| 63 | + reference="test-diagnostic", |
| 64 | + message="Oh no!\nIt broke. :(", |
| 65 | + context="Something went wrong\nvery wrong.", |
| 66 | + attention_stmt="You did something wrong, which is what caused this error.", |
| 67 | + hint_stmt="Do it better next time, by trying harder.", |
| 68 | + ) |
| 69 | + |
| 70 | + assert str(err) == textwrap.dedent( |
| 71 | + """\ |
| 72 | + Oh no! |
| 73 | + It broke. :( |
| 74 | +
|
| 75 | + Something went wrong |
| 76 | + very wrong. |
| 77 | +
|
| 78 | + Note: You did something wrong, which is what caused this error. |
| 79 | + Hint: Do it better next time, by trying harder. |
| 80 | + """ |
| 81 | + ) |
| 82 | + |
| 83 | + def test_no_context(self) -> None: |
| 84 | + err = DiagnosticPipError( |
| 85 | + reference="test-diagnostic", |
| 86 | + message="Oh no!\nIt broke. :(", |
| 87 | + context=None, |
| 88 | + attention_stmt="You did something wrong, which is what caused this error.", |
| 89 | + hint_stmt="Do it better next time, by trying harder.", |
| 90 | + ) |
| 91 | + |
| 92 | + assert str(err) == textwrap.dedent( |
| 93 | + """\ |
| 94 | + Oh no! |
| 95 | + It broke. :( |
| 96 | +
|
| 97 | + Note: You did something wrong, which is what caused this error. |
| 98 | + Hint: Do it better next time, by trying harder. |
| 99 | + """ |
| 100 | + ) |
| 101 | + |
| 102 | + def test_no_note(self) -> None: |
| 103 | + err = DiagnosticPipError( |
| 104 | + reference="test-diagnostic", |
| 105 | + message="Oh no!\nIt broke. :(", |
| 106 | + context="Something went wrong\nvery wrong.", |
| 107 | + attention_stmt=None, |
| 108 | + hint_stmt="Do it better next time, by trying harder.", |
| 109 | + ) |
| 110 | + |
| 111 | + assert str(err) == textwrap.dedent( |
| 112 | + """\ |
| 113 | + Oh no! |
| 114 | + It broke. :( |
| 115 | +
|
| 116 | + Something went wrong |
| 117 | + very wrong. |
| 118 | +
|
| 119 | + Hint: Do it better next time, by trying harder. |
| 120 | + """ |
| 121 | + ) |
| 122 | + |
| 123 | + def test_no_hint(self) -> None: |
| 124 | + err = DiagnosticPipError( |
| 125 | + reference="test-diagnostic", |
| 126 | + message="Oh no!\nIt broke. :(", |
| 127 | + context="Something went wrong\nvery wrong.", |
| 128 | + attention_stmt="You did something wrong, which is what caused this error.", |
| 129 | + hint_stmt=None, |
| 130 | + ) |
| 131 | + |
| 132 | + assert str(err) == textwrap.dedent( |
| 133 | + """\ |
| 134 | + Oh no! |
| 135 | + It broke. :( |
| 136 | +
|
| 137 | + Something went wrong |
| 138 | + very wrong. |
| 139 | +
|
| 140 | + Note: You did something wrong, which is what caused this error. |
| 141 | + """ |
| 142 | + ) |
| 143 | + |
| 144 | + def test_no_context_no_hint(self) -> None: |
| 145 | + err = DiagnosticPipError( |
| 146 | + reference="test-diagnostic", |
| 147 | + message="Oh no!\nIt broke. :(", |
| 148 | + context=None, |
| 149 | + attention_stmt="You did something wrong, which is what caused this error.", |
| 150 | + hint_stmt=None, |
| 151 | + ) |
| 152 | + |
| 153 | + assert str(err) == textwrap.dedent( |
| 154 | + """\ |
| 155 | + Oh no! |
| 156 | + It broke. :( |
| 157 | +
|
| 158 | + Note: You did something wrong, which is what caused this error. |
| 159 | + """ |
| 160 | + ) |
| 161 | + |
| 162 | + def test_no_context_no_note(self) -> None: |
| 163 | + err = DiagnosticPipError( |
| 164 | + reference="test-diagnostic", |
| 165 | + message="Oh no!\nIt broke. :(", |
| 166 | + context=None, |
| 167 | + attention_stmt=None, |
| 168 | + hint_stmt="Do it better next time, by trying harder.", |
| 169 | + ) |
| 170 | + |
| 171 | + assert str(err) == textwrap.dedent( |
| 172 | + """\ |
| 173 | + Oh no! |
| 174 | + It broke. :( |
| 175 | +
|
| 176 | + Hint: Do it better next time, by trying harder. |
| 177 | + """ |
| 178 | + ) |
| 179 | + |
| 180 | + def test_no_hint_no_note(self) -> None: |
| 181 | + err = DiagnosticPipError( |
| 182 | + reference="test-diagnostic", |
| 183 | + message="Oh no!\nIt broke. :(", |
| 184 | + context="Something went wrong\nvery wrong.", |
| 185 | + attention_stmt=None, |
| 186 | + hint_stmt=None, |
| 187 | + ) |
| 188 | + |
| 189 | + assert str(err) == textwrap.dedent( |
| 190 | + """\ |
| 191 | + Oh no! |
| 192 | + It broke. :( |
| 193 | +
|
| 194 | + Something went wrong |
| 195 | + very wrong. |
| 196 | + """ |
| 197 | + ) |
| 198 | + |
| 199 | + def test_no_hint_no_note_no_context(self) -> None: |
| 200 | + err = DiagnosticPipError( |
| 201 | + reference="test-diagnostic", |
| 202 | + message="Oh no!\nIt broke. :(", |
| 203 | + context=None, |
| 204 | + hint_stmt=None, |
| 205 | + attention_stmt=None, |
| 206 | + ) |
| 207 | + |
| 208 | + assert str(err) == textwrap.dedent( |
| 209 | + """\ |
| 210 | + Oh no! |
| 211 | + It broke. :( |
| 212 | + """ |
| 213 | + ) |
0 commit comments