| 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 sys |
| 12 |
from contextlib import contextmanager |
| 13 |
|
| 14 |
import pytest |
| 15 |
|
| 16 |
from hypothesis import given, strategies as st |
| 17 |
from hypothesis.errors import HypothesisWarning |
| 18 |
from hypothesis.internal.scrutineer import MONITORING_TOOL_ID |
| 19 |
|
| 20 |
|
| 21 |
@contextmanager |
| 22 |
def using_tool_id(tool_id, tool_name): |
| 23 |
try: |
| 24 |
sys.monitoring.use_tool_id(tool_id, tool_name) |
| 25 |
yield |
| 26 |
finally: |
| 27 |
sys.monitoring.free_tool_id(tool_id) |
| 28 |
|
| 29 |
|
| 30 |
@pytest.mark.skipif(sys.version_info[:2] < (3, 12), reason="new namespace") |
| 31 |
def test_monitoring_warns_on_registered_tool_id(warns_or_raises): |
| 32 |
|
| 33 |
# scrutineer can't run if something has already registered its tool id. |
| 34 |
with ( |
| 35 |
using_tool_id(MONITORING_TOOL_ID, "rogue"), |
| 36 |
warns_or_raises(HypothesisWarning, match=r"already taken by tool rogue"), |
| 37 |
): |
| 38 |
|
| 39 |
@given(st.integers()) |
| 40 |
def f(n): |
| 41 |
raise AssertionError |
| 42 |
|
| 43 |
with pytest.raises(AssertionError): |
| 44 |
f() |