"Fossies" - the Fresh Open Source Software Archive

Member "flask-1.1.2/tests/test_instance_config.py" (3 Apr 2020, 4208 Bytes) of package /linux/www/flask-1.1.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "test_instance_config.py": 1.1.1_vs_1.1.2.

    1 # -*- coding: utf-8 -*-
    2 """
    3     tests.test_instance
    4     ~~~~~~~~~~~~~~~~~~~
    5 
    6     :copyright: 2010 Pallets
    7     :license: BSD-3-Clause
    8 """
    9 import os
   10 import sys
   11 
   12 import pytest
   13 
   14 import flask
   15 from flask._compat import PY2
   16 
   17 
   18 def test_explicit_instance_paths(modules_tmpdir):
   19     with pytest.raises(ValueError) as excinfo:
   20         flask.Flask(__name__, instance_path="instance")
   21     assert "must be absolute" in str(excinfo.value)
   22 
   23     app = flask.Flask(__name__, instance_path=str(modules_tmpdir))
   24     assert app.instance_path == str(modules_tmpdir)
   25 
   26 
   27 @pytest.mark.xfail(reason="TODO: weird interaction with tox")
   28 def test_main_module_paths(modules_tmpdir, purge_module):
   29     app = modules_tmpdir.join("main_app.py")
   30     app.write('import flask\n\napp = flask.Flask("__main__")')
   31     purge_module("main_app")
   32 
   33     from main_app import app
   34 
   35     here = os.path.abspath(os.getcwd())
   36     assert app.instance_path == os.path.join(here, "instance")
   37 
   38 
   39 def test_uninstalled_module_paths(modules_tmpdir, purge_module):
   40     app = modules_tmpdir.join("config_module_app.py").write(
   41         "import os\n"
   42         "import flask\n"
   43         "here = os.path.abspath(os.path.dirname(__file__))\n"
   44         "app = flask.Flask(__name__)\n"
   45     )
   46     purge_module("config_module_app")
   47 
   48     from config_module_app import app
   49 
   50     assert app.instance_path == str(modules_tmpdir.join("instance"))
   51 
   52 
   53 def test_uninstalled_package_paths(modules_tmpdir, purge_module):
   54     app = modules_tmpdir.mkdir("config_package_app")
   55     init = app.join("__init__.py")
   56     init.write(
   57         "import os\n"
   58         "import flask\n"
   59         "here = os.path.abspath(os.path.dirname(__file__))\n"
   60         "app = flask.Flask(__name__)\n"
   61     )
   62     purge_module("config_package_app")
   63 
   64     from config_package_app import app
   65 
   66     assert app.instance_path == str(modules_tmpdir.join("instance"))
   67 
   68 
   69 def test_installed_module_paths(
   70     modules_tmpdir, modules_tmpdir_prefix, purge_module, site_packages, limit_loader
   71 ):
   72     site_packages.join("site_app.py").write(
   73         "import flask\napp = flask.Flask(__name__)\n"
   74     )
   75     purge_module("site_app")
   76 
   77     from site_app import app
   78 
   79     assert app.instance_path == modules_tmpdir.join("var").join("site_app-instance")
   80 
   81 
   82 def test_installed_package_paths(
   83     limit_loader, modules_tmpdir, modules_tmpdir_prefix, purge_module, monkeypatch
   84 ):
   85     installed_path = modules_tmpdir.mkdir("path")
   86     monkeypatch.syspath_prepend(installed_path)
   87 
   88     app = installed_path.mkdir("installed_package")
   89     init = app.join("__init__.py")
   90     init.write("import flask\napp = flask.Flask(__name__)")
   91     purge_module("installed_package")
   92 
   93     from installed_package import app
   94 
   95     assert app.instance_path == modules_tmpdir.join("var").join(
   96         "installed_package-instance"
   97     )
   98 
   99 
  100 def test_prefix_package_paths(
  101     limit_loader, modules_tmpdir, modules_tmpdir_prefix, purge_module, site_packages
  102 ):
  103     app = site_packages.mkdir("site_package")
  104     init = app.join("__init__.py")
  105     init.write("import flask\napp = flask.Flask(__name__)")
  106     purge_module("site_package")
  107 
  108     import site_package
  109 
  110     assert site_package.app.instance_path == modules_tmpdir.join("var").join(
  111         "site_package-instance"
  112     )
  113 
  114 
  115 def test_egg_installed_paths(install_egg, modules_tmpdir, modules_tmpdir_prefix):
  116     modules_tmpdir.mkdir("site_egg").join("__init__.py").write(
  117         "import flask\n\napp = flask.Flask(__name__)"
  118     )
  119     install_egg("site_egg")
  120     try:
  121         import site_egg
  122 
  123         assert site_egg.app.instance_path == str(
  124             modules_tmpdir.join("var/").join("site_egg-instance")
  125         )
  126     finally:
  127         if "site_egg" in sys.modules:
  128             del sys.modules["site_egg"]
  129 
  130 
  131 @pytest.mark.skipif(not PY2, reason="This only works under Python 2.")
  132 def test_meta_path_loader_without_is_package(request, modules_tmpdir):
  133     app = modules_tmpdir.join("unimportable.py")
  134     app.write("import flask\napp = flask.Flask(__name__)")
  135 
  136     class Loader(object):
  137         def find_module(self, name, path=None):
  138             return self
  139 
  140     sys.meta_path.append(Loader())
  141     request.addfinalizer(sys.meta_path.pop)
  142 
  143     with pytest.raises(AttributeError):
  144         import unimportable  # noqa: F401