forked from orbit-oss/flask
use tmp_path instead of tmpdir
This commit is contained in:
parent
1d7281fe07
commit
0a00e1b608
4 changed files with 59 additions and 64 deletions
|
|
@ -128,28 +128,27 @@ 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
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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,19 +1,20 @@
|
||||||
|
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"
|
||||||
|
|
@ -23,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"
|
||||||
|
|
@ -39,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")
|
||||||
|
|
@ -58,50 +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"
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue