Forward ported CLI tests from Flask-CLI and fixed a bug with the CLI's name. (#1806)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Forward port the CLI tests from Flask-CLI. * Make sure the parameter passed to the CLI's AppGroup is the app's name, not the app itself.
authored by Jannis Leidel · committed by Markus Unterwaditzer · parent 8011e1d · Browse files · 5 files·+149 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+149 added−1 removed
- tests:+148 added−0 removed
- other:+1 added−1 removed
Provenance
- Commit
88500f5- Tree
bd63e54- Author
Jannis Leidel <jannis@leidel.info>- Committer
Markus Unterwaditzer <markus@unterwaditzer.net>- Parent
8011e1d
5 files changed+149 added−1 removed
modified:flask/app.py+1 added-1 removed
544 unmodified lines545 #: application has been discovered. The default commands are546 #: provided by Flask itself and can be overridden.547 #:548 #: This is an instance of a :class:`click.Group` object.549- self.cli = cli.AppGroup(self)549+ self.cli = cli.AppGroup(self.name)550 551 def _get_error_handlers(self):552 from warnings import warn553 warn(DeprecationWarning('error_handlers is deprecated, use the '1437 unmodified linesadded:tests/test_apps/cliapp/__init__.py
added:tests/test_apps/cliapp/app.py+5 added
1+from __future__ import absolute_import, print_function2+3+from flask import Flask4+5+testapp = Flask('testapp')added:tests/test_apps/cliapp/multiapp.py+6 added
1+from __future__ import absolute_import, print_function2+3+from flask import Flask4+5+app1 = Flask('app1')6+app2 = Flask('app2')added:tests/test_cli.py+137 added
1+# -*- coding: utf-8 -*-2+"""3+ tests.test_cli4+ ~~~~~~~~~~~~~~5+6+ :copyright: (c) 2016 by the Flask Team, see AUTHORS for more details.7+ :license: BSD, see LICENSE for more details.8+"""9+#10+# This file was part of Flask-CLI and was modified under the terms its license,11+# the Revised BSD License.12+# Copyright (C) 2015 CERN.13+#14+from __future__ import absolute_import, print_function15+16+import click17+import pytest18+from click.testing import CliRunner19+from flask import Flask, current_app20+21+from flask.cli import AppGroup, FlaskGroup, NoAppException, ScriptInfo, \22+ find_best_app, locate_app, script_info_option, with_appcontext23+24+25+def test_cli_name(test_apps):26+ "Make sure the CLI object's name is the app's name and not the app itself"27+ from cliapp.app import testapp28+ assert testapp.cli.name == testapp.name29+30+31+def test_find_best_app(test_apps):32+ """Test of find_best_app."""33+ class mod:34+ app = Flask('appname')35+ assert find_best_app(mod) == mod.app36+37+ class mod:38+ application = Flask('appname')39+ assert find_best_app(mod) == mod.application40+41+ class mod:42+ myapp = Flask('appname')43+ assert find_best_app(mod) == mod.myapp44+45+ class mod:46+ myapp = Flask('appname')47+ myapp2 = Flask('appname2')48+49+ pytest.raises(NoAppException, find_best_app, mod)50+51+52+def test_locate_app(test_apps):53+ """Test of locate_app."""54+ assert locate_app("cliapp.app").name == "testapp"55+ assert locate_app("cliapp.app:testapp").name == "testapp"56+ assert locate_app("cliapp.multiapp:app1").name == "app1"57+ pytest.raises(RuntimeError, locate_app, "cliapp.app:notanapp")58+59+60+def test_scriptinfo(test_apps):61+ """Test of ScriptInfo."""62+ obj = ScriptInfo(app_import_path="cliapp.app:testapp")63+ assert obj.load_app().name == "testapp"64+ assert obj.load_app().name == "testapp"65+66+ def create_app(info):67+ return Flask("createapp")68+69+ obj = ScriptInfo(create_app=create_app)70+ app = obj.load_app()71+ assert app.name == "createapp"72+ assert obj.load_app() == app73+74+75+def test_with_appcontext():76+ """Test of with_appcontext."""77+ @click.command()78+ @with_appcontext79+ def testcmd():80+ click.echo(current_app.name)81+82+ obj = ScriptInfo(create_app=lambda info: Flask("testapp"))83+84+ runner = CliRunner()85+ result = runner.invoke(testcmd, obj=obj)86+ assert result.exit_code == 087+ assert result.output == 'testapp\n'88+89+90+def test_appgroup():91+ """Test of with_appcontext."""92+ @click.group(cls=AppGroup)93+ def cli():94+ pass95+96+ @cli.command(with_appcontext=True)97+ def test():98+ click.echo(current_app.name)99+100+ @cli.group()101+ def subgroup():102+ pass103+104+ @subgroup.command(with_appcontext=True)105+ def test2():106+ click.echo(current_app.name)107+108+ obj = ScriptInfo(create_app=lambda info: Flask("testappgroup"))109+110+ runner = CliRunner()111+ result = runner.invoke(cli, ['test'], obj=obj)112+ assert result.exit_code == 0113+ assert result.output == 'testappgroup\n'114+115+ result = runner.invoke(cli, ['subgroup', 'test2'], obj=obj)116+ assert result.exit_code == 0117+ assert result.output == 'testappgroup\n'118+119+120+def test_flaskgroup():121+ """Test FlaskGroup."""122+ def create_app(info):123+ return Flask("flaskgroup")124+125+ @click.group(cls=FlaskGroup, create_app=create_app)126+ @script_info_option('--config', script_info_key='config')127+ def cli(**params):128+ pass129+130+ @cli.command()131+ def test():132+ click.echo(current_app.name)133+134+ runner = CliRunner()135+ result = runner.invoke(cli, ['test'])136+ assert result.exit_code == 0137+ assert result.output == 'flaskgroup\n'