test_cli.py
This file
Path
tests/test_cli.py
661 lines · 18.5 KiB · Python
Last changed by
Merge pull request #3711 from pallets/cli-loading-errorDavid Lordbefore adoption6638432
History of this fileTake it
The walk stopped after 200 commits: lines older than 86bf9dc point at it.
Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 1–13why, one step back1# This file was part of Flask-CLI and was modified under the terms of2# its Revised BSD License. Copyright © 2015 CERN.3import os4import ssl5import sys6import types7from functools import partial89import click10import pytest11from _pytest.monkeypatch import notset12from click.testing import CliRunner13Merge pull request #3246 from pallets/reorder-importsDavid Lordbefore adoption3db4697
lines 14–28why, one step back14from flask import Blueprint15from 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_appcontextMerge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 29–30why, one step back2930cwd = os.getcwd()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 31why, one step back31test_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "test_apps"))Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 32–41why, one step back323334@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 testappMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 42why, one step back42Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 43–49why, one step back43 assert testapp.cli.name == testapp.name444546def test_find_best_app(test_apps):47 script_info = ScriptInfo()4849 class Module:Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 50why, one step back50 app = Flask("appname")Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 51–54why, one step back5152 assert find_best_app(script_info, Module) == Module.app5354 class Module:Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 55why, one step back55 application = Flask("appname")Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 56–59why, one step back5657 assert find_best_app(script_info, Module) == Module.application5859 class Module:Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 60why, one step back60 myapp = Flask("appname")Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 61–66why, one step back6162 assert find_best_app(script_info, Module) == Module.myapp6364 class Module:65 @staticmethod66 def create_app():Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 67why, one step back67 return Flask("appname")Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
line 68why, one step back68Merge pull request #3563 from pallets/deprecate-script-info-argDavid Lordbefore adoption0d04b40
lines 69–71why, one step back69 app = find_best_app(script_info, Module)70 assert isinstance(app, Flask)71 assert app.name == "appname"Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 72–75why, one step back7273 class Module:74 @staticmethod75 def create_app(foo):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 76why, one step back76 return Flask("appname")Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
line 77why, one step back77Merge pull request #3563 from pallets/deprecate-script-info-argDavid Lordbefore adoption0d04b40
lines 78–79why, one step back78 with pytest.deprecated_call(match="Script info"):79 app = find_best_app(script_info, Module)Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
line 80why, one step back80Merge pull request #3563 from pallets/deprecate-script-info-argDavid Lordbefore adoption0d04b40
lines 81–83why, one step back81 assert isinstance(app, Flask)82 assert app.name == "appname"83Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 84–86why, one step back84 class Module:85 @staticmethod86 def create_app(foo=None, script_info=None):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 87why, one step back87 return Flask("appname")Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
line 88why, one step back88Merge pull request #3563 from pallets/deprecate-script-info-argDavid Lordbefore adoption0d04b40
lines 89–90why, one step back89 with pytest.deprecated_call(match="script_info"):90 app = find_best_app(script_info, Module)Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
line 91why, one step back91Merge pull request #3563 from pallets/deprecate-script-info-argDavid Lordbefore adoption0d04b40
lines 92–94why, one step back92 assert isinstance(app, Flask)93 assert app.name == "appname"94Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 95–97why, one step back95 class Module:96 @staticmethod97 def make_app():Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 98why, one step back98 return Flask("appname")Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
line 99why, one step back99Merge pull request #3563 from pallets/deprecate-script-info-argDavid Lordbefore adoption0d04b40
lines 100–102why, one step back100 app = find_best_app(script_info, Module)101 assert isinstance(app, Flask)102 assert app.name == "appname"Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 103–104why, one step back103104 class Module:Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 105why, one step back105 myapp = Flask("appname1")Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 106–108why, one step back106107 @staticmethod108 def create_app():Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 109why, one step back109 return Flask("appname2")Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 110–113why, one step back110111 assert find_best_app(script_info, Module) == Module.myapp112113 class Module:Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 114why, one step back114 myapp = Flask("appname1")Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 115–117why, one step back115116 @staticmethod117 def create_app():Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 118why, one step back118 return Flask("appname2")Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 119–127why, one step back119120 assert find_best_app(script_info, Module) == Module.myapp121122 class Module:123 pass124125 pytest.raises(NoAppException, find_best_app, script_info, Module)126127 class Module:Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 128–129why, one step back128 myapp1 = Flask("appname1")129 myapp2 = Flask("appname2")Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 130–135why, one step back130131 pytest.raises(NoAppException, find_best_app, script_info, Module)132133 class Module:134 @staticmethod135 def create_app(foo, bar):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 136why, one step back136 return Flask("appname2")Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 137–142why, one step back137138 pytest.raises(NoAppException, find_best_app, script_info, Module)139140 class Module:141 @staticmethod142 def create_app():Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 143why, one step back143 raise TypeError("bad bad factory!")Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 144–147why, one step back144145 pytest.raises(TypeError, find_best_app, script_info, Module)146147Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 148–149why, one step back148@pytest.mark.parametrize(149 "value,path,result",Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
line 150why, one step back150 (Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 151–175why, one step back151 ("test", cwd, "test"),152 ("test.py", cwd, "test"),153 ("a/test", os.path.join(cwd, "a"), "test"),154 ("test/__init__.py", cwd, "test"),155 ("test/__init__", cwd, "test"),156 # nested package157 (158 os.path.join(test_path, "cliapp", "inner1", "__init__"),159 test_path,160 "cliapp.inner1",161 ),162 (163 os.path.join(test_path, "cliapp", "inner1", "inner2"),164 test_path,165 "cliapp.inner1.inner2",166 ),167 # dotted name168 ("test.a.b", cwd, "test.a.b"),169 (os.path.join(test_path, "cliapp.app"), test_path, "cliapp.app"),170 # not a Python file, will be caught during import171 (172 os.path.join(test_path, "cliapp", "message.txt"),173 test_path,174 "cliapp.message.txt",175 ),Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
line 176why, one step back176 ),Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 177why, one step back177)Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 178–196why, one step back178def test_prepare_import(request, value, path, result):179 """Expect the correct path to be set and the correct import and app names180 to be returned.181182 :func:`prepare_exec_for_file` has a side effect where the parent directory183 of the given import is added to :data:`sys.path`. This is reset after the184 test runs.185 """186 original_path = sys.path[:]187188 def reset_path():189 sys.path[:] = original_path190191 request.addfinalizer(reset_path)192193 assert prepare_import(value) == result194 assert sys.path[0] == path195196Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 197–211why, one step back197@pytest.mark.parametrize(198 "iname,aname,result",199 (200 ("cliapp.app", None, "testapp"),201 ("cliapp.app", "testapp", "testapp"),202 ("cliapp.factory", None, "app"),203 ("cliapp.factory", "create_app", "app"),204 ("cliapp.factory", "create_app()", "app"),205 ("cliapp.factory", 'create_app2("foo", "bar")', "app2_foo_bar"),206 # trailing comma space207 ("cliapp.factory", 'create_app2("foo", "bar", )', "app2_foo_bar"),208 # strip whitespace209 ("cliapp.factory", " create_app () ", "app"),210 ),211)Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 212–216why, one step back212def test_locate_app(test_apps, iname, aname, result):213 info = ScriptInfo()214 assert locate_app(info, iname, aname).name == result215216Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 217–234why, one step back217@pytest.mark.parametrize(218 "iname,aname",219 (220 ("notanapp.py", None),221 ("cliapp/app", None),222 ("cliapp.app", "notanapp"),223 # not enough arguments224 ("cliapp.factory", 'create_app2("foo")'),225 # invalid identifier226 ("cliapp.factory", "create_app("),227 # no app returned228 ("cliapp.factory", "no_app"),229 # nested import error230 ("cliapp.importerrorapp", None),231 # not a Python file232 ("cliapp.message.txt", None),233 ),234)Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 235–241why, one step back235def test_locate_app_raises(test_apps, iname, aname):236 info = ScriptInfo()237238 with pytest.raises(NoAppException):239 locate_app(info, iname, aname)240241Merge pull request #3711 from pallets/cli-loading-errorDavid Lordbefore adoption6638432
line 242why, one step back242def test_locate_app_suppress_raise(test_apps):Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
line 243why, one step back243 info = ScriptInfo()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 244why, one step back244 app = locate_app(info, "notanapp.py", None, raise_if_not_found=False)Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 245–248why, one step back245 assert app is None246247 # only direct import error is suppressed248 with pytest.raises(NoAppException):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 249why, one step back249 locate_app(info, "cliapp.importerrorapp", None, raise_if_not_found=False)Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 250–256why, one step back250251252def test_get_version(test_apps, capsys):253 from flask import __version__ as flask_version254 from werkzeug import __version__ as werkzeug_version255 from platform import python_version256Merge pull request #3554 from pallets/drop-python2David Lordbefore adoptioncd8a374
line 257why, one step back257 class MockCtx:Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 258–260why, one step back258 resilient_parsing = False259 color = None260Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 261–262why, one step back261 def exit(self):262 returnMerge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 263–266why, one step back263264 ctx = MockCtx()265 get_version(ctx, None, "test")266 out, err = capsys.readouterr()Merge pull request #3554 from pallets/drop-python2David Lordbefore adoptioncd8a374
lines 267–269why, one step back267 assert f"Python {python_version()}" in out268 assert f"Flask {flask_version}" in out269 assert f"Werkzeug {werkzeug_version}" in outMerge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 270–278why, one step back270271272def test_scriptinfo(test_apps, monkeypatch):273 obj = ScriptInfo(app_import_path="cliapp.app:testapp")274 app = obj.load_app()275 assert app.name == "testapp"276 assert obj.load_app() is app277278 # import app with module's absolute pathMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 279–281why, one step back279 cli_app_path = os.path.abspath(280 os.path.join(os.path.dirname(__file__), "test_apps", "cliapp", "app.py")281 )Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 282–283why, one step back282 obj = ScriptInfo(app_import_path=cli_app_path)283 app = obj.load_app()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 284why, one step back284 assert app.name == "testapp"Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
line 285why, one step back285 assert obj.load_app() is appMerge pull request #3554 from pallets/drop-python2David Lordbefore adoptioncd8a374
line 286why, one step back286 obj = ScriptInfo(app_import_path=f"{cli_app_path}:testapp")Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
line 287why, one step back287 app = obj.load_app()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 288why, one step back288 assert app.name == "testapp"Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 289–290why, one step back289 assert obj.load_app() is app290Merge pull request #3563 from pallets/deprecate-script-info-argDavid Lordbefore adoption0d04b40
line 291why, one step back291 def create_app():Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 292–302why, one step back292 return Flask("createapp")293294 obj = ScriptInfo(create_app=create_app)295 app = obj.load_app()296 assert app.name == "createapp"297 assert obj.load_app() is app298299 obj = ScriptInfo()300 pytest.raises(NoAppException, obj.load_app)301302 # import app from wsgi.py in current directoryMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 303–307why, one step back303 monkeypatch.chdir(304 os.path.abspath(305 os.path.join(os.path.dirname(__file__), "test_apps", "helloworld")306 )307 )Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 308–309why, one step back308 obj = ScriptInfo()309 app = obj.load_app()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 310why, one step back310 assert app.name == "hello"Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 311–312why, one step back311312 # import app from app.py in current directoryMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 313–315why, one step back313 monkeypatch.chdir(314 os.path.abspath(os.path.join(os.path.dirname(__file__), "test_apps", "cliapp"))315 )Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 316–317why, one step back316 obj = ScriptInfo()317 app = obj.load_app()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 318why, one step back318 assert app.name == "testapp"Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 319–326why, one step back319320321def test_with_appcontext(runner):322 @click.command()323 @with_appcontext324 def testcmd():325 click.echo(current_app.name)326Merge pull request #3563 from pallets/deprecate-script-info-argDavid Lordbefore adoption0d04b40
line 327why, one step back327 obj = ScriptInfo(create_app=lambda: Flask("testapp"))Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 328–330why, one step back328329 result = runner.invoke(testcmd, obj=obj)330 assert result.exit_code == 0Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 331why, one step back331 assert result.output == "testapp\n"Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 332–350why, one step back332333334def test_appgroup(runner):335 @click.group(cls=AppGroup)336 def cli():337 pass338339 @cli.command(with_appcontext=True)340 def test():341 click.echo(current_app.name)342343 @cli.group()344 def subgroup():345 pass346347 @subgroup.command(with_appcontext=True)348 def test2():349 click.echo(current_app.name)350Merge pull request #3563 from pallets/deprecate-script-info-argDavid Lordbefore adoption0d04b40
line 351why, one step back351 obj = ScriptInfo(create_app=lambda: Flask("testappgroup"))Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
line 352why, one step back352Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 353why, one step back353 result = runner.invoke(cli, ["test"], obj=obj)Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
line 354why, one step back354 assert result.exit_code == 0Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 355why, one step back355 assert result.output == "testappgroup\n"Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
line 356why, one step back356Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 357why, one step back357 result = runner.invoke(cli, ["subgroup", "test2"], obj=obj)Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
line 358why, one step back358 assert result.exit_code == 0Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 359why, one step back359 assert result.output == "testappgroup\n"Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 360–362why, one step back360361362def test_flaskgroup(runner):Merge pull request #3563 from pallets/deprecate-script-info-argDavid Lordbefore adoption0d04b40
line 363why, one step back363 def create_app():Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 364–373why, one step back364 return Flask("flaskgroup")365366 @click.group(cls=FlaskGroup, create_app=create_app)367 def cli(**params):368 pass369370 @cli.command()371 def test():372 click.echo(current_app.name)373Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 374why, one step back374 result = runner.invoke(cli, ["test"])Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
line 375why, one step back375 assert result.exit_code == 0Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 376why, one step back376 assert result.output == "flaskgroup\n"Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 377–378why, one step back377378Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 379why, one step back379@pytest.mark.parametrize("set_debug_flag", (True, False))Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
line 380why, one step back380def test_flaskgroup_debug(runner, set_debug_flag):Merge pull request #3563 from pallets/deprecate-script-info-argDavid Lordbefore adoption0d04b40
line 381why, one step back381 def create_app():Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 382–393why, one step back382 app = Flask("flaskgroup")383 app.debug = True384 return app385386 @click.group(cls=FlaskGroup, create_app=create_app, set_debug_flag=set_debug_flag)387 def cli(**params):388 pass389390 @cli.command()391 def test():392 click.echo(str(current_app.debug))393Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 394why, one step back394 result = runner.invoke(cli, ["test"])Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
line 395why, one step back395 assert result.exit_code == 0Merge pull request #3554 from pallets/drop-python2David Lordbefore adoptioncd8a374
line 396why, one step back396 assert result.output == f"{not set_debug_flag}\n"Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 397–398why, one step back397398Merge pull request #3711 from pallets/cli-loading-errorDavid Lordbefore adoption6638432
lines 399–400why, one step back399def test_no_command_echo_loading_error():400 from flask.cli import cliMerge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
line 401why, one step back401Merge pull request #3711 from pallets/cli-loading-errorDavid Lordbefore adoption6638432
lines 402–419why, one step back402 runner = CliRunner(mix_stderr=False)403 result = runner.invoke(cli, ["missing"])404 assert result.exit_code == 2405 assert "FLASK_APP" in result.stderr406 assert "Usage:" in result.stderr407408409def test_help_echo_loading_error():410 from flask.cli import cli411412 runner = CliRunner(mix_stderr=False)413 result = runner.invoke(cli, ["--help"])414 assert result.exit_code == 0415 assert "FLASK_APP" in result.stderr416 assert "Usage:" in result.stdout417418419def test_help_echo_exception():Merge pull request #3563 from pallets/deprecate-script-info-argDavid Lordbefore adoption0d04b40
line 420why, one step back420 def create_app():Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 421–422why, one step back421 raise Exception("oh no")422Merge pull request #3711 from pallets/cli-loading-errorDavid Lordbefore adoption6638432
lines 423–424why, one step back423 cli = FlaskGroup(create_app=create_app)424 runner = CliRunner(mix_stderr=False)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 425why, one step back425 result = runner.invoke(cli, ["--help"])Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
line 426why, one step back426 assert result.exit_code == 0Merge pull request #3711 from pallets/cli-loading-errorDavid Lordbefore adoption6638432
lines 427–428why, one step back427 assert "Exception: oh no" in result.stderr428 assert "Usage:" in result.stdoutMerge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 429–433why, one step back429430431class TestRoutes:432 @pytest.fixture433 def invoke(self, runner):Merge pull request #3563 from pallets/deprecate-script-info-argDavid Lordbefore adoption0d04b40
line 434why, one step back434 def create_app():Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 435–437why, one step back435 app = Flask(__name__)436 app.testing = True437Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 438why, one step back438 @app.route("/get_post/<int:x>/<int:y>", methods=["GET", "POST"])Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 439–441why, one step back439 def yyy_get_post(x, y):440 pass441Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 442why, one step back442 @app.route("/zzz_post", methods=["POST"])Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 443–452why, one step back443 def aaa_post():444 pass445446 return app447448 cli = FlaskGroup(create_app=create_app)449 return partial(runner.invoke, cli)450451 @pytest.fixture452 def invoke_no_routes(self, runner):Merge pull request #3563 from pallets/deprecate-script-info-argDavid Lordbefore adoption0d04b40
line 453why, one step back453 def create_app():Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 454–465why, one step back454 app = Flask(__name__, static_folder=None)455 app.testing = True456457 return app458459 cli = FlaskGroup(create_app=create_app)460 return partial(runner.invoke, cli)461462 def expect_order(self, order, output):463 # skip the header and match the start of each row464 for expect, line in zip(order, output.splitlines()[2:]):465 # do this instead of startswith for nicer pytest outputMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 466why, one step back466 assert line[: len(expect)] == expectMerge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 467–468why, one step back467468 def test_simple(self, invoke):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 469why, one step back469 result = invoke(["routes"])Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
line 470why, one step back470 assert result.exit_code == 0Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 471why, one step back471 self.expect_order(["aaa_post", "static", "yyy_get_post"], result.output)Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 472–473why, one step back472473 def test_sort(self, invoke):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 474–475why, one step back474 default_output = invoke(["routes"]).output475 endpoint_output = invoke(["routes", "-s", "endpoint"]).outputMerge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 476–477why, one step back476 assert default_output == endpoint_output477 self.expect_order(Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 478–479why, one step back478 ["static", "yyy_get_post", "aaa_post"],479 invoke(["routes", "-s", "methods"]).output,Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 480–481why, one step back480 )481 self.expect_order(Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 482–483why, one step back482 ["yyy_get_post", "static", "aaa_post"],483 invoke(["routes", "-s", "rule"]).output,Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 484–485why, one step back484 )485 self.expect_order(Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 486–487why, one step back486 ["aaa_post", "yyy_get_post", "static"],487 invoke(["routes", "-s", "match"]).output,Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 488–490why, one step back488 )489490 def test_all_methods(self, invoke):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 491–494why, one step back491 output = invoke(["routes"]).output492 assert "GET, HEAD, OPTIONS, POST" not in output493 output = invoke(["routes", "--all-methods"]).output494 assert "GET, HEAD, OPTIONS, POST" in outputMerge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 495–496why, one step back495496 def test_no_routes(self, invoke_no_routes):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 497why, one step back497 result = invoke_no_routes(["routes"])Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
line 498why, one step back498 assert result.exit_code == 0Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 499why, one step back499 assert "No routes were registered." in result.outputMerge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 500–501why, one step back500501Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 502why, one step back502need_dotenv = pytest.mark.skipif(dotenv is None, reason="dotenv is not installed")Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 503–507why, one step back503504505@need_dotenv506def test_load_dotenv(monkeypatch):507 # can't use monkeypatch.delitem since the keys don't exist yetMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 508why, one step back508 for item in ("FOO", "BAR", "SPAM"):Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 509–510why, one step back509 monkeypatch._setitem.append((os.environ, item, notset))510Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 511why, one step back511 monkeypatch.setenv("EGGS", "3")Merge pull request #3560 from greyli/fix-env-chdirJoshua Bronsonbefore adoptiona40c381
line 512why, one step back512 monkeypatch.chdir(test_path)Merge pull request #2939 from sharmaadarsh563/2937-correct-load_dotenv-return-valueDavid Lordbefore adoptiond3e1fed
line 513why, one step back513 assert load_dotenv()Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 514–515why, one step back514 assert os.getcwd() == test_path515 # .flaskenv doesn't overwrite .envMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 516why, one step back516 assert os.environ["FOO"] == "env"Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
line 517why, one step back517 # set only in .flaskenvMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 518why, one step back518 assert os.environ["BAR"] == "bar"Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
line 519why, one step back519 # set only in .envMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 520why, one step back520 assert os.environ["SPAM"] == "1"Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
line 521why, one step back521 # set manually, files don't overwriteMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 522why, one step back522 assert os.environ["EGGS"] == "3"Merge pull request #2939 from sharmaadarsh563/2937-correct-load_dotenv-return-valueDavid Lordbefore adoptiond3e1fed
lines 523–524why, one step back523524 # Non existent file should not loadMerge pull request #3229 from lordmauve/style-fixDavid Lordbefore adoption240e398
line 525why, one step back525 assert not load_dotenv("non-existent-file")Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 526–529why, one step back526527528@need_dotenv529def test_dotenv_path(monkeypatch):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 530why, one step back530 for item in ("FOO", "BAR", "EGGS"):Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 531–533why, one step back531 monkeypatch._setitem.append((os.environ, item, notset))532533 cwd = os.getcwd()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 534why, one step back534 load_dotenv(os.path.join(test_path, ".flaskenv"))Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
line 535why, one step back535 assert os.getcwd() == cwdMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 536why, one step back536 assert "FOO" in os.environMerge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 537–539why, one step back537538539def test_dotenv_optional(monkeypatch):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 540why, one step back540 monkeypatch.setattr("flask.cli.dotenv", None)Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 541–542why, one step back541 monkeypatch.chdir(test_path)542 load_dotenv()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 543why, one step back543 assert "FOO" not in os.environMerge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 544–548why, one step back544545546@need_dotenv547def test_disable_dotenv_from_env(monkeypatch, runner):548 monkeypatch.chdir(test_path)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 549why, one step back549 monkeypatch.setitem(os.environ, "FLASK_SKIP_DOTENV", "1")Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
line 550why, one step back550 runner.invoke(FlaskGroup())Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 551why, one step back551 assert "FOO" not in os.environMerge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 552–556why, one step back552553554def test_run_cert_path():555 # no key556 with pytest.raises(click.BadParameter):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 557why, one step back557 run_command.make_context("run", ["--cert", __file__])Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 558–560why, one step back558559 # no cert560 with pytest.raises(click.BadParameter):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 561why, one step back561 run_command.make_context("run", ["--key", __file__])Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
line 562why, one step back562Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 563–564why, one step back563 ctx = run_command.make_context("run", ["--cert", __file__, "--key", __file__])564 assert ctx.params["cert"] == (__file__, __file__)Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 565–567why, one step back565566567def test_run_cert_adhoc(monkeypatch):Merge pull request #3492 from pallets/cryptographyDavid Lordbefore adoptionb074a2e
line 568why, one step back568 monkeypatch.setitem(sys.modules, "cryptography", None)Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
line 569why, one step back569Merge pull request #3492 from pallets/cryptographyDavid Lordbefore adoptionb074a2e
line 570why, one step back570 # cryptography not installedMerge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
line 571why, one step back571 with pytest.raises(click.BadParameter):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 572why, one step back572 run_command.make_context("run", ["--cert", "adhoc"])Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
line 573why, one step back573Merge pull request #3492 from pallets/cryptographyDavid Lordbefore adoptionb074a2e
lines 574–575why, one step back574 # cryptography installed575 monkeypatch.setitem(sys.modules, "cryptography", types.ModuleType("cryptography"))Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 576–577why, one step back576 ctx = run_command.make_context("run", ["--cert", "adhoc"])577 assert ctx.params["cert"] == "adhoc"Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 578–580why, one step back578579 # no key with adhoc580 with pytest.raises(click.BadParameter):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 581why, one step back581 run_command.make_context("run", ["--cert", "adhoc", "--key", __file__])Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 582–584why, one step back582583584def test_run_cert_import(monkeypatch):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 585why, one step back585 monkeypatch.setitem(sys.modules, "not_here", None)Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 586–588why, one step back586587 # ImportError588 with pytest.raises(click.BadParameter):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 589why, one step back589 run_command.make_context("run", ["--cert", "not_here"])Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
line 590why, one step back590Merge pull request #3554 from pallets/drop-python2David Lordbefore adoptioncd8a374
lines 591–592why, one step back591 with pytest.raises(click.BadParameter):592 run_command.make_context("run", ["--cert", "flask"])Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 593–594why, one step back593594 # SSLContextMerge pull request #3554 from pallets/drop-python2David Lordbefore adoptioncd8a374
line 595why, one step back595 ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)Merge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
line 596why, one step back596Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 597–599why, one step back597 monkeypatch.setitem(sys.modules, "ssl_context", ssl_context)598 ctx = run_command.make_context("run", ["--cert", "ssl_context"])599 assert ctx.params["cert"] is ssl_contextMerge pull request #3103 from tbm/typosDavid Lordbefore adoption86bf9dc
lines 600–602why, one step back600601 # no --key with SSLContext602 with pytest.raises(click.BadParameter):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 603why, one step back603 run_command.make_context("run", ["--cert", "ssl_context", "--key", __file__])Merge pull request #2784 from doobeh/masterDavid Lordbefore adoptionb837606
lines 604–605why, one step back604605Merge pull request #3242 from cslecrone/3211-ssl-importDavid Lordbefore adoption13b689b
lines 606–611why, one step back606def test_run_cert_no_ssl(monkeypatch):607 monkeypatch.setattr("flask.cli.ssl", None)608 with pytest.raises(click.BadParameter):609 run_command.make_context("run", ["--cert", "not_here"])610611Merge pull request #2784 from doobeh/masterDavid Lordbefore adoptionb837606
lines 612–652why, one step back612def test_cli_blueprints(app):613 """Test blueprint commands register correctly to the application"""614 custom = Blueprint("custom", __name__, cli_group="customized")615 nested = Blueprint("nested", __name__)616 merged = Blueprint("merged", __name__, cli_group=None)617 late = Blueprint("late", __name__)618619 @custom.cli.command("custom")620 def custom_command():621 click.echo("custom_result")622623 @nested.cli.command("nested")624 def nested_command():625 click.echo("nested_result")626627 @merged.cli.command("merged")628 def merged_command():629 click.echo("merged_result")630631 @late.cli.command("late")632 def late_command():633 click.echo("late_result")634635 app.register_blueprint(custom)636 app.register_blueprint(nested)637 app.register_blueprint(merged)638 app.register_blueprint(late, cli_group="late_registration")639640 app_runner = app.test_cli_runner()641642 result = app_runner.invoke(args=["customized", "custom"])643 assert "custom_result" in result.output644645 result = app_runner.invoke(args=["nested", "nested"])646 assert "nested_result" in result.output647648 result = app_runner.invoke(args=["merged"])649 assert "merged_result" in result.output650651 result = app_runner.invoke(args=["late_registration", "late"])652 assert "late_result" in result.outputMerge pull request #3236 from lordmauve/no-cligroup-emptyDavid Lordbefore adoption91e53da
lines 653–660why, one step back653654655def test_cli_empty(app):656 """If a Blueprint's CLI group is empty, do not register it."""657 bp = Blueprint("blue", __name__, cli_group="blue")658 app.register_blueprint(bp)659660 result = app.test_cli_runner().invoke(args=["blue", "--help"])Merge pull request #3554 from pallets/drop-python2David Lordbefore adoptioncd8a374
line 661why, one step back661 assert result.exit_code == 2, f"Unexpected success:\n\n{result.output}"