Skip to content

Commit

Permalink
Add force_create keyword to Sanic.get_app
Browse files Browse the repository at this point in the history
  • Loading branch information
ahopkins committed Dec 28, 2020
1 parent 2a7d2fc commit 92c417f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
4 changes: 3 additions & 1 deletion sanic/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1424,9 +1424,11 @@ def register_app(cls, app: "Sanic") -> None:
cls._app_registry[name] = app

@classmethod
def get_app(cls, name: str) -> "Sanic":
def get_app(cls, name: str, *, force_create: bool = False) -> "Sanic":
"""Retrieve an instantiated Sanic instance"""
try:
return cls._app_registry[name]
except KeyError:
if force_create:
return cls(name)
raise SanicException(f'Sanic app name "{name}" not found.')
11 changes: 11 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,14 @@ def test_app_registry_name_reuse():
def test_app_registry_retrieval():
instance = Sanic("test")
assert Sanic.get_app("test") is instance


def test_get_app_does_not_exist():
with pytest.raises(SanicException):
Sanic.get_app("does-not-exist")


def test_get_app_does_not_exist_force_create():
assert isinstance(
Sanic.get_app("does-not-exist", force_create=True), Sanic
)

0 comments on commit 92c417f

Please sign in to comment.