| 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 _hypothesis_globals |
| 12 |
import pytest |
| 13 |
|
| 14 |
from hypothesis import configuration as fs, strategies as st |
| 15 |
from hypothesis.errors import HypothesisSideeffectWarning |
| 16 |
|
| 17 |
IN_INITIALIZATION_ATTR = "in_initialization" |
| 18 |
|
| 19 |
# These tests use the pytest plugin enabling infrastructure to restart the side-effect warnings, |
| 20 |
# rather than trying to induce side-effects during import (and entrypoint loading) itself, which is |
| 21 |
# hard to do. Manual verification of behaviour during initial import can be done by just injecting |
| 22 |
# one of the side-effect-inducing statements below directly into hypothesis.entry_points.run(). |
| 23 |
# Manual validation can also be done by inspecting the relevant state during import and verify that |
| 24 |
# it is the same as tested here |
| 25 |
# (_hypothesis_globals.in_initialization > 0, hypothesis.configuration._first_postinit_what is None) |
| 26 |
|
| 27 |
|
| 28 |
@pytest.fixture |
| 29 |
def _extend_initialization(monkeypatch): |
| 30 |
assert getattr(_hypothesis_globals, IN_INITIALIZATION_ATTR) <= 0 |
| 31 |
monkeypatch.setattr(_hypothesis_globals, IN_INITIALIZATION_ATTR, 1) |
| 32 |
fs.notice_initialization_restarted(warn=False) |
| 33 |
assert fs._first_postinit_what is None # validates state as given in comment above |
| 34 |
|
| 35 |
|
| 36 |
@pytest.mark.parametrize( |
| 37 |
"sideeffect, warning_text", |
| 38 |
[ |
| 39 |
# the inner lambda ensures the lazy strategy can't be cached |
| 40 |
(lambda: st.builds(lambda: None).wrapped_strategy, "lazy evaluation"), |
| 41 |
(lambda: st.deferred(st.none).wrapped_strategy, "deferred evaluation"), |
| 42 |
(fs.storage_directory, "accessing storage"), |
| 43 |
], |
| 44 |
) |
| 45 |
def test_sideeffect_warning(sideeffect, warning_text, _extend_initialization): |
| 46 |
with pytest.warns(HypothesisSideeffectWarning, match=warning_text): |
| 47 |
sideeffect() |
| 48 |
|
| 49 |
|
| 50 |
def test_sideeffect_delayed_warning(monkeypatch, _extend_initialization): |
| 51 |
what = "synthetic side-effect" |
| 52 |
# _extend_initialization ensures we start at known clean slate (no delayed warnings). |
| 53 |
# Then: stop initialization, check a side-effect operation, and restart it. |
| 54 |
monkeypatch.setattr(_hypothesis_globals, IN_INITIALIZATION_ATTR, 0) |
| 55 |
fs.check_sideeffect_during_initialization(what) |
| 56 |
fs.check_sideeffect_during_initialization("ignored since not first") |
| 57 |
# The warning should identify itself as happening after import but before plugin load |
| 58 |
with pytest.warns(HypothesisSideeffectWarning, match=what + ".*between import"): |
| 59 |
monkeypatch.setattr(_hypothesis_globals, IN_INITIALIZATION_ATTR, 1) |
| 60 |
fs.notice_initialization_restarted() |