mirror of https://github.com/pallets/flask.git
Merge pull request #4662 from pallets/fix-run-skip
re-add flag to skip unguarded app.run from CLI
This commit is contained in:
commit
3635583ce2
|
@ -916,8 +916,8 @@ class Flask(Scaffold):
|
||||||
if os.environ.get("FLASK_RUN_FROM_CLI") == "true":
|
if os.environ.get("FLASK_RUN_FROM_CLI") == "true":
|
||||||
if not is_running_from_reloader():
|
if not is_running_from_reloader():
|
||||||
click.secho(
|
click.secho(
|
||||||
" * Ignoring a call to 'app.run()', the server is"
|
" * Ignoring a call to 'app.run()' that would block"
|
||||||
" already being run with the 'flask run' command.\n"
|
" the current 'flask' CLI command.\n"
|
||||||
" Only call 'app.run()' in an 'if __name__ =="
|
" Only call 'app.run()' in an 'if __name__ =="
|
||||||
' "__main__"\' guard.',
|
' "__main__"\' guard.',
|
||||||
fg="red",
|
fg="red",
|
||||||
|
|
|
@ -715,6 +715,11 @@ class FlaskGroup(AppGroup):
|
||||||
parent: click.Context | None = None,
|
parent: click.Context | None = None,
|
||||||
**extra: t.Any,
|
**extra: t.Any,
|
||||||
) -> click.Context:
|
) -> click.Context:
|
||||||
|
# Set a flag to tell app.run to become a no-op. If app.run was
|
||||||
|
# not in a __name__ == __main__ guard, it would start the server
|
||||||
|
# when importing, blocking whatever command is being called.
|
||||||
|
os.environ["FLASK_RUN_FROM_CLI"] = "true"
|
||||||
|
|
||||||
# Attempt to load .env and .flask env files. The --env-file
|
# Attempt to load .env and .flask env files. The --env-file
|
||||||
# option can cause another file to be loaded.
|
# option can cause another file to be loaded.
|
||||||
if get_load_dotenv(self.load_dotenv):
|
if get_load_dotenv(self.load_dotenv):
|
||||||
|
|
|
@ -13,7 +13,6 @@ import pytest
|
||||||
from _pytest.monkeypatch import notset
|
from _pytest.monkeypatch import notset
|
||||||
from click.testing import CliRunner
|
from click.testing import CliRunner
|
||||||
|
|
||||||
from flask import _app_ctx_stack
|
|
||||||
from flask import Blueprint
|
from flask import Blueprint
|
||||||
from flask import current_app
|
from flask import current_app
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
|
@ -322,13 +321,11 @@ def test_app_cli_has_app_context(app, runner):
|
||||||
app = click.get_current_context().obj.load_app()
|
app = click.get_current_context().obj.load_app()
|
||||||
# the loaded app should be the same as current_app
|
# the loaded app should be the same as current_app
|
||||||
same_app = current_app._get_current_object() is app
|
same_app = current_app._get_current_object() is app
|
||||||
# only one app context should be pushed
|
return same_app, value
|
||||||
stack_size = len(_app_ctx_stack._local.stack)
|
|
||||||
return same_app, stack_size, value
|
|
||||||
|
|
||||||
cli = FlaskGroup(create_app=lambda: app)
|
cli = FlaskGroup(create_app=lambda: app)
|
||||||
result = runner.invoke(cli, ["check", "x"], standalone_mode=False)
|
result = runner.invoke(cli, ["check", "x"], standalone_mode=False)
|
||||||
assert result.return_value == (True, 1, True)
|
assert result.return_value == (True, True)
|
||||||
|
|
||||||
|
|
||||||
def test_with_appcontext(runner):
|
def test_with_appcontext(runner):
|
||||||
|
|
Loading…
Reference in New Issue