Skip to content
Myr

Merge pull request #3157 from fantix/fix-test-client-teardown

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fix teardown bug in FlaskClient

authored by David Lord · committed by GitHub · parents 754b729, a71c167 · Browse files · 3 files+29 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+29 added−6 removed

  • tests:+15 added−0 removed
  • other:+14 added−6 removed

3 files+29 added−6 removed

modified:flask/testing.py+11 added-6 removed
204 unmodified lines
205206 def __exit__(self, exc_type, exc_value, tb):207 self.preserve_context = False208209 # on exit we want to clean up earlier. Normally the request context210 # stays preserved until the next request in the same thread comes211 # in. See RequestGlobals.push() for the general behavior.212 top = _request_ctx_stack.top213 if top is not None and top.preserved:214 top.pop()209 # Normally the request context is preserved until the next210 # request in the same thread comes. When the client exits we211 # want to clean up earlier. Pop request contexts until the stack212 # is empty or a non-preserved one is found.213 while True:214 top = _request_ctx_stack.top215216 if top is not None and top.preserved:217 top.pop()218 else:219 break220221222class FlaskCliRunner(CliRunner):223 """A :class:`~click.testing.CliRunner` for testing a Flask app's224 CLI commands. Typically created using225 :meth:`~flask.Flask.test_cli_runner`. See :ref:`testing-cli`.
28 unmodified lines
modified:tests/test_testing.py+15 added
409 unmodified lines
410 script_info = ScriptInfo(create_app=create_app)411 runner = app.test_cli_runner()412 runner.invoke(hello_command, obj=script_info)413 assert NS.called414415416def test_client_pop_all_preserved(app, req_ctx, client):417 @app.route("/")418 def index():419 # stream_with_context pushes a third context, preserved by client420 return flask.Response(flask.stream_with_context("hello"))421422 # req_ctx fixture pushed an initial context, not marked preserved423 with client:424 # request pushes a second request context, preserved by client425 client.get("/")426427 # only req_ctx fixture should still be pushed428 assert flask._request_ctx_stack.top is req_ctx
modified:CHANGES.rst+3 added
59 unmodified lines
60 response. :pr:`3111`61- Blueprints have a ``cli`` Click group like ``app.cli``. CLI commands62 registered with a blueprint will be available as a group under the63 ``flask`` command. :issue:`1357`.64- When using the test client as a context manager (``with client:``),65 all preserved request contexts are popped when the block exits,66 ensuring nested contexts are cleaned up correctly. :pr:`3157`6768.. _#2935: https://github.com/pallets/flask/issues/293569.. _#2957: https://github.com/pallets/flask/issues/295770.. _#2994: https://github.com/pallets/flask/pull/2994
947 unmodified lines