| 
									
										
										
										
											2011-09-18 08:10:10 +08:00
										 |  |  | # -*- coding: utf-8 -*- | 
					
						
							|  |  |  | """
 | 
					
						
							| 
									
										
										
										
											2014-09-01 03:54:45 +08:00
										 |  |  |     tests.ext | 
					
						
							| 
									
										
										
										
											2011-09-18 08:10:10 +08:00
										 |  |  |     ~~~~~~~~~~~~~~~~~~~ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Tests the extension import thing. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-03 02:21:07 +08:00
										 |  |  |     :copyright: (c) 2014 by Armin Ronacher. | 
					
						
							| 
									
										
										
										
											2011-09-18 08:10:10 +08:00
										 |  |  |     :license: BSD, see LICENSE for more details. | 
					
						
							|  |  |  | """
 | 
					
						
							| 
									
										
										
										
											2011-09-22 03:34:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-18 08:10:10 +08:00
										 |  |  | import sys | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  | import pytest | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-23 03:40:30 +08:00
										 |  |  | try: | 
					
						
							|  |  |  |     from imp import reload as reload_module | 
					
						
							|  |  |  | except ImportError: | 
					
						
							|  |  |  |     reload_module = reload | 
					
						
							| 
									
										
										
										
											2014-09-04 03:02:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-23 03:03:01 +08:00
										 |  |  | from flask._compat import PY2 | 
					
						
							| 
									
										
										
										
											2011-09-18 08:10:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 02:50:54 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | @pytest.fixture(autouse=True) | 
					
						
							|  |  |  | def importhook_setup(monkeypatch, request): | 
					
						
							|  |  |  |     # 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. | 
					
						
							|  |  |  |     for entry, value in list(sys.modules.items()): | 
					
						
							| 
									
										
										
										
											2014-09-04 21:05:22 +08:00
										 |  |  |         if ( | 
					
						
							|  |  |  |             entry.startswith('flask.ext.') or | 
					
						
							| 
									
										
										
										
											2014-09-04 02:50:54 +08:00
										 |  |  |             entry.startswith('flask_') or | 
					
						
							|  |  |  |             entry.startswith('flaskext.') or | 
					
						
							| 
									
										
										
										
											2014-09-04 21:05:22 +08:00
										 |  |  |             entry == 'flaskext' | 
					
						
							|  |  |  |         ) and value is not None: | 
					
						
							| 
									
										
										
										
											2014-09-04 02:50:54 +08:00
										 |  |  |             monkeypatch.delitem(sys.modules, entry) | 
					
						
							|  |  |  |     from flask import ext | 
					
						
							|  |  |  |     reload_module(ext) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # reloading must not add more hooks | 
					
						
							|  |  |  |     import_hooks = 0 | 
					
						
							|  |  |  |     for item in sys.meta_path: | 
					
						
							|  |  |  |         cls = type(item) | 
					
						
							|  |  |  |         if cls.__module__ == 'flask.exthook' and \ | 
					
						
							|  |  |  |            cls.__name__ == 'ExtensionImporter': | 
					
						
							|  |  |  |             import_hooks += 1 | 
					
						
							|  |  |  |     assert import_hooks == 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def teardown(): | 
					
						
							| 
									
										
										
										
											2011-09-18 18:22:42 +08:00
										 |  |  |         from flask import ext | 
					
						
							|  |  |  |         for key in ext.__dict__: | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert '.' not in key | 
					
						
							| 
									
										
										
										
											2011-09-18 18:22:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 02:50:54 +08:00
										 |  |  |     request.addfinalizer(teardown) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:05:22 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | @pytest.fixture | 
					
						
							| 
									
										
										
										
											2014-09-04 21:22:57 +08:00
										 |  |  | def newext_simple(modules_tmpdir): | 
					
						
							|  |  |  |     x = modules_tmpdir.join('flask_newext_simple.py') | 
					
						
							| 
									
										
										
										
											2014-09-04 21:05:22 +08:00
										 |  |  |     x.write('ext_id = "newext_simple"') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @pytest.fixture | 
					
						
							| 
									
										
										
										
											2014-09-04 21:22:57 +08:00
										 |  |  | def oldext_simple(modules_tmpdir): | 
					
						
							|  |  |  |     flaskext = modules_tmpdir.mkdir('flaskext') | 
					
						
							| 
									
										
										
										
											2014-09-04 21:05:22 +08:00
										 |  |  |     flaskext.join('__init__.py').write('\n') | 
					
						
							|  |  |  |     flaskext.join('oldext_simple.py').write('ext_id = "oldext_simple"') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @pytest.fixture | 
					
						
							| 
									
										
										
										
											2014-09-04 21:22:57 +08:00
										 |  |  | def newext_package(modules_tmpdir): | 
					
						
							|  |  |  |     pkg = modules_tmpdir.mkdir('flask_newext_package') | 
					
						
							| 
									
										
										
										
											2014-09-04 21:05:22 +08:00
										 |  |  |     pkg.join('__init__.py').write('ext_id = "newext_package"') | 
					
						
							|  |  |  |     pkg.join('submodule.py').write('def test_function():\n    return 42\n') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @pytest.fixture | 
					
						
							| 
									
										
										
										
											2014-09-04 21:22:57 +08:00
										 |  |  | def oldext_package(modules_tmpdir): | 
					
						
							|  |  |  |     flaskext = modules_tmpdir.mkdir('flaskext') | 
					
						
							| 
									
										
										
										
											2014-09-04 21:05:22 +08:00
										 |  |  |     flaskext.join('__init__.py').write('\n') | 
					
						
							|  |  |  |     oldext = flaskext.mkdir('oldext_package') | 
					
						
							|  |  |  |     oldext.join('__init__.py').write('ext_id = "oldext_package"') | 
					
						
							|  |  |  |     oldext.join('submodule.py').write('def test_function():\n' | 
					
						
							|  |  |  |                                       '    return 42') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @pytest.fixture | 
					
						
							| 
									
										
										
										
											2014-09-04 21:22:57 +08:00
										 |  |  | def flaskext_broken(modules_tmpdir): | 
					
						
							|  |  |  |     ext = modules_tmpdir.mkdir('flask_broken') | 
					
						
							| 
									
										
										
										
											2014-09-04 21:05:22 +08:00
										 |  |  |     ext.join('b.py').write('\n') | 
					
						
							|  |  |  |     ext.join('__init__.py').write('import flask.ext.broken.b\n' | 
					
						
							|  |  |  |                                   'import missing_module') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_flaskext_new_simple_import_normal(newext_simple): | 
					
						
							| 
									
										
										
										
											2014-09-04 02:50:54 +08:00
										 |  |  |     from flask.ext.newext_simple import ext_id | 
					
						
							|  |  |  |     assert ext_id == 'newext_simple' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:05:22 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | def test_flaskext_new_simple_import_module(newext_simple): | 
					
						
							| 
									
										
										
										
											2014-09-04 02:50:54 +08:00
										 |  |  |     from flask.ext import newext_simple | 
					
						
							|  |  |  |     assert newext_simple.ext_id == 'newext_simple' | 
					
						
							|  |  |  |     assert newext_simple.__name__ == 'flask_newext_simple' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:05:22 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | def test_flaskext_new_package_import_normal(newext_package): | 
					
						
							| 
									
										
										
										
											2014-09-04 02:50:54 +08:00
										 |  |  |     from flask.ext.newext_package import ext_id | 
					
						
							|  |  |  |     assert ext_id == 'newext_package' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:05:22 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | def test_flaskext_new_package_import_module(newext_package): | 
					
						
							| 
									
										
										
										
											2014-09-04 02:50:54 +08:00
										 |  |  |     from flask.ext import newext_package | 
					
						
							|  |  |  |     assert newext_package.ext_id == 'newext_package' | 
					
						
							|  |  |  |     assert newext_package.__name__ == 'flask_newext_package' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:05:22 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | def test_flaskext_new_package_import_submodule_function(newext_package): | 
					
						
							| 
									
										
										
										
											2014-09-04 02:50:54 +08:00
										 |  |  |     from flask.ext.newext_package.submodule import test_function | 
					
						
							|  |  |  |     assert test_function() == 42 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:05:22 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | def test_flaskext_new_package_import_submodule(newext_package): | 
					
						
							| 
									
										
										
										
											2014-09-04 02:50:54 +08:00
										 |  |  |     from flask.ext.newext_package import submodule | 
					
						
							|  |  |  |     assert submodule.__name__ == 'flask_newext_package.submodule' | 
					
						
							|  |  |  |     assert submodule.test_function() == 42 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:05:22 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | def test_flaskext_old_simple_import_normal(oldext_simple): | 
					
						
							| 
									
										
										
										
											2014-09-04 02:50:54 +08:00
										 |  |  |     from flask.ext.oldext_simple import ext_id | 
					
						
							|  |  |  |     assert ext_id == 'oldext_simple' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:05:22 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | def test_flaskext_old_simple_import_module(oldext_simple): | 
					
						
							| 
									
										
										
										
											2014-09-04 02:50:54 +08:00
										 |  |  |     from flask.ext import oldext_simple | 
					
						
							|  |  |  |     assert oldext_simple.ext_id == 'oldext_simple' | 
					
						
							|  |  |  |     assert oldext_simple.__name__ == 'flaskext.oldext_simple' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:05:22 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | def test_flaskext_old_package_import_normal(oldext_package): | 
					
						
							| 
									
										
										
										
											2014-09-04 02:50:54 +08:00
										 |  |  |     from flask.ext.oldext_package import ext_id | 
					
						
							|  |  |  |     assert ext_id == 'oldext_package' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:05:22 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | def test_flaskext_old_package_import_module(oldext_package): | 
					
						
							| 
									
										
										
										
											2014-09-04 02:50:54 +08:00
										 |  |  |     from flask.ext import oldext_package | 
					
						
							|  |  |  |     assert oldext_package.ext_id == 'oldext_package' | 
					
						
							|  |  |  |     assert oldext_package.__name__ == 'flaskext.oldext_package' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:05:22 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | def test_flaskext_old_package_import_submodule(oldext_package): | 
					
						
							| 
									
										
										
										
											2014-09-04 02:50:54 +08:00
										 |  |  |     from flask.ext.oldext_package import submodule | 
					
						
							|  |  |  |     assert submodule.__name__ == 'flaskext.oldext_package.submodule' | 
					
						
							|  |  |  |     assert submodule.test_function() == 42 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:05:22 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | def test_flaskext_old_package_import_submodule_function(oldext_package): | 
					
						
							| 
									
										
										
										
											2014-09-04 02:50:54 +08:00
										 |  |  |     from flask.ext.oldext_package.submodule import test_function | 
					
						
							|  |  |  |     assert test_function() == 42 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:05:22 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | def test_flaskext_broken_package_no_module_caching(flaskext_broken): | 
					
						
							| 
									
										
										
										
											2014-09-04 02:50:54 +08:00
										 |  |  |     for x in range(2): | 
					
						
							|  |  |  |         with pytest.raises(ImportError): | 
					
						
							| 
									
										
										
										
											2011-09-27 19:33:23 +08:00
										 |  |  |             import flask.ext.broken | 
					
						
							| 
									
										
										
										
											2014-09-04 02:50:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:05:22 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | def test_no_error_swallowing(flaskext_broken): | 
					
						
							|  |  |  |     with pytest.raises(ImportError) as excinfo: | 
					
						
							| 
									
										
										
										
											2014-09-04 02:50:54 +08:00
										 |  |  |         import flask.ext.broken | 
					
						
							| 
									
										
										
										
											2014-09-04 21:05:22 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     assert excinfo.type is ImportError | 
					
						
							|  |  |  |     if PY2: | 
					
						
							|  |  |  |         message = 'No module named missing_module' | 
					
						
							| 
									
										
										
										
											2014-09-04 02:50:54 +08:00
										 |  |  |     else: | 
					
						
							| 
									
										
										
										
											2014-09-04 21:05:22 +08:00
										 |  |  |         message = 'No module named \'missing_module\'' | 
					
						
							|  |  |  |     assert str(excinfo.value) == message | 
					
						
							|  |  |  |     assert excinfo.tb.tb_frame.f_globals is globals() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # reraise() adds a second frame so we need to skip that one too. | 
					
						
							|  |  |  |     # On PY3 we even have another one :( | 
					
						
							|  |  |  |     next = excinfo.tb.tb_next.tb_next | 
					
						
							|  |  |  |     if not PY2: | 
					
						
							|  |  |  |         next = next.tb_next | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     import os.path | 
					
						
							|  |  |  |     assert os.path.join('flask_broken', '__init__.py') in \ | 
					
						
							|  |  |  |         next.tb_frame.f_code.co_filename |