Skip to content
Myr

test_cli.py

This file

Path

tests/test_cli.py

678 lines · 18.9 KiB · Python

Last changed by

Merge pull request #4718 from pallets/remove-lazy-optionDavid Lordbefore adoption633f421

History of this file

Take it

The walk stopped after 200 commits: lines older than 4240ace point at it.

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 1–7why, 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 partial

Merge branch '2.0.x'David Lordbefore adoption83f7efa

line 8why, one step back
8from pathlib import Path

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 9–29why, one step back
9
10import click11import pytest12from _pytest.monkeypatch import notset13from click.testing import CliRunner1415from flask import Blueprint16from flask import current_app17from flask import Flask18from flask.cli import AppGroup19from 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_appcontext29

Merge branch '2.0.x'David Lordbefore adoption83f7efa

lines 30–31why, one step back
30cwd = Path.cwd()
31test_path = (Path(__file__) / ".." / "test_apps").resolve()

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 32–49why, 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 testapp4243 assert testapp.cli.name == testapp.name444546def test_find_best_app(test_apps):47 class Module:48 app = Flask("appname")49

Merge pull request #4337 from pallets/remove-deprecated-codeDavid Lordbefore adoption7620cb7

line 50why, one step back
50 assert find_best_app(Module) == Module.app

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 51–54why, one step back
51
52 class Module:53 application = Flask("appname")54

Merge pull request #4337 from pallets/remove-deprecated-codeDavid Lordbefore adoption7620cb7

line 55why, one step back
55 assert find_best_app(Module) == Module.application

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 56–59why, one step back
56
57 class Module:58 myapp = Flask("appname")59

Merge pull request #4337 from pallets/remove-deprecated-codeDavid Lordbefore adoption7620cb7

line 60why, one step back
60 assert find_best_app(Module) == Module.myapp

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 61–66why, one step back
61
62 class Module:63 @staticmethod64 def create_app():65 return Flask("appname")66

Merge pull request #4337 from pallets/remove-deprecated-codeDavid Lordbefore adoption7620cb7

line 67why, one step back
67 app = find_best_app(Module)

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 68–72why, one step back
68 assert isinstance(app, Flask)
69 assert app.name == "appname"7071 class Module:72 @staticmethod

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoptionafc13b9

lines 73–75why, one step back
73 def create_app(**kwargs):
74 return Flask("appname")75

Merge pull request #4337 from pallets/remove-deprecated-codeDavid Lordbefore adoption7620cb7

line 76why, one step back
76 app = find_best_app(Module)

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoptionafc13b9

lines 77–81why, one step back
77 assert isinstance(app, Flask)
78 assert app.name == "appname"7980 class Module:81 @staticmethod

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 82–84why, one step back
82 def make_app():
83 return Flask("appname")84

Merge pull request #4337 from pallets/remove-deprecated-codeDavid Lordbefore adoption7620cb7

line 85why, one step back
85 app = find_best_app(Module)

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 86–95why, one step back
86 assert isinstance(app, Flask)
87 assert app.name == "appname"8889 class Module:90 myapp = Flask("appname1")9192 @staticmethod93 def create_app():94 return Flask("appname2")95

Merge pull request #4337 from pallets/remove-deprecated-codeDavid Lordbefore adoption7620cb7

line 96why, one step back
96 assert find_best_app(Module) == Module.myapp

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 97–104why, one step back
97
98 class Module:99 myapp = Flask("appname1")100101 @staticmethod102 def create_app():103 return Flask("appname2")104

Merge pull request #4337 from pallets/remove-deprecated-codeDavid Lordbefore adoption7620cb7

line 105why, one step back
105 assert find_best_app(Module) == Module.myapp

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 106–109why, one step back
106
107 class Module:108 pass109

Merge pull request #4337 from pallets/remove-deprecated-codeDavid Lordbefore adoption7620cb7

line 110why, one step back
110 pytest.raises(NoAppException, find_best_app, Module)

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 111–115why, one step back
111
112 class Module:113 myapp1 = Flask("appname1")114 myapp2 = Flask("appname2")115

Merge pull request #4337 from pallets/remove-deprecated-codeDavid Lordbefore adoption7620cb7

line 116why, one step back
116 pytest.raises(NoAppException, find_best_app, Module)

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 117–122why, one step back
117
118 class Module:119 @staticmethod120 def create_app(foo, bar):121 return Flask("appname2")122

Merge pull request #4337 from pallets/remove-deprecated-codeDavid Lordbefore adoption7620cb7

line 123why, one step back
123 pytest.raises(NoAppException, find_best_app, Module)

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 124–129why, one step back
124
125 class Module:126 @staticmethod127 def create_app():128 raise TypeError("bad bad factory!")129

Merge pull request #4337 from pallets/remove-deprecated-codeDavid Lordbefore adoption7620cb7

line 130why, one step back
130 pytest.raises(TypeError, find_best_app, Module)

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 131–137why, one step back
131
132133@pytest.mark.parametrize(134 "value,path,result",135 (136 ("test", cwd, "test"),137 ("test.py", cwd, "test"),

Merge branch '2.0.x'David Lordbefore adoption83f7efa

line 138why, one step back
138 ("a/test", cwd / "a", "test"),

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 139–142why, one step back
139 ("test/__init__.py", cwd, "test"),
140 ("test/__init__", cwd, "test"),141 # nested package142 (

Merge branch '2.0.x'David Lordbefore adoption83f7efa

line 143why, one step back
143 test_path / "cliapp" / "inner1" / "__init__",

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 144–147why, one step back
144 test_path,
145 "cliapp.inner1",146 ),147 (

Merge branch '2.0.x'David Lordbefore adoption83f7efa

line 148why, one step back
148 test_path / "cliapp" / "inner1" / "inner2",

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 149–153why, one step back
149 test_path,
150 "cliapp.inner1.inner2",151 ),152 # dotted name153 ("test.a.b", cwd, "test.a.b"),

Merge branch '2.0.x'David Lordbefore adoption83f7efa

line 154why, one step back
154 (test_path / "cliapp.app", test_path, "cliapp.app"),

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

line 155why, one step back
155 # not a Python file, will be caught during import

Merge branch '2.0.x'David Lordbefore adoption83f7efa

line 156why, one step back
156 (test_path / "cliapp" / "message.txt", test_path, "cliapp.message.txt"),

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 157–174why, one step back
157 ),
158)159def test_prepare_import(request, value, path, result):160 """Expect the correct path to be set and the correct import and app names161 to be returned.162163 :func:`prepare_exec_for_file` has a side effect where the parent directory164 of the given import is added to :data:`sys.path`. This is reset after the165 test runs.166 """167 original_path = sys.path[:]168169 def reset_path():170 sys.path[:] = original_path171172 request.addfinalizer(reset_path)173174 assert prepare_import(value) == result

Merge branch '2.0.x'David Lordbefore adoption83f7efa

line 175why, one step back
175 assert sys.path[0] == str(path)

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 176–193why, one step back
176
177178@pytest.mark.parametrize(179 "iname,aname,result",180 (181 ("cliapp.app", None, "testapp"),182 ("cliapp.app", "testapp", "testapp"),183 ("cliapp.factory", None, "app"),184 ("cliapp.factory", "create_app", "app"),185 ("cliapp.factory", "create_app()", "app"),186 ("cliapp.factory", 'create_app2("foo", "bar")', "app2_foo_bar"),187 # trailing comma space188 ("cliapp.factory", 'create_app2("foo", "bar", )', "app2_foo_bar"),189 # strip whitespace190 ("cliapp.factory", " create_app () ", "app"),191 ),192)193def test_locate_app(test_apps, iname, aname, result):

Merge pull request #4337 from pallets/remove-deprecated-codeDavid Lordbefore adoption7620cb7

line 194why, one step back
194 assert locate_app(iname, aname).name == result

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 195–216why, one step back
195
196197@pytest.mark.parametrize(198 "iname,aname",199 (200 ("notanapp.py", None),201 ("cliapp/app", None),202 ("cliapp.app", "notanapp"),203 # not enough arguments204 ("cliapp.factory", 'create_app2("foo")'),205 # invalid identifier206 ("cliapp.factory", "create_app("),207 # no app returned208 ("cliapp.factory", "no_app"),209 # nested import error210 ("cliapp.importerrorapp", None),211 # not a Python file212 ("cliapp.message.txt", None),213 ),214)215def test_locate_app_raises(test_apps, iname, aname):216 with pytest.raises(NoAppException):

Merge pull request #4337 from pallets/remove-deprecated-codeDavid Lordbefore adoption7620cb7

line 217why, one step back
217 locate_app(iname, aname)

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 218–220why, one step back
218
219220def test_locate_app_suppress_raise(test_apps):

Merge pull request #4337 from pallets/remove-deprecated-codeDavid Lordbefore adoption7620cb7

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

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 222–225why, one step back
222 assert app is None
223224 # only direct import error is suppressed225 with pytest.raises(NoAppException):

Merge pull request #4337 from pallets/remove-deprecated-codeDavid Lordbefore adoption7620cb7

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

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 227–255why, one step back
227
228229def test_get_version(test_apps, capsys):230 from flask import __version__ as flask_version231 from werkzeug import __version__ as werkzeug_version232 from platform import python_version233234 class MockCtx:235 resilient_parsing = False236 color = None237238 def exit(self):239 return240241 ctx = MockCtx()242 get_version(ctx, None, "test")243 out, err = capsys.readouterr()244 assert f"Python {python_version()}" in out245 assert f"Flask {flask_version}" in out246 assert f"Werkzeug {werkzeug_version}" in out247248249def test_scriptinfo(test_apps, monkeypatch):250 obj = ScriptInfo(app_import_path="cliapp.app:testapp")251 app = obj.load_app()252 assert app.name == "testapp"253 assert obj.load_app() is app254255 # import app with module's absolute path

Merge branch '2.0.x'David Lordbefore adoption83f7efa

lines 256–257why, one step back
256 cli_app_path = str(test_path / "cliapp" / "app.py")
257

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 258–278why, one step back
258 obj = ScriptInfo(app_import_path=cli_app_path)
259 app = obj.load_app()260 assert app.name == "testapp"261 assert obj.load_app() is app262 obj = ScriptInfo(app_import_path=f"{cli_app_path}:testapp")263 app = obj.load_app()264 assert app.name == "testapp"265 assert obj.load_app() is app266267 def create_app():268 return Flask("createapp")269270 obj = ScriptInfo(create_app=create_app)271 app = obj.load_app()272 assert app.name == "createapp"273 assert obj.load_app() is app274275 obj = ScriptInfo()276 pytest.raises(NoAppException, obj.load_app)277278 # import app from wsgi.py in current directory

Merge branch '2.0.x'David Lordbefore adoption83f7efa

line 279why, one step back
279 monkeypatch.chdir(test_path / "helloworld")

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 280–284why, one step back
280 obj = ScriptInfo()
281 app = obj.load_app()282 assert app.name == "hello"283284 # import app from app.py in current directory

Merge branch '2.0.x'David Lordbefore adoption83f7efa

line 285why, one step back
285 monkeypatch.chdir(test_path / "cliapp")

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 286–290why, one step back
286 obj = ScriptInfo()
287 app = obj.load_app()288 assert app.name == "testapp"289290

Merge pull request #4647 from pallets/cli-app_contextDavid Lordbefore adoptioncec5f74

lines 291–301why, one step back
291def test_app_cli_has_app_context(app, runner):
292 def _param_cb(ctx, param, value):293 # current_app should be available in parameter callbacks294 return bool(current_app)295296 @app.cli.command()297 @click.argument("value", callback=_param_cb)298 def check(value):299 app = click.get_current_context().obj.load_app()300 # the loaded app should be the same as current_app301 same_app = current_app._get_current_object() is app

Merge pull request #4662 from pallets/fix-run-skipDavid Lordbefore adoption3635583

line 302why, one step back
302 return same_app, value

Merge pull request #4647 from pallets/cli-app_contextDavid Lordbefore adoptioncec5f74

lines 303–305why, one step back
303
304 cli = FlaskGroup(create_app=lambda: app)305 result = runner.invoke(cli, ["check", "x"], standalone_mode=False)

Merge pull request #4662 from pallets/fix-run-skipDavid Lordbefore adoption3635583

line 306why, one step back
306 assert result.return_value == (True, True)

Merge pull request #4647 from pallets/cli-app_contextDavid Lordbefore adoptioncec5f74

lines 307–308why, one step back
307
308

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 309–321why, one step back
309def test_with_appcontext(runner):
310 @click.command()311 @with_appcontext312 def testcmd():313 click.echo(current_app.name)314315 obj = ScriptInfo(create_app=lambda: Flask("testapp"))316317 result = runner.invoke(testcmd, obj=obj)318 assert result.exit_code == 0319 assert result.output == "testapp\n"320321

Merge pull request #4647 from pallets/cli-app_contextDavid Lordbefore adoptioncec5f74

line 322why, one step back
322def test_appgroup_app_context(runner):

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 323–326why, one step back
323 @click.group(cls=AppGroup)
324 def cli():325 pass326

Merge pull request #4647 from pallets/cli-app_contextDavid Lordbefore adoptioncec5f74

line 327why, one step back
327 @cli.command()

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 328–334why, one step back
328 def test():
329 click.echo(current_app.name)330331 @cli.group()332 def subgroup():333 pass334

Merge pull request #4647 from pallets/cli-app_contextDavid Lordbefore adoptioncec5f74

line 335why, one step back
335 @subgroup.command()

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 336–349why, one step back
336 def test2():
337 click.echo(current_app.name)338339 obj = ScriptInfo(create_app=lambda: Flask("testappgroup"))340341 result = runner.invoke(cli, ["test"], obj=obj)342 assert result.exit_code == 0343 assert result.output == "testappgroup\n"344345 result = runner.invoke(cli, ["subgroup", "test2"], obj=obj)346 assert result.exit_code == 0347 assert result.output == "testappgroup\n"348349

Merge pull request #4647 from pallets/cli-app_contextDavid Lordbefore adoptioncec5f74

line 350why, one step back
350def test_flaskgroup_app_context(runner):

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 351–386why, one step back
351 def create_app():
352 return Flask("flaskgroup")353354 @click.group(cls=FlaskGroup, create_app=create_app)355 def cli(**params):356 pass357358 @cli.command()359 def test():360 click.echo(current_app.name)361362 result = runner.invoke(cli, ["test"])363 assert result.exit_code == 0364 assert result.output == "flaskgroup\n"365366367@pytest.mark.parametrize("set_debug_flag", (True, False))368def test_flaskgroup_debug(runner, set_debug_flag):369 def create_app():370 app = Flask("flaskgroup")371 app.debug = True372 return app373374 @click.group(cls=FlaskGroup, create_app=create_app, set_debug_flag=set_debug_flag)375 def cli(**params):376 pass377378 @cli.command()379 def test():380 click.echo(str(current_app.debug))381382 result = runner.invoke(cli, ["test"])383 assert result.exit_code == 0384 assert result.output == f"{not set_debug_flag}\n"385386

Merge pull request #4642 from pallets/cli-nestDavid Lordbefore adoptionfe4003b

lines 387–399why, one step back
387def test_flaskgroup_nested(app, runner):
388 cli = click.Group("cli")389 flask_group = FlaskGroup(name="flask", create_app=lambda: app)390 cli.add_command(flask_group)391392 @flask_group.command()393 def show():394 click.echo(current_app.name)395396 result = runner.invoke(cli, ["flask", "show"])397 assert result.output == "flask_test\n"398399

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 400–433why, one step back
400def test_no_command_echo_loading_error():
401 from flask.cli import cli402403 runner = CliRunner(mix_stderr=False)404 result = runner.invoke(cli, ["missing"])405 assert result.exit_code == 2406 assert "FLASK_APP" in result.stderr407 assert "Usage:" in result.stderr408409410def test_help_echo_loading_error():411 from flask.cli import cli412413 runner = CliRunner(mix_stderr=False)414 result = runner.invoke(cli, ["--help"])415 assert result.exit_code == 0416 assert "FLASK_APP" in result.stderr417 assert "Usage:" in result.stdout418419420def test_help_echo_exception():421 def create_app():422 raise Exception("oh no")423424 cli = FlaskGroup(create_app=create_app)425 runner = CliRunner(mix_stderr=False)426 result = runner.invoke(cli, ["--help"])427 assert result.exit_code == 0428 assert "Exception: oh no" in result.stderr429 assert "Usage:" in result.stdout430431432class TestRoutes:433 @pytest.fixture

Merge branch '2.1.x'David Lordbefore adoption1626aff

lines 434–436why, one step back
434 def app(self):
435 app = Flask(__name__)436 app.testing = True

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

line 437why, one step back
437

Merge branch '2.1.x'David Lordbefore adoption1626aff

lines 438–440why, one step back
438 @app.route("/get_post/<int:x>/<int:y>", methods=["GET", "POST"])
439 def yyy_get_post(x, y):440 pass

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

line 441why, one step back
441

Merge branch '2.1.x'David Lordbefore adoption1626aff

lines 442–444why, one step back
442 @app.route("/zzz_post", methods=["POST"])
443 def aaa_post():444 pass

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

line 445why, one step back
445

Merge branch '2.1.x'David Lordbefore adoption1626aff

line 446why, one step back
446 return app

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

line 447why, one step back
447

Merge branch '2.1.x'David Lordbefore adoption1626aff

lines 448–450why, one step back
448 @pytest.fixture
449 def invoke(self, app, runner):450 cli = FlaskGroup(create_app=lambda: app)

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 451–474why, one step back
451 return partial(runner.invoke, cli)
452453 @pytest.fixture454 def invoke_no_routes(self, runner):455 def create_app():456 app = Flask(__name__, static_folder=None)457 app.testing = True458459 return app460461 cli = FlaskGroup(create_app=create_app)462 return partial(runner.invoke, cli)463464 def expect_order(self, order, output):465 # skip the header and match the start of each row466 for expect, line in zip(order, output.splitlines()[2:]):467 # do this instead of startswith for nicer pytest output468 assert line[: len(expect)] == expect469470 def test_simple(self, invoke):471 result = invoke(["routes"])472 assert result.exit_code == 0473 self.expect_order(["aaa_post", "static", "yyy_get_post"], result.output)474

Merge branch '2.1.x'David Lordbefore adoption1626aff

line 475why, one step back
475 def test_sort(self, app, invoke):

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 476–486why, one step back
476 default_output = invoke(["routes"]).output
477 endpoint_output = invoke(["routes", "-s", "endpoint"]).output478 assert default_output == endpoint_output479 self.expect_order(480 ["static", "yyy_get_post", "aaa_post"],481 invoke(["routes", "-s", "methods"]).output,482 )483 self.expect_order(484 ["yyy_get_post", "static", "aaa_post"],485 invoke(["routes", "-s", "rule"]).output,486 )

Merge branch '2.1.x'David Lordbefore adoption1626aff

lines 487–488why, one step back
487 match_order = [r.endpoint for r in app.url_map.iter_rules()]
488 self.expect_order(match_order, invoke(["routes", "-s", "match"]).output)

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 489–501why, one step back
489
490 def test_all_methods(self, invoke):491 output = invoke(["routes"]).output492 assert "GET, HEAD, OPTIONS, POST" not in output493 output = invoke(["routes", "--all-methods"]).output494 assert "GET, HEAD, OPTIONS, POST" in output495496 def test_no_routes(self, invoke_no_routes):497 result = invoke_no_routes(["routes"])498 assert result.exit_code == 0499 assert "No routes were registered." in result.output500501

Merge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3

lines 502–506why, one step back
502def dotenv_not_available():
503 try:504 import dotenv # noqa: F401505 except ImportError:506 return True

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

line 507why, one step back
507

Merge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3

line 508why, one step back
508 return False

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

line 509why, one step back
509

Merge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3

lines 510–515why, one step back
510
511need_dotenv = pytest.mark.skipif(512 dotenv_not_available(), reason="dotenv is not installed"513)514515

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 516–524why, one step back
516@need_dotenv
517def test_load_dotenv(monkeypatch):518 # can't use monkeypatch.delitem since the keys don't exist yet519 for item in ("FOO", "BAR", "SPAM", "HAM"):520 monkeypatch._setitem.append((os.environ, item, notset))521522 monkeypatch.setenv("EGGS", "3")523 monkeypatch.chdir(test_path)524 assert load_dotenv()

Merge branch '2.0.x'David Lordbefore adoption83f7efa

line 525why, one step back
525 assert Path.cwd() == test_path

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 526–544why, one step back
526 # .flaskenv doesn't overwrite .env
527 assert os.environ["FOO"] == "env"528 # set only in .flaskenv529 assert os.environ["BAR"] == "bar"530 # set only in .env531 assert os.environ["SPAM"] == "1"532 # set manually, files don't overwrite533 assert os.environ["EGGS"] == "3"534 # test env file encoding535 assert os.environ["HAM"] == "火腿"536 # Non existent file should not load537 assert not load_dotenv("non-existent-file")538539540@need_dotenv541def test_dotenv_path(monkeypatch):542 for item in ("FOO", "BAR", "EGGS"):543 monkeypatch._setitem.append((os.environ, item, notset))544

Merge branch '2.0.x'David Lordbefore adoption83f7efa

lines 545–546why, one step back
545 load_dotenv(test_path / ".flaskenv")
546 assert Path.cwd() == cwd

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 547–550why, one step back
547 assert "FOO" in os.environ
548549550def test_dotenv_optional(monkeypatch):

Merge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3

line 551why, one step back
551 monkeypatch.setitem(sys.modules, "dotenv", None)

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 552–573why, one step back
552 monkeypatch.chdir(test_path)
553 load_dotenv()554 assert "FOO" not in os.environ555556557@need_dotenv558def test_disable_dotenv_from_env(monkeypatch, runner):559 monkeypatch.chdir(test_path)560 monkeypatch.setitem(os.environ, "FLASK_SKIP_DOTENV", "1")561 runner.invoke(FlaskGroup())562 assert "FOO" not in os.environ563564565def test_run_cert_path():566 # no key567 with pytest.raises(click.BadParameter):568 run_command.make_context("run", ["--cert", __file__])569570 # no cert571 with pytest.raises(click.BadParameter):572 run_command.make_context("run", ["--key", __file__])573

Merge branch '2.1.x'David Lordbefore adoptioncb4f742

line 574why, one step back
574 # cert specified first

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

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

Merge branch '2.1.x'David Lordbefore adoptioncb4f742

lines 578–580why, one step back
578 # key specified first
579 ctx = run_command.make_context("run", ["--key", __file__, "--cert", __file__])580 assert ctx.params["cert"] == (__file__, __file__)

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

line 581why, one step back
581

Merge branch '2.1.x'David Lordbefore adoptioncb4f742

line 582why, one step back
582

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 583–622why, one step back
583def test_run_cert_adhoc(monkeypatch):
584 monkeypatch.setitem(sys.modules, "cryptography", None)585586 # cryptography not installed587 with pytest.raises(click.BadParameter):588 run_command.make_context("run", ["--cert", "adhoc"])589590 # cryptography installed591 monkeypatch.setitem(sys.modules, "cryptography", types.ModuleType("cryptography"))592 ctx = run_command.make_context("run", ["--cert", "adhoc"])593 assert ctx.params["cert"] == "adhoc"594595 # no key with adhoc596 with pytest.raises(click.BadParameter):597 run_command.make_context("run", ["--cert", "adhoc", "--key", __file__])598599600def test_run_cert_import(monkeypatch):601 monkeypatch.setitem(sys.modules, "not_here", None)602603 # ImportError604 with pytest.raises(click.BadParameter):605 run_command.make_context("run", ["--cert", "not_here"])606607 with pytest.raises(click.BadParameter):608 run_command.make_context("run", ["--cert", "flask"])609610 # SSLContext611 ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)612613 monkeypatch.setitem(sys.modules, "ssl_context", ssl_context)614 ctx = run_command.make_context("run", ["--cert", "ssl_context"])615 assert ctx.params["cert"] is ssl_context616617 # no --key with SSLContext618 with pytest.raises(click.BadParameter):619 run_command.make_context("run", ["--cert", "ssl_context", "--key", __file__])620621622def test_run_cert_no_ssl(monkeypatch):

Merge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3

lines 623–624why, one step back
623 monkeypatch.setitem(sys.modules, "ssl", None)
624

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

lines 625–678why, one step back
625 with pytest.raises(click.BadParameter):
626 run_command.make_context("run", ["--cert", "not_here"])627628629def test_cli_blueprints(app):630 """Test blueprint commands register correctly to the application"""631 custom = Blueprint("custom", __name__, cli_group="customized")632 nested = Blueprint("nested", __name__)633 merged = Blueprint("merged", __name__, cli_group=None)634 late = Blueprint("late", __name__)635636 @custom.cli.command("custom")637 def custom_command():638 click.echo("custom_result")639640 @nested.cli.command("nested")641 def nested_command():642 click.echo("nested_result")643644 @merged.cli.command("merged")645 def merged_command():646 click.echo("merged_result")647648 @late.cli.command("late")649 def late_command():650 click.echo("late_result")651652 app.register_blueprint(custom)653 app.register_blueprint(nested)654 app.register_blueprint(merged)655 app.register_blueprint(late, cli_group="late_registration")656657 app_runner = app.test_cli_runner()658659 result = app_runner.invoke(args=["customized", "custom"])660 assert "custom_result" in result.output661662 result = app_runner.invoke(args=["nested", "nested"])663 assert "nested_result" in result.output664665 result = app_runner.invoke(args=["merged"])666 assert "merged_result" in result.output667668 result = app_runner.invoke(args=["late_registration", "late"])669 assert "late_result" in result.output670671672def test_cli_empty(app):673 """If a Blueprint's CLI group is empty, do not register it."""674 bp = Blueprint("blue", __name__, cli_group="blue")675 app.register_blueprint(bp)676677 result = app.test_cli_runner().invoke(args=["blue", "--help"])678 assert result.exit_code == 2, f"Unexpected success:\n\n{result.output}"