Skip to content

Commit

Permalink
Merge pull request #4019 from jobh/coverage_fixme
Browse files Browse the repository at this point in the history
Full coverage
  • Loading branch information
Zac-HD authored Jun 25, 2024
2 parents 49f64cd + adb0554 commit 0c885e7
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 19 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,11 @@ jobs:
- name: Run tests
run: |
source .venv-pyodide/bin/activate
python -m pytest -p no:cacheprovider hypothesis-python/tests/cover
# pyodide can't run multiple processes internally, so parallelize explicitly over
# discovered test files instead (20 at a time)
TEST_FILES=$(python -m pytest -p no:cacheprovider --setup-plan hypothesis-python/tests/cover | grep ^hypothesis-python/ | cut -d " " -f 1)
parallel --max-procs 100% --max-args 20 --keep-order --line-buffer \
python -m pytest -p no:cacheprovider <<< $TEST_FILES
deploy:
if: "github.event_name == 'push' && github.repository == 'HypothesisWorks/hypothesis'"
Expand Down
3 changes: 3 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RELEASE_TYPE: patch

Improves internal test coverage.
9 changes: 4 additions & 5 deletions hypothesis-python/src/hypothesis/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1104,12 +1104,11 @@ def _execute_once_for_engine(self, data: ConjectureData) -> None:
if TESTCASE_CALLBACKS:
if runner := getattr(self, "_runner", None):
phase = runner._current_phase
elif (
self.failed_normally or self.failed_due_to_deadline
): # pragma: no cover # FIXME
phase = "shrink"
else: # pragma: no cover # in case of messing with internals
phase = "unknown"
if self.failed_normally or self.failed_due_to_deadline:
phase = "shrink"
else:
phase = "unknown"
backend_desc = f", using backend={self.settings.backend!r}" * (
self.settings.backend != "hypothesis"
and not getattr(runner, "_switch_to_hypothesis_provider", False)
Expand Down
25 changes: 14 additions & 11 deletions hypothesis-python/src/hypothesis/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,17 +225,20 @@ def save(self, key: bytes, value: bytes) -> None:
# Note: we attempt to create the dir in question now. We
# already checked for permissions, but there can still be other issues,
# e.g. the disk is full, or permissions might have been changed.
self._key_path(key).mkdir(exist_ok=True, parents=True)
path = self._value_path(key, value)
if not path.exists():
suffix = binascii.hexlify(os.urandom(16)).decode("ascii")
tmpname = path.with_suffix(f"{path.suffix}.{suffix}")
tmpname.write_bytes(value)
try:
tmpname.rename(path)
except OSError: # pragma: no cover
tmpname.unlink()
assert not tmpname.exists()
try:
self._key_path(key).mkdir(exist_ok=True, parents=True)
path = self._value_path(key, value)
if not path.exists():
suffix = binascii.hexlify(os.urandom(16)).decode("ascii")
tmpname = path.with_suffix(f"{path.suffix}.{suffix}")
tmpname.write_bytes(value)
try:
tmpname.rename(path)
except OSError: # pragma: no cover
tmpname.unlink()
assert not tmpname.exists()
except OSError: # pragma: no cover
pass

def move(self, src: bytes, dest: bytes, value: bytes) -> None:
if src == dest:
Expand Down
2 changes: 1 addition & 1 deletion hypothesis-python/src/hypothesis/internal/entropy.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def my_WORKING_hook():
"PRNG. See the docs for `register_random` for more "
"details."
)
elif not FREE_THREADED_CPYTHON: # pragma: no cover # FIXME
elif not FREE_THREADED_CPYTHON: # pragma: no branch
# On CPython, check for the free-threaded build because
# gc.get_referrers() ignores objects with immortal refcounts
# and objects are immortalized in the Python 3.13
Expand Down
2 changes: 1 addition & 1 deletion hypothesis-python/src/hypothesis/stateful.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ def _add_result_to_targets(self, targets, result):
for target in targets:
name = self._new_name(target)

def printer(obj, p, cycle, name=name): # pragma: no cover # FIXME
def printer(obj, p, cycle, name=name):
return p.text(name)

self.__printer.singleton_pprinters.setdefault(id(result), printer)
Expand Down
20 changes: 20 additions & 0 deletions hypothesis-python/tests/cover/test_stateful.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# obtain one at https://mozilla.org/MPL/2.0/.

import base64
import re
from collections import defaultdict
from typing import ClassVar

Expand Down Expand Up @@ -114,6 +115,25 @@ def test_flaky_draw_less_raises_flaky():
FlakyDrawLessMachine.TestCase().runTest()


def test_result_is_added_to_target():
class TargetStateMachine(RuleBasedStateMachine):
nodes = Bundle("nodes")

@rule(target=nodes, source=lists(nodes))
def bunch(self, source):
assert len(source) == 0
return source

test_class = TargetStateMachine.TestCase
try:
test_class().runTest()
raise RuntimeError("Expected an assertion error")
except AssertionError as err:
notes = err.__notes__
regularized_notes = [re.sub(r"[0-9]+", "i", note) for note in notes]
assert "state.bunch(source=[nodes_i])" in regularized_notes


class FlakyStateMachine(RuleBasedStateMachine):
@rule()
def action(self):
Expand Down

0 comments on commit 0c885e7

Please sign in to comment.