conftest.py
This file
Path
tests/conftest.py
197 lines · 5.1 KiB · Python
Last changed by
Merge pull request #3711 from pallets/cli-loading-errorDavid Lordbefore adoption6638432
History of this fileTake it
The walk stopped after 200 commits: lines older than c5c8bbc point at it.
Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
lines 1–12why, one step back1import os2import pkgutil3import sys4import textwrap56import pytest7from _pytest import monkeypatch89import flask10from flask import Flask as _Flask1112Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 13why, one step back13@pytest.fixture(scope="session", autouse=True)Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
lines 14–20why, one step back14def _standard_os_environ():15 """Set up ``os.environ`` at the start of the test session to have16 standard values. Returns a list of operations that is used by17 :func:`._reset_os_environ` after each test.18 """19 mp = monkeypatch.MonkeyPatch()20 out = (Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 21–25why, one step back21 (os.environ, "FLASK_APP", monkeypatch.notset),22 (os.environ, "FLASK_ENV", monkeypatch.notset),23 (os.environ, "FLASK_DEBUG", monkeypatch.notset),24 (os.environ, "FLASK_RUN_FROM_CLI", monkeypatch.notset),25 (os.environ, "WERKZEUG_RUN_MAIN", monkeypatch.notset),Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
lines 26–47why, one step back26 )2728 for _, key, value in out:29 if value is monkeypatch.notset:30 mp.delenv(key, False)31 else:32 mp.setenv(key, value)3334 yield out35 mp.undo()363738@pytest.fixture(autouse=True)39def _reset_os_environ(monkeypatch, _standard_os_environ):40 """Reset ``os.environ`` to the standard environ after each test,41 in case a test changed something without cleaning up.42 """43 monkeypatch._setitem.extend(_standard_os_environ)444546class Flask(_Flask):47 testing = TrueMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 48why, one step back48 secret_key = "test key"Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
lines 49–52why, one step back495051@pytest.fixture52def app():Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 53why, one step back53 app = Flask("flask_test", root_path=os.path.dirname(__file__))Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
lines 54–75why, one step back54 return app555657@pytest.fixture58def app_ctx(app):59 with app.app_context() as ctx:60 yield ctx616263@pytest.fixture64def req_ctx(app):65 with app.test_request_context() as ctx:66 yield ctx676869@pytest.fixture70def client(app):71 return app.test_client()727374@pytest.fixture75def test_apps(monkeypatch):Merge pull request #3711 from pallets/cli-loading-errorDavid Lordbefore adoption6638432
lines 76–84why, one step back76 monkeypatch.syspath_prepend(os.path.join(os.path.dirname(__file__), "test_apps"))77 original_modules = set(sys.modules.keys())7879 yield8081 # Remove any imports cached during the test. Otherwise "import app"82 # will work in the next test even though it's no longer on the path.83 for key in sys.modules.keys() - original_modules:84 sys.modules.pop(key)Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
lines 85–112why, one step back858687@pytest.fixture(autouse=True)88def leak_detector():89 yield9091 # make sure we're not leaking a request context since we are92 # testing flask internally in debug mode in a few cases93 leaks = []94 while flask._request_ctx_stack.top is not None:95 leaks.append(flask._request_ctx_stack.pop())96 assert leaks == []979899@pytest.fixture(params=(True, False))100def limit_loader(request, monkeypatch):101 """Patch pkgutil.get_loader to give loader without get_filename or archive.102103 This provides for tests where a system has custom loaders, e.g. Google App104 Engine's HardenedModulesHook, which have neither the `get_filename` method105 nor the `archive` attribute.106107 This fixture will run the testcase twice, once with and once without the108 limitation/mock.109 """110 if not request.param:111 return112Merge pull request #3554 from pallets/drop-python2David Lordbefore adoptioncd8a374
line 113why, one step back113 class LimitedLoader:Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
lines 114–117why, one step back114 def __init__(self, loader):115 self.loader = loader116117 def __getattr__(self, name):Merge pull request #3554 from pallets/drop-python2David Lordbefore adoptioncd8a374
lines 118–119why, one step back118 if name in {"archive", "get_filename"}:119 raise AttributeError(f"Mocking a loader which does not have {name!r}.")Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
lines 120–126why, one step back120 return getattr(self.loader, name)121122 old_get_loader = pkgutil.get_loader123124 def get_loader(*args, **kwargs):125 return LimitedLoader(old_get_loader(*args, **kwargs))126Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 127why, one step back127 monkeypatch.setattr(pkgutil, "get_loader", get_loader)Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
lines 128–132why, one step back128129130@pytest.fixture131def modules_tmpdir(tmpdir, monkeypatch):132 """A tmpdir added to sys.path."""Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 133why, one step back133 rv = tmpdir.mkdir("modules_tmpdir")Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
lines 134–139why, one step back134 monkeypatch.syspath_prepend(str(rv))135 return rv136137138@pytest.fixture139def modules_tmpdir_prefix(modules_tmpdir, monkeypatch):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 140why, one step back140 monkeypatch.setattr(sys, "prefix", str(modules_tmpdir))Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
lines 141–146why, one step back141 return modules_tmpdir142143144@pytest.fixture145def site_packages(modules_tmpdir, monkeypatch):146 """Create a fake site-packages."""Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 147–148why, one step back147 rv = (148 modules_tmpdir.mkdir("lib")Merge pull request #3554 from pallets/drop-python2David Lordbefore adoptioncd8a374
line 149why, one step back149 .mkdir(f"python{sys.version_info.major}.{sys.version_info.minor}")Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 150–151why, one step back150 .mkdir("site-packages")151 )Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
lines 152–162why, one step back152 monkeypatch.syspath_prepend(str(rv))153 return rv154155156@pytest.fixture157def install_egg(modules_tmpdir, monkeypatch):158 """Generate egg from package name inside base and put the egg into159 sys.path."""160161 def inner(name, base=modules_tmpdir):162 base.join(name).ensure_dir()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 163why, one step back163 base.join(name).join("__init__.py").ensure()Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
line 164why, one step back164Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 165–167why, one step back165 egg_setup = base.join("setup.py")166 egg_setup.write(167 textwrap.dedent(Merge pull request #3554 from pallets/drop-python2David Lordbefore adoptioncd8a374
lines 168–174why, one step back168 f"""169 from setuptools import setup170 setup(171 name="{name}",172 version="1.0",173 packages=["site_egg"],174 zip_safe=True,Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 175why, one step back175 )Merge pull request #3554 from pallets/drop-python2David Lordbefore adoptioncd8a374
line 176why, one step back176 """Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 177–178why, one step back177 )178 )Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
lines 179–180why, one step back179180 import subprocessMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 181why, one step back181Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
line 182why, one step back182 subprocess.check_call(Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 183why, one step back183 [sys.executable, "setup.py", "bdist_egg"], cwd=str(modules_tmpdir)Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
line 184why, one step back184 )Merge pull request #3554 from pallets/drop-python2David Lordbefore adoptioncd8a374
line 185why, one step back185 (egg_path,) = modules_tmpdir.join("dist/").listdir()Merge pull request #3155 from zabbal/masterDavid Lordbefore adoptionc5c8bbc
lines 186–197why, one step back186 monkeypatch.syspath_prepend(str(egg_path))187 return egg_path188189 return inner190191192@pytest.fixture193def purge_module(request):194 def inner(name):195 request.addfinalizer(lambda: sys.modules.pop(name, None))196197 return inner