| 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 |
import sys |
| 13 |
import tempfile |
| 14 |
import time |
| 15 |
import unicodedata |
| 16 |
from typing import get_args |
| 17 |
|
| 18 |
import pytest |
| 19 |
|
| 20 |
from hypothesis import given, strategies as st |
| 21 |
from hypothesis.errors import InvalidArgument |
| 22 |
from hypothesis.internal import charmap as cm |
| 23 |
from hypothesis.internal.intervalsets import IntervalSet |
| 24 |
|
| 25 |
from tests.common.utils import skipif_threading |
| 26 |
|
| 27 |
|
| 28 |
def test_charmap_contains_all_unicode(): |
| 29 |
n = 0 |
| 30 |
for vs in cm.charmap().values(): |
| 31 |
for u, v in vs: |
| 32 |
n += v - u + 1 |
| 33 |
assert n == sys.maxunicode + 1 |
| 34 |
|
| 35 |
|
| 36 |
def test_charmap_has_right_categories(): |
| 37 |
for cat, intervals in cm.charmap().items(): |
| 38 |
for u, v in intervals: |
| 39 |
for i in range(u, v + 1): |
| 40 |
real = unicodedata.category(chr(i)) |
| 41 |
assert real == cat, f"{i} is {real} but reported in {cat}" |
| 42 |
|
| 43 |
|
| 44 |
def assert_valid_range_list(ls): |
| 45 |
for u, v in ls: |
| 46 |
assert u <= v |
| 47 |
for i in range(len(ls) - 1): |
| 48 |
assert ls[i] <= ls[i + 1] |
| 49 |
assert ls[i][-1] < ls[i + 1][0] |
| 50 |
|
| 51 |
|
| 52 |
@given(st.sets(st.sampled_from(cm.categories()))) |
| 53 |
def test_query_matches_categories(cats): |
| 54 |
values = cm.query(categories=cats).intervals |
| 55 |
assert_valid_range_list(values) |
| 56 |
for u, v in values: |
| 57 |
for i in (u, v, (u + v) // 2): |
| 58 |
assert unicodedata.category(chr(i)) in cats |
| 59 |
|
| 60 |
|
| 61 |
@given( |
| 62 |
st.sets(st.sampled_from(cm.categories())) | st.none(), |
| 63 |
st.integers(0, sys.maxunicode), |
| 64 |
st.integers(0, sys.maxunicode), |
| 65 |
) |
| 66 |
def test_query_matches_categories_codepoints(cats, m1, m2): |
| 67 |
m1, m2 = sorted((m1, m2)) |
| 68 |
values = cm.query(categories=cats, min_codepoint=m1, max_codepoint=m2).intervals |
| 69 |
assert_valid_range_list(values) |
| 70 |
for u, v in values: |
| 71 |
assert m1 <= u |
| 72 |
assert v <= m2 |
| 73 |
|
| 74 |
|
| 75 |
# any test which sets `_charmap = None` is thread-unsafe. |
| 76 |
@skipif_threading |
| 77 |
def test_reload_charmap(): |
| 78 |
x = cm.charmap() |
| 79 |
assert x is cm.charmap() |
| 80 |
cm._charmap = None |
| 81 |
y = cm.charmap() |
| 82 |
assert x is not y |
| 83 |
assert x == y |
| 84 |
|
| 85 |
|
| 86 |
@skipif_threading |
| 87 |
def test_recreate_charmap(): |
| 88 |
x = cm.charmap() |
| 89 |
assert x is cm.charmap() |
| 90 |
cm._charmap = None |
| 91 |
cm.charmap_file().unlink() |
| 92 |
y = cm.charmap() |
| 93 |
assert x is not y |
| 94 |
assert x == y |
| 95 |
|
| 96 |
|
| 97 |
# This test fails flakily (every 1 in ~10 full CI runs), but only on the test-pyodide |
| 98 |
# ci job: |
| 99 |
# |
| 100 |
# os.utime(cm.charmap_file(), (mtime, mtime)) |
| 101 |
# ~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 102 |
# FileNotFoundError: [Errno 44] No such file or directory: |
| 103 |
# '/home/runner/work/hypothesis/hypothesis/.hypothesis/unicode_data/15.1.0/charmap.json.gz' |
| 104 |
# |
| 105 |
# I suspect this is from a race condition due to how we parallelize the pyodide |
| 106 |
# tests in CI, (splitting test files across 20 processes). It's also possible |
| 107 |
# it's from some pyodide-specific weirdness, but since the test only fails sometimes, |
| 108 |
# I find this less likely. |
| 109 |
# |
| 110 |
# I'm xfailing this on emscripten for now, to get a consistent green CI. |
| 111 |
@pytest.mark.xfail( |
| 112 |
condition=sys.platform == "emscripten", strict=False, reason="see comment" |
| 113 |
) |
| 114 |
@skipif_threading |
| 115 |
def test_uses_cached_charmap(): |
| 116 |
cm.charmap() |
| 117 |
|
| 118 |
# Reset the last-modified time of the cache file to a point in the past. |
| 119 |
mtime = int(time.time() - 1000) |
| 120 |
os.utime(cm.charmap_file(), (mtime, mtime)) |
| 121 |
statinfo = cm.charmap_file().stat() |
| 122 |
assert statinfo.st_mtime == mtime |
| 123 |
|
| 124 |
# Force reload of charmap from cache file and check that mtime is unchanged. |
| 125 |
cm._charmap = None |
| 126 |
cm.charmap() |
| 127 |
statinfo = cm.charmap_file().stat() |
| 128 |
assert statinfo.st_mtime == mtime |
| 129 |
|
| 130 |
|
| 131 |
def _union_intervals(x, y): |
| 132 |
return IntervalSet(x).union(IntervalSet(y)).intervals |
| 133 |
|
| 134 |
|
| 135 |
def test_union_empty(): |
| 136 |
assert _union_intervals([], []) == () |
| 137 |
assert _union_intervals([], [[1, 2]]) == ((1, 2),) |
| 138 |
assert _union_intervals([[1, 2]], []) == ((1, 2),) |
| 139 |
|
| 140 |
|
| 141 |
def test_union_handles_totally_overlapped_gap(): |
| 142 |
# < xx > Imagine the intervals x and y as bit strings. |
| 143 |
# | <yy yy> The bit at position n is set if n falls inside that interval. |
| 144 |
# = <zzzzz> In this model _union_intervals() performs bit-wise or. |
| 145 |
assert _union_intervals([[2, 3]], [[1, 2], [4, 5]]) == ((1, 5),) |
| 146 |
|
| 147 |
|
| 148 |
def test_union_handles_partially_overlapped_gap(): |
| 149 |
# < x > Imagine the intervals x and y as bit strings. |
| 150 |
# | <yy y> The bit at position n is set if n falls inside that interval. |
| 151 |
# = <zzz z> In this model _union_intervals() performs bit-wise or. |
| 152 |
assert _union_intervals([[3, 3]], [[1, 2], [5, 5]]) == ((1, 3), (5, 5)) |
| 153 |
|
| 154 |
|
| 155 |
def test_successive_union(): |
| 156 |
x = [] |
| 157 |
for v in cm.charmap().values(): |
| 158 |
x = _union_intervals(x, v) |
| 159 |
assert x == ((0, sys.maxunicode),) |
| 160 |
|
| 161 |
|
| 162 |
@skipif_threading |
| 163 |
def test_can_handle_race_between_exist_and_create(monkeypatch): |
| 164 |
x = cm.charmap() |
| 165 |
cm._charmap = None |
| 166 |
monkeypatch.setattr(os.path, "exists", lambda p: False) |
| 167 |
y = cm.charmap() |
| 168 |
assert x is not y |
| 169 |
assert x == y |
| 170 |
|
| 171 |
|
| 172 |
@skipif_threading |
| 173 |
def test_exception_in_write_does_not_lead_to_broken_charmap(monkeypatch): |
| 174 |
def broken(*args, **kwargs): |
| 175 |
raise ValueError |
| 176 |
|
| 177 |
cm._charmap = None |
| 178 |
monkeypatch.setattr(os.path, "exists", lambda p: False) |
| 179 |
monkeypatch.setattr(os, "rename", broken) |
| 180 |
|
| 181 |
cm.charmap() |
| 182 |
cm.charmap() |
| 183 |
|
| 184 |
|
| 185 |
@skipif_threading |
| 186 |
def test_regenerate_broken_charmap_file(): |
| 187 |
cm.charmap() |
| 188 |
|
| 189 |
cm.charmap_file().write_bytes(b"") # overwrite with empty file |
| 190 |
|
| 191 |
cm._charmap = None |
| 192 |
cm.charmap() |
| 193 |
|
| 194 |
|
| 195 |
def test_query_rejects_min_codepoint_greater_than_max(): |
| 196 |
with pytest.raises(InvalidArgument): |
| 197 |
cm.query(min_codepoint=1, max_codepoint=0) |
| 198 |
|
| 199 |
|
| 200 |
def test_exclude_characters_are_included_in_key(): |
| 201 |
assert cm.query().intervals != cm.query(exclude_characters="0").intervals |
| 202 |
|
| 203 |
|
| 204 |
@skipif_threading |
| 205 |
def test_error_writing_charmap_file_is_suppressed(monkeypatch): |
| 206 |
def broken_mkstemp(dir): |
| 207 |
raise RuntimeError |
| 208 |
|
| 209 |
monkeypatch.setattr(tempfile, "mkstemp", broken_mkstemp) |
| 210 |
|
| 211 |
try: |
| 212 |
# Cache the charmap to avoid a performance hit the next time |
| 213 |
# somebody tries to use it. |
| 214 |
saved = cm._charmap |
| 215 |
cm._charmap = None |
| 216 |
cm.charmap_file().unlink() |
| 217 |
|
| 218 |
cm.charmap() |
| 219 |
finally: |
| 220 |
cm._charmap = saved |
| 221 |
|
| 222 |
|
| 223 |
def test_categoryname_literal_is_correct(): |
| 224 |
minor_categories = set(cm.categories()) |
| 225 |
major_categories = {c[0] for c in minor_categories} |
| 226 |
assert set(get_args(cm.CategoryName)) == minor_categories | major_categories |