Skip to content

Commit

Permalink
Merge pull request #2765 from ThiefMaster/custom-cli-ignore-debug
Browse files Browse the repository at this point in the history
Add option to not overwrite debug flag in cli
  • Loading branch information
ThiefMaster committed Jun 14, 2018
2 parents b34c717 + 50227f0 commit 1a9caed
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ Unreleased
- :func:`send_file` encodes filenames as ASCII instead of Latin-1
(ISO-8859-1). This fixes compatibility with Gunicorn, which is
stricter about header encodings than PEP 3333. (`#2766`_)
- Allow custom CLIs using ``FlaskGroup`` to set the debug flag without
it always being overwritten based on environment variables. (`#2765`_)

.. _#2766: https://github.com/pallets/flask/issues/2766
.. _#2765: https://github.com/pallets/flask/pull/2765


Version 1.0.2
Expand Down
23 changes: 14 additions & 9 deletions flask/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,8 @@ class ScriptInfo(object):
onwards as click object.
"""

def __init__(self, app_import_path=None, create_app=None):
def __init__(self, app_import_path=None, create_app=None,
set_debug_flag=True):
#: Optionally the import path for the Flask application.
self.app_import_path = app_import_path or os.environ.get('FLASK_APP')
#: Optionally a function that is passed the script info to create
Expand All @@ -349,6 +350,7 @@ def __init__(self, app_import_path=None, create_app=None):
#: A dictionary with arbitrary data that can be associated with
#: this script info.
self.data = {}
self.set_debug_flag = set_debug_flag
self._loaded_app = None

def load_app(self):
Expand Down Expand Up @@ -386,12 +388,10 @@ def load_app(self):
'"app.py" module was not found in the current directory.'
)

debug = get_debug_flag()

# Update the app's debug flag through the descriptor so that other
# values repopulate as well.
if debug is not None:
app.debug = debug
if self.set_debug_flag:
# Update the app's debug flag through the descriptor so that
# other values repopulate as well.
app.debug = get_debug_flag()

self._loaded_app = app
return app
Expand Down Expand Up @@ -459,14 +459,17 @@ class FlaskGroup(AppGroup):
:param load_dotenv: Load the nearest :file:`.env` and :file:`.flaskenv`
files to set environment variables. Will also change the working
directory to the directory containing the first file found.
:param set_debug_flag: Set the app's debug flag based on the active
environment
.. versionchanged:: 1.0
If installed, python-dotenv will be used to load environment variables
from :file:`.env` and :file:`.flaskenv` files.
"""

def __init__(self, add_default_commands=True, create_app=None,
add_version_option=True, load_dotenv=True, **extra):
add_version_option=True, load_dotenv=True,
set_debug_flag=True, **extra):
params = list(extra.pop('params', None) or ())

if add_version_option:
Expand All @@ -475,6 +478,7 @@ def __init__(self, add_default_commands=True, create_app=None,
AppGroup.__init__(self, params=params, **extra)
self.create_app = create_app
self.load_dotenv = load_dotenv
self.set_debug_flag = set_debug_flag

if add_default_commands:
self.add_command(run_command)
Expand Down Expand Up @@ -550,7 +554,8 @@ def main(self, *args, **kwargs):
obj = kwargs.get('obj')

if obj is None:
obj = ScriptInfo(create_app=self.create_app)
obj = ScriptInfo(create_app=self.create_app,
set_debug_flag=self.set_debug_flag)

kwargs['obj'] = obj
kwargs.setdefault('auto_envvar_prefix', 'FLASK')
Expand Down
22 changes: 22 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,28 @@ def test():
assert result.output == 'flaskgroup\n'


@pytest.mark.parametrize('set_debug_flag', (True, False))
def test_flaskgroup_debug(runner, set_debug_flag):
"""Test FlaskGroup debug flag behavior."""

def create_app(info):
app = Flask("flaskgroup")
app.debug = True
return app

@click.group(cls=FlaskGroup, create_app=create_app, set_debug_flag=set_debug_flag)
def cli(**params):
pass

@cli.command()
def test():
click.echo(str(current_app.debug))

result = runner.invoke(cli, ['test'])
assert result.exit_code == 0
assert result.output == '%s\n' % str(not set_debug_flag)


def test_print_exceptions(runner):
"""Print the stacktrace if the CLI."""

Expand Down

0 comments on commit 1a9caed

Please sign in to comment.