Merge branch '2.3.x'
This commit is contained in:
commit
57e926c791
5 changed files with 59 additions and 119 deletions
|
|
@ -865,7 +865,7 @@ def _find_package_path(import_name):
|
||||||
if hasattr(loader, "get_filename"):
|
if hasattr(loader, "get_filename"):
|
||||||
filename = loader.get_filename(root_mod_name)
|
filename = loader.get_filename(root_mod_name)
|
||||||
elif hasattr(loader, "archive"):
|
elif hasattr(loader, "archive"):
|
||||||
# zipimporter's loader.archive points to the .egg or .zip file.
|
# zipimporter's loader.archive points to the .zip file.
|
||||||
filename = loader.archive
|
filename = loader.archive
|
||||||
else:
|
else:
|
||||||
# At least one loader is missing both get_filename and archive:
|
# At least one loader is missing both get_filename and archive:
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
import os
|
import os
|
||||||
import pkgutil
|
import pkgutil
|
||||||
import sys
|
import sys
|
||||||
import textwrap
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from _pytest import monkeypatch
|
from _pytest import monkeypatch
|
||||||
|
|
@ -129,67 +128,30 @@ def limit_loader(request, monkeypatch):
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def modules_tmpdir(tmpdir, monkeypatch):
|
def modules_tmp_path(tmp_path, monkeypatch):
|
||||||
"""A tmpdir added to sys.path."""
|
"""A temporary directory added to sys.path."""
|
||||||
rv = tmpdir.mkdir("modules_tmpdir")
|
rv = tmp_path / "modules_tmp"
|
||||||
monkeypatch.syspath_prepend(str(rv))
|
rv.mkdir()
|
||||||
|
monkeypatch.syspath_prepend(os.fspath(rv))
|
||||||
return rv
|
return rv
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def modules_tmpdir_prefix(modules_tmpdir, monkeypatch):
|
def modules_tmp_path_prefix(modules_tmp_path, monkeypatch):
|
||||||
monkeypatch.setattr(sys, "prefix", str(modules_tmpdir))
|
monkeypatch.setattr(sys, "prefix", os.fspath(modules_tmp_path))
|
||||||
return modules_tmpdir
|
return modules_tmp_path
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def site_packages(modules_tmpdir, monkeypatch):
|
def site_packages(modules_tmp_path, monkeypatch):
|
||||||
"""Create a fake site-packages."""
|
"""Create a fake site-packages."""
|
||||||
rv = (
|
py_dir = f"python{sys.version_info.major}.{sys.version_info.minor}"
|
||||||
modules_tmpdir.mkdir("lib")
|
rv = modules_tmp_path / "lib" / py_dir / "site-packages"
|
||||||
.mkdir(f"python{sys.version_info.major}.{sys.version_info.minor}")
|
rv.mkdir(parents=True)
|
||||||
.mkdir("site-packages")
|
monkeypatch.syspath_prepend(os.fspath(rv))
|
||||||
)
|
|
||||||
monkeypatch.syspath_prepend(str(rv))
|
|
||||||
return rv
|
return rv
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def install_egg(modules_tmpdir, monkeypatch):
|
|
||||||
"""Generate egg from package name inside base and put the egg into
|
|
||||||
sys.path."""
|
|
||||||
|
|
||||||
def inner(name, base=modules_tmpdir):
|
|
||||||
base.join(name).ensure_dir()
|
|
||||||
base.join(name).join("__init__.py").ensure()
|
|
||||||
|
|
||||||
egg_setup = base.join("setup.py")
|
|
||||||
egg_setup.write(
|
|
||||||
textwrap.dedent(
|
|
||||||
f"""
|
|
||||||
from setuptools import setup
|
|
||||||
setup(
|
|
||||||
name="{name}",
|
|
||||||
version="1.0",
|
|
||||||
packages=["site_egg"],
|
|
||||||
zip_safe=True,
|
|
||||||
)
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
import subprocess
|
|
||||||
|
|
||||||
subprocess.check_call(
|
|
||||||
[sys.executable, "setup.py", "bdist_egg"], cwd=str(modules_tmpdir)
|
|
||||||
)
|
|
||||||
(egg_path,) = modules_tmpdir.join("dist/").listdir()
|
|
||||||
monkeypatch.syspath_prepend(str(egg_path))
|
|
||||||
return egg_path
|
|
||||||
|
|
||||||
return inner
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def purge_module(request):
|
def purge_module(request):
|
||||||
def inner(name):
|
def inner(name):
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import textwrap
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
|
@ -242,17 +241,10 @@ def test_get_namespace():
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("encoding", ["utf-8", "iso-8859-15", "latin-1"])
|
@pytest.mark.parametrize("encoding", ["utf-8", "iso-8859-15", "latin-1"])
|
||||||
def test_from_pyfile_weird_encoding(tmpdir, encoding):
|
def test_from_pyfile_weird_encoding(tmp_path, encoding):
|
||||||
f = tmpdir.join("my_config.py")
|
f = tmp_path / "my_config.py"
|
||||||
f.write_binary(
|
f.write_text(f'# -*- coding: {encoding} -*-\nTEST_VALUE = "föö"\n', encoding)
|
||||||
textwrap.dedent(
|
|
||||||
f"""
|
|
||||||
# -*- coding: {encoding} -*-
|
|
||||||
TEST_VALUE = "föö"
|
|
||||||
"""
|
|
||||||
).encode(encoding)
|
|
||||||
)
|
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
app.config.from_pyfile(str(f))
|
app.config.from_pyfile(os.fspath(f))
|
||||||
value = app.config["TEST_VALUE"]
|
value = app.config["TEST_VALUE"]
|
||||||
assert value == "föö"
|
assert value == "föö"
|
||||||
|
|
|
||||||
|
|
@ -218,8 +218,8 @@ class TestNoImports:
|
||||||
imp modules in the Python standard library.
|
imp modules in the Python standard library.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def test_name_with_import_error(self, modules_tmpdir):
|
def test_name_with_import_error(self, modules_tmp_path):
|
||||||
modules_tmpdir.join("importerror.py").write("raise NotImplementedError()")
|
(modules_tmp_path / "importerror.py").write_text("raise NotImplementedError()")
|
||||||
try:
|
try:
|
||||||
flask.Flask("importerror")
|
flask.Flask("importerror")
|
||||||
except NotImplementedError:
|
except NotImplementedError:
|
||||||
|
|
|
||||||
|
|
@ -1,21 +1,20 @@
|
||||||
import sys
|
import os
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
import flask
|
import flask
|
||||||
|
|
||||||
|
|
||||||
def test_explicit_instance_paths(modules_tmpdir):
|
def test_explicit_instance_paths(modules_tmp_path):
|
||||||
with pytest.raises(ValueError) as excinfo:
|
with pytest.raises(ValueError, match=".*must be absolute"):
|
||||||
flask.Flask(__name__, instance_path="instance")
|
flask.Flask(__name__, instance_path="instance")
|
||||||
assert "must be absolute" in str(excinfo.value)
|
|
||||||
|
|
||||||
app = flask.Flask(__name__, instance_path=str(modules_tmpdir))
|
app = flask.Flask(__name__, instance_path=os.fspath(modules_tmp_path))
|
||||||
assert app.instance_path == str(modules_tmpdir)
|
assert app.instance_path == os.fspath(modules_tmp_path)
|
||||||
|
|
||||||
|
|
||||||
def test_uninstalled_module_paths(modules_tmpdir, purge_module):
|
def test_uninstalled_module_paths(modules_tmp_path, purge_module):
|
||||||
app = modules_tmpdir.join("config_module_app.py").write(
|
(modules_tmp_path / "config_module_app.py").write_text(
|
||||||
"import os\n"
|
"import os\n"
|
||||||
"import flask\n"
|
"import flask\n"
|
||||||
"here = os.path.abspath(os.path.dirname(__file__))\n"
|
"here = os.path.abspath(os.path.dirname(__file__))\n"
|
||||||
|
|
@ -25,13 +24,13 @@ def test_uninstalled_module_paths(modules_tmpdir, purge_module):
|
||||||
|
|
||||||
from config_module_app import app
|
from config_module_app import app
|
||||||
|
|
||||||
assert app.instance_path == str(modules_tmpdir.join("instance"))
|
assert app.instance_path == os.fspath(modules_tmp_path / "instance")
|
||||||
|
|
||||||
|
|
||||||
def test_uninstalled_package_paths(modules_tmpdir, purge_module):
|
def test_uninstalled_package_paths(modules_tmp_path, purge_module):
|
||||||
app = modules_tmpdir.mkdir("config_package_app")
|
app = modules_tmp_path / "config_package_app"
|
||||||
init = app.join("__init__.py")
|
app.mkdir()
|
||||||
init.write(
|
(app / "__init__.py").write_text(
|
||||||
"import os\n"
|
"import os\n"
|
||||||
"import flask\n"
|
"import flask\n"
|
||||||
"here = os.path.abspath(os.path.dirname(__file__))\n"
|
"here = os.path.abspath(os.path.dirname(__file__))\n"
|
||||||
|
|
@ -41,16 +40,16 @@ def test_uninstalled_package_paths(modules_tmpdir, purge_module):
|
||||||
|
|
||||||
from config_package_app import app
|
from config_package_app import app
|
||||||
|
|
||||||
assert app.instance_path == str(modules_tmpdir.join("instance"))
|
assert app.instance_path == os.fspath(modules_tmp_path / "instance")
|
||||||
|
|
||||||
|
|
||||||
def test_uninstalled_namespace_paths(tmpdir, monkeypatch, purge_module):
|
def test_uninstalled_namespace_paths(tmp_path, monkeypatch, purge_module):
|
||||||
def create_namespace(package):
|
def create_namespace(package):
|
||||||
project = tmpdir.join(f"project-{package}")
|
project = tmp_path / f"project-{package}"
|
||||||
monkeypatch.syspath_prepend(str(project))
|
monkeypatch.syspath_prepend(os.fspath(project))
|
||||||
project.join("namespace").join(package).join("__init__.py").write(
|
ns = project / "namespace" / package
|
||||||
"import flask\napp = flask.Flask(__name__)\n", ensure=True
|
ns.mkdir(parents=True)
|
||||||
)
|
(ns / "__init__.py").write_text("import flask\napp = flask.Flask(__name__)\n")
|
||||||
return project
|
return project
|
||||||
|
|
||||||
_ = create_namespace("package1")
|
_ = create_namespace("package1")
|
||||||
|
|
@ -60,66 +59,53 @@ def test_uninstalled_namespace_paths(tmpdir, monkeypatch, purge_module):
|
||||||
|
|
||||||
from namespace.package2 import app
|
from namespace.package2 import app
|
||||||
|
|
||||||
assert app.instance_path == str(project2.join("instance"))
|
assert app.instance_path == os.fspath(project2 / "instance")
|
||||||
|
|
||||||
|
|
||||||
def test_installed_module_paths(
|
def test_installed_module_paths(
|
||||||
modules_tmpdir, modules_tmpdir_prefix, purge_module, site_packages, limit_loader
|
modules_tmp_path, modules_tmp_path_prefix, purge_module, site_packages, limit_loader
|
||||||
):
|
):
|
||||||
site_packages.join("site_app.py").write(
|
(site_packages / "site_app.py").write_text(
|
||||||
"import flask\napp = flask.Flask(__name__)\n"
|
"import flask\napp = flask.Flask(__name__)\n"
|
||||||
)
|
)
|
||||||
purge_module("site_app")
|
purge_module("site_app")
|
||||||
|
|
||||||
from site_app import app
|
from site_app import app
|
||||||
|
|
||||||
assert app.instance_path == modules_tmpdir.join("var").join("site_app-instance")
|
assert app.instance_path == os.fspath(
|
||||||
|
modules_tmp_path / "var" / "site_app-instance"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_installed_package_paths(
|
def test_installed_package_paths(
|
||||||
limit_loader, modules_tmpdir, modules_tmpdir_prefix, purge_module, monkeypatch
|
limit_loader, modules_tmp_path, modules_tmp_path_prefix, purge_module, monkeypatch
|
||||||
):
|
):
|
||||||
installed_path = modules_tmpdir.mkdir("path")
|
installed_path = modules_tmp_path / "path"
|
||||||
|
installed_path.mkdir()
|
||||||
monkeypatch.syspath_prepend(installed_path)
|
monkeypatch.syspath_prepend(installed_path)
|
||||||
|
|
||||||
app = installed_path.mkdir("installed_package")
|
app = installed_path / "installed_package"
|
||||||
init = app.join("__init__.py")
|
app.mkdir()
|
||||||
init.write("import flask\napp = flask.Flask(__name__)")
|
(app / "__init__.py").write_text("import flask\napp = flask.Flask(__name__)\n")
|
||||||
purge_module("installed_package")
|
purge_module("installed_package")
|
||||||
|
|
||||||
from installed_package import app
|
from installed_package import app
|
||||||
|
|
||||||
assert app.instance_path == modules_tmpdir.join("var").join(
|
assert app.instance_path == os.fspath(
|
||||||
"installed_package-instance"
|
modules_tmp_path / "var" / "installed_package-instance"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_prefix_package_paths(
|
def test_prefix_package_paths(
|
||||||
limit_loader, modules_tmpdir, modules_tmpdir_prefix, purge_module, site_packages
|
limit_loader, modules_tmp_path, modules_tmp_path_prefix, purge_module, site_packages
|
||||||
):
|
):
|
||||||
app = site_packages.mkdir("site_package")
|
app = site_packages / "site_package"
|
||||||
init = app.join("__init__.py")
|
app.mkdir()
|
||||||
init.write("import flask\napp = flask.Flask(__name__)")
|
(app / "__init__.py").write_text("import flask\napp = flask.Flask(__name__)\n")
|
||||||
purge_module("site_package")
|
purge_module("site_package")
|
||||||
|
|
||||||
import site_package
|
import site_package
|
||||||
|
|
||||||
assert site_package.app.instance_path == modules_tmpdir.join("var").join(
|
assert site_package.app.instance_path == os.fspath(
|
||||||
"site_package-instance"
|
modules_tmp_path / "var" / "site_package-instance"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_egg_installed_paths(install_egg, modules_tmpdir, modules_tmpdir_prefix):
|
|
||||||
modules_tmpdir.mkdir("site_egg").join("__init__.py").write(
|
|
||||||
"import flask\n\napp = flask.Flask(__name__)"
|
|
||||||
)
|
|
||||||
install_egg("site_egg")
|
|
||||||
try:
|
|
||||||
import site_egg
|
|
||||||
|
|
||||||
assert site_egg.app.instance_path == str(
|
|
||||||
modules_tmpdir.join("var/").join("site_egg-instance")
|
|
||||||
)
|
|
||||||
finally:
|
|
||||||
if "site_egg" in sys.modules:
|
|
||||||
del sys.modules["site_egg"]
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue