From 6c359e0f532d18a71895f035dd1326ca5d2d67f8 Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Fri, 3 Jun 2016 14:19:25 +0200 Subject: [PATCH] Eliminate some resource warnings --- tests/conftest.py | 2 ++ tests/test_basic.py | 4 ++++ tests/test_helpers.py | 39 ++++++++++++++++++++------------------- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index cea73092..8c9541de 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -7,6 +7,7 @@ :license: BSD, see LICENSE for more details. """ import flask +import gc import os import sys import pkgutil @@ -129,4 +130,5 @@ def purge_module(request): @pytest.yield_fixture(autouse=True) def catch_deprecation_warnings(recwarn): yield + gc.collect() assert not recwarn.list diff --git a/tests/test_basic.py b/tests/test_basic.py index 57d8e729..95417c35 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -1123,6 +1123,8 @@ def test_static_path_deprecated(recwarn): app.testing = True rv = app.test_client().get('/foo/index.html') assert rv.status_code == 200 + rv.close() + with app.test_request_context(): assert flask.url_for('static', filename='index.html') == '/foo/index.html' @@ -1132,6 +1134,8 @@ def test_static_url_path(): app.testing = True rv = app.test_client().get('/foo/index.html') assert rv.status_code == 200 + rv.close() + with app.test_request_context(): assert flask.url_for('static', filename='index.html') == '/foo/index.html' diff --git a/tests/test_helpers.py b/tests/test_helpers.py index ac18d26c..1fec1d87 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -353,13 +353,13 @@ class TestSendfile(object): app = flask.Flask(__name__) with app.test_request_context(): - f = open(os.path.join(app.root_path, 'static/index.html'), mode='rb') - rv = flask.send_file(f) - rv.direct_passthrough = False - with app.open_resource('static/index.html') as f: - assert rv.data == f.read() - assert rv.mimetype == 'text/html' - rv.close() + with open(os.path.join(app.root_path, 'static/index.html'), mode='rb') as f: + rv = flask.send_file(f) + rv.direct_passthrough = False + with app.open_resource('static/index.html') as f: + assert rv.data == f.read() + assert rv.mimetype == 'text/html' + rv.close() # mimetypes + etag recwarn.pop(DeprecationWarning) @@ -368,13 +368,13 @@ class TestSendfile(object): app.use_x_sendfile = True with app.test_request_context(): - f = open(os.path.join(app.root_path, 'static/index.html')) - rv = flask.send_file(f) - assert rv.mimetype == 'text/html' - assert 'x-sendfile' in rv.headers - assert rv.headers['x-sendfile'] == \ - os.path.join(app.root_path, 'static/index.html') - rv.close() + with open(os.path.join(app.root_path, 'static/index.html')) as f: + rv = flask.send_file(f) + assert rv.mimetype == 'text/html' + assert 'x-sendfile' in rv.headers + assert rv.headers['x-sendfile'] == \ + os.path.join(app.root_path, 'static/index.html') + rv.close() # mimetypes + etag recwarn.pop(DeprecationWarning) @@ -434,11 +434,12 @@ class TestSendfile(object): def test_attachment(self, recwarn): app = flask.Flask(__name__) with app.test_request_context(): - f = open(os.path.join(app.root_path, 'static/index.html')) - rv = flask.send_file(f, as_attachment=True) - value, options = parse_options_header(rv.headers['Content-Disposition']) - assert value == 'attachment' - rv.close() + with open(os.path.join(app.root_path, 'static/index.html')) as f: + rv = flask.send_file(f, as_attachment=True) + value, options = \ + parse_options_header(rv.headers['Content-Disposition']) + assert value == 'attachment' + rv.close() # mimetypes + etag assert len(recwarn.list) == 2