| 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 |
import os |
| 12 |
|
| 13 |
import pytest |
| 14 |
|
| 15 |
import hypothesis |
| 16 |
from hypothesis import errors |
| 17 |
from hypothesis.internal import escalation as esc |
| 18 |
from hypothesis.internal.compat import BaseExceptionGroup |
| 19 |
|
| 20 |
|
| 21 |
def test_is_hypothesis_file_not_confused_by_prefix(monkeypatch): |
| 22 |
# Errors in third-party extensions such as `hypothesis-trio` or |
| 23 |
# `hypothesis-jsonschema` used to be incorrectly considered to be |
| 24 |
# Hypothesis internal errors, which could result in confusing error |
| 25 |
# messages. This test makes sure that files like: |
| 26 |
# `[...]/python3.12/site-packages/hypothesis_something/[...]` |
| 27 |
# are not considered as hypothesis files. |
| 28 |
root = os.path.dirname(hypothesis.__file__) |
| 29 |
assert esc.is_hypothesis_file(hypothesis.__file__) |
| 30 |
assert esc.is_hypothesis_file(esc.__file__) |
| 31 |
|
| 32 |
assert not esc.is_hypothesis_file(pytest.__file__) |
| 33 |
assert not esc.is_hypothesis_file(root + "-suffix") |
| 34 |
assert not esc.is_hypothesis_file(root + "-suffix/something.py") |
| 35 |
|
| 36 |
|
| 37 |
@pytest.mark.parametrize("fname", ["", "<ipython-input-18-f7c304bea5eb>"]) |
| 38 |
def test_is_hypothesis_file_does_not_error_on_invalid_paths_issue_2319(fname): |
| 39 |
assert not esc.is_hypothesis_file(fname) |
| 40 |
|
| 41 |
|
| 42 |
def test_multiplefailures_deprecation(): |
| 43 |
with pytest.warns(errors.HypothesisDeprecationWarning): |
| 44 |
exc = errors.MultipleFailures |
| 45 |
assert exc is BaseExceptionGroup |
| 46 |
|
| 47 |
|
| 48 |
def test_errors_attribute_error(): |
| 49 |
with pytest.raises(AttributeError): |
| 50 |
errors.ThisIsNotARealAttributeDontCreateSomethingWithThisName |
| 51 |
|
| 52 |
|
| 53 |
def test_handles_null_traceback(): |
| 54 |
esc.InterestingOrigin.from_exception(Exception()) |
| 55 |
|
| 56 |
|
| 57 |
def test_handles_context(): |
| 58 |
e = ValueError() |
| 59 |
e.__context__ = KeyError() |
| 60 |
origin = esc.InterestingOrigin.from_exception(e) |
| 61 |
assert "ValueError at " in str(origin) |
| 62 |
assert " context: " in str(origin) |
| 63 |
assert "KeyError at " in str(origin) |
| 64 |
|
| 65 |
|
| 66 |
def test_handles_groups(): |
| 67 |
origin = esc.InterestingOrigin.from_exception( |
| 68 |
BaseExceptionGroup("message", [ValueError("msg2")]) |
| 69 |
) |
| 70 |
assert "ExceptionGroup at " in str(origin) |
| 71 |
assert "child exception" in str(origin) |
| 72 |
assert "ValueError at " in str(origin) |
| 73 |
|
| 74 |
|
| 75 |
def make_exceptions_with_cycles(): |
| 76 |
err = ValueError() |
| 77 |
err.__context__ = err |
| 78 |
yield err |
| 79 |
|
| 80 |
err = TypeError() |
| 81 |
err.__context__ = BaseExceptionGroup("msg", [err]) |
| 82 |
yield err |
| 83 |
|
| 84 |
inner = LookupError() |
| 85 |
err = BaseExceptionGroup("msg", [inner]) |
| 86 |
inner.__context__ = err |
| 87 |
yield err |
| 88 |
|
| 89 |
inner = OSError() |
| 90 |
yield BaseExceptionGroup("msg", [inner, inner, inner]) |
| 91 |
|
| 92 |
|
| 93 |
@pytest.mark.parametrize("err", list(make_exceptions_with_cycles())) |
| 94 |
def test_handles_cycles(err): |
| 95 |
esc.InterestingOrigin.from_exception(err) |