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
Provenance
- Commit
57e926c- Tree
5cc4891- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent 1
e374853- Parent 2
859d639
5 files changed+59 added−119 removed
modified:src/flask/scaffold.py+1 added-1 removed
863 unmodified lines864 865 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 linesmodified:tests/conftest.py+13 added-51 removed
1 import os2 import pkgutil3 import sys4-import textwrap4 5 import pytest6 from _pytest import monkeypatch7 119 unmodified lines127 monkeypatch.setattr(pkgutil, "get_loader", get_loader)128 129 130 @pytest.fixture132-def modules_tmpdir(tmpdir, monkeypatch):133- """A tmpdir added to sys.path."""134- rv = tmpdir.mkdir("modules_tmpdir")135- monkeypatch.syspath_prepend(str(rv))131+def 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 rv137 138 139 @pytest.fixture140-def modules_tmpdir_prefix(modules_tmpdir, monkeypatch):141- monkeypatch.setattr(sys, "prefix", str(modules_tmpdir))142- return modules_tmpdir140+def modules_tmp_path_prefix(modules_tmp_path, monkeypatch):141+ monkeypatch.setattr(sys, "prefix", os.fspath(modules_tmp_path))142+ return modules_tmp_path143 144 145 @pytest.fixture146-def site_packages(modules_tmpdir, monkeypatch):146+def 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 rv153 154 155 @pytest.fixture158-def install_egg(modules_tmpdir, monkeypatch):159- """Generate egg from package name inside base and put the egg into160- sys.path."""161-162- def inner(name, base=modules_tmpdir):163- base.join(name).ensure_dir()164- base.join(name).join("__init__.py").ensure()165-166- 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- )180-181- import subprocess182-183- 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_path189-190- return inner191-192-193-@pytest.fixture156 def purge_module(request):157 def inner(name):158 request.addfinalizer(lambda: sys.modules.pop(name, None))159 1 unmodified linemodified:tests/test_config.py+4 added-12 removed
1 import json2 import os3-import textwrap3 4 import pytest5 6 import flask233 unmodified lines240 assert "bar stuff 2" == bar_options["BAR_STUFF_2"]241 242 243 @pytest.mark.parametrize("encoding", ["utf-8", "iso-8859-15", "latin-1"])245-def 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- )244+def 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 lines217 retrieve file paths or metadata on a module or package, use the pkgutil and218 imp modules in the Python standard library.219 """220 221- 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 linesmodified:tests/test_instance_config.py+39 added-53 removed
1-import sys1+import os2 3 import pytest4 5 import flask6 7 8-def test_explicit_instance_paths(modules_tmpdir):9- with pytest.raises(ValueError) as excinfo:8+def 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)11 13- 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)14 15 17-def test_uninstalled_module_paths(modules_tmpdir, purge_module):18- app = modules_tmpdir.join("config_module_app.py").write(16+def 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 line23 purge_module("config_module_app")24 25 from config_module_app import app26 28- assert app.instance_path == str(modules_tmpdir.join("instance"))27+ assert app.instance_path == os.fspath(modules_tmp_path / "instance")28 29 31-def 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(30+def 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 line39 purge_module("config_package_app")40 41 from config_package_app import app42 44- assert app.instance_path == str(modules_tmpdir.join("instance"))43+ assert app.instance_path == os.fspath(modules_tmp_path / "instance")44 45 47-def test_uninstalled_namespace_paths(tmpdir, monkeypatch, purge_module):46+def 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 project54 55 _ = create_namespace("package1")56 project2 = create_namespace("package2")1 unmodified line58 purge_module("namespace")59 60 from namespace.package2 import app61 63- assert app.instance_path == str(project2.join("instance"))62+ assert app.instance_path == os.fspath(project2 / "instance")63 64 65 def 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")72 73 from site_app import app74 76- 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+ )78 79 80 def 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)86 85- 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")91 92 from installed_package import app93 92- 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 )97 98 99 def 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")106 107 import site_package108 107- 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 )110-111-112-def 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_egg119-120- 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"]