Browse Source

Merge pull request #1878 from untitaker/recwarn

Use recwarn everywhere
pull/1882/head
Markus Unterwaditzer 9 years ago
parent
commit
3d72099dcd
  1. 11
      tests/conftest.py
  2. 11
      tests/test_basic.py
  3. 20
      tests/test_deprecations.py
  4. 12
      tests/test_ext.py
  5. 125
      tests/test_helpers.py

11
tests/conftest.py

@ -7,6 +7,7 @@
:license: BSD, see LICENSE for more details. :license: BSD, see LICENSE for more details.
""" """
import flask import flask
import gc
import os import os
import sys import sys
import pkgutil import pkgutil
@ -126,8 +127,8 @@ def purge_module(request):
return inner return inner
@pytest.fixture @pytest.yield_fixture(autouse=True)
def catch_deprecation_warnings(): def catch_deprecation_warnings(recwarn):
import warnings yield
warnings.simplefilter('default', category=DeprecationWarning) gc.collect()
return lambda: warnings.catch_warnings(record=True) assert not recwarn.list

11
tests/test_basic.py

@ -1116,12 +1116,15 @@ def test_static_files():
rv.close() rv.close()
def test_static_path_deprecated(): def test_static_path_deprecated(recwarn):
with pytest.deprecated_call(): app = flask.Flask(__name__, static_path='/foo')
app = flask.Flask(__name__, static_path='/foo') recwarn.pop(DeprecationWarning)
app.testing = True app.testing = True
rv = app.test_client().get('/foo/index.html') rv = app.test_client().get('/foo/index.html')
assert rv.status_code == 200 assert rv.status_code == 200
rv.close()
with app.test_request_context(): with app.test_request_context():
assert flask.url_for('static', filename='index.html') == '/foo/index.html' assert flask.url_for('static', filename='index.html') == '/foo/index.html'
@ -1131,6 +1134,8 @@ def test_static_url_path():
app.testing = True app.testing = True
rv = app.test_client().get('/foo/index.html') rv = app.test_client().get('/foo/index.html')
assert rv.status_code == 200 assert rv.status_code == 200
rv.close()
with app.test_request_context(): with app.test_request_context():
assert flask.url_for('static', filename='index.html') == '/foo/index.html' assert flask.url_for('static', filename='index.html') == '/foo/index.html'

20
tests/test_deprecations.py

@ -16,7 +16,7 @@ import flask
class TestRequestDeprecation(object): class TestRequestDeprecation(object):
def test_request_json(self, catch_deprecation_warnings): def test_request_json(self, recwarn):
"""Request.json is deprecated""" """Request.json is deprecated"""
app = flask.Flask(__name__) app = flask.Flask(__name__)
app.testing = True app.testing = True
@ -27,13 +27,11 @@ class TestRequestDeprecation(object):
print(flask.request.json) print(flask.request.json)
return 'OK' return 'OK'
with catch_deprecation_warnings() as captured: c = app.test_client()
c = app.test_client() c.post('/', data='{"spam": 42}', content_type='application/json')
c.post('/', data='{"spam": 42}', content_type='application/json') recwarn.pop(DeprecationWarning)
assert len(captured) == 1 def test_request_module(self, recwarn):
def test_request_module(self, catch_deprecation_warnings):
"""Request.module is deprecated""" """Request.module is deprecated"""
app = flask.Flask(__name__) app = flask.Flask(__name__)
app.testing = True app.testing = True
@ -43,8 +41,6 @@ class TestRequestDeprecation(object):
assert flask.request.module is None assert flask.request.module is None
return 'OK' return 'OK'
with catch_deprecation_warnings() as captured: c = app.test_client()
c = app.test_client() c.get('/')
c.get('/') recwarn.pop(DeprecationWarning)
assert len(captured) == 1

12
tests/test_ext.py

@ -20,6 +20,18 @@ except ImportError:
from flask._compat import PY2 from flask._compat import PY2
@pytest.fixture(autouse=True)
def disable_extwarnings(request, recwarn):
from flask.exthook import ExtDeprecationWarning
def inner():
assert set(w.category for w in recwarn.list) \
<= set([ExtDeprecationWarning])
recwarn.clear()
request.addfinalizer(inner)
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def importhook_setup(monkeypatch, request): def importhook_setup(monkeypatch, request):
# we clear this out for various reasons. The most important one is # we clear this out for various reasons. The most important one is

125
tests/test_helpers.py

@ -337,7 +337,7 @@ class TestSendfile(object):
assert rv.data == f.read() assert rv.data == f.read()
rv.close() rv.close()
def test_send_file_xsendfile(self): def test_send_file_xsendfile(self, catch_deprecation_warnings):
app = flask.Flask(__name__) app = flask.Flask(__name__)
app.use_x_sendfile = True app.use_x_sendfile = True
with app.test_request_context(): with app.test_request_context():
@ -349,90 +349,101 @@ class TestSendfile(object):
assert rv.mimetype == 'text/html' assert rv.mimetype == 'text/html'
rv.close() rv.close()
def test_send_file_object(self, catch_deprecation_warnings): def test_send_file_object(self, recwarn):
app = flask.Flask(__name__) app = flask.Flask(__name__)
with catch_deprecation_warnings() as captured:
with app.test_request_context(): with app.test_request_context():
f = open(os.path.join(app.root_path, 'static/index.html'), mode='rb') with open(os.path.join(app.root_path, 'static/index.html'), mode='rb') as f:
rv = flask.send_file(f) rv = flask.send_file(f)
rv.direct_passthrough = False rv.direct_passthrough = False
with app.open_resource('static/index.html') as f: with app.open_resource('static/index.html') as f:
assert rv.data == f.read() assert rv.data == f.read()
assert rv.mimetype == 'text/html' assert rv.mimetype == 'text/html'
rv.close() rv.close()
# mimetypes + etag
assert len(captured) == 2 # mimetypes + etag
recwarn.pop(DeprecationWarning)
recwarn.pop(DeprecationWarning)
app.use_x_sendfile = True app.use_x_sendfile = True
with catch_deprecation_warnings() as captured:
with app.test_request_context(): with app.test_request_context():
f = open(os.path.join(app.root_path, 'static/index.html')) with open(os.path.join(app.root_path, 'static/index.html')) as f:
rv = flask.send_file(f) rv = flask.send_file(f)
assert rv.mimetype == 'text/html' assert rv.mimetype == 'text/html'
assert 'x-sendfile' in rv.headers assert 'x-sendfile' in rv.headers
assert rv.headers['x-sendfile'] == \ assert rv.headers['x-sendfile'] == \
os.path.join(app.root_path, 'static/index.html') os.path.join(app.root_path, 'static/index.html')
rv.close() rv.close()
# mimetypes + etag
assert len(captured) == 2 # mimetypes + etag
recwarn.pop(DeprecationWarning)
recwarn.pop(DeprecationWarning)
app.use_x_sendfile = False app.use_x_sendfile = False
with app.test_request_context(): with app.test_request_context():
with catch_deprecation_warnings() as captured: f = StringIO('Test')
f = StringIO('Test') rv = flask.send_file(f)
rv = flask.send_file(f) rv.direct_passthrough = False
rv.direct_passthrough = False assert rv.data == b'Test'
assert rv.data == b'Test' assert rv.mimetype == 'application/octet-stream'
assert rv.mimetype == 'application/octet-stream' rv.close()
rv.close()
# etags # etags
assert len(captured) == 1 recwarn.pop(DeprecationWarning)
with catch_deprecation_warnings() as captured:
class PyStringIO(object): class PyStringIO(object):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self._io = StringIO(*args, **kwargs) self._io = StringIO(*args, **kwargs)
def __getattr__(self, name): def __getattr__(self, name):
return getattr(self._io, name) return getattr(self._io, name)
f = PyStringIO('Test') f = PyStringIO('Test')
f.name = 'test.txt' f.name = 'test.txt'
rv = flask.send_file(f) rv = flask.send_file(f)
rv.direct_passthrough = False rv.direct_passthrough = False
assert rv.data == b'Test' assert rv.data == b'Test'
assert rv.mimetype == 'text/plain' assert rv.mimetype == 'text/plain'
rv.close() rv.close()
# attachment_filename and etags # attachment_filename and etags
assert len(captured) == 3 a = recwarn.pop(DeprecationWarning)
with catch_deprecation_warnings() as captured: b = recwarn.pop(DeprecationWarning)
f = StringIO('Test') c = recwarn.pop(UserWarning) # file not found
rv = flask.send_file(f, mimetype='text/plain')
rv.direct_passthrough = False f = StringIO('Test')
assert rv.data == b'Test' rv = flask.send_file(f, mimetype='text/plain')
assert rv.mimetype == 'text/plain' rv.direct_passthrough = False
rv.close() assert rv.data == b'Test'
assert rv.mimetype == 'text/plain'
rv.close()
# etags # etags
assert len(captured) == 1 recwarn.pop(DeprecationWarning)
app.use_x_sendfile = True app.use_x_sendfile = True
with catch_deprecation_warnings() as captured:
with app.test_request_context():
f = StringIO('Test')
rv = flask.send_file(f)
assert 'x-sendfile' not in rv.headers
rv.close()
# etags
assert len(captured) == 1
def test_attachment(self, catch_deprecation_warnings): with app.test_request_context():
f = StringIO('Test')
rv = flask.send_file(f)
assert 'x-sendfile' not in rv.headers
rv.close()
# etags
recwarn.pop(DeprecationWarning)
def test_attachment(self, recwarn):
app = flask.Flask(__name__) app = flask.Flask(__name__)
with catch_deprecation_warnings() as captured: with app.test_request_context():
with app.test_request_context(): with open(os.path.join(app.root_path, 'static/index.html')) as f:
f = open(os.path.join(app.root_path, 'static/index.html'))
rv = flask.send_file(f, as_attachment=True) rv = flask.send_file(f, as_attachment=True)
value, options = parse_options_header(rv.headers['Content-Disposition']) value, options = \
parse_options_header(rv.headers['Content-Disposition'])
assert value == 'attachment' assert value == 'attachment'
rv.close() rv.close()
# mimetypes + etag
assert len(captured) == 2 # mimetypes + etag
assert len(recwarn.list) == 2
recwarn.clear()
with app.test_request_context(): with app.test_request_context():
assert options['filename'] == 'index.html' assert options['filename'] == 'index.html'

Loading…
Cancel
Save