Skip to content
Myr

Move fixtures

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 ba82bac · Browse files · 2 files+105 added−86 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+105 added−86 removed

  • tests:+105 added−86 removed

2 files+105 added−86 removed

added:tests/conftest.py+105 added
1# -*- coding: utf-8 -*-2"""3 tests.conftest4 ~~~~~~~~~~~~~~56 :copyright: (c) 2014 by the Flask Team, see AUTHORS for more details.7 :license: BSD, see LICENSE for more details.8"""9import pkgutil10import pytest11import sys12import textwrap131415@pytest.fixture(params=(True, False))16def limit_loader(request, monkeypatch):17 """Patch pkgutil.get_loader to give loader without get_filename or archive.1819 This provides for tests where a system has custom loaders, e.g. Google App20 Engine's HardenedModulesHook, which have neither the `get_filename` method21 nor the `archive` attribute.2223 This fixture will run the testcase twice, once with and once without the24 limitation/mock.25 """26 if not request.param:27 return2829 class LimitedLoader(object):30 def __init__(self, loader):31 self.loader = loader3233 def __getattr__(self, name):34 if name in ('archive', 'get_filename'):35 msg = 'Mocking a loader which does not have `%s.`' % name36 raise AttributeError(msg)37 return getattr(self.loader, name)3839 old_get_loader = pkgutil.get_loader4041 def get_loader(*args, **kwargs):42 return LimitedLoader(old_get_loader(*args, **kwargs))43 monkeypatch.setattr(pkgutil, 'get_loader', get_loader)444546@pytest.fixture47def apps_tmpdir(tmpdir, monkeypatch):48 '''Test folder for all instance tests.'''49 rv = tmpdir.mkdir('test_apps')50 monkeypatch.syspath_prepend(str(rv))51 return rv525354@pytest.fixture55def apps_tmpdir_prefix(apps_tmpdir, monkeypatch):56 monkeypatch.setattr(sys, 'prefix', str(apps_tmpdir))57 return apps_tmpdir585960@pytest.fixture61def site_packages(apps_tmpdir, monkeypatch):62 '''Create a fake site-packages'''63 rv = apps_tmpdir \64 .mkdir('lib')\65 .mkdir('python{x[0]}.{x[1]}'.format(x=sys.version_info))\66 .mkdir('site-packages')67 monkeypatch.syspath_prepend(str(rv))68 return rv697071@pytest.fixture72def install_egg(apps_tmpdir, monkeypatch):73 '''Generate egg from package name inside base and put the egg into74 sys.path'''75 def inner(name, base=apps_tmpdir):76 if not isinstance(name, str):77 raise ValueError(name)78 base.join(name).ensure_dir()79 base.join(name).join('__init__.py').ensure()8081 egg_setup = base.join('setup.py')82 egg_setup.write(textwrap.dedent("""83 from setuptools import setup84 setup(name='{0}',85 version='1.0',86 packages=['site_egg'],87 zip_safe=True)88 """.format(name)))8990 import subprocess91 subprocess.check_call(92 [sys.executable, 'setup.py', 'bdist_egg'],93 cwd=str(apps_tmpdir)94 )95 egg_path, = apps_tmpdir.join('dist/').listdir()96 monkeypatch.syspath_prepend(str(egg_path))97 return egg_path98 return inner99100101@pytest.fixture102def purge_module(request):103 def inner(name):104 request.addfinalizer(lambda: sys.modules.pop(name, None))105 return inner
modified:tests/test_config.py-86 removed
179 unmodified lines
180 self.assert_equal('bar stuff 1', bar_options['STUFF_1'])181 self.assert_equal('bar stuff 2', bar_options['STUFF_2'])182183184@pytest.fixture(params=(True, False))185def limit_loader(request, monkeypatch):186 """Patch pkgutil.get_loader to give loader without get_filename or archive.187188 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.191192 This fixture will run the testcase twice, once with and once without the193 limitation/mock.194 """195 if not request.param:196 return197198 class LimitedLoader(object):199 def __init__(self, loader):200 self.loader = loader201202 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)207208 old_get_loader = pkgutil.get_loader209 def get_loader(*args, **kwargs):210 return LimitedLoader(old_get_loader(*args, **kwargs))211 monkeypatch.setattr(pkgutil, 'get_loader', get_loader)212184class 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 rv220221 @pytest.fixture222 def apps_tmpdir_prefix(self, apps_tmpdir, monkeypatch):223 monkeypatch.setattr(sys, 'prefix', str(apps_tmpdir))224 return apps_tmpdir225226 @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 rv235236 @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()245246 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)))254255 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 inner264265 @pytest.fixture266 def purge_module(self, request):267 def inner(name):268 request.addfinalizer(lambda: sys.modules.pop(name, None))269 return inner270185 def test_explicit_instance_paths(self, apps_tmpdir):186 with pytest.raises(ValueError) as excinfo:187 flask.Flask(__name__, instance_path='instance')188 assert 'must be absolute' in str(excinfo.value)
103 unmodified lines