| 1 |
# This file is part of Hypothesis, which may be found at |
| 2 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 3 |
# |
| 4 |
# Copyright the Hypothesis Authors. |
| 5 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 8 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 9 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 10 |
|
| 11 |
from contextlib import suppress |
| 12 |
|
| 13 |
import pytest |
| 14 |
|
| 15 |
from hypothesis import given, strategies as st |
| 16 |
from hypothesis.errors import Flaky, FlakyBackendFailure, FlakyFailure, Frozen, StopTest |
| 17 |
from hypothesis.internal.compat import BaseExceptionGroup, ExceptionGroup |
| 18 |
from hypothesis.strategies import DataObject |
| 19 |
|
| 20 |
|
| 21 |
def test_discard_frozen() -> None: |
| 22 |
@given(st.data()) |
| 23 |
def discard_frozen(data: DataObject) -> None: |
| 24 |
# Accessing .conjecture_data is internal API. Other possible ways of freezing |
| 25 |
# data might go through ConjectureRunner.cached_test_function_ir or |
| 26 |
# ConjectureRunner.test_function |
| 27 |
data.conjecture_data.freeze() |
| 28 |
# Raising Frozen doesn't actually do anything, what matters is |
| 29 |
# whether the data is frozen. |
| 30 |
raise ExceptionGroup("", [Frozen()]) |
| 31 |
|
| 32 |
discard_frozen() |
| 33 |
|
| 34 |
|
| 35 |
def test_discard_multiple_frozen() -> None: |
| 36 |
@given(st.data()) |
| 37 |
def discard_multiple_frozen(data: DataObject) -> None: |
| 38 |
data.conjecture_data.freeze() |
| 39 |
raise ExceptionGroup("", [Frozen(), Frozen()]) |
| 40 |
|
| 41 |
discard_multiple_frozen() |
| 42 |
|
| 43 |
|
| 44 |
def test_user_error_and_frozen() -> None: |
| 45 |
@given(st.data()) |
| 46 |
def user_error_and_frozen(data: DataObject) -> None: |
| 47 |
raise ExceptionGroup("", [Frozen(), TypeError()]) |
| 48 |
|
| 49 |
with pytest.raises(ExceptionGroup) as excinfo: |
| 50 |
user_error_and_frozen() |
| 51 |
e = excinfo.value |
| 52 |
assert isinstance(e, ExceptionGroup) |
| 53 |
assert len(e.exceptions) == 2 |
| 54 |
assert isinstance(e.exceptions[0], Frozen) |
| 55 |
assert isinstance(e.exceptions[1], TypeError) |
| 56 |
|
| 57 |
|
| 58 |
def test_user_error_and_stoptest() -> None: |
| 59 |
# if the code base had "proper" handling of exceptiongroups, the StopTest would |
| 60 |
# probably be handled by an except*. |
| 61 |
# TODO: which I suppose is an argument in favor of stripping it?? |
| 62 |
@given(st.data()) |
| 63 |
def user_error_and_stoptest(data: DataObject) -> None: |
| 64 |
raise BaseExceptionGroup( |
| 65 |
"", [StopTest(data.conjecture_data.testcounter), TypeError()] |
| 66 |
) |
| 67 |
|
| 68 |
with pytest.raises(BaseExceptionGroup) as excinfo: |
| 69 |
user_error_and_stoptest() |
| 70 |
e = excinfo.value |
| 71 |
assert isinstance(e, BaseExceptionGroup) |
| 72 |
assert len(e.exceptions) == 2 |
| 73 |
assert isinstance(e.exceptions[0], StopTest) |
| 74 |
assert isinstance(e.exceptions[1], TypeError) |
| 75 |
|
| 76 |
|
| 77 |
def test_lone_user_error() -> None: |
| 78 |
# we don't want to unwrap exceptiongroups, since they might contain |
| 79 |
# useful debugging info |
| 80 |
@given(st.data()) |
| 81 |
def lone_user_error(data: DataObject) -> None: |
| 82 |
raise ExceptionGroup("foo", [TypeError()]) |
| 83 |
|
| 84 |
with pytest.raises(ExceptionGroup) as excinfo: |
| 85 |
lone_user_error() |
| 86 |
e = excinfo.value |
| 87 |
assert isinstance(e, ExceptionGroup) |
| 88 |
assert len(e.exceptions) == 1 |
| 89 |
assert isinstance(e.exceptions[0], TypeError) |
| 90 |
|
| 91 |
|
| 92 |
def test_nested_stoptest() -> None: |
| 93 |
@given(st.data()) |
| 94 |
def nested_stoptest(data: DataObject) -> None: |
| 95 |
raise BaseExceptionGroup( |
| 96 |
"", |
| 97 |
[BaseExceptionGroup("", [StopTest(data.conjecture_data.testcounter)])], |
| 98 |
) |
| 99 |
|
| 100 |
nested_stoptest() |
| 101 |
|
| 102 |
|
| 103 |
def test_frozen_and_stoptest() -> None: |
| 104 |
# frozen+stoptest => strip frozen and let engine handle StopTest |
| 105 |
# actually.. I don't think I've got a live repo for this either. |
| 106 |
@given(st.data()) |
| 107 |
def frozen_and_stoptest(data: DataObject) -> None: |
| 108 |
raise BaseExceptionGroup( |
| 109 |
"", [StopTest(data.conjecture_data.testcounter), Frozen()] |
| 110 |
) |
| 111 |
|
| 112 |
frozen_and_stoptest() |
| 113 |
|
| 114 |
|
| 115 |
def test_multiple_stoptest_1() -> None: |
| 116 |
# multiple stoptest, reraise the one with lowest testcounter |
| 117 |
@given(st.data()) |
| 118 |
def multiple_stoptest(data: DataObject) -> None: |
| 119 |
c = data.conjecture_data.testcounter |
| 120 |
raise BaseExceptionGroup("", [StopTest(c), StopTest(c + 1)]) |
| 121 |
|
| 122 |
multiple_stoptest() |
| 123 |
|
| 124 |
|
| 125 |
def test_multiple_stoptest_2() -> None: |
| 126 |
# the lower value is raised, which does not match data.conjecture_data.testcounter |
| 127 |
# so it is not handled by the engine |
| 128 |
@given(st.data()) |
| 129 |
def multiple_stoptest_2(data: DataObject) -> None: |
| 130 |
c = data.conjecture_data.testcounter |
| 131 |
raise BaseExceptionGroup("", [StopTest(c), StopTest(c - 1)]) |
| 132 |
|
| 133 |
with pytest.raises(StopTest): |
| 134 |
multiple_stoptest_2() |
| 135 |
|
| 136 |
|
| 137 |
def test_stoptest_and_hypothesisexception() -> None: |
| 138 |
# current code raises the first hypothesisexception and throws away stoptest |
| 139 |
@given(st.data()) |
| 140 |
def stoptest_and_hypothesisexception(data: DataObject) -> None: |
| 141 |
c = data.conjecture_data.testcounter |
| 142 |
raise BaseExceptionGroup("", [StopTest(c), Flaky()]) |
| 143 |
|
| 144 |
with pytest.raises(Flaky): |
| 145 |
stoptest_and_hypothesisexception() |
| 146 |
|
| 147 |
|
| 148 |
def test_multiple_hypothesisexception() -> None: |
| 149 |
# this can happen in several ways, see nocover/test_exceptiongroup.py |
| 150 |
@given(st.data()) |
| 151 |
def stoptest_and_hypothesisexception(data: DataObject) -> None: |
| 152 |
c = data.conjecture_data.testcounter |
| 153 |
raise BaseExceptionGroup("", [StopTest(c), Flaky()]) |
| 154 |
|
| 155 |
with pytest.raises(Flaky): |
| 156 |
stoptest_and_hypothesisexception() |
| 157 |
|
| 158 |
|
| 159 |
@pytest.mark.parametrize("ExceptionClass", [FlakyFailure, FlakyBackendFailure]) |
| 160 |
def test_exceptiongroups_reconstruct_original_type(ExceptionClass): |
| 161 |
# contextlib.suppress uses .split, and if ExceptionClass does not implement |
| 162 |
# .derive, .split will return a standard ExceptionGroup instead of |
| 163 |
# ExceptionClass. |
| 164 |
# see https://github.com/python/cpython/issues/119287 |
| 165 |
class UnrelatedException(Exception): |
| 166 |
pass |
| 167 |
|
| 168 |
with pytest.raises(ExceptionClass), suppress(UnrelatedException): |
| 169 |
raise ExceptionClass("exception message", [Exception()]) |
| 170 |
|
| 171 |
|
| 172 |
@pytest.mark.parametrize("ExceptionClass", [FlakyFailure, FlakyBackendFailure]) |
| 173 |
def test_derived_exception_group(ExceptionClass): |
| 174 |
exception = ExceptionClass("exception message", [Exception()]) |
| 175 |
# we don't expect a match, we just want to test .derive. |
| 176 |
match, rest = exception.split(()) |
| 177 |
assert match is None |
| 178 |
assert isinstance(rest, ExceptionClass) |