From 192da325f8b171e7512bea13edd4ddd3330a6ca8 Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Sat, 6 Sep 2014 02:20:00 +0200 Subject: [PATCH] Testsuite is not a package --- tests/__init__.py | 62 ----------------------------------- tests/conftest.py | 7 ++++ tests/test_helpers.py | 46 +++++++++++--------------- tests/test_instance_config.py | 16 +++++---- 4 files changed, 35 insertions(+), 96 deletions(-) delete mode 100644 tests/__init__.py diff --git a/tests/__init__.py b/tests/__init__.py deleted file mode 100644 index d8985e5f..00000000 --- a/tests/__init__.py +++ /dev/null @@ -1,62 +0,0 @@ -# -*- coding: utf-8 -*- -""" - tests - ~~~~~~~~~~~~~~~ - - Tests Flask itself. The majority of Flask is already tested - as part of Werkzeug. - - :copyright: (c) 2014 by Armin Ronacher. - :license: BSD, see LICENSE for more details. -""" - -from __future__ import print_function - -import os -import sys -import warnings -from functools import update_wrapper -from contextlib import contextmanager -from flask._compat import StringIO - - -@contextmanager -def catch_warnings(): - """Catch warnings in a with block in a list""" - # make sure deprecation warnings are active in tests - warnings.simplefilter('default', category=DeprecationWarning) - - filters = warnings.filters - warnings.filters = filters[:] - old_showwarning = warnings.showwarning - log = [] - - def showwarning(message, category, filename, lineno, file=None, line=None): - log.append(locals()) - try: - warnings.showwarning = showwarning - yield log - finally: - warnings.filters = filters - warnings.showwarning = old_showwarning - - -@contextmanager -def catch_stderr(): - """Catch stderr in a StringIO""" - old_stderr = sys.stderr - sys.stderr = rv = StringIO() - try: - yield rv - finally: - sys.stderr = old_stderr - - -def emits_module_deprecation_warning(f): - def new_f(self, *args, **kwargs): - with catch_warnings() as log: - f(self, *args, **kwargs) - assert log, 'expected deprecation warning' - for entry in log: - assert 'Modules are deprecated' in str(entry['message']) - return update_wrapper(new_f, f) diff --git a/tests/conftest.py b/tests/conftest.py index aa3782cf..adbf3629 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -125,3 +125,10 @@ def purge_module(request): def inner(name): request.addfinalizer(lambda: sys.modules.pop(name, None)) return inner + + +@pytest.fixture +def catch_deprecation_warnings(): + import warnings + warnings.simplefilter('default', category=DeprecationWarning) + return lambda: warnings.catch_warnings(record=True) diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 872906d8..e9738f76 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -13,9 +13,7 @@ import pytest import os import flask -import unittest from logging import StreamHandler -from tests import catch_warnings, catch_stderr from werkzeug.http import parse_cache_control_header, parse_options_header from flask._compat import StringIO, text_type @@ -266,9 +264,9 @@ class TestSendfile(object): assert rv.mimetype == 'text/html' rv.close() - def test_send_file_object(self): + def test_send_file_object(self, catch_deprecation_warnings): app = flask.Flask(__name__) - with catch_warnings() as captured: + with catch_deprecation_warnings() as captured: with app.test_request_context(): f = open(os.path.join(app.root_path, 'static/index.html'), mode='rb') rv = flask.send_file(f) @@ -281,7 +279,7 @@ class TestSendfile(object): assert len(captured) == 2 app.use_x_sendfile = True - with catch_warnings() as captured: + with catch_deprecation_warnings() as captured: with app.test_request_context(): f = open(os.path.join(app.root_path, 'static/index.html')) rv = flask.send_file(f) @@ -295,7 +293,7 @@ class TestSendfile(object): app.use_x_sendfile = False with app.test_request_context(): - with catch_warnings() as captured: + with catch_deprecation_warnings() as captured: f = StringIO('Test') rv = flask.send_file(f) rv.direct_passthrough = False @@ -304,7 +302,7 @@ class TestSendfile(object): rv.close() # etags assert len(captured) == 1 - with catch_warnings() as captured: + with catch_deprecation_warnings() as captured: class PyStringIO(object): def __init__(self, *args, **kwargs): self._io = StringIO(*args, **kwargs) @@ -319,7 +317,7 @@ class TestSendfile(object): rv.close() # attachment_filename and etags assert len(captured) == 3 - with catch_warnings() as captured: + with catch_deprecation_warnings() as captured: f = StringIO('Test') rv = flask.send_file(f, mimetype='text/plain') rv.direct_passthrough = False @@ -330,7 +328,7 @@ class TestSendfile(object): assert len(captured) == 1 app.use_x_sendfile = True - with catch_warnings() as captured: + with catch_deprecation_warnings() as captured: with app.test_request_context(): f = StringIO('Test') rv = flask.send_file(f) @@ -339,9 +337,9 @@ class TestSendfile(object): # etags assert len(captured) == 1 - def test_attachment(self): + def test_attachment(self, catch_deprecation_warnings): app = flask.Flask(__name__) - with catch_warnings() as captured: + with catch_deprecation_warnings() as captured: with app.test_request_context(): f = open(os.path.join(app.root_path, 'static/index.html')) rv = flask.send_file(f, as_attachment=True) @@ -433,7 +431,7 @@ class TestLogging(object): app.logger_name = __name__ + '/test_logger_cache' assert app.logger is not logger1 - def test_debug_log(self): + def test_debug_log(self, capsys): app = flask.Flask(__name__) app.debug = True @@ -448,21 +446,15 @@ class TestLogging(object): 1 // 0 with app.test_client() as c: - with catch_stderr() as err: - c.get('/') - out = err.getvalue() - assert 'WARNING in test_helpers [' in out - assert os.path.basename(__file__.rsplit('.', 1)[0] + '.py') in out - assert 'the standard library is dead' in out - assert 'this is a debug statement' in out - - with catch_stderr() as err: - try: - c.get('/exc') - except ZeroDivisionError: - pass - else: - assert False, 'debug log ate the exception' + c.get('/') + out, err = capsys.readouterr() + assert 'WARNING in test_helpers [' in err + assert os.path.basename(__file__.rsplit('.', 1)[0] + '.py') in err + assert 'the standard library is dead' in err + assert 'this is a debug statement' in err + + with pytest.raises(ZeroDivisionError): + c.get('/exc') def test_debug_log_override(self): app = flask.Flask(__name__) diff --git a/tests/test_instance_config.py b/tests/test_instance_config.py index 522d674e..733ee099 100644 --- a/tests/test_instance_config.py +++ b/tests/test_instance_config.py @@ -119,14 +119,16 @@ def test_egg_installed_paths(install_egg, modules_tmpdir, @pytest.mark.skipif(not PY2, reason='This only works under Python 2.') -def test_meta_path_loader_without_is_package(): +def test_meta_path_loader_without_is_package(request, modules_tmpdir): + app = modules_tmpdir.join('unimportable.py') + app.write('import flask\napp = flask.Flask(__name__)') + class Loader(object): - def find_module(self, name): + def find_module(self, name, path=None): return self sys.meta_path.append(Loader()) - try: - with pytest.raises(AttributeError): - flask.Flask(__name__) - finally: - sys.meta_path.pop() + request.addfinalizer(sys.meta_path.pop) + + with pytest.raises(AttributeError): + import unimportable