Add test for limited imp loaders, #380.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Ron DuPlain · committed by Ron DuPlain · parent 56177bc · Browse files · 1 file·+87 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+87 added−0 removed
- other:+87 added−0 removed
Provenance
- Commit
b786eac- Tree
6b92bec- Author
Ron DuPlain <ron.duplain@gmail.com>- Committer
Ron DuPlain <ron.duplain@gmail.com>- Parent
56177bc
1 file changed+87 added−0 removed
modified:flask/testsuite/config.py+87 added
6 unmodified lines7 8 :copyright: (c) 2011 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10 """11+from __future__ import with_statement12+13 import os14 import sys15 import flask16+import pkgutil17 import unittest18+from contextlib import contextmanager19 from flask.testsuite import FlaskTestCase20 21 22 # config keys used for the ConfigTestCase64 unmodified lines87 app.config['PERMANENT_SESSION_LIFETIME'] = 4288 self.assert_equal(app.permanent_session_lifetime.seconds, 42)89 90 91+class LimitedLoaderMockWrapper(object):92+ def __init__(self, loader):93+ self.loader = loader94+95+ def __getattr__(self, name):96+ if name in ('archive', 'get_filename'):97+ msg = 'Mocking a loader which does not have `%s.`' % name98+ raise AttributeError, msg99+ return getattr(self.loader, name)100+101+102+@contextmanager103+def patch_pkgutil_get_loader(wrapper_class=LimitedLoaderMockWrapper):104+ """Patch pkgutil.get_loader to give loader without get_filename or archive.105+106+ This provides for tests where a system has custom loaders, e.g. Google App107+ Engine's HardenedModulesHook, which have neither the `get_filename` method108+ nor the `archive` attribute.109+ """110+ old_get_loader = pkgutil.get_loader111+ def get_loader(*args, **kwargs):112+ return wrapper_class(old_get_loader(*args, **kwargs))113+ try:114+ pkgutil.get_loader = get_loader115+ yield116+ finally:117+ pkgutil.get_loader = old_get_loader118+119+120 class InstanceTestCase(FlaskTestCase):121 122 def test_explicit_instance_paths(self):123 here = os.path.abspath(os.path.dirname(__file__))41 unmodified lines165 sys.path.remove(site_packages)166 if 'site_app' in sys.modules:167 del sys.modules['site_app']168 169+ def test_installed_module_paths_with_limited_loader(self):170+ here = os.path.abspath(os.path.dirname(__file__))171+ expected_prefix = os.path.join(here, 'test_apps')172+ real_prefix, sys.prefix = sys.prefix, expected_prefix173+ site_packages = os.path.join(expected_prefix, 'lib', 'python2.5', 'site-packages')174+ sys.path.append(site_packages)175+ with patch_pkgutil_get_loader():176+ try:177+ import site_app178+ self.assert_equal(site_app.app.instance_path,179+ os.path.join(expected_prefix, 'var',180+ 'site_app-instance'))181+ finally:182+ sys.prefix = real_prefix183+ sys.path.remove(site_packages)184+ if 'site_app' in sys.modules:185+ del sys.modules['site_app']186+187 def test_installed_package_paths(self):188 here = os.path.abspath(os.path.dirname(__file__))189 expected_prefix = os.path.join(here, 'test_apps')190 real_prefix, sys.prefix = sys.prefix, expected_prefix9 unmodified lines200 sys.path.remove(installed_path)201 if 'installed_package' in sys.modules:202 del sys.modules['installed_package']203 204+ def test_installed_package_paths_with_limited_loader(self):205+ here = os.path.abspath(os.path.dirname(__file__))206+ expected_prefix = os.path.join(here, 'test_apps')207+ real_prefix, sys.prefix = sys.prefix, expected_prefix208+ installed_path = os.path.join(expected_prefix, 'path')209+ sys.path.append(installed_path)210+ with patch_pkgutil_get_loader():211+ try:212+ import installed_package213+ self.assert_equal(installed_package.app.instance_path,214+ os.path.join(expected_prefix, 'var',215+ 'installed_package-instance'))216+ finally:217+ sys.prefix = real_prefix218+ sys.path.remove(installed_path)219+ if 'installed_package' in sys.modules:220+ del sys.modules['installed_package']221+222 def test_prefix_package_paths(self):223 here = os.path.abspath(os.path.dirname(__file__))224 expected_prefix = os.path.join(here, 'test_apps')225 real_prefix, sys.prefix = sys.prefix, expected_prefix9 unmodified lines235 sys.path.remove(site_packages)236 if 'site_package' in sys.modules:237 del sys.modules['site_package']238 239+ def test_prefix_package_paths_with_limited_loader(self):240+ here = os.path.abspath(os.path.dirname(__file__))241+ expected_prefix = os.path.join(here, 'test_apps')242+ real_prefix, sys.prefix = sys.prefix, expected_prefix243+ site_packages = os.path.join(expected_prefix, 'lib', 'python2.5', 'site-packages')244+ sys.path.append(site_packages)245+ with patch_pkgutil_get_loader():246+ try:247+ import site_package248+ self.assert_equal(site_package.app.instance_path,249+ os.path.join(expected_prefix, 'var',250+ 'site_package-instance'))251+ finally:252+ sys.prefix = real_prefix253+ sys.path.remove(site_packages)254+ if 'site_package' in sys.modules:255+ del sys.modules['site_package']256+257 def test_egg_installed_paths(self):258 here = os.path.abspath(os.path.dirname(__file__))259 expected_prefix = os.path.join(here, 'test_apps')260 real_prefix, sys.prefix = sys.prefix, expected_prefix22 unmodified lines