Skip to content
Myr

test_cli.py

This file

Path

tests/test_cli.py

656 lines · 18.4 KiB · Python

Last changed by

Merge pull request #3564 from pallets/cli-ast-parseDavid Lordbefore adoption2062d98

History of this file

Take it

The walk stopped after 200 commits: lines older than 0123e65 point at it.

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 1–13why, one step back
1# This file was part of Flask-CLI and was modified under the terms of
2# its Revised BSD License. Copyright © 2015 CERN.3import os4import ssl5import sys6import types7from functools import partial89import click10import pytest11from _pytest.monkeypatch import notset12from click.testing import CliRunner13

Merge pull request #3246 from pallets/reorder-importsDavid Lordbefore adoption3db4697

lines 14–28why, one step back
14from flask import Blueprint
15from flask import current_app16from flask import Flask17from flask.cli import AppGroup18from flask.cli import dotenv19from flask.cli import find_best_app20from flask.cli import FlaskGroup21from flask.cli import get_version22from flask.cli import load_dotenv23from flask.cli import locate_app24from flask.cli import NoAppException25from flask.cli import prepare_import26from flask.cli import run_command27from flask.cli import ScriptInfo28from flask.cli import with_appcontext

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 29–30why, one step back
29
30cwd = os.getcwd()

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 31why, one step back
31test_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "test_apps"))

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 32–41why, one step back
32
3334@pytest.fixture35def runner():36 return CliRunner()373839def test_cli_name(test_apps):40 """Make sure the CLI object's name is the app's name and not the app itself"""41 from cliapp.app import testapp

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 42why, one step back
42

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 43–46why, one step back
43 assert testapp.cli.name == testapp.name
444546def test_find_best_app(test_apps):

Merge pull request #3241 from jon-stumpf/jss-updatesDavid Lordbefore adoption48d0e86

line 47why, one step back
47 """Test if `find_best_app` behaves as expected with different combinations of input.""" # noqa: B950

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 48–50why, one step back
48 script_info = ScriptInfo()
4950 class Module:

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 51why, one step back
51 app = Flask("appname")

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 52–55why, one step back
52
53 assert find_best_app(script_info, Module) == Module.app5455 class Module:

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 56why, one step back
56 application = Flask("appname")

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 57–60why, one step back
57
58 assert find_best_app(script_info, Module) == Module.application5960 class Module:

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 61why, one step back
61 myapp = Flask("appname")

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 62–67why, one step back
62
63 assert find_best_app(script_info, Module) == Module.myapp6465 class Module:66 @staticmethod67 def create_app():

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 68why, one step back
68 return Flask("appname")

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

line 69why, one step back
69

Merge pull request #3563 from pallets/deprecate-script-info-argDavid Lordbefore adoption0d04b40

lines 70–72why, one step back
70 app = find_best_app(script_info, Module)
71 assert isinstance(app, Flask)72 assert app.name == "appname"

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 73–76why, one step back
73
74 class Module:75 @staticmethod76 def create_app(foo):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 77why, one step back
77 return Flask("appname")

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

line 78why, one step back
78

Merge pull request #3563 from pallets/deprecate-script-info-argDavid Lordbefore adoption0d04b40

lines 79–80why, one step back
79 with pytest.deprecated_call(match="Script info"):
80 app = find_best_app(script_info, Module)

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

line 81why, one step back
81

Merge pull request #3563 from pallets/deprecate-script-info-argDavid Lordbefore adoption0d04b40

lines 82–84why, one step back
82 assert isinstance(app, Flask)
83 assert app.name == "appname"84

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 85–87why, one step back
85 class Module:
86 @staticmethod87 def create_app(foo=None, script_info=None):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 88why, one step back
88 return Flask("appname")

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

line 89why, one step back
89

Merge pull request #3563 from pallets/deprecate-script-info-argDavid Lordbefore adoption0d04b40

lines 90–91why, one step back
90 with pytest.deprecated_call(match="script_info"):
91 app = find_best_app(script_info, Module)

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

line 92why, one step back
92

Merge pull request #3563 from pallets/deprecate-script-info-argDavid Lordbefore adoption0d04b40

lines 93–95why, one step back
93 assert isinstance(app, Flask)
94 assert app.name == "appname"95

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 96–98why, one step back
96 class Module:
97 @staticmethod98 def make_app():

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 99why, one step back
99 return Flask("appname")

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

line 100why, one step back
100

Merge pull request #3563 from pallets/deprecate-script-info-argDavid Lordbefore adoption0d04b40

lines 101–103why, one step back
101 app = find_best_app(script_info, Module)
102 assert isinstance(app, Flask)103 assert app.name == "appname"

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 104–105why, one step back
104
105 class Module:

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 106why, one step back
106 myapp = Flask("appname1")

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 107–109why, one step back
107
108 @staticmethod109 def create_app():

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 110why, one step back
110 return Flask("appname2")

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 111–114why, one step back
111
112 assert find_best_app(script_info, Module) == Module.myapp113114 class Module:

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 115why, one step back
115 myapp = Flask("appname1")

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 116–118why, one step back
116
117 @staticmethod118 def create_app():

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 119why, one step back
119 return Flask("appname2")

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 120–128why, one step back
120
121 assert find_best_app(script_info, Module) == Module.myapp122123 class Module:124 pass125126 pytest.raises(NoAppException, find_best_app, script_info, Module)127128 class Module:

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 129–130why, one step back
129 myapp1 = Flask("appname1")
130 myapp2 = Flask("appname2")

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 131–136why, one step back
131
132 pytest.raises(NoAppException, find_best_app, script_info, Module)133134 class Module:135 @staticmethod136 def create_app(foo, bar):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 137why, one step back
137 return Flask("appname2")

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 138–143why, one step back
138
139 pytest.raises(NoAppException, find_best_app, script_info, Module)140141 class Module:142 @staticmethod143 def create_app():

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 144why, one step back
144 raise TypeError("bad bad factory!")

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 145–148why, one step back
145
146 pytest.raises(TypeError, find_best_app, script_info, Module)147148

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 149–150why, one step back
149@pytest.mark.parametrize(
150 "value,path,result",

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

line 151why, one step back
151 (

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 152–176why, one step back
152 ("test", cwd, "test"),
153 ("test.py", cwd, "test"),154 ("a/test", os.path.join(cwd, "a"), "test"),155 ("test/__init__.py", cwd, "test"),156 ("test/__init__", cwd, "test"),157 # nested package158 (159 os.path.join(test_path, "cliapp", "inner1", "__init__"),160 test_path,161 "cliapp.inner1",162 ),163 (164 os.path.join(test_path, "cliapp", "inner1", "inner2"),165 test_path,166 "cliapp.inner1.inner2",167 ),168 # dotted name169 ("test.a.b", cwd, "test.a.b"),170 (os.path.join(test_path, "cliapp.app"), test_path, "cliapp.app"),171 # not a Python file, will be caught during import172 (173 os.path.join(test_path, "cliapp", "message.txt"),174 test_path,175 "cliapp.message.txt",176 ),

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

line 177why, one step back
177 ),

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 178why, one step back
178)

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 179–197why, one step back
179def test_prepare_import(request, value, path, result):
180 """Expect the correct path to be set and the correct import and app names181 to be returned.182183 :func:`prepare_exec_for_file` has a side effect where the parent directory184 of the given import is added to :data:`sys.path`. This is reset after the185 test runs.186 """187 original_path = sys.path[:]188189 def reset_path():190 sys.path[:] = original_path191192 request.addfinalizer(reset_path)193194 assert prepare_import(value) == result195 assert sys.path[0] == path196197

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 198–212why, one step back
198@pytest.mark.parametrize(
199 "iname,aname,result",200 (201 ("cliapp.app", None, "testapp"),202 ("cliapp.app", "testapp", "testapp"),203 ("cliapp.factory", None, "app"),204 ("cliapp.factory", "create_app", "app"),205 ("cliapp.factory", "create_app()", "app"),206 ("cliapp.factory", 'create_app2("foo", "bar")', "app2_foo_bar"),207 # trailing comma space208 ("cliapp.factory", 'create_app2("foo", "bar", )', "app2_foo_bar"),209 # strip whitespace210 ("cliapp.factory", " create_app () ", "app"),211 ),212)

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 213–217why, one step back
213def test_locate_app(test_apps, iname, aname, result):
214 info = ScriptInfo()215 assert locate_app(info, iname, aname).name == result216217

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 218–235why, one step back
218@pytest.mark.parametrize(
219 "iname,aname",220 (221 ("notanapp.py", None),222 ("cliapp/app", None),223 ("cliapp.app", "notanapp"),224 # not enough arguments225 ("cliapp.factory", 'create_app2("foo")'),226 # invalid identifier227 ("cliapp.factory", "create_app("),228 # no app returned229 ("cliapp.factory", "no_app"),230 # nested import error231 ("cliapp.importerrorapp", None),232 # not a Python file233 ("cliapp.message.txt", None),234 ),235)

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 236–244why, one step back
236def test_locate_app_raises(test_apps, iname, aname):
237 info = ScriptInfo()238239 with pytest.raises(NoAppException):240 locate_app(info, iname, aname)241242243def test_locate_app_suppress_raise():244 info = ScriptInfo()

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 245why, one step back
245 app = locate_app(info, "notanapp.py", None, raise_if_not_found=False)

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 246–249why, one step back
246 assert app is None
247248 # only direct import error is suppressed249 with pytest.raises(NoAppException):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 250why, one step back
250 locate_app(info, "cliapp.importerrorapp", None, raise_if_not_found=False)

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 251–253why, one step back
251
252253def test_get_version(test_apps, capsys):

Merge branch '1.0-maintenance'David Lordbefore adoption4f3dbb3

lines 254–256why, one step back
254 from flask import __version__ as flask_version
255 from werkzeug import __version__ as werkzeug_version256 from platform import python_version

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

line 257why, one step back
257

Merge pull request #3554 from pallets/drop-python2David Lordbefore adoptioncd8a374

line 258why, one step back
258 class MockCtx:

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 259–261why, one step back
259 resilient_parsing = False
260 color = None261

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 262–263why, one step back
262 def exit(self):
263 return

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 264–267why, one step back
264
265 ctx = MockCtx()266 get_version(ctx, None, "test")267 out, err = capsys.readouterr()

Merge pull request #3554 from pallets/drop-python2David Lordbefore adoptioncd8a374

lines 268–270why, one step back
268 assert f"Python {python_version()}" in out
269 assert f"Flask {flask_version}" in out270 assert f"Werkzeug {werkzeug_version}" in out

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 271–275why, one step back
271
272273def test_scriptinfo(test_apps, monkeypatch):274 """Test of ScriptInfo."""275 obj = ScriptInfo(app_import_path="cliapp.app:testapp")

Merge branch '1.0-maintenance'David Lordbefore adoptionf7d50d4

lines 276–278why, one step back
276 app = obj.load_app()
277 assert app.name == "testapp"278 assert obj.load_app() is app

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

line 279why, one step back
279

Merge branch '1.0-maintenance'David Lordbefore adoptionf7d50d4

line 280why, one step back
280 # import app with module's absolute path

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 281–283why, one step back
281 cli_app_path = os.path.abspath(
282 os.path.join(os.path.dirname(__file__), "test_apps", "cliapp", "app.py")283 )

Merge branch '1.0-maintenance'David Lordbefore adoptionf7d50d4

lines 284–285why, one step back
284 obj = ScriptInfo(app_import_path=cli_app_path)
285 app = obj.load_app()

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 286why, one step back
286 assert app.name == "testapp"

Merge branch '1.0-maintenance'David Lordbefore adoptionf7d50d4

line 287why, one step back
287 assert obj.load_app() is app

Merge pull request #3554 from pallets/drop-python2David Lordbefore adoptioncd8a374

line 288why, one step back
288 obj = ScriptInfo(app_import_path=f"{cli_app_path}:testapp")

Merge branch '1.0-maintenance'David Lordbefore adoptionf7d50d4

line 289why, one step back
289 app = obj.load_app()

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 290why, one step back
290 assert app.name == "testapp"

Merge branch '1.0-maintenance'David Lordbefore adoptionf7d50d4

lines 291–292why, one step back
291 assert obj.load_app() is app
292

Merge pull request #3563 from pallets/deprecate-script-info-argDavid Lordbefore adoption0d04b40

line 293why, one step back
293 def create_app():

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 294–298why, one step back
294 return Flask("createapp")
295296 obj = ScriptInfo(create_app=create_app)297 app = obj.load_app()298 assert app.name == "createapp"

Merge branch '1.0-maintenance'David Lordbefore adoptionf7d50d4

line 299why, one step back
299 assert obj.load_app() is app

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 300–304why, one step back
300
301 obj = ScriptInfo()302 pytest.raises(NoAppException, obj.load_app)303304 # import app from wsgi.py in current directory

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 305–309why, one step back
305 monkeypatch.chdir(
306 os.path.abspath(307 os.path.join(os.path.dirname(__file__), "test_apps", "helloworld")308 )309 )

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 310–311why, one step back
310 obj = ScriptInfo()
311 app = obj.load_app()

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 312why, one step back
312 assert app.name == "hello"

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 313–314why, one step back
313
314 # import app from app.py in current directory

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 315–317why, one step back
315 monkeypatch.chdir(
316 os.path.abspath(os.path.join(os.path.dirname(__file__), "test_apps", "cliapp"))317 )

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 318–319why, one step back
318 obj = ScriptInfo()
319 app = obj.load_app()

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 320why, one step back
320 assert app.name == "testapp"

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 321–330why, one step back
321
322323def test_with_appcontext(runner):324 """Test of with_appcontext."""325326 @click.command()327 @with_appcontext328 def testcmd():329 click.echo(current_app.name)330

Merge pull request #3563 from pallets/deprecate-script-info-argDavid Lordbefore adoption0d04b40

line 331why, one step back
331 obj = ScriptInfo(create_app=lambda: Flask("testapp"))

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 332–334why, one step back
332
333 result = runner.invoke(testcmd, obj=obj)334 assert result.exit_code == 0

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 335why, one step back
335 assert result.output == "testapp\n"

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 336–356why, one step back
336
337338def test_appgroup(runner):339 """Test of with_appcontext."""340341 @click.group(cls=AppGroup)342 def cli():343 pass344345 @cli.command(with_appcontext=True)346 def test():347 click.echo(current_app.name)348349 @cli.group()350 def subgroup():351 pass352353 @subgroup.command(with_appcontext=True)354 def test2():355 click.echo(current_app.name)356

Merge pull request #3563 from pallets/deprecate-script-info-argDavid Lordbefore adoption0d04b40

line 357why, one step back
357 obj = ScriptInfo(create_app=lambda: Flask("testappgroup"))

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

line 358why, one step back
358

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 359why, one step back
359 result = runner.invoke(cli, ["test"], obj=obj)

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

line 360why, one step back
360 assert result.exit_code == 0

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 361why, one step back
361 assert result.output == "testappgroup\n"

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

line 362why, one step back
362

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 363why, one step back
363 result = runner.invoke(cli, ["subgroup", "test2"], obj=obj)

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

line 364why, one step back
364 assert result.exit_code == 0

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 365why, one step back
365 assert result.output == "testappgroup\n"

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 366–370why, one step back
366
367368def test_flaskgroup(runner):369 """Test FlaskGroup."""370

Merge pull request #3563 from pallets/deprecate-script-info-argDavid Lordbefore adoption0d04b40

line 371why, one step back
371 def create_app():

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 372–381why, one step back
372 return Flask("flaskgroup")
373374 @click.group(cls=FlaskGroup, create_app=create_app)375 def cli(**params):376 pass377378 @cli.command()379 def test():380 click.echo(current_app.name)381

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 382why, one step back
382 result = runner.invoke(cli, ["test"])

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

line 383why, one step back
383 assert result.exit_code == 0

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 384why, one step back
384 assert result.output == "flaskgroup\n"

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 385–386why, one step back
385
386

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 387why, one step back
387@pytest.mark.parametrize("set_debug_flag", (True, False))

Merge branch '1.0-maintenance'ThiefMasterbefore adoption161c436

lines 388–390why, one step back
388def test_flaskgroup_debug(runner, set_debug_flag):
389 """Test FlaskGroup debug flag behavior."""390

Merge pull request #3563 from pallets/deprecate-script-info-argDavid Lordbefore adoption0d04b40

line 391why, one step back
391 def create_app():

Merge branch '1.0-maintenance'ThiefMasterbefore adoption161c436

lines 392–403why, one step back
392 app = Flask("flaskgroup")
393 app.debug = True394 return app395396 @click.group(cls=FlaskGroup, create_app=create_app, set_debug_flag=set_debug_flag)397 def cli(**params):398 pass399400 @cli.command()401 def test():402 click.echo(str(current_app.debug))403

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 404why, one step back
404 result = runner.invoke(cli, ["test"])

Merge branch '1.0-maintenance'ThiefMasterbefore adoption161c436

line 405why, one step back
405 assert result.exit_code == 0

Merge pull request #3554 from pallets/drop-python2David Lordbefore adoptioncd8a374

line 406why, one step back
406 assert result.output == f"{not set_debug_flag}\n"

Merge branch '1.0-maintenance'ThiefMasterbefore adoption161c436

lines 407–408why, one step back
407
408

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 409–411why, one step back
409def test_print_exceptions(runner):
410 """Print the stacktrace if the CLI."""411

Merge pull request #3563 from pallets/deprecate-script-info-argDavid Lordbefore adoption0d04b40

line 412why, one step back
412 def create_app():

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 413–419why, one step back
413 raise Exception("oh no")
414 return Flask("flaskgroup")415416 @click.group(cls=FlaskGroup, create_app=create_app)417 def cli(**params):418 pass419

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 420why, one step back
420 result = runner.invoke(cli, ["--help"])

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

line 421why, one step back
421 assert result.exit_code == 0

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 422–423why, one step back
422 assert "Exception: oh no" in result.output
423 assert "Traceback" in result.output

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 424–428why, one step back
424
425426class TestRoutes:427 @pytest.fixture428 def invoke(self, runner):

Merge pull request #3563 from pallets/deprecate-script-info-argDavid Lordbefore adoption0d04b40

line 429why, one step back
429 def create_app():

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 430–432why, one step back
430 app = Flask(__name__)
431 app.testing = True432

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 433why, one step back
433 @app.route("/get_post/<int:x>/<int:y>", methods=["GET", "POST"])

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 434–436why, one step back
434 def yyy_get_post(x, y):
435 pass436

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 437why, one step back
437 @app.route("/zzz_post", methods=["POST"])

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 438–445why, one step back
438 def aaa_post():
439 pass440441 return app442443 cli = FlaskGroup(create_app=create_app)444 return partial(runner.invoke, cli)445

Merge pull request #2811 from greyli/test-routes-command-for-no-routesDavid Lordbefore adoptionb6a8f75

lines 446–447why, one step back
446 @pytest.fixture
447 def invoke_no_routes(self, runner):

Merge pull request #3563 from pallets/deprecate-script-info-argDavid Lordbefore adoption0d04b40

line 448why, one step back
448 def create_app():

Merge pull request #2811 from greyli/test-routes-command-for-no-routesDavid Lordbefore adoptionb6a8f75

lines 449–456why, one step back
449 app = Flask(__name__, static_folder=None)
450 app.testing = True451452 return app453454 cli = FlaskGroup(create_app=create_app)455 return partial(runner.invoke, cli)456

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 457–460why, one step back
457 def expect_order(self, order, output):
458 # skip the header and match the start of each row459 for expect, line in zip(order, output.splitlines()[2:]):460 # do this instead of startswith for nicer pytest output

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 461why, one step back
461 assert line[: len(expect)] == expect

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 462–463why, one step back
462
463 def test_simple(self, invoke):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 464why, one step back
464 result = invoke(["routes"])

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

line 465why, one step back
465 assert result.exit_code == 0

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 466why, one step back
466 self.expect_order(["aaa_post", "static", "yyy_get_post"], result.output)

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 467–468why, one step back
467
468 def test_sort(self, invoke):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 469–470why, one step back
469 default_output = invoke(["routes"]).output
470 endpoint_output = invoke(["routes", "-s", "endpoint"]).output

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 471–472why, one step back
471 assert default_output == endpoint_output
472 self.expect_order(

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 473–474why, one step back
473 ["static", "yyy_get_post", "aaa_post"],
474 invoke(["routes", "-s", "methods"]).output,

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 475–476why, one step back
475 )
476 self.expect_order(

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 477–478why, one step back
477 ["yyy_get_post", "static", "aaa_post"],
478 invoke(["routes", "-s", "rule"]).output,

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 479–480why, one step back
479 )
480 self.expect_order(

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 481–482why, one step back
481 ["aaa_post", "yyy_get_post", "static"],
482 invoke(["routes", "-s", "match"]).output,

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 483–485why, one step back
483 )
484485 def test_all_methods(self, invoke):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 486–489why, one step back
486 output = invoke(["routes"]).output
487 assert "GET, HEAD, OPTIONS, POST" not in output488 output = invoke(["routes", "--all-methods"]).output489 assert "GET, HEAD, OPTIONS, POST" in output

Merge pull request #2811 from greyli/test-routes-command-for-no-routesDavid Lordbefore adoptionb6a8f75

lines 490–491why, one step back
490
491 def test_no_routes(self, invoke_no_routes):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 492why, one step back
492 result = invoke_no_routes(["routes"])

Merge pull request #2811 from greyli/test-routes-command-for-no-routesDavid Lordbefore adoptionb6a8f75

line 493why, one step back
493 assert result.exit_code == 0

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 494why, one step back
494 assert "No routes were registered." in result.output

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 495–496why, one step back
495
496

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 497why, one step back
497need_dotenv = pytest.mark.skipif(dotenv is None, reason="dotenv is not installed")

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 498–502why, one step back
498
499500@need_dotenv501def test_load_dotenv(monkeypatch):502 # can't use monkeypatch.delitem since the keys don't exist yet

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 503why, one step back
503 for item in ("FOO", "BAR", "SPAM"):

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 504–505why, one step back
504 monkeypatch._setitem.append((os.environ, item, notset))
505

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 506–507why, one step back
506 monkeypatch.setenv("EGGS", "3")
507 monkeypatch.chdir(os.path.join(test_path, "cliapp", "inner1"))

Merge pull request #2939 from sharmaadarsh563/2937-correct-load_dotenv-return-valueDavid Lordbefore adoptiond3e1fed

line 508why, one step back
508 assert load_dotenv()

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 509–510why, one step back
509 assert os.getcwd() == test_path
510 # .flaskenv doesn't overwrite .env

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 511why, one step back
511 assert os.environ["FOO"] == "env"

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

line 512why, one step back
512 # set only in .flaskenv

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 513why, one step back
513 assert os.environ["BAR"] == "bar"

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

line 514why, one step back
514 # set only in .env

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 515why, one step back
515 assert os.environ["SPAM"] == "1"

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

line 516why, one step back
516 # set manually, files don't overwrite

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 517why, one step back
517 assert os.environ["EGGS"] == "3"

Merge pull request #2939 from sharmaadarsh563/2937-correct-load_dotenv-return-valueDavid Lordbefore adoptiond3e1fed

lines 518–519why, one step back
518
519 # Non existent file should not load

Merge pull request #3229 from lordmauve/style-fixDavid Lordbefore adoption240e398

line 520why, one step back
520 assert not load_dotenv("non-existent-file")

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 521–524why, one step back
521
522523@need_dotenv524def test_dotenv_path(monkeypatch):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 525why, one step back
525 for item in ("FOO", "BAR", "EGGS"):

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 526–528why, one step back
526 monkeypatch._setitem.append((os.environ, item, notset))
527528 cwd = os.getcwd()

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 529why, one step back
529 load_dotenv(os.path.join(test_path, ".flaskenv"))

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

line 530why, one step back
530 assert os.getcwd() == cwd

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 531why, one step back
531 assert "FOO" in os.environ

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 532–534why, one step back
532
533534def test_dotenv_optional(monkeypatch):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 535why, one step back
535 monkeypatch.setattr("flask.cli.dotenv", None)

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 536–537why, one step back
536 monkeypatch.chdir(test_path)
537 load_dotenv()

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 538why, one step back
538 assert "FOO" not in os.environ

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 539–540why, one step back
539
540

Merge branch '1.0-maintenance'David Lordbefore adoption1fa9185

lines 541–543why, one step back
541@need_dotenv
542def test_disable_dotenv_from_env(monkeypatch, runner):543 monkeypatch.chdir(test_path)

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 544why, one step back
544 monkeypatch.setitem(os.environ, "FLASK_SKIP_DOTENV", "1")

Merge branch '1.0-maintenance'David Lordbefore adoption1fa9185

line 545why, one step back
545 runner.invoke(FlaskGroup())

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 546why, one step back
546 assert "FOO" not in os.environ

Merge branch '1.0-maintenance'David Lordbefore adoption1fa9185

lines 547–548why, one step back
547
548

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 549–551why, one step back
549def test_run_cert_path():
550 # no key551 with pytest.raises(click.BadParameter):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 552why, one step back
552 run_command.make_context("run", ["--cert", __file__])

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 553–555why, one step back
553
554 # no cert555 with pytest.raises(click.BadParameter):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 556why, one step back
556 run_command.make_context("run", ["--key", __file__])

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

line 557why, one step back
557

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 558–559why, one step back
558 ctx = run_command.make_context("run", ["--cert", __file__, "--key", __file__])
559 assert ctx.params["cert"] == (__file__, __file__)

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 560–562why, one step back
560
561562def test_run_cert_adhoc(monkeypatch):

Merge pull request #3492 from pallets/cryptographyDavid Lordbefore adoptionb074a2e

line 563why, one step back
563 monkeypatch.setitem(sys.modules, "cryptography", None)

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

line 564why, one step back
564

Merge pull request #3492 from pallets/cryptographyDavid Lordbefore adoptionb074a2e

line 565why, one step back
565 # cryptography not installed

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

line 566why, one step back
566 with pytest.raises(click.BadParameter):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 567why, one step back
567 run_command.make_context("run", ["--cert", "adhoc"])

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

line 568why, one step back
568

Merge pull request #3492 from pallets/cryptographyDavid Lordbefore adoptionb074a2e

lines 569–570why, one step back
569 # cryptography installed
570 monkeypatch.setitem(sys.modules, "cryptography", types.ModuleType("cryptography"))

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 571–572why, one step back
571 ctx = run_command.make_context("run", ["--cert", "adhoc"])
572 assert ctx.params["cert"] == "adhoc"

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 573–575why, one step back
573
574 # no key with adhoc575 with pytest.raises(click.BadParameter):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 576why, one step back
576 run_command.make_context("run", ["--cert", "adhoc", "--key", __file__])

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 577–579why, one step back
577
578579def test_run_cert_import(monkeypatch):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 580why, one step back
580 monkeypatch.setitem(sys.modules, "not_here", None)

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 581–583why, one step back
581
582 # ImportError583 with pytest.raises(click.BadParameter):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 584why, one step back
584 run_command.make_context("run", ["--cert", "not_here"])

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

line 585why, one step back
585

Merge pull request #3554 from pallets/drop-python2David Lordbefore adoptioncd8a374

lines 586–587why, one step back
586 with pytest.raises(click.BadParameter):
587 run_command.make_context("run", ["--cert", "flask"])

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 588–589why, one step back
588
589 # SSLContext

Merge pull request #3554 from pallets/drop-python2David Lordbefore adoptioncd8a374

line 590why, one step back
590 ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

line 591why, one step back
591

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 592–594why, one step back
592 monkeypatch.setitem(sys.modules, "ssl_context", ssl_context)
593 ctx = run_command.make_context("run", ["--cert", "ssl_context"])594 assert ctx.params["cert"] is ssl_context

Merge pull request #2717 from filipefilardi/masterDavid Lordbefore adoption0123e65

lines 595–597why, one step back
595
596 # no --key with SSLContext597 with pytest.raises(click.BadParameter):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 598why, one step back
598 run_command.make_context("run", ["--cert", "ssl_context", "--key", __file__])

Merge pull request #2784 from doobeh/masterDavid Lordbefore adoptionb837606

lines 599–600why, one step back
599
600

Merge pull request #3242 from cslecrone/3211-ssl-importDavid Lordbefore adoption13b689b

lines 601–606why, one step back
601def test_run_cert_no_ssl(monkeypatch):
602 monkeypatch.setattr("flask.cli.ssl", None)603 with pytest.raises(click.BadParameter):604 run_command.make_context("run", ["--cert", "not_here"])605606

Merge pull request #2784 from doobeh/masterDavid Lordbefore adoptionb837606

lines 607–647why, one step back
607def test_cli_blueprints(app):
608 """Test blueprint commands register correctly to the application"""609 custom = Blueprint("custom", __name__, cli_group="customized")610 nested = Blueprint("nested", __name__)611 merged = Blueprint("merged", __name__, cli_group=None)612 late = Blueprint("late", __name__)613614 @custom.cli.command("custom")615 def custom_command():616 click.echo("custom_result")617618 @nested.cli.command("nested")619 def nested_command():620 click.echo("nested_result")621622 @merged.cli.command("merged")623 def merged_command():624 click.echo("merged_result")625626 @late.cli.command("late")627 def late_command():628 click.echo("late_result")629630 app.register_blueprint(custom)631 app.register_blueprint(nested)632 app.register_blueprint(merged)633 app.register_blueprint(late, cli_group="late_registration")634635 app_runner = app.test_cli_runner()636637 result = app_runner.invoke(args=["customized", "custom"])638 assert "custom_result" in result.output639640 result = app_runner.invoke(args=["nested", "nested"])641 assert "nested_result" in result.output642643 result = app_runner.invoke(args=["merged"])644 assert "merged_result" in result.output645646 result = app_runner.invoke(args=["late_registration", "late"])647 assert "late_result" in result.output

Merge pull request #3236 from lordmauve/no-cligroup-emptyDavid Lordbefore adoption91e53da

lines 648–655why, one step back
648
649650def test_cli_empty(app):651 """If a Blueprint's CLI group is empty, do not register it."""652 bp = Blueprint("blue", __name__, cli_group="blue")653 app.register_blueprint(bp)654655 result = app.test_cli_runner().invoke(args=["blue", "--help"])

Merge pull request #3554 from pallets/drop-python2David Lordbefore adoptioncd8a374

line 656why, one step back
656 assert result.exit_code == 2, f"Unexpected success:\n\n{result.output}"