Skip to content

Commit

Permalink
Merge pull request #4153 from HypothesisWorks/DRMacIver/reload-on-reg…
Browse files Browse the repository at this point in the history
…ister

Automatically reload settings profile if it's redefined
  • Loading branch information
Zac-HD authored Nov 7, 2024
2 parents 843f6f6 + 364f309 commit 1ce7770
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
5 changes: 5 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
RELEASE_TYPE: minor

This changes the behaviour of settings profiles so that if you reregister the currently loaded profile it will automatically reload it. Previously you would have had to load it again.

In particular this means that if you register a "ci" profile, it will automatically be used when Hypothesis detects you are running on CI.
18 changes: 16 additions & 2 deletions hypothesis-python/docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,27 @@ by your conftest you can load one with the command line option ``--hypothesis-pr
Hypothesis comes with two built-in profiles, ``ci`` and ``default``.
``ci`` is set up to have good defaults for running in a CI environment, so emphasizes determinism, while the
``ci`` is set up to have good defaults for running in a CI environment, so emphasizes determinism, while the
``default`` settings are picked to be more likely to find bugs and to have a good workflow when used for local development.

Hypothesis will automatically detect certain common CI environments and use the CI profile automatically
Hypothesis will automatically detect certain common CI environments and use the ci profile automatically
when running in them.
In particular, if you wish to use the ``ci`` profile, setting the ``CI`` environment variable will do this.

This will still be the case if you register your own ci profile. For example, if you wanted to run more examples in CI, you might configure it as follows:

.. code-block:: python
settings.register_profile(
"ci",
settings(
settings.get_profile("ci"),
max_examples=1000,
),
)
This will configure your CI to run 1000 examples per test rather than the default of 100, and this change will automatically be picked up when running on a CI server.

.. _healthchecks:

-------------
Expand Down
12 changes: 9 additions & 3 deletions hypothesis-python/src/hypothesis/_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def default(cls):
v = default_variable.value
if v is not None:
return v
if hasattr(settings, "_current_profile"):
if getattr(settings, "_current_profile", None) is not None:
settings.load_profile(settings._current_profile)
assert default_variable.value is not None
return default_variable.value
Expand Down Expand Up @@ -132,6 +132,7 @@ class settings(metaclass=settingsMeta):
__definitions_are_locked = False
_profiles: ClassVar[dict[str, "settings"]] = {}
__module__ = "hypothesis"
_current_profile = None

def __getattr__(self, name):
if name in all_settings:
Expand Down Expand Up @@ -316,9 +317,15 @@ def register_profile(
:class:`~hypothesis.settings`: optional ``parent`` settings, and
keyword arguments for each setting that will be set differently to
parent (or settings.default, if parent is None).
If you register a profile that has already been defined and that profile
is the currently loaded profile, the new changes will take effect immediately,
and do not require reloading the profile.
"""
check_type(str, name, "name")
settings._profiles[name] = settings(parent=parent, **kwargs)
if settings._current_profile == name:
settings.load_profile(name)

@staticmethod
def get_profile(name: str) -> "settings":
Expand Down Expand Up @@ -767,8 +774,7 @@ def note_deprecation(
settings.register_profile("ci", CI)


# This is tested in a subprocess so the branch doesn't show up in coverage.
if is_in_ci(): # pragma: no cover
if is_in_ci():
settings.load_profile("ci")

assert settings.default is not None
Expand Down
37 changes: 37 additions & 0 deletions hypothesis-python/tests/cover/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,3 +558,40 @@ def test_check_defaults_to_randomize_when_not_running_on_ci():
).strip()
== "False"
)


def test_reloads_the_loaded_profile_if_registered_again():
prev_profile = settings._current_profile
try:
test_profile = "some nonsense profile purely for this test"
test_value = 123456
settings.register_profile(test_profile, settings(max_examples=test_value))
settings.load_profile(test_profile)
assert settings.default.max_examples == test_value
test_value_2 = 42
settings.register_profile(test_profile, settings(max_examples=test_value_2))
assert settings.default.max_examples == test_value_2
finally:
if prev_profile is not None:
settings.load_profile(prev_profile)


CI_TESTING_SCRIPT = """
from hypothesis import settings
if __name__ == '__main__':
settings.register_profile("ci", settings(max_examples=42))
assert settings.default.max_examples == 42
"""


@skipif_emscripten
def test_will_automatically_pick_up_changes_to_ci_profile_in_ci():
env = dict(os.environ)
env["CI"] = "true"
subprocess.check_call(
[sys.executable, "-c", CI_TESTING_SCRIPT],
env=env,
text=True,
encoding="utf-8",
)

0 comments on commit 1ce7770

Please sign in to comment.