Skip to content

Commit 88500f5

Browse files
jezdezuntitaker
authored andcommitted
Forward ported CLI tests from Flask-CLI and fixed a bug with the CLI's name. (pallets#1806)
* 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.
1 parent 8011e1d commit 88500f5

File tree

5 files changed

+149
-1
lines changed

5 files changed

+149
-1
lines changed

flask/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ def __init__(self, import_name, static_path=None, static_url_path=None,
546546
#: provided by Flask itself and can be overridden.
547547
#:
548548
#: This is an instance of a :class:`click.Group` object.
549-
self.cli = cli.AppGroup(self)
549+
self.cli = cli.AppGroup(self.name)
550550

551551
def _get_error_handlers(self):
552552
from warnings import warn

tests/test_apps/cliapp/__init__.py

Whitespace-only changes.

tests/test_apps/cliapp/app.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from __future__ import absolute_import, print_function
2+
3+
from flask import Flask
4+
5+
testapp = Flask('testapp')

tests/test_apps/cliapp/multiapp.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from __future__ import absolute_import, print_function
2+
3+
from flask import Flask
4+
5+
app1 = Flask('app1')
6+
app2 = Flask('app2')

tests/test_cli.py

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
tests.test_cli
4+
~~~~~~~~~~~~~~
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_function
15+
16+
import click
17+
import pytest
18+
from click.testing import CliRunner
19+
from flask import Flask, current_app
20+
21+
from flask.cli import AppGroup, FlaskGroup, NoAppException, ScriptInfo, \
22+
find_best_app, locate_app, script_info_option, with_appcontext
23+
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 testapp
28+
assert testapp.cli.name == testapp.name
29+
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.app
36+
37+
class mod:
38+
application = Flask('appname')
39+
assert find_best_app(mod) == mod.application
40+
41+
class mod:
42+
myapp = Flask('appname')
43+
assert find_best_app(mod) == mod.myapp
44+
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() == app
73+
74+
75+
def test_with_appcontext():
76+
"""Test of with_appcontext."""
77+
@click.command()
78+
@with_appcontext
79+
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 == 0
87+
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+
pass
95+
96+
@cli.command(with_appcontext=True)
97+
def test():
98+
click.echo(current_app.name)
99+
100+
@cli.group()
101+
def subgroup():
102+
pass
103+
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 == 0
113+
assert result.output == 'testappgroup\n'
114+
115+
result = runner.invoke(cli, ['subgroup', 'test2'], obj=obj)
116+
assert result.exit_code == 0
117+
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+
pass
129+
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 == 0
137+
assert result.output == 'flaskgroup\n'

0 commit comments

Comments
 (0)