Skip to content
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
1 change: 1 addition & 0 deletions tools/testing/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ envoy_py_binary(
requirement("pytest"),
requirement("pytest-asyncio"),
requirement("pytest-cov"),
requirement("pytest-patches"),
"//tools/base:runner",
"//tools/base:utils",
],
Expand Down
36 changes: 1 addition & 35 deletions tools/testing/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,11 @@
#

import functools
from contextlib import contextmanager, ExitStack
from typing import Callable, ContextManager, Iterator
from unittest.mock import patch
from typing import Callable

import pytest


@contextmanager
def nested(*contexts) -> Iterator[tuple]:
with ExitStack() as stack:
yield tuple(stack.enter_context(context) for context in contexts)


def _patches(*args: str, prefix: str = "") -> ContextManager[tuple]:
"""Takes a list of module/class paths to patch and an optional prefix

The prefix is used to prefix all of the paths

The patches are applied in a nested set of context managers.

The yields (mocks) are yielded as a tuple.
"""

patched = []
prefix = f"{prefix}." if prefix else ""
for arg in args:
if isinstance(arg, (list, tuple)):
path, kwargs = arg
patched.append(patch(f"{prefix}{path}", **kwargs))
else:
patched.append(patch(f"{prefix}{arg}"))
return nested(*patched)


@pytest.fixture
def patches() -> Callable[..., ContextManager[tuple]]:
return _patches


def _async_command_main(patches, main: Callable, handler: str, args: tuple) -> None:
parts = handler.split(".")
patched = patches("asyncio.run", parts.pop(), prefix=".".join(parts))
Expand Down
8 changes: 6 additions & 2 deletions tools/testing/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ iniconfig==1.1.1 \
# -r tools/testing/requirements.txt
# pytest
packaging==21.0 \
--hash=sha256:c86254f9220d55e31cc94d69bade760f0847da8000def4dfe1c6b872fd14ff14 \
--hash=sha256:7dc96269f53a4ccec5c0670940a4281106dd0bb343f47b7471f779df49c2fbe7
--hash=sha256:7dc96269f53a4ccec5c0670940a4281106dd0bb343f47b7471f779df49c2fbe7 \
--hash=sha256:c86254f9220d55e31cc94d69bade760f0847da8000def4dfe1c6b872fd14ff14
# via
# -r tools/testing/requirements.txt
# pytest
Expand Down Expand Up @@ -104,13 +104,17 @@ pytest-cov==2.12.1 \
--hash=sha256:261bb9e47e65bd099c89c3edf92972865210c36813f80ede5277dceb77a4a62a \
--hash=sha256:261ceeb8c227b726249b376b8526b600f38667ee314f910353fa318caa01f4d7
# via -r tools/testing/requirements.txt
pytest-patches==0.0.1 \
--hash=sha256:48fe763f7e603f20a73a8c09684d31ec9929646b86fceb496599970fe86685b0
# via -r tools/testing/requirements.txt
pytest==6.2.4 \
--hash=sha256:50bcad0a0b9c5a72c8e4e7c9855a3ad496ca6a881a3641b4260605450772c54b \
--hash=sha256:91ef2131a9bd6be8f76f1f08eac5c5317221d6ad1e143ae03894b862e8976890
# via
# -r tools/testing/requirements.txt
# pytest-asyncio
# pytest-cov
# pytest-patches
toml==0.10.2 \
--hash=sha256:806143ae5bfb6a3c6e736a764057db0e6a0e05e338b5630894a5f779cabb4f9b \
--hash=sha256:b3bda1d108d5dd99f4a20d24d9c348e91c4db7ab1b749200bded2f839ccbe68f
Expand Down