Skip to content

Commit

Permalink
Merge pull request #280 from facebook/no_async_context
Browse files Browse the repository at this point in the history
Add check against async context
  • Loading branch information
fornellas committed Jan 29, 2021
2 parents ef94d99 + fad044e commit 51772e7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
12 changes: 12 additions & 0 deletions tests/dsl_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,18 @@ def not_callable(context):
):
not_callable(None)

def test_contex_cant_be_async(self):
with self.assertRaisesRegex(
RuntimeError,
"TestSlide DSL context function `async_context` can not be async!",
):

@context
async def async_context(context):
@context.example
async def async_example(self):
pass

# Duplicate names

def test_cant_create_top_contexts_with_same_name(self):
Expand Down
10 changes: 8 additions & 2 deletions testslide/dsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@
from . import Skip # noqa: F401


def _validate_parameter(code: Callable, name: str, index: int) -> None:
def _validate_parameter(
code: Callable, name: str, index: int, allow_async=True
) -> None:
parameters = list(inspect.signature(code).parameters.keys())
if not parameters or parameters[index] != name:
raise ValueError(
f"Function must receive parameter #{index+1} named "
f"'{name}', but given function has parameters: {parameters}."
)
if not allow_async and inspect.iscoroutinefunction(code):
raise RuntimeError(
f"TestSlide DSL context function `{code.__name__}` can not be async!"
)


def _require_context(action: str) -> Callable:
Expand Down Expand Up @@ -72,7 +78,7 @@ def _create_context(
new_context = self.current_context.add_child_context(
name, skip=self.skip, focus=self.focus
)
_validate_parameter(context_code, "context", 0)
_validate_parameter(context_code, "context", 0, allow_async=False)
context_code(
type(self)(current_context=new_context, skip=self.skip, focus=self.focus),
*args,
Expand Down

0 comments on commit 51772e7

Please sign in to comment.