Skip to content

Commit

Permalink
Merge pull request #46 from njsmith/cleanups
Browse files Browse the repository at this point in the history
Minor cleanups
  • Loading branch information
njsmith authored Jul 23, 2018
2 parents 70894fd + 61a72bc commit a972dc6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
2 changes: 1 addition & 1 deletion ci/travis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -ex

# See https://github.com/python-trio/trio/issues/334
YAPF_VERSION=0.17.0
YAPF_VERSION=0.22.0

if [ "$TRAVIS_OS_NAME" = "osx" ]; then
curl -Lo macpython.pkg https://www.python.org/ftp/python/${MACPYTHON}/python-${MACPYTHON}-macosx10.6.pkg
Expand Down
9 changes: 8 additions & 1 deletion pytest_trio/_tests/test_hypothesis_interaction.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import pytest
from hypothesis import given, strategies as st
from hypothesis import given, settings, strategies as st

# To avoid unpredictable warnings/errors when CI happens to be slow
# Example: https://travis-ci.org/python-trio/pytest-trio/jobs/406738296
our_settings = settings(deadline=None)


@our_settings
@given(st.integers())
@pytest.mark.trio
async def test_mark_inner(n):
assert isinstance(n, int)


@our_settings
@pytest.mark.trio
@given(st.integers())
async def test_mark_outer(n):
assert isinstance(n, int)


@our_settings
@pytest.mark.parametrize('y', [1, 2])
@given(x=st.none())
@pytest.mark.trio
Expand Down
48 changes: 31 additions & 17 deletions pytest_trio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
async_generator, yield_, asynccontextmanager, isasyncgenfunction
)

################################################################
# Basic setup
################################################################

if sys.version_info >= (3, 6):
ORDERED_DICTS = True
else:
Expand All @@ -17,14 +21,26 @@


def pytest_configure(config):
"""Inject documentation."""
# So that it shows up in 'pytest --markers' output:
config.addinivalue_line(
"markers", "trio: "
"mark the test as an async trio test; "
"it will be run using trio.run"
)


@pytest.hookimpl(tryfirst=True)
def pytest_exception_interact(node, call, report):
if issubclass(call.excinfo.type, trio.MultiError):
# TODO: not really elegant (pytest cannot output color with this hack)
report.longrepr = ''.join(format_exception(*call.excinfo._excinfo))


################################################################
# Core support for running tests and constructing fixtures
################################################################


def _trio_test_runner_factory(item, testfunc=None):
testfunc = testfunc or item.function

Expand All @@ -51,7 +67,8 @@ async def _bootstrap_fixture_and_run_test(**kwargs):
await testfunc(**resolved_kwargs)
except BaseException as exc:
# Regular pytest fixture don't have access to the test
# exception in there teardown, we mimic this behavior here.
# exception in there teardown, we mimic this behavior
# here.
user_exc = exc
except BaseException as exc:
# If we are here, the exception comes from the fixtures setup
Expand All @@ -61,10 +78,12 @@ async def _bootstrap_fixture_and_run_test(**kwargs):
else:
raise exc
finally:
# No matter what the nursery fixture should be closed when test is over
# No matter what the nursery fixture should be closed when
# test is over
nursery.cancel_scope.cancel()

# Finally re-raise or original exception coming from the test if needed
# Finally re-raise or original exception coming from the test if
# needed
if user_exc:
raise user_exc

Expand Down Expand Up @@ -97,16 +116,14 @@ async def _recursive_setup(deps_stack):
if not deps_stack:
await yield_([(name, resolved)])
else:
async with _recursive_setup(
deps_stack
) as remains_deps_stack_resolved:
async with _recursive_setup(deps_stack
) as remains_deps_stack_resolved:
await yield_(
remains_deps_stack_resolved + [(name, resolved)]
)

async with _recursive_setup(
need_resolved_deps_stack
) as resolved_deps_stack:
async with _recursive_setup(need_resolved_deps_stack
) as resolved_deps_stack:
await yield_({**deps, **dict(resolved_deps_stack)})


Expand Down Expand Up @@ -212,8 +229,7 @@ def _install_async_fixture_if_needed(fixturedef, request):
asyncfix = AsyncFixture(fixturedef, deps)
elif isasyncgenfunction(fixturedef.func):
asyncfix = AsyncYieldFixture(fixturedef, deps)
elif any(dep for dep in deps.values()
if isinstance(dep, BaseAsyncFixture)):
elif any(isinstance(dep, BaseAsyncFixture) for dep in deps.values()):
if isgeneratorfunction(fixturedef.func):
asyncfix = SyncYieldFixtureWithAsyncDeps(fixturedef, deps)
else:
Expand Down Expand Up @@ -248,11 +264,9 @@ def pytest_fixture_setup(fixturedef, request):
return _install_async_fixture_if_needed(fixturedef, request)


@pytest.hookimpl(tryfirst=True)
def pytest_exception_interact(node, call, report):
if issubclass(call.excinfo.type, trio.MultiError):
# TODO: not really elegant (pytest cannot output color with this hack)
report.longrepr = ''.join(format_exception(*call.excinfo._excinfo))
################################################################
# Built-in fixtures
################################################################


@pytest.fixture
Expand Down

0 comments on commit a972dc6

Please sign in to comment.