mirror of
https://github.com/open-mmlab/mmengine.git
synced 2025-06-03 21:54:44 +08:00
[Fix] Fix ut error in docker (#1204)
This commit is contained in:
parent
1c3332c96e
commit
04b0ffee76
@ -24,7 +24,13 @@ def is_installed(package: str) -> bool:
|
|||||||
get_distribution(package)
|
get_distribution(package)
|
||||||
return True
|
return True
|
||||||
except pkg_resources.DistributionNotFound:
|
except pkg_resources.DistributionNotFound:
|
||||||
return importlib.util.find_spec(package) is not None
|
spec = importlib.util.find_spec(package)
|
||||||
|
if spec is None:
|
||||||
|
return False
|
||||||
|
elif spec.origin is not None:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def get_installed_path(package: str) -> str:
|
def get_installed_path(package: str) -> str:
|
||||||
@ -54,20 +60,12 @@ def get_installed_path(package: str) -> str:
|
|||||||
if spec is not None:
|
if spec is not None:
|
||||||
if spec.origin is not None:
|
if spec.origin is not None:
|
||||||
return osp.dirname(spec.origin)
|
return osp.dirname(spec.origin)
|
||||||
# For namespace packages, the origin is None, and the first path
|
|
||||||
# in submodule_search_locations will be returned.
|
|
||||||
# namespace packages: https://packaging.python.org/en/latest/guides/packaging-namespace-packages/ # noqa: E501
|
|
||||||
elif spec.submodule_search_locations is not None:
|
|
||||||
locations = spec.submodule_search_locations
|
|
||||||
if isinstance(locations, list):
|
|
||||||
return locations[0]
|
|
||||||
else:
|
else:
|
||||||
# `submodule_search_locations` is not subscriptable in
|
# `get_installed_path` cannot get the installed path of
|
||||||
# python3.7. There for we use `_path` to get the first
|
# namespace packages
|
||||||
# path.
|
raise RuntimeError(
|
||||||
return locations._path[0] # type: ignore
|
f'{package} is a namespace package, which is invalid '
|
||||||
else:
|
'for `get_install_path`')
|
||||||
raise e
|
|
||||||
else:
|
else:
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
# Copyright (c) OpenMMLab. All rights reserved.
|
# Copyright (c) OpenMMLab. All rights reserved.
|
||||||
import os.path as osp
|
import os.path as osp
|
||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
|
||||||
|
import pkg_resources
|
||||||
|
import pytest
|
||||||
|
|
||||||
from mmengine.utils import get_installed_path, is_installed
|
from mmengine.utils import get_installed_path, is_installed
|
||||||
|
|
||||||
@ -10,32 +12,26 @@ def test_is_installed():
|
|||||||
# TODO: Windows CI may failed in unknown reason. Skip check the value
|
# TODO: Windows CI may failed in unknown reason. Skip check the value
|
||||||
is_installed('mmengine')
|
is_installed('mmengine')
|
||||||
|
|
||||||
# package set by PYTHONPATH
|
# If there is `__init__.py` in the directory which is added into
|
||||||
assert not is_installed('py_config')
|
# `sys.path`, the directory will be recognized as a package.
|
||||||
sys.path.append(osp.abspath(osp.join(osp.dirname(__file__), '..')))
|
PYTHONPATH = osp.abspath(
|
||||||
assert is_installed('test_utils')
|
osp.join(osp.dirname(__file__), '..', '..', 'mmengine'))
|
||||||
|
sys.path.append(PYTHONPATH)
|
||||||
|
assert is_installed('optim')
|
||||||
sys.path.pop()
|
sys.path.pop()
|
||||||
|
|
||||||
|
|
||||||
def test_get_install_path(tmp_path: Path):
|
def test_get_install_path():
|
||||||
# TODO: Windows CI may failed in unknown reason. Skip check the value
|
# TODO: Windows CI may failed in unknown reason. Skip check the value
|
||||||
get_installed_path('mmengine')
|
get_installed_path('mmengine')
|
||||||
|
|
||||||
# get path for package "installed" by setting PYTHONPATH
|
# get path for package "installed" by setting PYTHONPATH
|
||||||
PYTHONPATH = osp.abspath(osp.join(
|
PYTHONPATH = osp.abspath(osp.join(osp.dirname(__file__), '..'))
|
||||||
osp.dirname(__file__),
|
PYTHONPATH = osp.abspath(
|
||||||
'..',
|
osp.join(osp.dirname(__file__), '..', '..', 'mmengine'))
|
||||||
))
|
|
||||||
sys.path.append(PYTHONPATH)
|
sys.path.append(PYTHONPATH)
|
||||||
res_path = get_installed_path('test_utils')
|
assert get_installed_path('optim') == osp.join(PYTHONPATH, 'optim')
|
||||||
assert osp.join(PYTHONPATH, 'test_utils') == res_path
|
sys.path.pop()
|
||||||
|
|
||||||
# return the first path for namespace package
|
with pytest.raises(pkg_resources.DistributionNotFound):
|
||||||
# See more information about namespace package in:
|
get_installed_path('unknown')
|
||||||
# https://packaging.python.org/en/latest/guides/packaging-namespace-packages/ # noqa:E501
|
|
||||||
(tmp_path / 'test_utils').mkdir()
|
|
||||||
sys.path.insert(-1, str(tmp_path))
|
|
||||||
res_path = get_installed_path('test_utils')
|
|
||||||
assert osp.abspath(osp.join(tmp_path, 'test_utils')) == res_path
|
|
||||||
sys.path.pop()
|
|
||||||
sys.path.pop()
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user