Skip to content
Myr

test_instance_config.py

This file

Path

tests/test_instance_config.py

111 lines · 3.2 KiB · Python

Last changed by

Merge branch 'stable'David Lordbefore adoptionb78b5a2

History of this file

Take it

1import os23import pytest45import flask678def test_explicit_instance_paths(modules_tmp_path):9    with pytest.raises(ValueError, match=".*must be absolute"):10        flask.Flask(__name__, instance_path="instance")1112    app = flask.Flask(__name__, instance_path=os.fspath(modules_tmp_path))13    assert app.instance_path == os.fspath(modules_tmp_path)141516def 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"22    )23    purge_module("config_module_app")2425    from config_module_app import app2627    assert app.instance_path == os.fspath(modules_tmp_path / "instance")282930def 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"38    )39    purge_module("config_package_app")4041    from config_package_app import app4243    assert app.instance_path == os.fspath(modules_tmp_path / "instance")444546def test_uninstalled_namespace_paths(tmp_path, monkeypatch, purge_module):47    def create_namespace(package):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")57    purge_module("namespace.package2")58    purge_module("namespace")5960    from namespace.package2 import app6162    assert app.instance_path == os.fspath(project2 / "instance")636465def test_installed_module_paths(66    modules_tmp_path, modules_tmp_path_prefix, purge_module, site_packages67):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 app7475    assert app.instance_path == os.fspath(76        modules_tmp_path / "var" / "site_app-instance"77    )787980def test_installed_package_paths(81    modules_tmp_path, modules_tmp_path_prefix, purge_module, monkeypatch82):83    installed_path = modules_tmp_path / "path"84    installed_path.mkdir()85    monkeypatch.syspath_prepend(installed_path)8687    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 app9394    assert app.instance_path == os.fspath(95        modules_tmp_path / "var" / "installed_package-instance"96    )979899def test_prefix_package_paths(100    modules_tmp_path, modules_tmp_path_prefix, purge_module, site_packages101):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_package108109    assert site_package.app.instance_path == os.fspath(110        modules_tmp_path / "var" / "site_package-instance"111    )