Browse Source

Auto-detect create_app() function in find_best_app() and call if it exists.

pull/2294/head
Hendrik Makait 8 years ago
parent
commit
0ea972723c
  1. 13
      flask/cli.py
  2. 27
      tests/test_cli.py

13
flask/cli.py

@ -46,6 +46,19 @@ def find_best_app(module):
if len(matches) == 1: if len(matches) == 1:
return matches[0] return matches[0]
# Search for app factory callable.
app_factory = getattr(module, 'create_app', None)
if app_factory is not None and callable(app_factory):
try:
app = app_factory()
if app is not None and isinstance(app, Flask):
return app
except TypeError:
raise NoAppException('Auto-detected a create_app() function in '
'module "%s", but could not call it without '
'specifying arguments.' % module.__name__)
raise NoAppException('Failed to find application in module "%s". Are ' raise NoAppException('Failed to find application in module "%s". Are '
'you sure it contains a Flask application? Maybe ' 'you sure it contains a Flask application? Maybe '
'you wrapped it in a WSGI middleware or you are ' 'you wrapped it in a WSGI middleware or you are '

27
tests/test_cli.py

@ -51,6 +51,27 @@ def test_find_best_app(test_apps):
myapp = Flask('appname') myapp = Flask('appname')
assert find_best_app(Module) == Module.myapp assert find_best_app(Module) == Module.myapp
class Module:
@staticmethod
def create_app():
return Flask('appname')
assert isinstance(find_best_app(Module), Flask)
assert find_best_app(Module).name == 'appname'
class Module:
myapp = Flask('appname1')
@staticmethod
def create_app():
return Flask('appname2')
assert find_best_app(Module) == Module.myapp
class Module:
myapp = Flask('appname1')
@staticmethod
def create_app(foo):
return Flask('appname2')
assert find_best_app(Module) == Module.myapp
class Module: class Module:
pass pass
pytest.raises(NoAppException, find_best_app, Module) pytest.raises(NoAppException, find_best_app, Module)
@ -60,6 +81,12 @@ def test_find_best_app(test_apps):
myapp2 = Flask('appname2') myapp2 = Flask('appname2')
pytest.raises(NoAppException, find_best_app, Module) pytest.raises(NoAppException, find_best_app, Module)
class Module:
@staticmethod
def create_app(foo):
return Flask('appname2')
pytest.raises(NoAppException, find_best_app, Module)
def test_prepare_exec_for_file(test_apps): def test_prepare_exec_for_file(test_apps):
"""Expect the correct path to be set and the correct module name to be returned. """Expect the correct path to be set and the correct module name to be returned.

Loading…
Cancel
Save