Skip to content
Myr

Merge branch '2.3.x'

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

authored by David Lord · committed by David Lord · parents e374853, 859d639 · Browse files · 5 files+59 added−119 removed
This commit

State

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

Diff stats

5 files+59 added−119 removed

  • tests:+58 added−118 removed
  • other:+1 added−1 removed

5 files+59 added−119 removed

modified:src/flask/scaffold.py+1 added-1 removed
863 unmodified lines
864865 if hasattr(loader, "get_filename"):866 filename = loader.get_filename(root_mod_name)867 elif hasattr(loader, "archive"):868 # zipimporter's loader.archive points to the .egg or .zip file.868 # zipimporter's loader.archive points to the .zip file.869 filename = loader.archive870 else:871 # At least one loader is missing both get_filename and archive:872 # Google App Engine's HardenedModulesHook, use __file__.
51 unmodified lines
modified:tests/conftest.py+13 added-51 removed
1import os2import pkgutil3import sys4import textwrap45import pytest6from _pytest import monkeypatch7
119 unmodified lines
127 monkeypatch.setattr(pkgutil, "get_loader", get_loader)128129130@pytest.fixture132def modules_tmpdir(tmpdir, monkeypatch):133 """A tmpdir added to sys.path."""134 rv = tmpdir.mkdir("modules_tmpdir")135 monkeypatch.syspath_prepend(str(rv))131def modules_tmp_path(tmp_path, monkeypatch):132 """A temporary directory added to sys.path."""133 rv = tmp_path / "modules_tmp"134 rv.mkdir()135 monkeypatch.syspath_prepend(os.fspath(rv))136 return rv137138139@pytest.fixture140def modules_tmpdir_prefix(modules_tmpdir, monkeypatch):141 monkeypatch.setattr(sys, "prefix", str(modules_tmpdir))142 return modules_tmpdir140def modules_tmp_path_prefix(modules_tmp_path, monkeypatch):141 monkeypatch.setattr(sys, "prefix", os.fspath(modules_tmp_path))142 return modules_tmp_path143144145@pytest.fixture146def site_packages(modules_tmpdir, monkeypatch):146def site_packages(modules_tmp_path, monkeypatch):147 """Create a fake site-packages."""148 rv = (149 modules_tmpdir.mkdir("lib")150 .mkdir(f"python{sys.version_info.major}.{sys.version_info.minor}")151 .mkdir("site-packages")152 )153 monkeypatch.syspath_prepend(str(rv))148 py_dir = f"python{sys.version_info.major}.{sys.version_info.minor}"149 rv = modules_tmp_path / "lib" / py_dir / "site-packages"150 rv.mkdir(parents=True)151 monkeypatch.syspath_prepend(os.fspath(rv))152 return rv153154155@pytest.fixture158def install_egg(modules_tmpdir, monkeypatch):159 """Generate egg from package name inside base and put the egg into160 sys.path."""161162 def inner(name, base=modules_tmpdir):163 base.join(name).ensure_dir()164 base.join(name).join("__init__.py").ensure()165166 egg_setup = base.join("setup.py")167 egg_setup.write(168 textwrap.dedent(169 f"""170 from setuptools import setup171 setup(172 name="{name}",173 version="1.0",174 packages=["site_egg"],175 zip_safe=True,176 )177 """178 )179 )180181 import subprocess182183 subprocess.check_call(184 [sys.executable, "setup.py", "bdist_egg"], cwd=str(modules_tmpdir)185 )186 (egg_path,) = modules_tmpdir.join("dist/").listdir()187 monkeypatch.syspath_prepend(str(egg_path))188 return egg_path189190 return inner191192193@pytest.fixture156def purge_module(request):157 def inner(name):158 request.addfinalizer(lambda: sys.modules.pop(name, None))159
1 unmodified line
modified:tests/test_config.py+4 added-12 removed
1import json2import os3import textwrap34import pytest56import flask
233 unmodified lines
240 assert "bar stuff 2" == bar_options["BAR_STUFF_2"]241242243@pytest.mark.parametrize("encoding", ["utf-8", "iso-8859-15", "latin-1"])245def test_from_pyfile_weird_encoding(tmpdir, encoding):246 f = tmpdir.join("my_config.py")247 f.write_binary(248 textwrap.dedent(249 f"""250 # -*- coding: {encoding} -*-251 TEST_VALUE = "föö"252 """253 ).encode(encoding)254 )244def test_from_pyfile_weird_encoding(tmp_path, encoding):245 f = tmp_path / "my_config.py"246 f.write_text(f'# -*- coding: {encoding} -*-\nTEST_VALUE = "föö"\n', encoding)247 app = flask.Flask(__name__)256 app.config.from_pyfile(str(f))248 app.config.from_pyfile(os.fspath(f))249 value = app.config["TEST_VALUE"]250 assert value == "föö"
modified:tests/test_helpers.py+2 added-2 removed
216 unmodified lines
217 retrieve file paths or metadata on a module or package, use the pkgutil and218 imp modules in the Python standard library.219 """220221 def test_name_with_import_error(self, modules_tmpdir):222 modules_tmpdir.join("importerror.py").write("raise NotImplementedError()")221 def test_name_with_import_error(self, modules_tmp_path):222 (modules_tmp_path / "importerror.py").write_text("raise NotImplementedError()")223 try:224 flask.Flask("importerror")225 except NotImplementedError:226 AssertionError("Flask(import_name) is importing import_name.")
116 unmodified lines
modified:tests/test_instance_config.py+39 added-53 removed
1import sys1import os23import pytest45import flask678def test_explicit_instance_paths(modules_tmpdir):9 with pytest.raises(ValueError) as excinfo:8def test_explicit_instance_paths(modules_tmp_path):9 with pytest.raises(ValueError, match=".*must be absolute"):10 flask.Flask(__name__, instance_path="instance")11 assert "must be absolute" in str(excinfo.value)1113 app = flask.Flask(__name__, instance_path=str(modules_tmpdir))14 assert app.instance_path == str(modules_tmpdir)12 app = flask.Flask(__name__, instance_path=os.fspath(modules_tmp_path))13 assert app.instance_path == os.fspath(modules_tmp_path)141517def test_uninstalled_module_paths(modules_tmpdir, purge_module):18 app = modules_tmpdir.join("config_module_app.py").write(16def test_uninstalled_module_paths(modules_tmp_path, purge_module):17 (modules_tmp_path / "config_module_app.py").write_text(18 "import os\n"19 "import flask\n"20 "here = os.path.abspath(os.path.dirname(__file__))\n"21 "app = flask.Flask(__name__)\n"
1 unmodified line
23 purge_module("config_module_app")2425 from config_module_app import app2628 assert app.instance_path == str(modules_tmpdir.join("instance"))27 assert app.instance_path == os.fspath(modules_tmp_path / "instance")282931def test_uninstalled_package_paths(modules_tmpdir, purge_module):32 app = modules_tmpdir.mkdir("config_package_app")33 init = app.join("__init__.py")34 init.write(30def test_uninstalled_package_paths(modules_tmp_path, purge_module):31 app = modules_tmp_path / "config_package_app"32 app.mkdir()33 (app / "__init__.py").write_text(34 "import os\n"35 "import flask\n"36 "here = os.path.abspath(os.path.dirname(__file__))\n"37 "app = flask.Flask(__name__)\n"
1 unmodified line
39 purge_module("config_package_app")4041 from config_package_app import app4244 assert app.instance_path == str(modules_tmpdir.join("instance"))43 assert app.instance_path == os.fspath(modules_tmp_path / "instance")444547def test_uninstalled_namespace_paths(tmpdir, monkeypatch, purge_module):46def test_uninstalled_namespace_paths(tmp_path, monkeypatch, purge_module):47 def create_namespace(package):49 project = tmpdir.join(f"project-{package}")50 monkeypatch.syspath_prepend(str(project))51 project.join("namespace").join(package).join("__init__.py").write(52 "import flask\napp = flask.Flask(__name__)\n", ensure=True53 )48 project = tmp_path / f"project-{package}"49 monkeypatch.syspath_prepend(os.fspath(project))50 ns = project / "namespace" / package51 ns.mkdir(parents=True)52 (ns / "__init__.py").write_text("import flask\napp = flask.Flask(__name__)\n")53 return project5455 _ = create_namespace("package1")56 project2 = create_namespace("package2")
1 unmodified line
58 purge_module("namespace")5960 from namespace.package2 import app6163 assert app.instance_path == str(project2.join("instance"))62 assert app.instance_path == os.fspath(project2 / "instance")636465def test_installed_module_paths(67 modules_tmpdir, modules_tmpdir_prefix, purge_module, site_packages, limit_loader66 modules_tmp_path, modules_tmp_path_prefix, purge_module, site_packages, limit_loader67):69 site_packages.join("site_app.py").write(68 (site_packages / "site_app.py").write_text(69 "import flask\napp = flask.Flask(__name__)\n"70 )71 purge_module("site_app")7273 from site_app import app7476 assert app.instance_path == modules_tmpdir.join("var").join("site_app-instance")75 assert app.instance_path == os.fspath(76 modules_tmp_path / "var" / "site_app-instance"77 )787980def test_installed_package_paths(80 limit_loader, modules_tmpdir, modules_tmpdir_prefix, purge_module, monkeypatch81 limit_loader, modules_tmp_path, modules_tmp_path_prefix, purge_module, monkeypatch82):82 installed_path = modules_tmpdir.mkdir("path")83 installed_path = modules_tmp_path / "path"84 installed_path.mkdir()85 monkeypatch.syspath_prepend(installed_path)8685 app = installed_path.mkdir("installed_package")86 init = app.join("__init__.py")87 init.write("import flask\napp = flask.Flask(__name__)")87 app = installed_path / "installed_package"88 app.mkdir()89 (app / "__init__.py").write_text("import flask\napp = flask.Flask(__name__)\n")90 purge_module("installed_package")9192 from installed_package import app9392 assert app.instance_path == modules_tmpdir.join("var").join(93 "installed_package-instance"94 assert app.instance_path == os.fspath(95 modules_tmp_path / "var" / "installed_package-instance"96 )979899def test_prefix_package_paths(98 limit_loader, modules_tmpdir, modules_tmpdir_prefix, purge_module, site_packages100 limit_loader, modules_tmp_path, modules_tmp_path_prefix, purge_module, site_packages101):100 app = site_packages.mkdir("site_package")101 init = app.join("__init__.py")102 init.write("import flask\napp = flask.Flask(__name__)")102 app = site_packages / "site_package"103 app.mkdir()104 (app / "__init__.py").write_text("import flask\napp = flask.Flask(__name__)\n")105 purge_module("site_package")106107 import site_package108107 assert site_package.app.instance_path == modules_tmpdir.join("var").join(108 "site_package-instance"109 assert site_package.app.instance_path == os.fspath(110 modules_tmp_path / "var" / "site_package-instance"111 )110111112def test_egg_installed_paths(install_egg, modules_tmpdir, modules_tmpdir_prefix):113 modules_tmpdir.mkdir("site_egg").join("__init__.py").write(114 "import flask\n\napp = flask.Flask(__name__)"115 )116 install_egg("site_egg")117 try:118 import site_egg119120 assert site_egg.app.instance_path == str(121 modules_tmpdir.join("var/").join("site_egg-instance")122 )123 finally:124 if "site_egg" in sys.modules:125 del sys.modules["site_egg"]