Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use tmp_path in ghostwriter tests #4050

Merged
merged 7 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions hypothesis-python/scripts/other-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ $PYTEST tests/redis/
pip uninstall -y redis fakeredis

$PYTEST tests/typing_extensions/
pip uninstall -y typing_extensions
if [[ "$HYPOTHESIS_PROFILE" != "crosshair" ]]; then
pip uninstall -y typing_extensions
fi

if [ "$(python -c 'import sys; print(sys.version_info[:2] >= (3, 9))')" = "True" ] ; then
pip install "$(grep 'annotated-types==' ../requirements/coverage.txt)"
Expand Down Expand Up @@ -62,14 +64,10 @@ if [ "$(python -c $'import platform, sys; print(sys.version_info.releaselevel ==
pip install "$(grep 'numpy==' ../requirements/coverage.txt)"
fi

case "$(python -c 'import platform; print(platform.python_implementation())')" in
PyPy|GraalVM)
;;
*)
$PYTEST tests/array_api
$PYTEST tests/numpy
esac

$PYTEST tests/ghostwriter/
pip uninstall -y black numpy
pip uninstall -y black

if [ "$(python -c "import platform; print(platform.python_implementation() not in {'PyPy', 'GraalVM'})")" = "True" ] ; then
$PYTEST tests/array_api tests/numpy
fi
fi
2 changes: 1 addition & 1 deletion hypothesis-python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def local_file(name):
"pytest": ["pytest>=4.6"],
"dpcontracts": ["dpcontracts>=0.4"],
"redis": ["redis>=3.0.0"],
"crosshair": ["hypothesis-crosshair>=0.0.7", "crosshair-tool>=0.0.61"],
"crosshair": ["hypothesis-crosshair>=0.0.8", "crosshair-tool>=0.0.62"],
# zoneinfo is an odd one: every dependency is conditional, because they're
# only necessary on old versions of Python or Windows systems or emscripten.
"zoneinfo": [
Expand Down
4 changes: 2 additions & 2 deletions hypothesis-python/tests/cover/test_draw_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
from tests.common.debug import check_can_generate_examples


@pytest.mark.parametrize("spec", standard_types, ids=list(map(repr, standard_types)))
@pytest.mark.parametrize("spec", standard_types, ids=repr)
def test_single_example(spec):
check_can_generate_examples(spec)


@pytest.mark.parametrize("spec", standard_types, ids=list(map(repr, standard_types)))
@pytest.mark.parametrize("spec", standard_types, ids=repr)
def test_list_example(spec):
check_can_generate_examples(lists(spec))
17 changes: 7 additions & 10 deletions hypothesis-python/tests/cover/test_explicit_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from hypothesis.internal.compat import ExceptionGroup
from hypothesis.strategies import floats, integers, text

from tests.common.utils import assert_falsifying_output, capture_out
from tests.common.utils import assert_falsifying_output, capture_out, fails_with


class TestInstanceMethods(TestCase):
Expand Down Expand Up @@ -222,15 +222,12 @@ def test(a):
test()


def test_runs_deadline_for_examples():
@example(10)
@settings(phases=[Phase.explicit])
@given(integers())
def test(x):
time.sleep(10)

with pytest.raises(DeadlineExceeded):
test()
@fails_with(DeadlineExceeded)
@example(10)
@settings(phases=[Phase.explicit])
@given(integers())
def test(x):
time.sleep(10)


@given(value=floats(0, 1))
Expand Down
42 changes: 27 additions & 15 deletions hypothesis-python/tests/ghostwriter/test_ghostwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import ast
import enum
import json
import os
import platform
import re
import socket
import unittest
Expand Down Expand Up @@ -450,13 +452,22 @@ def test_get_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.
def in_temp_path(tmp_path):
"""Fixture to execute tests in a temporary path."""
old_path = Path.cwd()
os.chdir(tmp_path)
yield
os.chdir(old_path)


@pytest.fixture
def temp_script_file(in_temp_path):
"""Fixture to create a script file in a temporary working directory.

Changes the working directory to a temporary directory, then yields an extant file
whose name will end in .py and which includes 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(
"""
Expand All @@ -466,18 +477,17 @@ def say_hello():
),
encoding="utf-8",
)
yield p
p.unlink()
return p


@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"
def temp_script_file_with_py_function(in_temp_path):
"""Fixture to create a python file in a temporary working directory.

Changes the working directory to a temporary directory, then yields an extant file
whose name will end in .py and which includes 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(
"""
Expand All @@ -487,8 +497,7 @@ def py():
),
encoding="utf-8",
)
yield p
p.unlink()
return p


def test_obj_name(temp_script_file, temp_script_file_with_py_function):
Expand All @@ -500,7 +509,7 @@ def test_obj_name(temp_script_file, temp_script_file_with_py_function):
)
# Windows paths (strings including a "\") should also raise a meaningful UsageError
with pytest.raises(click.exceptions.UsageError) as e:
cli.obj_name("mydirectory\\myscript.py")
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."
)
Expand All @@ -510,7 +519,10 @@ def test_obj_name(temp_script_file, temp_script_file_with_py_function):
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
if platform.system() == "Darwin":
return # bad/flaky interaction between importlib and tempdirs here
with pytest.raises(click.exceptions.UsageError) as e:
cli.obj_name(str(temp_script_file))
assert e.match(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from tests.common.debug import minimal


@pytest.mark.parametrize("spec", standard_types, ids=list(map(repr, standard_types)))
@pytest.mark.parametrize("spec", standard_types, ids=repr)
def test_can_collectively_minimize(spec):
n = 10
try:
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ ignore = [
"PIE790", # See https://github.com/astral-sh/ruff/issues/10538
"PT001",
"PT003",
"PT004",
"PT006",
"PT007",
"PT009",
Expand Down
Loading