remove uses of LocalStack

This commit is contained in:
David Lord 2022-07-05 06:33:03 -07:00
parent d597db67de
commit 82c2e0366c
No known key found for this signature in database
GPG key ID: 7A1C87E3F5BC42A8
13 changed files with 114 additions and 131 deletions

View file

@ -6,8 +6,8 @@ import textwrap
import pytest
from _pytest import monkeypatch
import flask
from flask import Flask as _Flask
from flask import Flask
from flask.globals import request_ctx
@pytest.fixture(scope="session", autouse=True)
@ -44,14 +44,13 @@ def _reset_os_environ(monkeypatch, _standard_os_environ):
monkeypatch._setitem.extend(_standard_os_environ)
class Flask(_Flask):
testing = True
secret_key = "test key"
@pytest.fixture
def app():
app = Flask("flask_test", root_path=os.path.dirname(__file__))
app.config.update(
TESTING=True,
SECRET_KEY="test key",
)
return app
@ -92,8 +91,10 @@ def leak_detector():
# make sure we're not leaking a request context since we are
# testing flask internally in debug mode in a few cases
leaks = []
while flask._request_ctx_stack.top is not None:
leaks.append(flask._request_ctx_stack.pop())
while request_ctx:
leaks.append(request_ctx._get_current_object())
request_ctx.pop()
assert leaks == []

View file

@ -1,6 +1,8 @@
import pytest
import flask
from flask.globals import app_ctx
from flask.globals import request_ctx
def test_basic_url_generation(app):
@ -29,14 +31,14 @@ def test_url_generation_without_context_fails():
def test_request_context_means_app_context(app):
with app.test_request_context():
assert flask.current_app._get_current_object() == app
assert flask._app_ctx_stack.top is None
assert flask.current_app._get_current_object() is app
assert not flask.current_app
def test_app_context_provides_current_app(app):
with app.app_context():
assert flask.current_app._get_current_object() == app
assert flask._app_ctx_stack.top is None
assert flask.current_app._get_current_object() is app
assert not flask.current_app
def test_app_tearing_down(app):
@ -175,11 +177,11 @@ def test_context_refcounts(app, client):
@app.route("/")
def index():
with flask._app_ctx_stack.top:
with flask._request_ctx_stack.top:
with app_ctx:
with request_ctx:
pass
env = flask._request_ctx_stack.top.request.environ
assert env["werkzeug.request"] is not None
assert flask.request.environ["werkzeug.request"] is not None
return ""
res = client.get("/")

View file

@ -1110,14 +1110,10 @@ def test_enctype_debug_helper(app, client):
def index():
return flask.request.files["foo"].filename
# with statement is important because we leave an exception on the
# stack otherwise and we want to ensure that this is not the case
# to not negatively affect other tests.
with client:
with pytest.raises(DebugFilesKeyError) as e:
client.post("/fail", data={"foo": "index.txt"})
assert "no file contents were transmitted" in str(e.value)
assert "This was submitted: 'index.txt'" in str(e.value)
with pytest.raises(DebugFilesKeyError) as e:
client.post("/fail", data={"foo": "index.txt"})
assert "no file contents were transmitted" in str(e.value)
assert "This was submitted: 'index.txt'" in str(e.value)
def test_response_types(app, client):
@ -1548,29 +1544,21 @@ def test_server_name_subdomain():
assert rv.data == b"subdomain"
@pytest.mark.filterwarnings("ignore::pytest.PytestUnraisableExceptionWarning")
@pytest.mark.filterwarnings("ignore::pytest.PytestUnhandledThreadExceptionWarning")
def test_exception_propagation(app, client):
def apprunner(config_key):
@app.route("/")
def index():
1 // 0
@pytest.mark.parametrize("key", ["TESTING", "PROPAGATE_EXCEPTIONS", "DEBUG", None])
def test_exception_propagation(app, client, key):
app.testing = False
if config_key is not None:
app.config[config_key] = True
with pytest.raises(Exception):
client.get("/")
else:
assert client.get("/").status_code == 500
@app.route("/")
def index():
1 // 0
# we have to run this test in an isolated thread because if the
# debug flag is set to true and an exception happens the context is
# not torn down. This causes other tests that run after this fail
# when they expect no exception on the stack.
for config_key in "TESTING", "PROPAGATE_EXCEPTIONS", "DEBUG", None:
t = Thread(target=apprunner, args=(config_key,))
t.start()
t.join()
if key is not None:
app.config[key] = True
with pytest.raises(ZeroDivisionError):
client.get("/")
else:
assert client.get("/").status_code == 500
@pytest.mark.parametrize("debug", [True, False])

View file

@ -3,6 +3,7 @@ import warnings
import pytest
import flask
from flask.globals import request_ctx
from flask.sessions import SecureCookieSessionInterface
from flask.sessions import SessionInterface
@ -116,7 +117,7 @@ def test_context_binding(app):
assert index() == "Hello World!"
with app.test_request_context("/meh"):
assert meh() == "http://localhost/meh"
assert flask._request_ctx_stack.top is None
assert not flask.request
def test_context_test(app):
@ -152,7 +153,7 @@ class TestGreenletContextCopying:
@app.route("/")
def index():
flask.session["fizz"] = "buzz"
reqctx = flask._request_ctx_stack.top.copy()
reqctx = request_ctx.copy()
def g():
assert not flask.request

View file

@ -1,4 +1,5 @@
import flask
from flask.globals import request_ctx
from flask.sessions import SessionInterface
@ -13,7 +14,7 @@ def test_open_session_with_endpoint():
pass
def open_session(self, app, request):
flask._request_ctx_stack.top.match_request()
request_ctx.match_request()
assert request.endpoint is not None
app = flask.Flask(__name__)

View file

@ -5,6 +5,7 @@ import werkzeug
import flask
from flask import appcontext_popped
from flask.cli import ScriptInfo
from flask.globals import _cv_req
from flask.json import jsonify
from flask.testing import EnvironBuilder
from flask.testing import FlaskCliRunner
@ -399,4 +400,4 @@ def test_client_pop_all_preserved(app, req_ctx, client):
# close the response, releasing the context held by stream_with_context
rv.close()
# only req_ctx fixture should still be pushed
assert flask._request_ctx_stack.top is req_ctx
assert _cv_req.get(None) is req_ctx