Skip to content
Myr

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

1 file+87 added−0 removed

modified:flask/testsuite/config.py+87 added
6 unmodified lines
78 :copyright: (c) 2011 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10"""11from __future__ import with_statement1213import os14import sys15import flask16import pkgutil17import unittest18from contextlib import contextmanager19from flask.testsuite import FlaskTestCase202122# config keys used for the ConfigTestCase
64 unmodified lines
87 app.config['PERMANENT_SESSION_LIFETIME'] = 4288 self.assert_equal(app.permanent_session_lifetime.seconds, 42)899091class LimitedLoaderMockWrapper(object):92 def __init__(self, loader):93 self.loader = loader9495 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)100101102@contextmanager103def patch_pkgutil_get_loader(wrapper_class=LimitedLoaderMockWrapper):104 """Patch pkgutil.get_loader to give loader without get_filename or archive.105106 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_loader118119120class InstanceTestCase(FlaskTestCase):121122 def test_explicit_instance_paths(self):123 here = os.path.abspath(os.path.dirname(__file__))
41 unmodified lines
165 sys.path.remove(site_packages)166 if 'site_app' in sys.modules:167 del sys.modules['site_app']168169 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']186187 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_prefix
9 unmodified lines
200 sys.path.remove(installed_path)201 if 'installed_package' in sys.modules:202 del sys.modules['installed_package']203204 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']221222 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_prefix
9 unmodified lines
235 sys.path.remove(site_packages)236 if 'site_package' in sys.modules:237 del sys.modules['site_package']238239 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']256257 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_prefix
22 unmodified lines