1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
|
# This file is part of Hypothesis, which may be found at
# https://github.com/HypothesisWorks/hypothesis/
#
# Copyright the Hypothesis Authors.
# Individual contributors are listed in AUTHORS.rst and the git log.
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.
import ast
import enum
import json
import re
import socket
import unittest
import unittest.mock
from collections.abc import KeysView, Sequence, Sized, ValuesView
from decimal import Decimal
from pathlib import Path
from textwrap import dedent
from types import FunctionType, ModuleType
from typing import Any, Union
import attr
import click
import pytest
from hypothesis import HealthCheck, assume, settings
from hypothesis.errors import InvalidArgument, Unsatisfiable
from hypothesis.extra import cli, ghostwriter
from hypothesis.internal.compat import BaseExceptionGroup
from hypothesis.strategies import builds, from_type, just, lists
from hypothesis.strategies._internal.core import from_regex
from hypothesis.strategies._internal.lazy import LazyStrategy
varied_excepts = pytest.mark.parametrize("ex", [(), ValueError, (TypeError, re.error)])
pytestmark = pytest.mark.skipif(
settings._current_profile == "threading",
reason="ghostwriter is not thread safe",
)
def get_test_function(source_code, settings_decorator=lambda fn: fn):
# A helper function to get the dynamically-defined test function.
# Note that this also tests that the module is syntatically-valid,
# AND free from undefined names, import problems, and so on.
namespace = {}
try:
exec(source_code, namespace)
except Exception:
print(f"************\n{source_code}\n************")
raise
tests = [
v
for k, v in namespace.items()
if k.startswith(("test_", "Test")) and not isinstance(v, ModuleType)
]
assert len(tests) == 1, tests
return settings_decorator(tests[0])
@pytest.mark.parametrize(
"badness", ["not an exception", BaseException, [ValueError], (Exception, "bad")]
)
def test_invalid_exceptions(badness):
with pytest.raises(InvalidArgument):
ghostwriter._check_except(badness)
def test_style_validation():
ghostwriter._check_style("pytest")
ghostwriter._check_style("unittest")
with pytest.raises(InvalidArgument):
ghostwriter._check_style("not a valid style")
def test_strategies_with_invalid_syntax_repr_as_nothing():
msg = "$$ this repr is not Python syntax $$"
class NoRepr:
def __repr__(self):
return msg
s = just(NoRepr())
assert repr(s) == f"just({msg})"
assert ghostwriter._valid_syntax_repr(s)[1] == "nothing()"
class AnEnum(enum.Enum):
a = "value of AnEnum.a"
b = "value of AnEnum.b"
def takes_enum(foo=AnEnum.a):
# This can only fail if we use the default argument to guess
# that any instance of that enum type should be allowed.
assert foo != AnEnum.b
def test_ghostwriter_exploits_arguments_with_enum_defaults():
source_code = ghostwriter.fuzz(takes_enum)
test = get_test_function(source_code)
with pytest.raises(AssertionError):
test()
def timsort(seq: Sequence[int]) -> list[int]:
return sorted(seq)
def non_type_annotation(x: 3): # type: ignore
pass
def annotated_any(x: Any):
pass
space_in_name = type("a name", (type,), {"__init__": lambda self: None})
class NotResolvable:
def __init__(self, unannotated_required):
pass
def non_resolvable_arg(x: NotResolvable):
pass
def test_flattens_one_of_repr():
strat = from_type(Union[int, Sequence[int]])
assert repr(strat).count("one_of(") == 2
assert ghostwriter._valid_syntax_repr(strat)[1].count("one_of(") == 1
def takes_keys(x: KeysView[int]) -> None:
pass
def takes_values(x: ValuesView[int]) -> None:
pass
def takes_match(x: re.Match[bytes]) -> None:
pass
def takes_pattern(x: re.Pattern[str]) -> None:
pass
def takes_sized(x: Sized) -> None:
pass
def takes_frozensets(a: frozenset[int], b: frozenset[int]) -> None:
pass
@attr.s()
class Foo:
foo: str = attr.ib()
def takes_attrs_class(x: Foo) -> None:
pass
@varied_excepts
@pytest.mark.parametrize(
"func",
[
re.compile,
json.loads,
json.dump,
timsort,
ast.literal_eval,
non_type_annotation,
annotated_any,
space_in_name,
non_resolvable_arg,
takes_keys,
takes_values,
takes_match,
takes_pattern,
takes_sized,
takes_frozensets,
takes_attrs_class,
],
)
def test_ghostwriter_fuzz(func, ex):
source_code = ghostwriter.fuzz(func, except_=ex)
get_test_function(source_code)
def test_socket_module():
source_code = ghostwriter.magic(socket)
exec(source_code, {})
def test_binary_op_also_handles_frozensets():
# Using str.replace in a loop would convert `frozensets()` into
# `st.frozenst.sets()` instead of `st.frozensets()`; fixed with re.sub.
source_code = ghostwriter.binary_operation(takes_frozensets)
exec(source_code, {})
@varied_excepts
@pytest.mark.parametrize(
"func", [re.compile, json.loads, json.dump, timsort, ast.literal_eval]
)
def test_ghostwriter_unittest_style(func, ex):
source_code = ghostwriter.fuzz(func, except_=ex, style="unittest")
assert issubclass(get_test_function(source_code), unittest.TestCase)
def no_annotations(foo=None, *, bar=False):
pass
def test_inference_from_defaults_and_none_booleans_reprs_not_just_and_sampled_from():
source_code = ghostwriter.fuzz(no_annotations)
assert "@given(foo=st.none(), bar=st.booleans())" in source_code
def hopefully_hashable(foo: set[Decimal]):
pass
def test_no_hashability_filter():
# In from_type, we ordinarily protect users from really weird cases like
# `Decimal('snan')` - a unhashable value of a hashable type - but in the
# ghostwriter we instead want to present this to the user for an explicit
# decision. They can pass `allow_nan=False`, fix their custom type's
# hashing logic, or whatever else; simply doing nothing will usually work.
source_code = ghostwriter.fuzz(hopefully_hashable)
assert "@given(foo=st.sets(st.decimals()))" in source_code
assert "_can_hash" not in source_code
@pytest.mark.parametrize(
"gw,args",
[
(ghostwriter.fuzz, ["not callable"]),
(ghostwriter.idempotent, ["not callable"]),
(ghostwriter.roundtrip, []),
(ghostwriter.roundtrip, ["not callable"]),
(ghostwriter.equivalent, [sorted]),
(ghostwriter.equivalent, [sorted, "not callable"]),
],
)
def test_invalid_func_inputs(gw, args):
with pytest.raises(InvalidArgument):
gw(*args)
class A:
@classmethod
def to_json(cls, obj: Union[dict, list]) -> str:
return json.dumps(obj)
@classmethod
def from_json(cls, obj: str) -> Union[dict, list]:
return json.loads(obj)
@staticmethod
def static_sorter(seq: Sequence[int]) -> list[int]:
return sorted(seq)
@pytest.mark.parametrize(
"gw,args",
[
(ghostwriter.fuzz, [A.static_sorter]),
(ghostwriter.idempotent, [A.static_sorter]),
(ghostwriter.roundtrip, [A.to_json, A.from_json]),
(ghostwriter.equivalent, [A.to_json, json.dumps]),
],
)
def test_class_methods_inputs(gw, args):
source_code = gw(*args)
get_test_function(source_code)()
def test_run_ghostwriter_fuzz():
# Our strategy-guessing code works for all the arguments to sorted,
# and we handle positional-only arguments in calls correctly too.
source_code = ghostwriter.fuzz(sorted)
assert "st.nothing()" not in source_code
get_test_function(source_code)()
class MyError(UnicodeDecodeError):
pass
@pytest.mark.parametrize(
"exceptions,output",
[
# Discard subclasses of other exceptions to catch, including non-builtins,
# and replace OSError aliases with OSError.
((Exception, UnicodeError), "Exception"),
((UnicodeError, MyError), "UnicodeError"),
((IOError,), "OSError"),
((IOError, UnicodeError), "(OSError, UnicodeError)"),
],
)
def test_exception_deduplication(exceptions, output):
_, body = ghostwriter._make_test_body(
lambda: None,
ghost="",
test_body="pass",
except_=exceptions,
style="pytest",
annotate=False,
)
assert f"except {output}:" in body
def test_run_ghostwriter_roundtrip():
# This test covers the whole lifecycle: first, we get the default code.
# The first argument is unknown, so we fail to draw from st.nothing()
source_code = ghostwriter.roundtrip(json.dumps, json.loads)
with pytest.raises(Unsatisfiable):
get_test_function(source_code)()
# Replacing that nothing() with a strategy for JSON allows us to discover
# two possible failures: `nan` is not equal to itself, and if dumps is
# passed allow_nan=False it is a ValueError to pass a non-finite float.
source_code = source_code.replace(
"st.nothing()",
"st.recursive(st.one_of(st.none(), st.booleans(), st.floats(), st.text()), "
"lambda v: st.lists(v, max_size=2) | st.dictionaries(st.text(), v, max_size=2)"
", max_leaves=2)",
)
s = settings(deadline=None, suppress_health_check=[HealthCheck.too_slow])
try:
get_test_function(source_code, settings_decorator=s)()
except (AssertionError, ValueError, BaseExceptionGroup):
pass
# Finally, restricting ourselves to finite floats makes the test pass!
source_code = source_code.replace(
"st.floats()", "st.floats(allow_nan=False, allow_infinity=False)"
)
get_test_function(source_code, settings_decorator=s)()
@varied_excepts
@pytest.mark.parametrize("func", [sorted, timsort])
def test_ghostwriter_idempotent(func, ex):
source_code = ghostwriter.idempotent(func, except_=ex)
test = get_test_function(source_code)
if "=st.nothing()" in source_code:
with pytest.raises(Unsatisfiable):
test()
else:
test()
def test_overlapping_args_use_union_of_strategies():
def f(arg: int) -> None:
pass
def g(arg: float) -> None:
pass
source_code = ghostwriter.equivalent(f, g)
assert "arg=st.one_of(st.integers(), st.floats())" in source_code
def test_module_with_mock_does_not_break():
# Before we added an explicit check for unspec'd mocks, they would pass
# through the initial validation and then fail when used in more detailed
# logic in the ghostwriter machinery.
ghostwriter.magic(unittest.mock)
def compose_types(x: type, y: type):
pass
def test_unrepr_identity_elem():
# Works with inferred identity element
source_code = ghostwriter.binary_operation(compose_types)
exec(source_code, {})
# and also works with explicit identity element
source_code = ghostwriter.binary_operation(compose_types, identity=type)
exec(source_code, {})
@pytest.mark.parametrize(
"strategy, imports",
# The specifics don't matter much here; we're just demonstrating that
# we can walk the strategy and collect all the objects to import.
[
# Lazy from_type() is handled without being unwrapped
(LazyStrategy(from_type, (enum.Enum,), {}), {("enum", "Enum")}),
# Mapped, filtered, and flatmapped check both sides of the method
(
builds(enum.Enum).map(Decimal),
{("enum", "Enum"), ("decimal", "Decimal")},
),
(
builds(enum.Enum).flatmap(Decimal),
{("enum", "Enum"), ("decimal", "Decimal")},
),
(
builds(enum.Enum).filter(Decimal).filter(re.compile),
{("enum", "Enum"), ("decimal", "Decimal"), ("re", "compile")},
),
# one_of() strategies recurse into all the branches
(
builds(enum.Enum) | builds(Decimal) | builds(re.compile),
{("enum", "Enum"), ("decimal", "Decimal"), ("re", "compile")},
),
# and builds() checks the arguments as well as the target
(
builds(enum.Enum, builds(Decimal), kw=builds(re.compile)),
{("enum", "Enum"), ("decimal", "Decimal"), ("re", "compile")},
),
# lists recurse on imports
(
lists(builds(Decimal)),
{("decimal", "Decimal")},
),
# find the needed import for from_regex if needed
(
from_regex(re.compile(".+")),
{"re"},
),
# but don't add superfluous imports
(
from_regex(".+"),
set(),
),
],
)
def test_get_imports_for_strategy(strategy, imports):
assert ghostwriter._imports_for_strategy(strategy) == imports
@pytest.fixture
def temp_script_file():
"""Fixture to yield a Path to a temporary file in the local directory. File name will end
in .py and will include an importable function.
"""
p = Path("my_temp_script.py")
if p.exists():
raise FileExistsError(f"Did not expect {p} to exist during testing")
p.write_text(
dedent(
"""
def say_hello():
print("Hello world!")
"""
),
encoding="utf-8",
)
yield p
p.unlink()
@pytest.fixture
def temp_script_file_with_py_function():
"""Fixture to yield a Path to a temporary file in the local directory. File name will end
in .py and will include an importable function named "py"
"""
p = Path("my_temp_script_with_py_function.py")
if p.exists():
raise FileExistsError(f"Did not expect {p} to exist during testing")
p.write_text(
dedent(
"""
def py():
print('A function named "py" has been called')
"""
),
encoding="utf-8",
)
yield p
p.unlink()
def test_obj_name(temp_script_file, temp_script_file_with_py_function):
# Module paths (strings including a "/") should raise a meaningful UsageError
with pytest.raises(click.exceptions.UsageError) as e:
cli.obj_name("mydirectory/myscript.py")
assert e.match(
"Remember that the ghostwriter should be passed the name of a module, not a path."
)
# Windows paths (strings including a "\") should also raise a meaningful UsageError
with pytest.raises(click.exceptions.UsageError) as e:
cli.obj_name(R"mydirectory\myscript.py")
assert e.match(
"Remember that the ghostwriter should be passed the name of a module, not a path."
)
# File names of modules (strings ending in ".py") should raise a meaningful UsageError
with pytest.raises(click.exceptions.UsageError) as e:
cli.obj_name("myscript.py")
assert e.match(
"Remember that the ghostwriter should be passed the name of a module, not a file."
)
# File names of modules (strings ending in ".py") that exist should get a suggestion
with pytest.raises(click.exceptions.UsageError) as e:
cli.obj_name(str(temp_script_file))
assert e.match(
"Remember that the ghostwriter should be passed the name of a module, not a file."
f"\n\tTry: hypothesis write {temp_script_file.stem}"
)
# File names of modules (strings ending in ".py") that define a py function should succeed
assert isinstance(
cli.obj_name(str(temp_script_file_with_py_function)), FunctionType
)
def test_gets_public_location_not_impl_location():
assert ghostwriter._get_module(assume) == "hypothesis" # not "hypothesis.control"
|