fix uninstalled package tests under tox

This commit is contained in:
David Lord 2022-06-06 08:24:05 -07:00
parent 88bcf78439
commit 3ba37d2afe
No known key found for this signature in database
GPG key ID: 7A1C87E3F5BC42A8
3 changed files with 15 additions and 7 deletions

View file

@ -1,5 +1,6 @@
import importlib.util import importlib.util
import os import os
import pathlib
import pkgutil import pkgutil
import sys import sys
import typing as t import typing as t
@ -780,6 +781,15 @@ def _matching_loader_thinks_module_is_package(loader, mod_name):
) )
def _path_is_relative_to(path: pathlib.PurePath, base: str) -> bool:
# Path.is_relative_to doesn't exist until Python 3.9
try:
path.relative_to(base)
return True
except ValueError:
return False
def _find_package_path(import_name): def _find_package_path(import_name):
"""Find the path that contains the package or module.""" """Find the path that contains the package or module."""
root_mod_name, _, _ = import_name.partition(".") root_mod_name, _, _ = import_name.partition(".")
@ -802,13 +812,13 @@ def _find_package_path(import_name):
package_spec = importlib.util.find_spec(import_name) package_spec = importlib.util.find_spec(import_name)
if package_spec is not None and package_spec.submodule_search_locations: if package_spec is not None and package_spec.submodule_search_locations:
# Pick the path in the namespace that contains the submodule. # Pick the path in the namespace that contains the submodule.
package_path = os.path.commonpath( package_path = pathlib.Path(
package_spec.submodule_search_locations os.path.commonpath(package_spec.submodule_search_locations)
) )
search_locations = ( search_locations = (
location location
for location in root_spec.submodule_search_locations for location in root_spec.submodule_search_locations
if package_path.startswith(location) if _path_is_relative_to(package_path, location)
) )
else: else:
# Pick the first path. # Pick the first path.
@ -865,7 +875,7 @@ def find_package(import_name: str):
py_prefix = os.path.abspath(sys.prefix) py_prefix = os.path.abspath(sys.prefix)
# installed to the system # installed to the system
if package_path.startswith(py_prefix): if _path_is_relative_to(pathlib.PurePath(package_path), py_prefix):
return py_prefix, package_path return py_prefix, package_path
site_parent, site_folder = os.path.split(package_path) site_parent, site_folder = os.path.split(package_path)

View file

@ -15,7 +15,6 @@ def test_explicit_instance_paths(modules_tmpdir):
assert app.instance_path == str(modules_tmpdir) assert app.instance_path == str(modules_tmpdir)
@pytest.mark.xfail(reason="weird interaction with tox")
def test_main_module_paths(modules_tmpdir, purge_module): def test_main_module_paths(modules_tmpdir, purge_module):
app = modules_tmpdir.join("main_app.py") app = modules_tmpdir.join("main_app.py")
app.write('import flask\n\napp = flask.Flask("__main__")') app.write('import flask\n\napp = flask.Flask("__main__")')
@ -27,7 +26,6 @@ def test_main_module_paths(modules_tmpdir, purge_module):
assert app.instance_path == os.path.join(here, "instance") assert app.instance_path == os.path.join(here, "instance")
@pytest.mark.xfail(reason="weird interaction with tox")
def test_uninstalled_module_paths(modules_tmpdir, purge_module): def test_uninstalled_module_paths(modules_tmpdir, purge_module):
app = modules_tmpdir.join("config_module_app.py").write( app = modules_tmpdir.join("config_module_app.py").write(
"import os\n" "import os\n"
@ -42,7 +40,6 @@ def test_uninstalled_module_paths(modules_tmpdir, purge_module):
assert app.instance_path == str(modules_tmpdir.join("instance")) assert app.instance_path == str(modules_tmpdir.join("instance"))
@pytest.mark.xfail(reason="weird interaction with tox")
def test_uninstalled_package_paths(modules_tmpdir, purge_module): def test_uninstalled_package_paths(modules_tmpdir, purge_module):
app = modules_tmpdir.mkdir("config_package_app") app = modules_tmpdir.mkdir("config_package_app")
init = app.join("__init__.py") init = app.join("__init__.py")

View file

@ -9,6 +9,7 @@ envlist =
skip_missing_interpreters = true skip_missing_interpreters = true
[testenv] [testenv]
envtmpdir = {toxworkdir}/tmp/{envname}
deps = deps =
-r requirements/tests.txt -r requirements/tests.txt
min: -r requirements/tests-pallets-min.txt min: -r requirements/tests-pallets-min.txt