flask/tests/test_instance_config.py

147 lines
4.4 KiB
Python
Raw Normal View History

import importlib.util
2023-05-02 10:38:27 -07:00
import os
2014-09-04 15:43:18 +02:00
import pytest
2014-09-04 15:43:18 +02:00
import flask
@pytest.fixture(params=(True, False))
def limit_loader(request, monkeypatch):
"""Patch importlib.util.find_spec to give loader without get_filename or archive.
This provides for tests where a system has custom loaders, e.g. Google App
Engine's HardenedModulesHook, which have neither the `get_filename` method
nor the `archive` attribute.
This fixture will run the testcase twice, once with and once without the
limitation/mock.
"""
if not request.param:
return
class LimitedLoader:
def __init__(self, loader):
self.loader = loader
def __getattr__(self, name):
if name in {"archive", "get_filename"}:
raise AttributeError(f"Mocking a loader which does not have {name!r}.")
return getattr(self.loader, name)
original_find_spec = importlib.util.find_spec
def mock_find_spec(name, package=None):
spec = original_find_spec(name, package)
if spec and spec.loader:
spec.loader = LimitedLoader(spec.loader)
return spec
monkeypatch.setattr(importlib.util, "find_spec", mock_find_spec)
2023-05-02 10:38:27 -07:00
def test_explicit_instance_paths(modules_tmp_path):
with pytest.raises(ValueError, match=".*must be absolute"):
flask.Flask(__name__, instance_path="instance")
2014-09-04 15:43:18 +02:00
2023-05-02 10:38:27 -07:00
app = flask.Flask(__name__, instance_path=os.fspath(modules_tmp_path))
assert app.instance_path == os.fspath(modules_tmp_path)
2014-09-04 15:43:18 +02:00
2023-05-02 10:38:27 -07:00
def test_uninstalled_module_paths(modules_tmp_path, purge_module):
(modules_tmp_path / "config_module_app.py").write_text(
"import os\n"
"import flask\n"
"here = os.path.abspath(os.path.dirname(__file__))\n"
"app = flask.Flask(__name__)\n"
2014-09-04 15:43:18 +02:00
)
purge_module("config_module_app")
2014-09-04 15:43:18 +02:00
from config_module_app import app
2023-05-02 10:38:27 -07:00
assert app.instance_path == os.fspath(modules_tmp_path / "instance")
2014-09-04 15:43:18 +02:00
2023-05-02 10:38:27 -07:00
def test_uninstalled_package_paths(modules_tmp_path, purge_module):
app = modules_tmp_path / "config_package_app"
app.mkdir()
(app / "__init__.py").write_text(
"import os\n"
"import flask\n"
"here = os.path.abspath(os.path.dirname(__file__))\n"
"app = flask.Flask(__name__)\n"
2014-09-04 15:43:18 +02:00
)
purge_module("config_package_app")
2014-09-04 15:43:18 +02:00
from config_package_app import app
2023-05-02 10:38:27 -07:00
assert app.instance_path == os.fspath(modules_tmp_path / "instance")
2014-09-04 15:43:18 +02:00
2023-05-02 10:38:27 -07:00
def test_uninstalled_namespace_paths(tmp_path, monkeypatch, purge_module):
def create_namespace(package):
2023-05-02 10:38:27 -07:00
project = tmp_path / f"project-{package}"
monkeypatch.syspath_prepend(os.fspath(project))
ns = project / "namespace" / package
ns.mkdir(parents=True)
(ns / "__init__.py").write_text("import flask\napp = flask.Flask(__name__)\n")
return project
_ = create_namespace("package1")
project2 = create_namespace("package2")
purge_module("namespace.package2")
purge_module("namespace")
from namespace.package2 import app
2023-05-02 10:38:27 -07:00
assert app.instance_path == os.fspath(project2 / "instance")
def test_installed_module_paths(
2023-05-02 10:38:27 -07:00
modules_tmp_path, modules_tmp_path_prefix, purge_module, site_packages, limit_loader
):
2023-05-02 10:38:27 -07:00
(site_packages / "site_app.py").write_text(
2019-06-01 09:22:20 -07:00
"import flask\napp = flask.Flask(__name__)\n"
2014-09-04 15:43:18 +02:00
)
purge_module("site_app")
2014-09-04 15:43:18 +02:00
from site_app import app
2023-05-02 10:38:27 -07:00
assert app.instance_path == os.fspath(
modules_tmp_path / "var" / "site_app-instance"
)
2014-09-04 15:43:18 +02:00
def test_installed_package_paths(
2023-05-02 10:38:27 -07:00
limit_loader, modules_tmp_path, modules_tmp_path_prefix, purge_module, monkeypatch
):
2023-05-02 10:38:27 -07:00
installed_path = modules_tmp_path / "path"
installed_path.mkdir()
2014-09-04 15:43:18 +02:00
monkeypatch.syspath_prepend(installed_path)
2023-05-02 10:38:27 -07:00
app = installed_path / "installed_package"
app.mkdir()
(app / "__init__.py").write_text("import flask\napp = flask.Flask(__name__)\n")
purge_module("installed_package")
2014-09-04 15:43:18 +02:00
from installed_package import app
2023-05-02 10:38:27 -07:00
assert app.instance_path == os.fspath(
modules_tmp_path / "var" / "installed_package-instance"
)
2014-09-04 15:43:18 +02:00
def test_prefix_package_paths(
2023-05-02 10:38:27 -07:00
limit_loader, modules_tmp_path, modules_tmp_path_prefix, purge_module, site_packages
):
2023-05-02 10:38:27 -07:00
app = site_packages / "site_package"
app.mkdir()
(app / "__init__.py").write_text("import flask\napp = flask.Flask(__name__)\n")
purge_module("site_package")
2014-09-04 15:43:18 +02:00
import site_package
2023-05-02 10:38:27 -07:00
assert site_package.app.instance_path == os.fspath(
modules_tmp_path / "var" / "site_package-instance"
)