Merge pull request #2765 from ThiefMaster/custom-cli-ignore-debug
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Add option to not overwrite debug flag in cli
authored by Adrian · committed by GitHub · parents b34c717, 50227f0 · Browse files · 3 files·+39 added−9 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+39 added−9 removed
- tests:+22 added−0 removed
- other:+17 added−9 removed
Provenance
- Commit
1a9caed- Tree
c59e476- Author
Adrian <adrian@planetcoding.net>- Committer
GitHub <noreply@github.com>- Parent 1
b34c717- Parent 2
50227f0
3 files changed+39 added−9 removed
modified:flask/cli.py+14 added-9 removed
338 unmodified lines339 :class:`FlaskGroup` but you can also manually create it and pass it340 onwards as click object.341 """342 343- def __init__(self, app_import_path=None, create_app=None):343+ def __init__(self, app_import_path=None, create_app=None,344+ set_debug_flag=True):345 #: Optionally the import path for the Flask application.346 self.app_import_path = app_import_path or os.environ.get('FLASK_APP')347 #: Optionally a function that is passed the script info to create348 #: the instance of the application.349 self.create_app = create_app350 #: A dictionary with arbitrary data that can be associated with351 #: this script info.352 self.data = {}353+ self.set_debug_flag = set_debug_flag354 self._loaded_app = None355 356 def load_app(self):357 """Loads the Flask app (if not yet loaded) and returns it. Calling29 unmodified lines387 'the "FLASK_APP" environment variable, and a "wsgi.py" or '388 '"app.py" module was not found in the current directory.'389 )390 391+ if self.set_debug_flag:392+ # Update the app's debug flag through the descriptor so that393+ # other values repopulate as well.389- debug = get_debug_flag()394+ app.debug = get_debug_flag()395 391- # Update the app's debug flag through the descriptor so that other392- # values repopulate as well.393- if debug is not None:394- app.debug = debug395-396 self._loaded_app = app397 return app398 399 58 unmodified lines458 returns the loaded app.459 :param load_dotenv: Load the nearest :file:`.env` and :file:`.flaskenv`460 files to set environment variables. Will also change the working461 directory to the directory containing the first file found.462+ :param set_debug_flag: Set the app's debug flag based on the active463+ environment464 465 .. versionchanged:: 1.0466 If installed, python-dotenv will be used to load environment variables467 from :file:`.env` and :file:`.flaskenv` files.468 """469 470 def __init__(self, add_default_commands=True, create_app=None,469- add_version_option=True, load_dotenv=True, **extra):471+ add_version_option=True, load_dotenv=True,472+ set_debug_flag=True, **extra):473 params = list(extra.pop('params', None) or ())474 475 if add_version_option:476 params.append(version_option)477 478 AppGroup.__init__(self, params=params, **extra)479 self.create_app = create_app480 self.load_dotenv = load_dotenv481+ self.set_debug_flag = set_debug_flag482 483 if add_default_commands:484 self.add_command(run_command)485 self.add_command(shell_command)67 unmodified lines553 554 obj = kwargs.get('obj')555 556 if obj is None:553- obj = ScriptInfo(create_app=self.create_app)557+ obj = ScriptInfo(create_app=self.create_app,558+ set_debug_flag=self.set_debug_flag)559 560 kwargs['obj'] = obj561 kwargs.setdefault('auto_envvar_prefix', 'FLASK')562 return super(FlaskGroup, self).main(*args, **kwargs)341 unmodified linesmodified:tests/test_cli.py+22 added
354 unmodified lines355 assert result.exit_code == 0356 assert result.output == 'flaskgroup\n'357 358 359+@pytest.mark.parametrize('set_debug_flag', (True, False))360+def test_flaskgroup_debug(runner, set_debug_flag):361+ """Test FlaskGroup debug flag behavior."""362+363+ def create_app(info):364+ app = Flask("flaskgroup")365+ app.debug = True366+ return app367+368+ @click.group(cls=FlaskGroup, create_app=create_app, set_debug_flag=set_debug_flag)369+ def cli(**params):370+ pass371+372+ @cli.command()373+ def test():374+ click.echo(str(current_app.debug))375+376+ result = runner.invoke(cli, ['test'])377+ assert result.exit_code == 0378+ assert result.output == '%s\n' % str(not set_debug_flag)379+380+381 def test_print_exceptions(runner):382 """Print the stacktrace if the CLI."""383 384 def create_app(info):179 unmodified linesmodified:CHANGES.rst+3 added
10 unmodified lines11 12 - :func:`send_file` encodes filenames as ASCII instead of Latin-113 (ISO-8859-1). This fixes compatibility with Gunicorn, which is14 stricter about header encodings than PEP 3333. (`#2766`_)15+- Allow custom CLIs using ``FlaskGroup`` to set the debug flag without16+ it always being overwritten based on environment variables. (`#2765`_)17 18 .. _#2766: https://github.com/pallets/flask/issues/276619+.. _#2765: https://github.com/pallets/flask/pull/276520 21 22 Version 1.0.223 -------------896 unmodified lines