Skip to content
Myr

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

5 files+149 added−1 removed

modified:flask/app.py+1 added-1 removed
544 unmodified lines
545 #: 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)550551 def _get_error_handlers(self):552 from warnings import warn553 warn(DeprecationWarning('error_handlers is deprecated, use the '
1437 unmodified lines
added:tests/test_apps/cliapp/__init__.py
added:tests/test_apps/cliapp/app.py+5 added
1from __future__ import absolute_import, print_function23from flask import Flask45testapp = Flask('testapp')
added:tests/test_apps/cliapp/multiapp.py+6 added
1from __future__ import absolute_import, print_function23from flask import Flask45app1 = Flask('app1')6app2 = Flask('app2')
added:tests/test_cli.py+137 added
1# -*- coding: utf-8 -*-2"""3 tests.test_cli4 ~~~~~~~~~~~~~~56 :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#14from __future__ import absolute_import, print_function1516import click17import pytest18from click.testing import CliRunner19from flask import Flask, current_app2021from flask.cli import AppGroup, FlaskGroup, NoAppException, ScriptInfo, \22 find_best_app, locate_app, script_info_option, with_appcontext232425def 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.name293031def 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.app3637 class mod:38 application = Flask('appname')39 assert find_best_app(mod) == mod.application4041 class mod:42 myapp = Flask('appname')43 assert find_best_app(mod) == mod.myapp4445 class mod:46 myapp = Flask('appname')47 myapp2 = Flask('appname2')4849 pytest.raises(NoAppException, find_best_app, mod)505152def 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")585960def 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"6566 def create_app(info):67 return Flask("createapp")6869 obj = ScriptInfo(create_app=create_app)70 app = obj.load_app()71 assert app.name == "createapp"72 assert obj.load_app() == app737475def test_with_appcontext():76 """Test of with_appcontext."""77 @click.command()78 @with_appcontext79 def testcmd():80 click.echo(current_app.name)8182 obj = ScriptInfo(create_app=lambda info: Flask("testapp"))8384 runner = CliRunner()85 result = runner.invoke(testcmd, obj=obj)86 assert result.exit_code == 087 assert result.output == 'testapp\n'888990def test_appgroup():91 """Test of with_appcontext."""92 @click.group(cls=AppGroup)93 def cli():94 pass9596 @cli.command(with_appcontext=True)97 def test():98 click.echo(current_app.name)99100 @cli.group()101 def subgroup():102 pass103104 @subgroup.command(with_appcontext=True)105 def test2():106 click.echo(current_app.name)107108 obj = ScriptInfo(create_app=lambda info: Flask("testappgroup"))109110 runner = CliRunner()111 result = runner.invoke(cli, ['test'], obj=obj)112 assert result.exit_code == 0113 assert result.output == 'testappgroup\n'114115 result = runner.invoke(cli, ['subgroup', 'test2'], obj=obj)116 assert result.exit_code == 0117 assert result.output == 'testappgroup\n'118119120def test_flaskgroup():121 """Test FlaskGroup."""122 def create_app(info):123 return Flask("flaskgroup")124125 @click.group(cls=FlaskGroup, create_app=create_app)126 @script_info_option('--config', script_info_key='config')127 def cli(**params):128 pass129130 @cli.command()131 def test():132 click.echo(current_app.name)133134 runner = CliRunner()135 result = runner.invoke(cli, ['test'])136 assert result.exit_code == 0137 assert result.output == 'flaskgroup\n'