test_cli.py
This file
Path
tests/test_cli.py
654 lines · 18.1 KiB · Python
Last changed by
Merge pull request #2784 from doobeh/masterDavid Lordbefore adoptionb837606
History of this fileTake it
The walk stopped after 200 commits: lines older than 817903c point at it.
Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 1–5why, one step back1# -*- coding: utf-8 -*-2"""3 tests.test_cli4 ~~~~~~~~~~~~~~5Merge pull request #2624 from pallets/metadataDavid Lordbefore adoption4309c7c
line 6why, one step back6 :copyright: © 2010 by the Pallets team.Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 7–8why, one step back7 :license: BSD, see LICENSE for more details.8"""Merge pull request #2624 from pallets/metadataDavid Lordbefore adoption4309c7c
lines 9–12why, one step back910# This file was part of Flask-CLI and was modified under the terms of11# its Revised BSD License. Copyright © 2015 CERN.12Merge pull request #2414 from davidism/cli-load-local-packageDavid Lordbefore adoption77b98a2
lines 13–14why, one step back13from __future__ import absolute_import14Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
line 15why, one step back15import osMerge pull request #2606 from pallets/run-sslDavid Lordbefore adoption8ff0cef
line 16why, one step back16import sslMerge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
line 17why, one step back17import sysMerge pull request #2606 from pallets/run-sslDavid Lordbefore adoption8ff0cef
line 18why, one step back18import typesMerge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 19–22why, one step back19from functools import partial2021import click22import pytestMerge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4
line 23why, one step back23from _pytest.monkeypatch import notsetMerge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
line 24why, one step back24from click.testing import CliRunnerMerge pull request #2414 from davidism/cli-load-local-packageDavid Lordbefore adoption77b98a2
line 25why, one step back25Merge pull request #2784 from doobeh/masterDavid Lordbefore adoptionb837606
line 26why, one step back26from flask import Flask, current_app, BlueprintMerge pull request #2592 from pallets/tests-envDavid Lordbefore adoption2949306
line 27why, one step back27from flask.cli import (Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 28–39why, one step back28 AppGroup,29 FlaskGroup,30 NoAppException,31 ScriptInfo,32 dotenv,33 find_best_app,34 get_version,35 load_dotenv,36 locate_app,37 prepare_import,38 run_command,39 with_appcontext,Merge pull request #2592 from pallets/tests-envDavid Lordbefore adoption2949306
line 40why, one step back40)Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
line 41why, one step back41Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4
line 42why, one step back42cwd = os.getcwd()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 43why, one step back43test_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "test_apps"))Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4
lines 44–45why, one step back4445Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 46–53why, one step back46@pytest.fixture47def runner():48 return CliRunner()495051def test_cli_name(test_apps):52 """Make sure the CLI object's name is the app's name and not the app itself"""53 from cliapp.app import testappMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 54why, one step back54Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 55–62why, one step back55 assert testapp.cli.name == testapp.name565758def test_find_best_app(test_apps):59 """Test if `find_best_app` behaves as expected with different combinations of input."""60 script_info = ScriptInfo()6162 class Module:Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 63why, one step back63 app = Flask("appname")Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 64–67why, one step back6465 assert find_best_app(script_info, Module) == Module.app6667 class Module:Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 68why, one step back68 application = Flask("appname")Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 69–72why, one step back6970 assert find_best_app(script_info, Module) == Module.application7172 class Module:Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 73why, one step back73 myapp = Flask("appname")Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 74–79why, one step back7475 assert find_best_app(script_info, Module) == Module.myapp7677 class Module:78 @staticmethod79 def create_app():Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 80why, one step back80 return Flask("appname")Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 81–82why, one step back8182 assert isinstance(find_best_app(script_info, Module), Flask)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 83why, one step back83 assert find_best_app(script_info, Module).name == "appname"Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 84–87why, one step back8485 class Module:86 @staticmethod87 def create_app(foo):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 88why, one step back88 return Flask("appname")Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 89–90why, one step back8990 assert isinstance(find_best_app(script_info, Module), Flask)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 91why, one step back91 assert find_best_app(script_info, Module).name == "appname"Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 92–95why, one step back9293 class Module:94 @staticmethod95 def create_app(foo=None, script_info=None):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 96why, one step back96 return Flask("appname")Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 97–98why, one step back9798 assert isinstance(find_best_app(script_info, Module), Flask)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 99why, one step back99 assert find_best_app(script_info, Module).name == "appname"Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 100–103why, one step back100101 class Module:102 @staticmethod103 def make_app():Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 104why, one step back104 return Flask("appname")Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 105–106why, one step back105106 assert isinstance(find_best_app(script_info, Module), Flask)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 107why, one step back107 assert find_best_app(script_info, Module).name == "appname"Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 108–109why, one step back108109 class Module:Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 110why, one step back110 myapp = Flask("appname1")Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 111–113why, one step back111112 @staticmethod113 def create_app():Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 114why, one step back114 return Flask("appname2")Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 115–118why, one step back115116 assert find_best_app(script_info, Module) == Module.myapp117118 class Module:Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 119why, one step back119 myapp = Flask("appname1")Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 120–122why, one step back120121 @staticmethod122 def create_app():Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 123why, one step back123 return Flask("appname2")Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 124–132why, one step back124125 assert find_best_app(script_info, Module) == Module.myapp126127 class Module:128 pass129130 pytest.raises(NoAppException, find_best_app, script_info, Module)131132 class Module:Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 133–134why, one step back133 myapp1 = Flask("appname1")134 myapp2 = Flask("appname2")Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 135–140why, one step back135136 pytest.raises(NoAppException, find_best_app, script_info, Module)137138 class Module:139 @staticmethod140 def create_app(foo, bar):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 141why, one step back141 return Flask("appname2")Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 142–144why, one step back142143 pytest.raises(NoAppException, find_best_app, script_info, Module)144Merge pull request #2530 from pallets/feature/factory-detectionDavid Lordbefore adoption03a2996
lines 145–147why, one step back145 class Module:146 @staticmethod147 def create_app():Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 148why, one step back148 raise TypeError("bad bad factory!")Merge pull request #2530 from pallets/feature/factory-detectionDavid Lordbefore adoption03a2996
lines 149–151why, one step back149150 pytest.raises(TypeError, find_best_app, script_info, Module)151Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
line 152why, one step back152Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 153–154why, one step back153@pytest.mark.parametrize(154 "value,path,result",Merge pull request #2414 from davidism/cli-load-local-packageDavid Lordbefore adoption77b98a2
line 155why, one step back155 (Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 156–180why, one step back156 ("test", cwd, "test"),157 ("test.py", cwd, "test"),158 ("a/test", os.path.join(cwd, "a"), "test"),159 ("test/__init__.py", cwd, "test"),160 ("test/__init__", cwd, "test"),161 # nested package162 (163 os.path.join(test_path, "cliapp", "inner1", "__init__"),164 test_path,165 "cliapp.inner1",166 ),167 (168 os.path.join(test_path, "cliapp", "inner1", "inner2"),169 test_path,170 "cliapp.inner1.inner2",171 ),172 # dotted name173 ("test.a.b", cwd, "test.a.b"),174 (os.path.join(test_path, "cliapp.app"), test_path, "cliapp.app"),175 # not a Python file, will be caught during import176 (177 os.path.join(test_path, "cliapp", "message.txt"),178 test_path,179 "cliapp.message.txt",180 ),Merge pull request #2414 from davidism/cli-load-local-packageDavid Lordbefore adoption77b98a2
line 181why, one step back181 ),Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 182why, one step back182)Merge pull request #2414 from davidism/cli-load-local-packageDavid Lordbefore adoption77b98a2
lines 183–189why, one step back183def test_prepare_import(request, value, path, result):184 """Expect the correct path to be set and the correct import and app names185 to be returned.186187 :func:`prepare_exec_for_file` has a side effect where the parent directory188 of the given import is added to :data:`sys.path`. This is reset after the189 test runs.Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
line 190why, one step back190 """Merge pull request #2414 from davidism/cli-load-local-packageDavid Lordbefore adoption77b98a2
line 191why, one step back191 original_path = sys.path[:]Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
line 192why, one step back192Merge pull request #2414 from davidism/cli-load-local-packageDavid Lordbefore adoption77b98a2
lines 193–194why, one step back193 def reset_path():194 sys.path[:] = original_pathMerge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
line 195why, one step back195Merge pull request #2414 from davidism/cli-load-local-packageDavid Lordbefore adoption77b98a2
line 196why, one step back196 request.addfinalizer(reset_path)Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
line 197why, one step back197Merge pull request #2414 from davidism/cli-load-local-packageDavid Lordbefore adoption77b98a2
lines 198–199why, one step back198 assert prepare_import(value) == result199 assert sys.path[0] == pathMerge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 200–201why, one step back200201Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 202–219why, one step back202@pytest.mark.parametrize(203 "iname,aname,result",204 (205 ("cliapp.app", None, "testapp"),206 ("cliapp.app", "testapp", "testapp"),207 ("cliapp.factory", None, "app"),208 ("cliapp.factory", "create_app", "app"),209 ("cliapp.factory", "create_app()", "app"),210 # no script_info211 ("cliapp.factory", 'create_app2("foo", "bar")', "app2_foo_bar"),212 # trailing comma space213 ("cliapp.factory", 'create_app2("foo", "bar", )', "app2_foo_bar"),214 # takes script_info215 ("cliapp.factory", 'create_app3("foo")', "app3_foo_spam"),216 # strip whitespace217 ("cliapp.factory", " create_app () ", "app"),218 ),219)Merge pull request #2414 from davidism/cli-load-local-packageDavid Lordbefore adoption77b98a2
lines 220–221why, one step back220def test_locate_app(test_apps, iname, aname, result):221 info = ScriptInfo()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 222why, one step back222 info.data["test"] = "spam"Merge pull request #2414 from davidism/cli-load-local-packageDavid Lordbefore adoption77b98a2
line 223why, one step back223 assert locate_app(info, iname, aname).name == resultMerge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
line 224why, one step back224Merge pull request #2414 from davidism/cli-load-local-packageDavid Lordbefore adoption77b98a2
line 225why, one step back225Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 226–243why, one step back226@pytest.mark.parametrize(227 "iname,aname",228 (229 ("notanapp.py", None),230 ("cliapp/app", None),231 ("cliapp.app", "notanapp"),232 # not enough arguments233 ("cliapp.factory", 'create_app2("foo")'),234 # invalid identifier235 ("cliapp.factory", "create_app("),236 # no app returned237 ("cliapp.factory", "no_app"),238 # nested import error239 ("cliapp.importerrorapp", None),240 # not a Python file241 ("cliapp.message.txt", None),242 ),243)Merge pull request #2414 from davidism/cli-load-local-packageDavid Lordbefore adoption77b98a2
lines 244–252why, one step back244def test_locate_app_raises(test_apps, iname, aname):245 info = ScriptInfo()246247 with pytest.raises(NoAppException):248 locate_app(info, iname, aname)249250251def test_locate_app_suppress_raise():252 info = ScriptInfo()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 253why, one step back253 app = locate_app(info, "notanapp.py", None, raise_if_not_found=False)Merge pull request #2414 from davidism/cli-load-local-packageDavid Lordbefore adoption77b98a2
lines 254–257why, one step back254 assert app is None255256 # only direct import error is suppressed257 with pytest.raises(NoAppException):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 258why, one step back258 locate_app(info, "cliapp.importerrorapp", None, raise_if_not_found=False)Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 259–261why, one step back259260261def test_get_version(test_apps, capsys):Merge branch '1.0-maintenance'David Lordbefore adoption4f3dbb3
lines 262–264why, one step back262 from flask import __version__ as flask_version263 from werkzeug import __version__ as werkzeug_version264 from platform import python_versionMerge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 265–269why, one step back265266 class MockCtx(object):267 resilient_parsing = False268 color = None269Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 270–271why, one step back270 def exit(self):271 returnMerge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 272–275why, one step back272273 ctx = MockCtx()274 get_version(ctx, None, "test")275 out, err = capsys.readouterr()Merge branch '1.0-maintenance'David Lordbefore adoption4f3dbb3
lines 276–278why, one step back276 assert "Python " + python_version() in out277 assert "Flask " + flask_version in out278 assert "Werkzeug " + werkzeug_version in outMerge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 279–283why, one step back279280281def test_scriptinfo(test_apps, monkeypatch):282 """Test of ScriptInfo."""283 obj = ScriptInfo(app_import_path="cliapp.app:testapp")Merge branch '1.0-maintenance'David Lordbefore adoptionf7d50d4
lines 284–286why, one step back284 app = obj.load_app()285 assert app.name == "testapp"286 assert obj.load_app() is appMerge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
line 287why, one step back287Merge branch '1.0-maintenance'David Lordbefore adoptionf7d50d4
line 288why, one step back288 # import app with module's absolute pathMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 289–291why, one step back289 cli_app_path = os.path.abspath(290 os.path.join(os.path.dirname(__file__), "test_apps", "cliapp", "app.py")291 )Merge branch '1.0-maintenance'David Lordbefore adoptionf7d50d4
lines 292–293why, one step back292 obj = ScriptInfo(app_import_path=cli_app_path)293 app = obj.load_app()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 294why, one step back294 assert app.name == "testapp"Merge branch '1.0-maintenance'David Lordbefore adoptionf7d50d4
line 295why, one step back295 assert obj.load_app() is appMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 296why, one step back296 obj = ScriptInfo(app_import_path=cli_app_path + ":testapp")Merge branch '1.0-maintenance'David Lordbefore adoptionf7d50d4
line 297why, one step back297 app = obj.load_app()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 298why, one step back298 assert app.name == "testapp"Merge branch '1.0-maintenance'David Lordbefore adoptionf7d50d4
lines 299–300why, one step back299 assert obj.load_app() is app300Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 301–306why, one step back301 def create_app(info):302 return Flask("createapp")303304 obj = ScriptInfo(create_app=create_app)305 app = obj.load_app()306 assert app.name == "createapp"Merge branch '1.0-maintenance'David Lordbefore adoptionf7d50d4
line 307why, one step back307 assert obj.load_app() is appMerge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 308–312why, one step back308309 obj = ScriptInfo()310 pytest.raises(NoAppException, obj.load_app)311312 # import app from wsgi.py in current directoryMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 313–317why, one step back313 monkeypatch.chdir(314 os.path.abspath(315 os.path.join(os.path.dirname(__file__), "test_apps", "helloworld")316 )317 )Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 318–319why, one step back318 obj = ScriptInfo()319 app = obj.load_app()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 320why, one step back320 assert app.name == "hello"Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 321–322why, one step back321322 # import app from app.py in current directoryMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 323–325why, one step back323 monkeypatch.chdir(324 os.path.abspath(os.path.join(os.path.dirname(__file__), "test_apps", "cliapp"))325 )Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 326–327why, one step back326 obj = ScriptInfo()327 app = obj.load_app()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 328why, one step back328 assert app.name == "testapp"Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 329–342why, one step back329330331def test_with_appcontext(runner):332 """Test of with_appcontext."""333334 @click.command()335 @with_appcontext336 def testcmd():337 click.echo(current_app.name)338339 obj = ScriptInfo(create_app=lambda info: Flask("testapp"))340341 result = runner.invoke(testcmd, obj=obj)342 assert result.exit_code == 0Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 343why, one step back343 assert result.output == "testapp\n"Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 344–366why, one step back344345346def test_appgroup(runner):347 """Test of with_appcontext."""348349 @click.group(cls=AppGroup)350 def cli():351 pass352353 @cli.command(with_appcontext=True)354 def test():355 click.echo(current_app.name)356357 @cli.group()358 def subgroup():359 pass360361 @subgroup.command(with_appcontext=True)362 def test2():363 click.echo(current_app.name)364365 obj = ScriptInfo(create_app=lambda info: Flask("testappgroup"))366Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 367why, one step back367 result = runner.invoke(cli, ["test"], obj=obj)Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
line 368why, one step back368 assert result.exit_code == 0Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 369why, one step back369 assert result.output == "testappgroup\n"Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
line 370why, one step back370Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 371why, one step back371 result = runner.invoke(cli, ["subgroup", "test2"], obj=obj)Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
line 372why, one step back372 assert result.exit_code == 0Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 373why, one step back373 assert result.output == "testappgroup\n"Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 374–389why, one step back374375376def test_flaskgroup(runner):377 """Test FlaskGroup."""378379 def create_app(info):380 return Flask("flaskgroup")381382 @click.group(cls=FlaskGroup, create_app=create_app)383 def cli(**params):384 pass385386 @cli.command()387 def test():388 click.echo(current_app.name)389Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 390why, one step back390 result = runner.invoke(cli, ["test"])Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
line 391why, one step back391 assert result.exit_code == 0Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 392why, one step back392 assert result.output == "flaskgroup\n"Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 393–394why, one step back393394Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 395why, one step back395@pytest.mark.parametrize("set_debug_flag", (True, False))Merge branch '1.0-maintenance'ThiefMasterbefore adoption161c436
lines 396–411why, one step back396def test_flaskgroup_debug(runner, set_debug_flag):397 """Test FlaskGroup debug flag behavior."""398399 def create_app(info):400 app = Flask("flaskgroup")401 app.debug = True402 return app403404 @click.group(cls=FlaskGroup, create_app=create_app, set_debug_flag=set_debug_flag)405 def cli(**params):406 pass407408 @cli.command()409 def test():410 click.echo(str(current_app.debug))411Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 412why, one step back412 result = runner.invoke(cli, ["test"])Merge branch '1.0-maintenance'ThiefMasterbefore adoption161c436
line 413why, one step back413 assert result.exit_code == 0Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 414why, one step back414 assert result.output == "%s\n" % str(not set_debug_flag)Merge branch '1.0-maintenance'ThiefMasterbefore adoption161c436
lines 415–416why, one step back415416Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 417–427why, one step back417def test_print_exceptions(runner):418 """Print the stacktrace if the CLI."""419420 def create_app(info):421 raise Exception("oh no")422 return Flask("flaskgroup")423424 @click.group(cls=FlaskGroup, create_app=create_app)425 def cli(**params):426 pass427Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 428why, one step back428 result = runner.invoke(cli, ["--help"])Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
line 429why, one step back429 assert result.exit_code == 0Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 430–431why, one step back430 assert "Exception: oh no" in result.output431 assert "Traceback" in result.outputMerge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 432–440why, one step back432433434class TestRoutes:435 @pytest.fixture436 def invoke(self, runner):437 def create_app(info):438 app = Flask(__name__)439 app.testing = True440Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 441why, one step back441 @app.route("/get_post/<int:x>/<int:y>", methods=["GET", "POST"])Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 442–444why, one step back442 def yyy_get_post(x, y):443 pass444Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 445why, one step back445 @app.route("/zzz_post", methods=["POST"])Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 446–453why, one step back446 def aaa_post():447 pass448449 return app450451 cli = FlaskGroup(create_app=create_app)452 return partial(runner.invoke, cli)453Merge pull request #2811 from greyli/test-routes-command-for-no-routesDavid Lordbefore adoptionb6a8f75
lines 454–464why, one step back454 @pytest.fixture455 def invoke_no_routes(self, runner):456 def create_app(info):457 app = Flask(__name__, static_folder=None)458 app.testing = True459460 return app461462 cli = FlaskGroup(create_app=create_app)463 return partial(runner.invoke, cli)464Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 465–468why, one step back465 def expect_order(self, order, output):466 # skip the header and match the start of each row467 for expect, line in zip(order, output.splitlines()[2:]):468 # do this instead of startswith for nicer pytest outputMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 469why, one step back469 assert line[: len(expect)] == expectMerge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 470–471why, one step back470471 def test_simple(self, invoke):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 472why, one step back472 result = invoke(["routes"])Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
line 473why, one step back473 assert result.exit_code == 0Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 474why, one step back474 self.expect_order(["aaa_post", "static", "yyy_get_post"], result.output)Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 475–476why, one step back475476 def test_sort(self, invoke):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 477–478why, one step back477 default_output = invoke(["routes"]).output478 endpoint_output = invoke(["routes", "-s", "endpoint"]).outputMerge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 479–480why, one step back479 assert default_output == endpoint_output480 self.expect_order(Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 481–482why, one step back481 ["static", "yyy_get_post", "aaa_post"],482 invoke(["routes", "-s", "methods"]).output,Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 483–484why, one step back483 )484 self.expect_order(Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 485–486why, one step back485 ["yyy_get_post", "static", "aaa_post"],486 invoke(["routes", "-s", "rule"]).output,Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 487–488why, one step back487 )488 self.expect_order(Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 489–490why, one step back489 ["aaa_post", "yyy_get_post", "static"],490 invoke(["routes", "-s", "match"]).output,Merge pull request #2409 from bijanvakili/clarify-app-teardownDavid Lordbefore adoption817903c
lines 491–493why, one step back491 )492493 def test_all_methods(self, invoke):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 494–497why, one step back494 output = invoke(["routes"]).output495 assert "GET, HEAD, OPTIONS, POST" not in output496 output = invoke(["routes", "--all-methods"]).output497 assert "GET, HEAD, OPTIONS, POST" in outputMerge pull request #2811 from greyli/test-routes-command-for-no-routesDavid Lordbefore adoptionb6a8f75
lines 498–499why, one step back498499 def test_no_routes(self, invoke_no_routes):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 500why, one step back500 result = invoke_no_routes(["routes"])Merge pull request #2811 from greyli/test-routes-command-for-no-routesDavid Lordbefore adoptionb6a8f75
line 501why, one step back501 assert result.exit_code == 0Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 502why, one step back502 assert "No routes were registered." in result.outputMerge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4
lines 503–504why, one step back503504Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 505why, one step back505need_dotenv = pytest.mark.skipif(dotenv is None, reason="dotenv is not installed")Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4
lines 506–510why, one step back506507508@need_dotenv509def test_load_dotenv(monkeypatch):510 # can't use monkeypatch.delitem since the keys don't exist yetMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 511why, one step back511 for item in ("FOO", "BAR", "SPAM"):Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4
lines 512–513why, one step back512 monkeypatch._setitem.append((os.environ, item, notset))513Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 514–515why, one step back514 monkeypatch.setenv("EGGS", "3")515 monkeypatch.chdir(os.path.join(test_path, "cliapp", "inner1"))Merge pull request #2939 from sharmaadarsh563/2937-correct-load_dotenv-return-valueDavid Lordbefore adoptiond3e1fed
line 516why, one step back516 assert load_dotenv()Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4
lines 517–518why, one step back517 assert os.getcwd() == test_path518 # .flaskenv doesn't overwrite .envMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 519why, one step back519 assert os.environ["FOO"] == "env"Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4
line 520why, one step back520 # set only in .flaskenvMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 521why, one step back521 assert os.environ["BAR"] == "bar"Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4
line 522why, one step back522 # set only in .envMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 523why, one step back523 assert os.environ["SPAM"] == "1"Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4
line 524why, one step back524 # set manually, files don't overwriteMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 525why, one step back525 assert os.environ["EGGS"] == "3"Merge pull request #2939 from sharmaadarsh563/2937-correct-load_dotenv-return-valueDavid Lordbefore adoptiond3e1fed
lines 526–528why, one step back526527 # Non existent file should not load528 assert not load_dotenv('non-existent-file')Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4
lines 529–532why, one step back529530531@need_dotenv532def test_dotenv_path(monkeypatch):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 533why, one step back533 for item in ("FOO", "BAR", "EGGS"):Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4
lines 534–536why, one step back534 monkeypatch._setitem.append((os.environ, item, notset))535536 cwd = os.getcwd()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 537why, one step back537 load_dotenv(os.path.join(test_path, ".flaskenv"))Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4
line 538why, one step back538 assert os.getcwd() == cwdMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 539why, one step back539 assert "FOO" in os.environMerge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4
lines 540–542why, one step back540541542def test_dotenv_optional(monkeypatch):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 543why, one step back543 monkeypatch.setattr("flask.cli.dotenv", None)Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4
lines 544–545why, one step back544 monkeypatch.chdir(test_path)545 load_dotenv()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 546why, one step back546 assert "FOO" not in os.environMerge pull request #2606 from pallets/run-sslDavid Lordbefore adoption8ff0cef
lines 547–548why, one step back547548Merge branch '1.0-maintenance'David Lordbefore adoption1fa9185
lines 549–551why, one step back549@need_dotenv550def test_disable_dotenv_from_env(monkeypatch, runner):551 monkeypatch.chdir(test_path)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 552why, one step back552 monkeypatch.setitem(os.environ, "FLASK_SKIP_DOTENV", "1")Merge branch '1.0-maintenance'David Lordbefore adoption1fa9185
line 553why, one step back553 runner.invoke(FlaskGroup())Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 554why, one step back554 assert "FOO" not in os.environMerge branch '1.0-maintenance'David Lordbefore adoption1fa9185
lines 555–556why, one step back555556Merge pull request #2606 from pallets/run-sslDavid Lordbefore adoption8ff0cef
lines 557–559why, one step back557def test_run_cert_path():558 # no key559 with pytest.raises(click.BadParameter):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 560why, one step back560 run_command.make_context("run", ["--cert", __file__])Merge pull request #2606 from pallets/run-sslDavid Lordbefore adoption8ff0cef
lines 561–563why, one step back561562 # no cert563 with pytest.raises(click.BadParameter):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 564why, one step back564 run_command.make_context("run", ["--key", __file__])Merge pull request #2606 from pallets/run-sslDavid Lordbefore adoption8ff0cef
line 565why, one step back565Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 566–567why, one step back566 ctx = run_command.make_context("run", ["--cert", __file__, "--key", __file__])567 assert ctx.params["cert"] == (__file__, __file__)Merge pull request #2606 from pallets/run-sslDavid Lordbefore adoption8ff0cef
lines 568–570why, one step back568569570def test_run_cert_adhoc(monkeypatch):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 571why, one step back571 monkeypatch.setitem(sys.modules, "OpenSSL", None)Merge pull request #2606 from pallets/run-sslDavid Lordbefore adoption8ff0cef
lines 572–574why, one step back572573 # pyOpenSSL not installed574 with pytest.raises(click.BadParameter):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 575why, one step back575 run_command.make_context("run", ["--cert", "adhoc"])Merge pull request #2606 from pallets/run-sslDavid Lordbefore adoption8ff0cef
lines 576–577why, one step back576577 # pyOpenSSL installedMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 578–580why, one step back578 monkeypatch.setitem(sys.modules, "OpenSSL", types.ModuleType("OpenSSL"))579 ctx = run_command.make_context("run", ["--cert", "adhoc"])580 assert ctx.params["cert"] == "adhoc"Merge pull request #2606 from pallets/run-sslDavid Lordbefore adoption8ff0cef
lines 581–583why, one step back581582 # no key with adhoc583 with pytest.raises(click.BadParameter):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 584why, one step back584 run_command.make_context("run", ["--cert", "adhoc", "--key", __file__])Merge pull request #2606 from pallets/run-sslDavid Lordbefore adoption8ff0cef
lines 585–587why, one step back585586587def test_run_cert_import(monkeypatch):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 588why, one step back588 monkeypatch.setitem(sys.modules, "not_here", None)Merge pull request #2606 from pallets/run-sslDavid Lordbefore adoption8ff0cef
lines 589–591why, one step back589590 # ImportError591 with pytest.raises(click.BadParameter):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 592why, one step back592 run_command.make_context("run", ["--cert", "not_here"])Merge pull request #2606 from pallets/run-sslDavid Lordbefore adoption8ff0cef
lines 593–594why, one step back593594 # not an SSLContextMerge branch '1.0-maintenance'ThiefMasterbefore adoption161c436
line 595why, one step back595 if sys.version_info >= (2, 7, 9):Merge pull request #2606 from pallets/run-sslDavid Lordbefore adoption8ff0cef
line 596why, one step back596 with pytest.raises(click.BadParameter):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 597why, one step back597 run_command.make_context("run", ["--cert", "flask"])Merge pull request #2606 from pallets/run-sslDavid Lordbefore adoption8ff0cef
lines 598–599why, one step back598599 # SSLContextMerge branch '1.0-maintenance'ThiefMasterbefore adoption161c436
line 600why, one step back600 if sys.version_info < (2, 7, 9):Merge pull request #2606 from pallets/run-sslDavid Lordbefore adoption8ff0cef
lines 601–604why, one step back601 ssl_context = object()602 else:603 ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)604Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 605–607why, one step back605 monkeypatch.setitem(sys.modules, "ssl_context", ssl_context)606 ctx = run_command.make_context("run", ["--cert", "ssl_context"])607 assert ctx.params["cert"] is ssl_contextMerge pull request #2606 from pallets/run-sslDavid Lordbefore adoption8ff0cef
lines 608–610why, one step back608609 # no --key with SSLContext610 with pytest.raises(click.BadParameter):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 611why, one step back611 run_command.make_context("run", ["--cert", "ssl_context", "--key", __file__])Merge pull request #2784 from doobeh/masterDavid Lordbefore adoptionb837606
lines 612–654why, one step back612613614def test_cli_blueprints(app):615 """Test blueprint commands register correctly to the application"""616 custom = Blueprint("custom", __name__, cli_group="customized")617 nested = Blueprint("nested", __name__)618 merged = Blueprint("merged", __name__, cli_group=None)619 late = Blueprint("late", __name__)620621 @custom.cli.command("custom")622 def custom_command():623 click.echo("custom_result")624625 @nested.cli.command("nested")626 def nested_command():627 click.echo("nested_result")628629 @merged.cli.command("merged")630 def merged_command():631 click.echo("merged_result")632633 @late.cli.command("late")634 def late_command():635 click.echo("late_result")636637 app.register_blueprint(custom)638 app.register_blueprint(nested)639 app.register_blueprint(merged)640 app.register_blueprint(late, cli_group="late_registration")641642 app_runner = app.test_cli_runner()643644 result = app_runner.invoke(args=["customized", "custom"])645 assert "custom_result" in result.output646647 result = app_runner.invoke(args=["nested", "nested"])648 assert "nested_result" in result.output649650 result = app_runner.invoke(args=["merged"])651 assert "merged_result" in result.output652653 result = app_runner.invoke(args=["late_registration", "late"])654 assert "late_result" in result.output