mirror of https://github.com/mitsuhiko/flask.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
137 lines
5.1 KiB
137 lines
5.1 KiB
13 years ago
|
# -*- coding: utf-8 -*-
|
||
|
"""
|
||
10 years ago
|
tests.ext
|
||
13 years ago
|
~~~~~~~~~~~~~~~~~~~
|
||
|
|
||
|
Tests the extension import thing.
|
||
|
|
||
11 years ago
|
:copyright: (c) 2014 by Armin Ronacher.
|
||
13 years ago
|
:license: BSD, see LICENSE for more details.
|
||
|
"""
|
||
13 years ago
|
|
||
13 years ago
|
import sys
|
||
|
import unittest
|
||
12 years ago
|
try:
|
||
|
from imp import reload as reload_module
|
||
|
except ImportError:
|
||
|
reload_module = reload
|
||
10 years ago
|
from tests import FlaskTestCase
|
||
12 years ago
|
from flask._compat import PY2
|
||
13 years ago
|
|
||
|
class ExtImportHookTestCase(FlaskTestCase):
|
||
|
|
||
|
def setup(self):
|
||
13 years ago
|
# we clear this out for various reasons. The most important one is
|
||
|
# that a real flaskext could be in there which would disable our
|
||
|
# fake package. Secondly we want to make sure that the flaskext
|
||
|
# import hook does not break on reloading.
|
||
12 years ago
|
for entry, value in list(sys.modules.items()):
|
||
13 years ago
|
if (entry.startswith('flask.ext.') or
|
||
|
entry.startswith('flask_') or
|
||
13 years ago
|
entry.startswith('flaskext.') or
|
||
|
entry == 'flaskext') and value is not None:
|
||
13 years ago
|
sys.modules.pop(entry, None)
|
||
|
from flask import ext
|
||
12 years ago
|
reload_module(ext)
|
||
13 years ago
|
|
||
|
# reloading must not add more hooks
|
||
|
import_hooks = 0
|
||
|
for item in sys.meta_path:
|
||
|
cls = type(item)
|
||
13 years ago
|
if cls.__module__ == 'flask.exthook' and \
|
||
|
cls.__name__ == 'ExtensionImporter':
|
||
13 years ago
|
import_hooks += 1
|
||
|
self.assert_equal(import_hooks, 1)
|
||
|
|
||
13 years ago
|
def teardown(self):
|
||
|
from flask import ext
|
||
|
for key in ext.__dict__:
|
||
12 years ago
|
self.assert_not_in('.', key)
|
||
13 years ago
|
|
||
13 years ago
|
def test_flaskext_new_simple_import_normal(self):
|
||
13 years ago
|
from flask.ext.newext_simple import ext_id
|
||
|
self.assert_equal(ext_id, 'newext_simple')
|
||
|
|
||
13 years ago
|
def test_flaskext_new_simple_import_module(self):
|
||
13 years ago
|
from flask.ext import newext_simple
|
||
|
self.assert_equal(newext_simple.ext_id, 'newext_simple')
|
||
|
self.assert_equal(newext_simple.__name__, 'flask_newext_simple')
|
||
|
|
||
13 years ago
|
def test_flaskext_new_package_import_normal(self):
|
||
13 years ago
|
from flask.ext.newext_package import ext_id
|
||
|
self.assert_equal(ext_id, 'newext_package')
|
||
|
|
||
13 years ago
|
def test_flaskext_new_package_import_module(self):
|
||
13 years ago
|
from flask.ext import newext_package
|
||
|
self.assert_equal(newext_package.ext_id, 'newext_package')
|
||
|
self.assert_equal(newext_package.__name__, 'flask_newext_package')
|
||
|
|
||
13 years ago
|
def test_flaskext_new_package_import_submodule_function(self):
|
||
|
from flask.ext.newext_package.submodule import test_function
|
||
|
self.assert_equal(test_function(), 42)
|
||
|
|
||
13 years ago
|
def test_flaskext_new_package_import_submodule(self):
|
||
13 years ago
|
from flask.ext.newext_package import submodule
|
||
|
self.assert_equal(submodule.__name__, 'flask_newext_package.submodule')
|
||
|
self.assert_equal(submodule.test_function(), 42)
|
||
|
|
||
13 years ago
|
def test_flaskext_old_simple_import_normal(self):
|
||
|
from flask.ext.oldext_simple import ext_id
|
||
|
self.assert_equal(ext_id, 'oldext_simple')
|
||
|
|
||
|
def test_flaskext_old_simple_import_module(self):
|
||
|
from flask.ext import oldext_simple
|
||
|
self.assert_equal(oldext_simple.ext_id, 'oldext_simple')
|
||
|
self.assert_equal(oldext_simple.__name__, 'flaskext.oldext_simple')
|
||
|
|
||
|
def test_flaskext_old_package_import_normal(self):
|
||
|
from flask.ext.oldext_package import ext_id
|
||
|
self.assert_equal(ext_id, 'oldext_package')
|
||
|
|
||
|
def test_flaskext_old_package_import_module(self):
|
||
|
from flask.ext import oldext_package
|
||
|
self.assert_equal(oldext_package.ext_id, 'oldext_package')
|
||
|
self.assert_equal(oldext_package.__name__, 'flaskext.oldext_package')
|
||
|
|
||
|
def test_flaskext_old_package_import_submodule(self):
|
||
|
from flask.ext.oldext_package import submodule
|
||
|
self.assert_equal(submodule.__name__, 'flaskext.oldext_package.submodule')
|
||
|
self.assert_equal(submodule.test_function(), 42)
|
||
|
|
||
13 years ago
|
def test_flaskext_old_package_import_submodule_function(self):
|
||
|
from flask.ext.oldext_package.submodule import test_function
|
||
|
self.assert_equal(test_function(), 42)
|
||
|
|
||
13 years ago
|
def test_flaskext_broken_package_no_module_caching(self):
|
||
12 years ago
|
for x in range(2):
|
||
13 years ago
|
with self.assert_raises(ImportError):
|
||
|
import flask.ext.broken
|
||
|
|
||
13 years ago
|
def test_no_error_swallowing(self):
|
||
|
try:
|
||
|
import flask.ext.broken
|
||
|
except ImportError:
|
||
|
exc_type, exc_value, tb = sys.exc_info()
|
||
12 years ago
|
self.assert_true(exc_type is ImportError)
|
||
12 years ago
|
if PY2:
|
||
|
message = 'No module named missing_module'
|
||
|
else:
|
||
|
message = 'No module named \'missing_module\''
|
||
|
self.assert_equal(str(exc_value), message)
|
||
12 years ago
|
self.assert_true(tb.tb_frame.f_globals is globals())
|
||
13 years ago
|
|
||
12 years ago
|
# reraise() adds a second frame so we need to skip that one too.
|
||
12 years ago
|
# On PY3 we even have another one :(
|
||
12 years ago
|
next = tb.tb_next.tb_next
|
||
12 years ago
|
if not PY2:
|
||
|
next = next.tb_next
|
||
11 years ago
|
|
||
|
import os.path
|
||
|
self.assert_in(os.path.join('flask_broken', '__init__.py'), next.tb_frame.f_code.co_filename)
|
||
13 years ago
|
|
||
13 years ago
|
|
||
|
def suite():
|
||
|
suite = unittest.TestSuite()
|
||
|
suite.addTest(unittest.makeSuite(ExtImportHookTestCase))
|
||
|
return suite
|