Rework test_config
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Markus Unterwaditzer · committed by Markus Unterwaditzer · parent a497b10 · Browse files · 1 file·+153 added−160 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+153 added−160 removed
- tests:+153 added−160 removed
Provenance
- Commit
ba82bac- Tree
d58a718- Author
Markus Unterwaditzer <markus@unterwaditzer.net>- Committer
Markus Unterwaditzer <markus@unterwaditzer.net>- Parent
a497b10
1 file changed+153 added−160 removed
modified:tests/test_config.py+153 added-160 removed
7 unmodified lines8 :copyright: (c) 2014 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10 """11 12+import pytest13+14 import os15 import sys16 import flask17 import pkgutil18 import unittest19+import textwrap20 from contextlib import contextmanager21 from tests import TestFlask22 from flask._compat import PY223 156 unmodified lines180 self.assert_equal('bar stuff 1', bar_options['STUFF_1'])181 self.assert_equal('bar stuff 2', bar_options['STUFF_2'])182 183 181-class LimitedLoaderMockWrapper(object):182- def __init__(self, loader):183- self.loader = loader184-185- def __getattr__(self, name):186- if name in ('archive', 'get_filename'):187- msg = 'Mocking a loader which does not have `%s.`' % name188- raise AttributeError(msg)189- return getattr(self.loader, name)190-191-192-@contextmanager193-def patch_pkgutil_get_loader(wrapper_class=LimitedLoaderMockWrapper):184+@pytest.fixture(params=(True, False))185+def limit_loader(request, monkeypatch):186 """Patch pkgutil.get_loader to give loader without get_filename or archive.187 188 This provides for tests where a system has custom loaders, e.g. Google App189 Engine's HardenedModulesHook, which have neither the `get_filename` method190 nor the `archive` attribute.191+192+ This fixture will run the testcase twice, once with and once without the193+ limitation/mock.194 """195+ if not request.param:196+ return197+198+ class LimitedLoader(object):199+ def __init__(self, loader):200+ self.loader = loader201+202+ def __getattr__(self, name):203+ if name in ('archive', 'get_filename'):204+ msg = 'Mocking a loader which does not have `%s.`' % name205+ raise AttributeError(msg)206+ return getattr(self.loader, name)207+208 old_get_loader = pkgutil.get_loader209 def get_loader(*args, **kwargs):202- return wrapper_class(old_get_loader(*args, **kwargs))203- try:204- pkgutil.get_loader = get_loader205- yield206- finally:207- pkgutil.get_loader = old_get_loader210+ return LimitedLoader(old_get_loader(*args, **kwargs))211+ monkeypatch.setattr(pkgutil, 'get_loader', get_loader)212 209-213 class TestInstance(TestFlask):214+ @pytest.fixture215+ def apps_tmpdir(self, tmpdir, monkeypatch):216+ '''Test folder for all instance tests.'''217+ rv = tmpdir.mkdir('test_apps')218+ monkeypatch.syspath_prepend(str(rv))219+ return rv220 212- def test_explicit_instance_paths(self):213- here = os.path.abspath(os.path.dirname(__file__))214- try:221+ @pytest.fixture222+ def apps_tmpdir_prefix(self, apps_tmpdir, monkeypatch):223+ monkeypatch.setattr(sys, 'prefix', str(apps_tmpdir))224+ return apps_tmpdir225+226+ @pytest.fixture227+ def site_packages(self, apps_tmpdir, monkeypatch):228+ '''Create a fake site-packages'''229+ rv = apps_tmpdir \230+ .mkdir('lib')\231+ .mkdir('python{x[0]}.{x[1]}'.format(x=sys.version_info))\232+ .mkdir('site-packages')233+ monkeypatch.syspath_prepend(str(rv))234+ return rv235+236+ @pytest.fixture237+ def install_egg(self, apps_tmpdir, monkeypatch):238+ '''Generate egg from package name inside base and put the egg into239+ sys.path'''240+ def inner(name, base=apps_tmpdir):241+ if not isinstance(name, str):242+ raise ValueError(name)243+ base.join(name).ensure_dir()244+ base.join(name).join('__init__.py').ensure()245+246+ egg_setup = base.join('setup.py')247+ egg_setup.write(textwrap.dedent("""248+ from setuptools import setup249+ setup(name='{0}',250+ version='1.0',251+ packages=['site_egg'],252+ zip_safe=True)253+ """.format(name)))254+255+ import subprocess256+ subprocess.check_call(257+ [sys.executable, 'setup.py', 'bdist_egg'],258+ cwd=str(apps_tmpdir)259+ )260+ egg_path, = apps_tmpdir.join('dist/').listdir()261+ monkeypatch.syspath_prepend(str(egg_path))262+ return egg_path263+ return inner264+265+ @pytest.fixture266+ def purge_module(self, request):267+ def inner(name):268+ request.addfinalizer(lambda: sys.modules.pop(name, None))269+ return inner270+271+ def test_explicit_instance_paths(self, apps_tmpdir):272+ with pytest.raises(ValueError) as excinfo:273 flask.Flask(__name__, instance_path='instance')216- except ValueError as e:217- self.assert_in('must be absolute', str(e))218- else:219- self.fail('Expected value error')274+ assert 'must be absolute' in str(excinfo.value)275 221- app = flask.Flask(__name__, instance_path=here)222- self.assert_equal(app.instance_path, here)276+ app = flask.Flask(__name__, instance_path=str(apps_tmpdir))277+ self.assert_equal(app.instance_path, str(apps_tmpdir))278 224- def test_main_module_paths(self):225- # Test an app with '__main__' as the import name, uses cwd.279+ def test_main_module_paths(self, apps_tmpdir, purge_module):280+ app = apps_tmpdir.join('main_app.py')281+ app.write('import flask\n\napp = flask.Flask("__main__")')282+ purge_module('main_app')283+284 from main_app import app285 here = os.path.abspath(os.getcwd())228- self.assert_equal(app.instance_path, os.path.join(here, 'instance'))229- if 'main_app' in sys.modules:230- del sys.modules['main_app']286+ assert app.instance_path == os.path.join(here, 'instance')287 232- def test_uninstalled_module_paths(self):288+ def test_uninstalled_module_paths(self, apps_tmpdir, purge_module):289+ app = apps_tmpdir.join('config_module_app.py').write(290+ 'import os\n'291+ 'import flask\n'292+ 'here = os.path.abspath(os.path.dirname(__file__))\n'293+ 'app = flask.Flask(__name__)\n'294+ )295+ purge_module('config_module_app')296+297 from config_module_app import app234- here = os.path.abspath(os.path.dirname(__file__))235- self.assert_equal(app.instance_path, os.path.join(here, 'test_apps', 'instance'))298+ assert app.instance_path == str(apps_tmpdir.join('instance'))299 237- def test_uninstalled_package_paths(self):300+ def test_uninstalled_package_paths(self, apps_tmpdir, purge_module):301+ app = apps_tmpdir.mkdir('config_package_app')302+ init = app.join('__init__.py')303+ init.write(304+ 'import os\n'305+ 'import flask\n'306+ 'here = os.path.abspath(os.path.dirname(__file__))\n'307+ 'app = flask.Flask(__name__)\n'308+ )309+ purge_module('config_package_app')310+311 from config_package_app import app239- here = os.path.abspath(os.path.dirname(__file__))240- self.assert_equal(app.instance_path, os.path.join(here, 'test_apps', 'instance'))312+ assert app.instance_path == str(apps_tmpdir.join('instance'))313 242- def test_installed_module_paths(self):243- here = os.path.abspath(os.path.dirname(__file__))244- expected_prefix = os.path.join(here, 'test_apps')245- real_prefix, sys.prefix = sys.prefix, expected_prefix246- site_packages = os.path.join(expected_prefix, 'lib', 'python2.5', 'site-packages')247- sys.path.append(site_packages)248- try:249- import site_app250- self.assert_equal(site_app.app.instance_path,251- os.path.join(expected_prefix, 'var',252- 'site_app-instance'))253- finally:254- sys.prefix = real_prefix255- sys.path.remove(site_packages)256- if 'site_app' in sys.modules:257- del sys.modules['site_app']314+ def test_installed_module_paths(self, apps_tmpdir, apps_tmpdir_prefix,315+ purge_module, site_packages, limit_loader):316+ site_packages.join('site_app.py').write(317+ 'import flask\n'318+ 'app = flask.Flask(__name__)\n'319+ )320+ purge_module('site_app')321 259- def test_installed_module_paths_with_limited_loader(self):260- here = os.path.abspath(os.path.dirname(__file__))261- expected_prefix = os.path.join(here, 'test_apps')262- real_prefix, sys.prefix = sys.prefix, expected_prefix263- site_packages = os.path.join(expected_prefix, 'lib', 'python2.5', 'site-packages')264- sys.path.append(site_packages)265- with patch_pkgutil_get_loader():266- try:267- import site_app268- self.assert_equal(site_app.app.instance_path,269- os.path.join(expected_prefix, 'var',270- 'site_app-instance'))271- finally:272- sys.prefix = real_prefix273- sys.path.remove(site_packages)274- if 'site_app' in sys.modules:275- del sys.modules['site_app']322+ from site_app import app323+ assert app.instance_path == \324+ apps_tmpdir.join('var').join('site_app-instance')325 277- def test_installed_package_paths(self):278- here = os.path.abspath(os.path.dirname(__file__))279- expected_prefix = os.path.join(here, 'test_apps')280- real_prefix, sys.prefix = sys.prefix, expected_prefix281- installed_path = os.path.join(expected_prefix, 'path')282- sys.path.append(installed_path)283- try:284- import installed_package285- self.assert_equal(installed_package.app.instance_path,286- os.path.join(expected_prefix, 'var',287- 'installed_package-instance'))288- finally:289- sys.prefix = real_prefix290- sys.path.remove(installed_path)291- if 'installed_package' in sys.modules:292- del sys.modules['installed_package']326+ def test_installed_package_paths(self, limit_loader, apps_tmpdir,327+ apps_tmpdir_prefix, purge_module,328+ monkeypatch):329+ installed_path = apps_tmpdir.mkdir('path')330+ monkeypatch.syspath_prepend(installed_path)331 294- def test_installed_package_paths_with_limited_loader(self):295- here = os.path.abspath(os.path.dirname(__file__))296- expected_prefix = os.path.join(here, 'test_apps')297- real_prefix, sys.prefix = sys.prefix, expected_prefix298- installed_path = os.path.join(expected_prefix, 'path')299- sys.path.append(installed_path)300- with patch_pkgutil_get_loader():301- try:302- import installed_package303- self.assert_equal(installed_package.app.instance_path,304- os.path.join(expected_prefix, 'var',305- 'installed_package-instance'))306- finally:307- sys.prefix = real_prefix308- sys.path.remove(installed_path)309- if 'installed_package' in sys.modules:310- del sys.modules['installed_package']332+ app = installed_path.mkdir('installed_package')333+ init = app.join('__init__.py')334+ init.write('import flask\napp = flask.Flask(__name__)')335+ purge_module('installed_package')336 312- def test_prefix_package_paths(self):313- here = os.path.abspath(os.path.dirname(__file__))314- expected_prefix = os.path.join(here, 'test_apps')315- real_prefix, sys.prefix = sys.prefix, expected_prefix316- site_packages = os.path.join(expected_prefix, 'lib', 'python2.5', 'site-packages')317- sys.path.append(site_packages)318- try:319- import site_package320- self.assert_equal(site_package.app.instance_path,321- os.path.join(expected_prefix, 'var',322- 'site_package-instance'))323- finally:324- sys.prefix = real_prefix325- sys.path.remove(site_packages)326- if 'site_package' in sys.modules:327- del sys.modules['site_package']337+ from installed_package import app338+ assert app.instance_path == \339+ apps_tmpdir.join('var').join('installed_package-instance')340 329- def test_prefix_package_paths_with_limited_loader(self):330- here = os.path.abspath(os.path.dirname(__file__))331- expected_prefix = os.path.join(here, 'test_apps')332- real_prefix, sys.prefix = sys.prefix, expected_prefix333- site_packages = os.path.join(expected_prefix, 'lib', 'python2.5', 'site-packages')334- sys.path.append(site_packages)335- with patch_pkgutil_get_loader():336- try:337- import site_package338- self.assert_equal(site_package.app.instance_path,339- os.path.join(expected_prefix, 'var',340- 'site_package-instance'))341- finally:342- sys.prefix = real_prefix343- sys.path.remove(site_packages)344- if 'site_package' in sys.modules:345- del sys.modules['site_package']341+ def test_prefix_package_paths(self, limit_loader, apps_tmpdir,342+ apps_tmpdir_prefix, purge_module,343+ site_packages):344+ app = site_packages.mkdir('site_package')345+ init = app.join('__init__.py')346+ init.write('import flask\napp = flask.Flask(__name__)')347+ purge_module('site_package')348 347- def test_egg_installed_paths(self, monkeypatch):348- here = os.path.abspath(os.path.dirname(__file__))349- expected_prefix = os.path.join(here, 'test_apps')350- monkeypatch.setattr(sys, 'prefix', expected_prefix)349+ import site_package350+ assert site_package.app.instance_path == \351+ apps_tmpdir.join('var').join('site_package-instance')352 352- site_packages = os.path.join(expected_prefix, 'lib', 'python2.5',353- 'site-packages')354- egg_path = os.path.join(site_packages, 'SiteEgg.egg')355- monkeypatch.syspath_prepend(egg_path)356- monkeypatch.syspath_prepend(site_packages)357-353+ def test_egg_installed_paths(self, install_egg, apps_tmpdir,354+ apps_tmpdir_prefix):355+ apps_tmpdir.mkdir('site_egg').join('__init__.py').write(356+ 'import flask\n\napp = flask.Flask(__name__)'357+ )358+ install_egg('site_egg')359 try:359- import site_egg # in SiteEgg.egg360- self.assert_equal(site_egg.app.instance_path,361- os.path.join(expected_prefix, 'var',362- 'site_egg-instance'))360+ import site_egg361+ assert site_egg.app.instance_path == \362+ str(apps_tmpdir.join('var/').join('site_egg-instance'))363 finally:364 if 'site_egg' in sys.modules:365 del sys.modules['site_egg']366 7 unmodified lines374 with self.assert_raises(AttributeError):375 flask.Flask(__name__)376 finally:377 sys.meta_path.pop()378-379-380-def suite():381- suite = unittest.TestSuite()382- suite.addTest(unittest.makeSuite(TestConfig))383- suite.addTest(unittest.makeSuite(TestInstance))384- return suite